chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
@@ -0,0 +1,108 @@
# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
load("//tensorflow/lite:build_def.bzl", "tflite_cc_shared_object")
package(default_visibility = [
"//visibility:public",
])
licenses(["notice"]) # Apache 2.0
#Just header file, needed for data types in the interface.
cc_library(
name = "hexagon_nn_header",
hdrs = [
"hexagon_nn.h",
"hexagon_nn_init.h",
],
deps = [
"@hexagon_nn//:hexagon_nn_header",
],
)
# Fetches the QC SoC of the device.
cc_library(
name = "soc_model",
srcs = [
"soc_model.cc",
],
hdrs = [
"soc_model.h",
],
deps = [
"@hexagon_nn//:hexagon_soc",
"@hexagon_nn//:remote",
],
)
# DSP interface used to communicate to the aDSP/cDSP.
cc_library(
name = "adsprpc_interface",
srcs = ["adsprpc_interface.cc"],
deps = [
":hexagon_nn_header",
":soc_model",
],
)
cc_library(
name = "hexagon_nn_init",
srcs = ["hexagon_nn_init.cc"],
hdrs = ["hexagon_nn_init.h"],
deps = [
":adsprpc_interface",
":soc_model",
"@hexagon_nn//:AEEStdDef",
"@hexagon_nn//:remote",
"@hexagon_nn//:rpcmem",
],
alwayslink = 1,
)
tflite_cc_shared_object(
name = "libhexagon_interface.so",
linkopts = [
"-Wl,-soname=libhexagon_interface.so",
"-z defs",
"-Wl,--version-script,$(location //tensorflow/lite/delegates/hexagon/hexagon_nn:version_scripts.lds)",
] + select({
"//tensorflow:android": [
"-llog",
"-ldl",
],
"//conditions:default": [
"-ldl",
],
}),
linkstatic = 1,
deps = [
":hexagon_nn_init",
":version_scripts.lds",
"@hexagon_nn",
],
)
cc_library(
name = "hexagon_interface_so",
srcs = [":libhexagon_interface.so"],
)
android_library(
name = "hexagon_interface_android",
exports = [":hexagon_interface_so"],
)
exports_files(["version_script.lds"])
@@ -0,0 +1,217 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <dlfcn.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/stat.h>
#include <cstdio>
#include <cstring>
#include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn.h"
#include "tensorflow/lite/delegates/hexagon/hexagon_nn/soc_model.h"
namespace {
void* LoadLibadsprpc() {
void* lib = dlopen("libadsprpc.so", RTLD_LAZY | RTLD_LOCAL);
if (lib) {
fprintf(stdout, "loaded libadsprpc.so\n");
return lib;
}
return nullptr;
}
void* LoadLibcdsprpc() {
void* lib = dlopen("libcdsprpc.so", RTLD_LAZY | RTLD_LOCAL);
if (lib) {
fprintf(stdout, "loaded libcdsprpc.so\n");
return lib;
}
return nullptr;
}
void* LoadDsprpc() {
SocSkelTable soc_model = tflite::delegates::getsoc_model();
// Use aDSP for 835 and 820, otherwise cDSP.
if (soc_model.mode == NON_DOMAINS ||
(soc_model.dsp_type != nullptr &&
strcmp(soc_model.dsp_type, "adsp") == 0)) {
return LoadLibadsprpc();
}
return LoadLibcdsprpc();
}
void* LoadFunction(const char* name) {
static void* libadsprpc = LoadDsprpc();
if (libadsprpc == nullptr) {
fprintf(stderr, "libadsprpc handle is NULL\n");
return nullptr;
}
auto* func_pt = dlsym(libadsprpc, name);
if (func_pt == nullptr) {
fprintf(stderr, "Func %s not available on this device (NULL).\n", name);
}
return func_pt;
}
using remote_handle_open_fn = decltype(remote_handle_open);
using remote_handle64_open_fn = decltype(remote_handle64_open);
using remote_handle_invoke_fn = decltype(remote_handle_invoke);
using remote_handle64_invoke_fn = decltype(remote_handle64_invoke);
using remote_handle_close_fn = decltype(remote_handle_close);
using remote_handle64_close_fn = decltype(remote_handle64_close);
using remote_mmap_fn = decltype(remote_mmap);
using remote_mmap64_fn = decltype(remote_mmap64);
using remote_munmap_fn = decltype(remote_munmap);
using remote_munmap64_fn = decltype(remote_munmap64);
using remote_register_buf_fn = decltype(remote_register_buf);
using remote_set_mode_fn = decltype(remote_set_mode);
using remote_handle_control_fn = decltype(remote_handle_control);
struct AdsprpcInterface {
remote_handle_open_fn* handle_open_fn =
reinterpret_cast<remote_handle_open_fn*>(
LoadFunction("remote_handle_open"));
remote_handle64_open_fn* handle64_open_fn =
reinterpret_cast<remote_handle64_open_fn*>(
LoadFunction("remote_handle64_open"));
remote_handle_invoke_fn* handle_invoke_fn =
reinterpret_cast<remote_handle_invoke_fn*>(
LoadFunction("remote_handle_invoke"));
remote_handle64_invoke_fn* handle64_invoke_fn =
reinterpret_cast<remote_handle64_invoke_fn*>(
LoadFunction("remote_handle64_invoke"));
remote_handle_close_fn* handle_close_fn =
reinterpret_cast<remote_handle_close_fn*>(
LoadFunction("remote_handle_close"));
remote_handle64_close_fn* handle64_close_fn =
reinterpret_cast<remote_handle64_close_fn*>(
LoadFunction("remote_handle64_close"));
remote_handle_control_fn* handle_control_fn =
reinterpret_cast<remote_handle_control_fn*>(
LoadFunction("remote_handle_control"));
remote_mmap_fn* mmap_fn =
reinterpret_cast<remote_mmap_fn*>(LoadFunction("remote_mmap"));
remote_munmap_fn* munmap_fn =
reinterpret_cast<remote_munmap_fn*>(LoadFunction("remote_munmap"));
remote_mmap64_fn* mmap64_fn =
reinterpret_cast<remote_mmap64_fn*>(LoadFunction("remote_mmap64"));
remote_munmap64_fn* munmap64_fn =
reinterpret_cast<remote_munmap64_fn*>(LoadFunction("remote_munmap64"));
remote_register_buf_fn* register_buf_fn =
reinterpret_cast<remote_register_buf_fn*>(
LoadFunction("remote_register_buf"));
remote_set_mode_fn* set_mode_fn =
reinterpret_cast<remote_set_mode_fn*>(LoadFunction("remote_set_mode"));
// Returns singleton instance.
static AdsprpcInterface* Singleton() {
static AdsprpcInterface* instance = new AdsprpcInterface();
return instance;
}
};
} // namespace
extern "C" {
int remote_handle_open(const char* name, remote_handle* h) {
return AdsprpcInterface::Singleton()->handle_open_fn
? AdsprpcInterface::Singleton()->handle_open_fn(name, h)
: -1;
}
int remote_handle64_open(const char* name, remote_handle64* h) {
return AdsprpcInterface::Singleton()->handle64_open_fn
? AdsprpcInterface::Singleton()->handle64_open_fn(name, h)
: -1;
}
int remote_handle_invoke(remote_handle h, uint32_t scalars, remote_arg* args) {
return AdsprpcInterface::Singleton()->handle_invoke_fn
? AdsprpcInterface::Singleton()->handle_invoke_fn(h, scalars, args)
: -1;
}
int remote_handle64_invoke(remote_handle64 h, uint32_t scalars,
remote_arg* args) {
return AdsprpcInterface::Singleton()->handle64_invoke_fn
? AdsprpcInterface::Singleton()->handle64_invoke_fn(h, scalars,
args)
: -1;
}
int remote_handle_close(remote_handle h) {
return AdsprpcInterface::Singleton()->handle_close_fn
? AdsprpcInterface::Singleton()->handle_close_fn(h)
: -1;
}
int remote_handle64_close(remote_handle64 h) {
return AdsprpcInterface::Singleton()->handle64_close_fn
? AdsprpcInterface::Singleton()->handle64_close_fn(h)
: -1;
}
int remote_handle_control(uint32_t req, void* data, uint32_t datalen) {
return AdsprpcInterface::Singleton()->handle_control_fn
? AdsprpcInterface::Singleton()->handle_control_fn(req, data,
datalen)
: -1;
}
int remote_mmap(int fd, uint32_t flags, uint32_t addr, int size,
uint32_t* result) {
return AdsprpcInterface::Singleton()->mmap_fn
? AdsprpcInterface::Singleton()->mmap_fn(fd, flags, addr, size,
result)
: -1;
}
int remote_mmap64(int fd, uint32_t flags, uintptr_t vaddrin, int64_t size,
uintptr_t* vaddrout) {
return AdsprpcInterface::Singleton()->mmap64_fn
? AdsprpcInterface::Singleton()->mmap64_fn(fd, flags, vaddrin,
size, vaddrout)
: -1;
}
int remote_munmap(uint32_t addr, int size) {
return AdsprpcInterface::Singleton()->munmap_fn
? AdsprpcInterface::Singleton()->munmap_fn(addr, size)
: -1;
}
int remote_munmap64(uintptr_t vaddrout, int64_t size) {
return AdsprpcInterface::Singleton()->munmap64_fn
? AdsprpcInterface::Singleton()->munmap64_fn(vaddrout, size)
: -1;
}
void remote_register_buf(void* buf, int size, int fd) {
if (AdsprpcInterface::Singleton()->register_buf_fn) {
AdsprpcInterface::Singleton()->register_buf_fn(buf, size, fd);
}
}
int remote_set_mode(uint32_t mode) {
return AdsprpcInterface::Singleton()->set_mode_fn
? AdsprpcInterface::Singleton()->set_mode_fn(mode)
: -1;
}
} // extern "C"
@@ -0,0 +1,21 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_HEXAGON_NN_H_
#define TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_HEXAGON_NN_H_
#include "hexagon/hexagon_nn.h"
#include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn_init.h"
#endif // TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_HEXAGON_NN_H_
@@ -0,0 +1,49 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn_init.h"
#include <fcntl.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include "hexagon/remote.h" // NOLINT
#include "hexagon/rpcmem.h" // NOLINT
#include "tensorflow/lite/delegates/hexagon/hexagon_nn/soc_model.h"
extern "C" {
// Version 1.20
static const int kHexagonNNVersion = 137729;
#pragma weak remote_handle_control // Declare it as a weak symbol
void hexagon_nn_global_init() {
rpcmem_init();
// Non-domains QoS invocation
struct remote_rpc_control_latency data;
data.enable = RPC_PM_QOS;
if (remote_handle_control) { // Check if API is available before invoking
remote_handle_control(DSPRPC_CONTROL_LATENCY, (void*)&data, sizeof(data));
}
}
void hexagon_nn_global_teardown() { rpcmem_deinit(); }
bool hexagon_nn_is_device_supported() {
return tflite::delegates::getsoc_model().mode != UNSPECIFIED_MODE;
}
int hexagon_nn_hexagon_interface_version() { return kHexagonNNVersion; }
}
@@ -0,0 +1,28 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_HEXAGON_NN_INIT_H_
#define TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_HEXAGON_NN_INIT_H_
#ifdef __cplusplus
extern "C" {
#endif
void hexagon_nn_global_teardown(void);
void hexagon_nn_global_init(void);
bool hexagon_nn_is_device_supported();
int hexagon_nn_hexagon_interface_version(void);
#ifdef __cplusplus
}
#endif
#endif // TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_HEXAGON_NN_INIT_H_
@@ -0,0 +1,63 @@
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/lite/delegates/hexagon/hexagon_nn/soc_model.h"
#include <cstdlib>
namespace tflite {
namespace delegates {
// Implementation below is similar to the one inside the Hexagon SDK for
// fetching the SoC information.
// TODO(b/144536839): Look in sharing the code with Hexagon SDK if possible.
int get_soc_id(int* soc_id) {
int fd;
if (!access("/sys/devices/soc0/soc_id", F_OK)) {
fd = open("/sys/devices/soc0/soc_id", O_RDONLY);
} else {
fd = open("/sys/devices/system/soc/soc0/id", O_RDONLY);
}
if (fd == -1) {
return -1;
}
char raw_buf[SOC_ID_BUFFER_LENGTH];
const int bytes_read = read(fd, raw_buf, SOC_ID_BUFFER_LENGTH - 1);
// read returns -1 on failure, so check and return if failed.
if (bytes_read == -1) {
return -1; // failure
}
raw_buf[SOC_ID_BUFFER_LENGTH - 1] = 0;
*soc_id = atoi(raw_buf);
close(fd);
return 0;
}
SocSkelTable getsoc_model() {
int soc_id;
get_soc_id(&soc_id);
int i = 0;
for (i = 0; socSkelInfo[i].soc_id != 0; i++) {
if (socSkelInfo[i].soc_id == soc_id) {
return socSkelInfo[i];
}
}
return socSkelInfo[i];
}
} // namespace delegates
} // namespace tflite
@@ -0,0 +1,43 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_SOC_MODEL_H_
#define TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_SOC_MODEL_H_
#include <dlfcn.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdio>
#include "hexagon/remote.h"
#include "hexagon/remote64.h"
#include "hexagon/hexnn_soc_defines.h"
namespace tflite {
namespace delegates {
#define SOC_ID_BUFFER_LENGTH 5
#define URI_BUFFER_LENGTH 100
// Returns QC SoC ID of the device.
int get_soc_id(int* soc_id);
// Returns structure that has SoC information.
SocSkelTable getsoc_model();
} // namespace delegates
} // namespace tflite
#endif // TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_NN_SOC_MODEL_H_
@@ -0,0 +1,27 @@
VERS_1.0 {
global:
hexagon_nn_config;
hexagon_nn_init;
hexagon_nn_prepare;
hexagon_nn_set_powersave_level;
hexagon_nn_set_debug_level;
hexagon_nn_append_node;
hexagon_nn_append_const_node;
hexagon_nn_execute;
hexagon_nn_execute_new;
hexagon_nn_teardown;
hexagon_nn_snpprint;
hexagon_nn_getlog;
hexagon_nn_get_perfinfo;
hexagon_nn_reset_perfinfo;
hexagon_nn_op_id_to_name;
hexagon_nn_global_teardown;
hexagon_nn_global_init;
hexagon_nn_is_device_supported;
hexagon_nn_version;
hexagon_nn_hexagon_interface_version;
# Hide everything else.
local:
*;
};