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,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2018 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.tensorflow.lite.benchmark">
<!-- Necessary for loading custom models from disk. -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-sdk
android:minSdkVersion="23"
android:targetSdkVersion="31" />
<application
android:debuggable="true">
<!-- This Activity runs the TensorFlow Lite benchmark at creation, using
a provided set of arguments, then immediately terminates. -->
<activity
android:name=".BenchmarkModelActivity"
android:screenOrientation="portrait"
android:label="TFLite Benchmark"
android:theme="@android:style/Theme.NoDisplay"
android:exported="true"
android:noHistory="true" />
<uses-library android:name="libOpenCL.so"
android:required="false"/>
<uses-library android:name="libOpenCL-pixel.so"
android:required="false"/>
</application>
</manifest>
@@ -0,0 +1,74 @@
# Description:
# BenchmarkModel Android harness for TensorFlow Lite benchmarks.
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("//tensorflow/lite:build_def.bzl", "tflite_jni_binary")
load("//tensorflow/lite:special_rules.bzl", "tflite_hexagon_nn_skel_libraries")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
default_visibility = ["//visibility:private"],
licenses = ["notice"],
)
# See README.md for details about building and executing the benchmark in APK
# format.
APK_VARIANTS = [
# (suffix, extra deps)
("", []),
(
"_plus_flex",
["//tensorflow/lite/delegates/flex:delegate"],
),
]
[android_binary(
name = "benchmark_model%s" % suffix,
srcs = glob([
"src/**/*.java",
]),
custom_package = "org.tensorflow.lite.benchmark",
manifest = "AndroidManifest.xml",
# In some platforms we don't have an Android SDK/NDK and this target
# can't be built. We need to prevent the build system from trying to
# use the target in that case.
tags = ["manual"],
deps = [
":hexagon_libs",
":tensorflowlite_benchmark_native%s" % suffix,
],
) for suffix, _ in APK_VARIANTS]
[tflite_jni_binary(
name = "libtensorflowlite_benchmark%s.so" % suffix,
srcs = glob([
"jni/**/*.cc",
"jni/**/*.h",
]),
deps = [
"//tensorflow/lite/java/jni",
"//tensorflow/lite/tools/benchmark:benchmark_tflite_model_lib",
] + extra_deps,
) for suffix, extra_deps in APK_VARIANTS]
[cc_library(
name = "tensorflowlite_benchmark_native%s" % suffix,
srcs = ["libtensorflowlite_benchmark%s.so" % suffix],
visibility = ["//visibility:private"],
) for suffix, _ in APK_VARIANTS]
cc_library(
name = "hexagon_libs",
srcs = select({
"//tensorflow:android_arm64": [
"//tensorflow/lite/delegates/hexagon/hexagon_nn:libhexagon_interface.so",
] + tflite_hexagon_nn_skel_libraries(),
"//tensorflow:android_arm": [
"//tensorflow/lite/delegates/hexagon/hexagon_nn:libhexagon_interface.so",
] + tflite_hexagon_nn_skel_libraries(),
"//conditions:default": [],
}),
visibility = ["//visibility:private"],
)
@@ -0,0 +1,157 @@
# TFLite Model Benchmark Tool with Android Apk
## Description
This Android benchmark app is a simple wrapper around the TensorFlow Lite
[command-line benchmark utility](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/benchmark).
Pushing and executing binaries directly on an Android device is a valid approach
to benchmarking, but it can result in subtle (but observable) differences in
performance relative to execution within an actual Android app. In particular,
Android's scheduler tailors behavior based on thread and process priorities,
which differ between a foreground Activity/Application and a regular background
binary executed via `adb shell ...`. This tailored behavior is most evident when
enabling multi-threaded CPU execution with TensorFlow Lite.
To that end, this app offers perhaps a more faithful view of runtime performance
that developers can expect when deploying TensorFlow Lite with their
application.
## To build/install/run
(0) Refer to
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/android/test
to edit the `WORKSPACE` to configure the android NDK/SDK.
(1) Build for your specific platform, e.g.:
```
bazel build -c opt \
--config=android_arm64 \
tensorflow/lite/tools/benchmark/android:benchmark_model
```
(Optional) To enable Hexagon delegate with `--use_hexagon=true` option, you can
download and install the libraries as the guided in [hexagon delegate]
(https://www.tensorflow.org/lite/performance/hexagon_delegate#step_2_add_hexagon_libraries_to_your_android_app)
page. For example, if you installed the libraries at third_party/hexagon_nn_skel
and created third_party/hexagon_nn_skel/BUILD with a build target,
```
filegroup(
name = "libhexagon_nn_skel",
srcs = glob(["*.so"]),
)
```
you need to modify tflite_hexagon_nn_skel_libraries macro in
tensorflow/lite/special_rules.bzl to specify the build target.
```
return ["//third_party/hexagon_nn_skel:libhexagon_nn_skel"]
```
(2) Connect your phone. Install the benchmark APK to your phone with adb:
```
adb install -r -d -g bazel-bin/tensorflow/lite/tools/benchmark/android/benchmark_model.apk
```
Note: Make sure to install with "-g" option to grant the permission for reading
external storage.
(3) Push the compute graph that you need to test.
```
adb push mobilenet_quant_v1_224.tflite /data/local/tmp
```
(4) Run the benchmark. Additional command-line flags are documented
[here](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/tools/benchmark/README.md)
and can be appended to the `args` string alongside the required `--graph` flag
(note that all args must be nested in the single quoted string that follows the
args key).
```
adb shell am start -S \
-n org.tensorflow.lite.benchmark/.BenchmarkModelActivity \
--es args '"--graph=/data/local/tmp/mobilenet_quant_v1_224.tflite \
--num_threads=4"'
```
(5) The results will be available in Android's logcat, e.g.:
```
adb logcat | grep "Inference timings in us"
... tflite : Inference timings in us: Init: 1007529, First inference: 4098, Warmup (avg): 1686.59, Inference (avg): 1687.92
```
## To trace Tensorflow Lite internals including operator invocation
The steps described here follows the method of
https://developer.android.com/topic/performance/tracing/on-device. Refer to the
page for more detailed information.
(0)-(3) Follow the steps (0)-(3) of [build/install/run](#to-buildinstallrun)
section.
(4) Enable platform tracing.
```
adb shell setprop debug.tflite.trace 1
```
(5) Set up Quick Settings tile for System Tracing app on your device. Follow the
[instruction](https://developer.android.com/topic/performance/tracing/on-device#set-up-tile).
The System Tracing tile will be added to the Quick Settings panel.
Optionally, you can set up other configurations for tracing from the app menu.
Refer to the
[guide](https://developer.android.com/topic/performance/tracing/on-device#app-menu)
for more information.
(6) Tap the System Tracing tile, which has the label "Record trace". The tile
becomes enabled, and a persistent notification appears to notify you that the
system is now recording a trace.
(7) Run the benchmark with platform tracing enabled.
```
adb shell am start -S \
-n org.tensorflow.lite.benchmark/.BenchmarkModelActivity \
--es args '"--graph=/data/local/tmp/mobilenet_quant_v1_224.tflite \
--num_threads=4"'
```
(8) Wait until the benchmark finishes. It can be checked from Android log
messages, e.g.,
```
adb logcat | grep "Inference timings in us"
... tflite : Inference timings in us: Init: 1007529, First inference: 4098, Warmup (avg): 1686.59, Inference (avg): 1687.92
```
(9) Stop tracing by tapping either the System Tracing tile in the Quick Settings
panel or on the System Tracing notification. The system displays a new
notification that contains the message "Saving trace". When saving is complete,
the system dismisses the notification and displays a third notification "Trace
saved", confirming that your trace has been saved and that you're ready to share
the system trace.
(10)
[Share](https://developer.android.com/topic/performance/tracing/on-device#share-trace)
a trace file,
[convert](https://developer.android.com/topic/performance/tracing/on-device#converting_between_trace_formats)
between tracing formats and
[create](https://developer.android.com/topic/performance/tracing/on-device#create-html-report)
an HTML report. Note that, the captured tracing file format is either in
Perfetto format or in Systrace format depending on the Android version of your
device. Select the appropriate method to handle the generated file.
(11) Disable platform tracing.
```
adb shell setprop debug.tflite.trace 0
```
@@ -0,0 +1,66 @@
/* Copyright 2018 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 <string>
#include "tensorflow/lite/tools/benchmark/benchmark_tflite_model.h"
#ifdef __ANDROID__
#include <android/log.h>
#endif
namespace tflite {
namespace benchmark {
namespace {
void Run(int argc, char** argv) {
BenchmarkTfLiteModel benchmark;
benchmark.Run(argc, argv);
}
} // namespace
} // namespace benchmark
} // namespace tflite
extern "C" {
JNIEXPORT void JNICALL
Java_org_tensorflow_lite_benchmark_BenchmarkModel_nativeRun(JNIEnv* env,
jclass clazz,
jstring args_obj) {
const char* args_chars = env->GetStringUTFChars(args_obj, nullptr);
// Split the args string into individual arg tokens.
std::istringstream iss(args_chars);
std::vector<std::string> args_split{std::istream_iterator<std::string>(iss),
{}};
// Construct a fake argv command-line object for the benchmark.
std::vector<char*> argv;
std::string arg0 = "(BenchmarkModelAndroid)";
argv.push_back(const_cast<char*>(arg0.data()));
for (auto& arg : args_split) {
argv.push_back(const_cast<char*>(arg.data()));
}
tflite::benchmark::Run(static_cast<int>(argv.size()), argv.data());
env->ReleaseStringUTFChars(args_obj, args_chars);
}
} // extern "C"
@@ -0,0 +1,37 @@
/* Copyright 2018 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.benchmark;
/** Helper class for running a native TensorFlow Lite benchmark. */
class BenchmarkModel {
static {
// Try loading flex first if available. If not load regular tflite shared library.
try {
System.loadLibrary("tensorflowlite_benchmark_plus_flex");
} catch (UnsatisfiedLinkError e) {
System.loadLibrary("tensorflowlite_benchmark");
}
}
// Executes a standard TensorFlow Lite benchmark according to the provided args.
//
// Note that {@code args} will be split by the native execution code.
public static void run(String args) {
nativeRun(args);
}
private static native void nativeRun(String args);
}
@@ -0,0 +1,51 @@
/* Copyright 2018 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.benchmark;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Trace;
import android.util.Log;
/** Main {@code Activity} class for the benchmark app. */
public class BenchmarkModelActivity extends Activity {
private static final String TAG = "tflite_BenchmarkModelActivity";
private static final String ARGS_INTENT_KEY_0 = "args";
private static final String ARGS_INTENT_KEY_1 = "--args";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String args = bundle.getString(ARGS_INTENT_KEY_0, bundle.getString(ARGS_INTENT_KEY_1));
if (args.contains("--use_hexagon=true") || args.contains("--use_hexagon=1")) {
// Users should not specify this argument.
args = args + " --hexagon_lib_path=" + getApplicationInfo().nativeLibraryDir;
}
Log.i(TAG, "Running TensorFlow Lite benchmark with args: " + args);
Trace.beginSection("TFLite Benchmark Model");
BenchmarkModel.run(args);
Trace.endSection();
finish();
}
}