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,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.tensorflow.lite">
<uses-sdk
android:targetSdkVersion="19" />
<application />
</manifest>
@@ -0,0 +1,73 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_library")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//tensorflow/lite:build_def.bzl", "tflite_jni_binary", "tflite_jni_linkopts")
load("//tensorflow/lite/java:aar_with_jni.bzl", "aar_with_jni")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)
# EXPERIMENTAL: Native target that runs inference on the Hexagon backend.
# The Hexagon backend-related targets are intentionally not included in BUILD.bazel.
tflite_jni_binary(
name = "libtensorflowlite_hexagon_jni.so",
linkopts = tflite_jni_linkopts() + select({
"//tensorflow:android": ["-ldl"],
"//conditions:default": [],
}),
linkscript = "//tensorflow/lite/delegates/hexagon:version_script.lds",
tags = [
"manual",
"nobuilder",
"notap",
],
deps = [
"//tensorflow/lite/delegates/hexagon/java/src/main/native",
],
)
cc_library(
name = "tensorflowlite_hexagon",
srcs = [
"libtensorflowlite_hexagon_jni.so",
] + select({
# copybara:uncomment_begin(separate repo in OSS)
# "//tensorflow:android_arm64": ["//tensorflow/lite/delegates/hexagon/hexagon_nn:libhexagon_interface.so"],
# "//tensorflow:android_arm": ["//tensorflow/lite/delegates/hexagon/hexagon_nn:libhexagon_interface.so"],
# copybara:uncomment_end_and_comment_begin
"//tensorflow:android_arm64": ["@hexagon_nn//:hexagon/arm64-v8a/libhexagon_interface.so"],
"//tensorflow:android_arm": ["@hexagon_nn//:hexagon/armeabi-v7a/libhexagon_interface.so"],
# copybara:comment_end
"//conditions:default": [],
}),
tags = [
"manual",
"nobuilder",
"notap",
],
)
android_library(
name = "tensorflowlite_java_hexagon",
srcs = ["//tensorflow/lite/delegates/hexagon/java/src/main/java/org/tensorflow/lite:hexagon_delegate"],
manifest = "AndroidManifest.xml",
proguard_specs = ["proguard.flags"],
tags = [
"manual",
"nobuilder",
"notap",
],
deps = [
":tensorflowlite_hexagon",
"//tensorflow/lite/java:tensorflowlite_java",
"@org_checkerframework_qual",
],
)
aar_with_jni(
name = "tensorflow-lite-hexagon",
android_library = ":tensorflowlite_java_hexagon",
headers = ["//tensorflow/lite/delegates/hexagon:hexagon_delegate.h"],
)
@@ -0,0 +1,3 @@
-keepclassmembers class org.tensorflow.lite.NativeInterpreterWrapper {
private long inferenceDurationNanoseconds;
}
@@ -0,0 +1,9 @@
# copybara:uncomment package(default_applicable_licenses = ["//tensorflow:LICENSE"])
licenses(["notice"])
filegroup(
name = "hexagon_delegate",
srcs = ["HexagonDelegate.java"],
visibility = ["//visibility:public"],
)
@@ -0,0 +1,78 @@
/* 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.
==============================================================================*/
package org.tensorflow.lite;
import android.content.Context;
import java.io.Closeable;
/** {@link Delegate} for Hexagon inference. */
public class HexagonDelegate implements Delegate, Closeable {
private static final long INVALID_DELEGATE_HANDLE = 0;
private static final String TFLITE_HEXAGON_LIB = "tensorflowlite_hexagon_jni";
private static volatile boolean nativeLibraryLoaded = false;
private long delegateHandle;
/*
* Creates a new HexagonDelegate object given the current 'context'.
* Throws UnsupportedOperationException if Hexagon DSP delegation is not available
* on this device.
*/
public HexagonDelegate(Context context) throws UnsupportedOperationException {
ensureNativeLibraryLoaded();
setAdspLibraryPath(context.getApplicationInfo().nativeLibraryDir);
delegateHandle = createDelegate();
if (delegateHandle == INVALID_DELEGATE_HANDLE) {
throw new UnsupportedOperationException("This Device doesn't support Hexagon DSP execution.");
}
}
@Override
public long getNativeHandle() {
return delegateHandle;
}
/**
* Frees TFLite resources in C runtime.
*
* <p>User is expected to call this method explicitly.
*/
@Override
public void close() {
if (delegateHandle != INVALID_DELEGATE_HANDLE) {
deleteDelegate(delegateHandle);
delegateHandle = INVALID_DELEGATE_HANDLE;
}
}
private static void ensureNativeLibraryLoaded() {
if (nativeLibraryLoaded) {
return;
}
try {
System.loadLibrary(TFLITE_HEXAGON_LIB);
nativeLibraryLoaded = true;
} catch (Exception e) {
throw new UnsupportedOperationException("Failed to load native Hexagon shared library: " + e);
}
}
private static native long createDelegate();
private static native void deleteDelegate(long delegateHandle);
private static native boolean setAdspLibraryPath(String libraryPath);
}
@@ -0,0 +1,30 @@
# Description:
# Java Native Interface (JNI) library intended for implementing the
# TensorFlow Lite Hexagon delegate Java API using the TensorFlow Lite CC library.
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//tensorflow/lite:build_def.bzl", "tflite_copts")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = ["//tensorflow/lite/delegates/hexagon/java:__subpackages__"],
)
licenses(["notice"])
cc_library(
name = "native",
srcs = ["hexagon_delegate_jni.cc"],
copts = tflite_copts(),
tags = [
"manual",
"nobuilder",
"notap",
],
deps = [
"//tensorflow/lite/c:c_api_types",
"//tensorflow/lite/delegates/hexagon:hexagon_delegate",
"//tensorflow/lite/java/jni",
],
alwayslink = 1,
)
@@ -0,0 +1,53 @@
/* 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 <jni.h>
#include <sstream>
#include "tensorflow/lite/c/c_api_types.h"
#include "tensorflow/lite/delegates/hexagon/hexagon_delegate.h"
extern "C" {
JNIEXPORT jlong JNICALL
Java_org_tensorflow_lite_HexagonDelegate_createDelegate(
JNIEnv* env, jclass clazz) {
// Auto-choosing the best performing config for closed release.
TfLiteHexagonDelegateOptions options = {0};
TfLiteHexagonInit();
return reinterpret_cast<jlong>(TfLiteHexagonDelegateCreate(&options));
}
JNIEXPORT void JNICALL
Java_org_tensorflow_lite_HexagonDelegate_deleteDelegate(
JNIEnv* env, jclass clazz, jlong delegate) {
TfLiteHexagonDelegateDelete(reinterpret_cast<TfLiteDelegate*>(delegate));
TfLiteHexagonTearDown();
}
JNIEXPORT jboolean JNICALL
Java_org_tensorflow_lite_HexagonDelegate_setAdspLibraryPath(
JNIEnv* env, jclass clazz, jstring native_lib_path) {
const char* lib_dir_path = env->GetStringUTFChars(native_lib_path, nullptr);
std::stringstream path;
path << lib_dir_path
<< ";/system/lib/rfsa/adsp;/system/vendor/lib/rfsa/adsp;/dsp";
env->ReleaseStringUTFChars(native_lib_path, lib_dir_path);
return setenv("ADSP_LIBRARY_PATH", path.str().c_str(), 1 /*override*/) == 0
? JNI_TRUE
: JNI_FALSE;
}
} // extern "C"