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
+42
View File
@@ -0,0 +1,42 @@
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable")
load("//tensorflow/lite:build_def.bzl", "tflite_copts", "tflite_copts_warnings")
load("//tensorflow/lite:special_rules.bzl", "nnapi_sl_visibility_allowlist")
_DEFAULT_VISIBILITY = ["//tensorflow/lite:__subpackages__"]
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = _DEFAULT_VISIBILITY,
licenses = ["notice"],
)
cc_library(
name = "nnapi_support_library_headers",
hdrs = [
"include/SupportLibrary.h",
"include/SupportLibrarySymbols.h",
"public/NeuralNetworksSupportLibraryImpl.h",
],
compatible_with = get_compatible_with_portable(),
visibility = _DEFAULT_VISIBILITY + nnapi_sl_visibility_allowlist(),
deps = [
"//tensorflow/lite/kernels/internal:compatibility",
"//tensorflow/lite/nnapi:nnapi_lib",
],
)
cc_library(
name = "nnapi_support_library",
srcs = [
"SupportLibrary.cc",
],
compatible_with = get_compatible_with_portable(),
copts = tflite_copts() + tflite_copts_warnings(),
visibility = _DEFAULT_VISIBILITY + nnapi_sl_visibility_allowlist(),
deps = [
":nnapi_support_library_headers",
"//tensorflow/lite:minimal_logging",
"//tensorflow/lite/nnapi:nnapi_lib",
],
)
+21
View File
@@ -0,0 +1,21 @@
# NNAPI Support Library
Files in this directory are a copy of NNAPI Support Library
[files](https://cs.android.com/android/platform/superproject/+/master:packages/modules/NeuralNetworks/shim_and_sl/;drc=629cea610b447266b1e6b01e4cb6a952dcb56e7e)
in AOSP.
The files had to be modified to make them work in TF Lite context. Here is the
list of differences from the AOSP version:
* `#include` directives use fully-qualified paths.
* `#pragma once` directives are changed to header guards. Android paths in
header guards are changed to TF Lite paths.
* `tensorflow/lite/nnapi/NeuralNetworksTypes.h` is used for definitions of
NNAPI types instead of
[`NeuralNetworksTypes.h` from AOSP](https://cs.android.com/android/_/android/platform/packages/modules/NeuralNetworks/+/6f0a05b9abdfe0d17afe0269c5329340809175b5:runtime/include/NeuralNetworksTypes.h;drc=a62e56b26b7382a62c5aa0e5964266eba55853d8).
* `loadNnApiSupportLibrary(...)` is using `tensorflow/lite/minimal_logging.h`
for logging on errors.
* `SupportLibrary.h` declarations are wrapped into `tflite::nnapi` namespace.
* `__BEGIN_DECLS` and `__END_DECLS` are changed to explicit `extern "C"`
blocks.
* Copyright notice is changed to the one used in Tensorflow project.
@@ -0,0 +1,93 @@
/* 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.
==============================================================================*/
// Changed when importing from AOSP
#include "tensorflow/lite/nnapi/sl/include/SupportLibrary.h"
// Changed when importing from AOSP
#include <dlfcn.h>
#include <cinttypes>
#include <memory>
#include <string>
#include "tensorflow/lite/minimal_logging.h"
#include "tensorflow/lite/nnapi/NeuralNetworksTypes.h"
namespace tflite {
namespace nnapi {
using tflite::TFLITE_LOG_ERROR;
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
const std::string& libName) {
void* libHandle = dlopen(libName.c_str(), RTLD_LAZY | RTLD_LOCAL);
if (libHandle == nullptr) {
TFLITE_LOG(TFLITE_LOG_ERROR, "nnapi error: unable to open library %s: %s",
libName.c_str(), dlerror());
return nullptr;
}
auto result = loadNnApiSupportLibrary(libHandle);
if (!result) {
dlclose(libHandle);
}
return result;
}
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
void* libHandle) {
NnApiSLDriverImpl* (*getSlDriverImpl)();
getSlDriverImpl = reinterpret_cast<decltype(getSlDriverImpl)>(
dlsym(libHandle, "ANeuralNetworks_getSLDriverImpl"));
if (getSlDriverImpl == nullptr) {
TFLITE_LOG(TFLITE_LOG_ERROR,
"Failed to find ANeuralNetworks_getSLDriverImpl symbol");
return nullptr;
}
NnApiSLDriverImpl* impl = getSlDriverImpl();
if (impl == nullptr) {
TFLITE_LOG(TFLITE_LOG_ERROR,
"ANeuralNetworks_getSLDriverImpl returned nullptr");
return nullptr;
}
if (impl->implFeatureLevel < ANEURALNETWORKS_FEATURE_LEVEL_5 ||
impl->implFeatureLevel > ANEURALNETWORKS_FEATURE_LEVEL_7) {
TFLITE_LOG(TFLITE_LOG_ERROR,
"Unsupported NnApiSLDriverImpl->implFeatureLevel: %" PRId64,
impl->implFeatureLevel);
return nullptr;
}
if (impl->implFeatureLevel == ANEURALNETWORKS_FEATURE_LEVEL_5) {
return std::make_unique<NnApiSupportLibrary>(
reinterpret_cast<NnApiSLDriverImplFL5*>(impl), libHandle);
}
if (impl->implFeatureLevel == ANEURALNETWORKS_FEATURE_LEVEL_6) {
return std::make_unique<NnApiSupportLibrary>(
reinterpret_cast<NnApiSLDriverImplFL6*>(impl), libHandle);
}
if (impl->implFeatureLevel == ANEURALNETWORKS_FEATURE_LEVEL_7) {
return std::make_unique<NnApiSupportLibrary>(
reinterpret_cast<NnApiSLDriverImplFL7*>(impl), libHandle);
}
return nullptr;
}
} // namespace nnapi
} // namespace tflite
@@ -0,0 +1,99 @@
/* 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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_
#define TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_
// WARNING: this header file is DEPRECATED.
// See https://developer.android.com/ndk/guides/neuralnetworks/migration-guide.
#include <dlfcn.h>
#include <cstdlib>
#include <memory>
#include <string>
#include <variant>
// Changed when importing from AOSP
#include "tensorflow/lite/kernels/internal/compatibility.h"
#include "tensorflow/lite/nnapi/NeuralNetworksTypes.h"
#include "tensorflow/lite/nnapi/sl/public/NeuralNetworksSupportLibraryImpl.h"
namespace tflite {
namespace nnapi {
#ifndef __NNAPI_FL5_MIN_ANDROID_API__
#define __NNAPI_FL5_MIN_ANDROID_API__ __ANDROID_API_S__
#endif
/**
* Helper struct, wraps different versions of NnApiSLDriverImpl.
*
* Owns the .so handle, and will close it in destructor.
* Sets proper implStructFeatureLevel in constructor.
*
* There's expectation that for M>N, NnApiSLDriverImplFL(M) is
* a strict superset of NnApiSLDriverImplFL(N), and *NnApiSLDriverImplFL(M) can
* be reinterpret_cast to *NnApiSLDriverImplFL(N) safely.
*
* The base->implFeatureLevel is set to the actual Feature Level
* implemented by the SLDriverImpl,
*/
struct NnApiSupportLibrary {
NnApiSupportLibrary(const NnApiSLDriverImplFL5* impl, void* libHandle)
: libHandle(libHandle), fl5(impl) {}
// No need for ctor below since FL6&7 are typedefs of FL5
// NnApiSupportLibrary(const NnApiSLDriverImplFL6& impl, void* libHandle):
// impl(impl), NnApiSupportLibrary(const NnApiSLDriverImplFL7& impl, void*
// libHandle): impl(impl), libHandle(libHandle) {}
~NnApiSupportLibrary() {
if (libHandle != nullptr) {
dlclose(libHandle);
libHandle = nullptr;
}
}
NnApiSupportLibrary(const NnApiSupportLibrary&) = delete;
NnApiSupportLibrary& operator=(const NnApiSupportLibrary&) = delete;
int64_t getFeatureLevel() const { return fl5->base.implFeatureLevel; }
const NnApiSLDriverImplFL5* getFL5() const { return fl5; }
const NnApiSLDriverImplFL6* getFL6() const {
TFLITE_CHECK_GE(getFeatureLevel(), ANEURALNETWORKS_FEATURE_LEVEL_6);
return reinterpret_cast<const NnApiSLDriverImplFL6*>(&fl5);
}
const NnApiSLDriverImplFL7* getFL7() const {
TFLITE_CHECK_GE(getFeatureLevel(), ANEURALNETWORKS_FEATURE_LEVEL_7);
return reinterpret_cast<const NnApiSLDriverImplFL6*>(&fl5);
}
void* libHandle = nullptr;
const NnApiSLDriverImplFL5* fl5;
};
/**
* Loads the NNAPI support library.
* The NnApiSupportLibrary structure is filled with all the pointers. If one
* function doesn't exist, a null pointer is stored.
*/
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
const std::string& libName);
std::unique_ptr<const NnApiSupportLibrary> loadNnApiSupportLibrary(
void* libHandle);
} // namespace nnapi
} // namespace tflite
#endif // TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_H_
@@ -0,0 +1,43 @@
/* 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.
==============================================================================*/
#ifndef TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_SYMBOLS_H_
#define TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_SYMBOLS_H_
// WARNING: this header file is DEPRECATED.
// See https://developer.android.com/ndk/guides/neuralnetworks/migration-guide.
#include <stddef.h>
// Changed when importing from AOSP
#include "tensorflow/lite/nnapi/sl/public/NeuralNetworksSupportLibraryImpl.h"
// If you are linking against SL driver implementation through DT_NEEDED,
// you can use this declaration to access its implementation instead
// of doing dlsym.
#ifdef __cplusplus
extern "C" {
#endif
/**
* Get the NNAPI SL Driver NnApiSLDriverImpl with all
* driver functions.
*/
NnApiSLDriverImpl* ANeuralNetworks_getSLDriverImpl();
#ifdef __cplusplus
} // extern "C"
#endif
#endif // TENSORFLOW_LITE_NNAPI_SL_INCLUDE_SUPPORT_LIBRARY_SYMBOLS_H_
File diff suppressed because it is too large Load Diff