Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tflite] add xnnpack delegate to label_image #36749

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tensorflow/lite/examples/label_image/BUILD
Expand Up @@ -32,6 +32,7 @@ cc_binary(
"//tensorflow/lite:framework",
"//tensorflow/lite:string_util",
"//tensorflow/lite/delegates/nnapi:nnapi_delegate",
"//tensorflow/lite/delegates/xnnpack:xnnpack_delegate",
"//tensorflow/lite/kernels:builtin_ops",
"//tensorflow/lite/profiling:profiler",
"//tensorflow/lite/tools/evaluation:utils",
Expand Down
117 changes: 87 additions & 30 deletions tensorflow/lite/examples/label_image/README.md
Expand Up @@ -90,39 +90,96 @@ adb push tensorflow/lite/examples/label_image/testdata/grace_hopper.bmp /data/l
adb push /tmp/labels.txt /data/local/tmp
```

Run it, `adb shell "/data/local/tmp/label_image \ -m
/data/local/tmp/mobilenet_v1_1.0_224.tflite \ -i
/data/local/tmp/grace_hopper.bmp \ -l /data/local/tmp/labels.txt"` then you
should see something like the followings: `Loaded model
/data/local/tmp/mobilenet_v1_1.0_224.tflite resolved reporter INFO: Initialized
TensorFlow Lite runtime. invoked average time: 25.03 ms 0.907071: 653 military
uniform 0.0372416: 907 Windsor tie 0.00733753: 466 bulletproof vest 0.00592852:
458 bow tie 0.00414091: 514 cornet`

Run the model with NNAPI delegate (`-a 1`), `adb shell
"/data/local/tmp/label_image \ -m /data/local/tmp/mobilenet_v1_1.0_224.tflite \
-i /data/local/tmp/grace_hopper.bmp \ -l /data/local/tmp/labels.txt -a 1 -f 1"`
then you should see something like the followings: `Loaded model
/data/local/tmp/mobilenet_v1_1.0_224.tflite resolved reporter INFO: Initialized
TensorFlow Lite runtime. INFO: Created TensorFlow Lite delegate for NNAPI.
Applied NNAPI delegate.invoked average time: 10.348 ms 0.905401: 653 military
uniform 0.0379589: 907 Windsor tie 0.00735866: 466 bulletproof vest 0.00605307:
458 bow tie 0.00422573: 514 cornet`
Run it,
```
adb shell "/data/local/tmp/label_image \
-m /data/local/tmp/mobilenet_v1_1.0_224.tflite \
-i /data/local/tmp/grace_hopper.bmp \
-l /data/local/tmp/labels.txt"
```
then you should see something like the followings:
```
Loaded model
/data/local/tmp/mobilenet_v1_1.0_224.tflite
resolved reporter
INFO: Initialized TensorFlow Lite runtime.
invoked
average time: 25.03 ms
0.907071: 653 military uniform
0.0372416: 907 Windsor tie
0.00733753: 466 bulletproof vest
0.00592852: 458 bow tie
0.00414091: 514 cornet
```

Run the model with NNAPI delegate (`-a 1`),
```
adb shell "/data/local/tmp/label_image \
-m /data/local/tmp/mobilenet_v1_1.0_224.tflite \
-i /data/local/tmp/grace_hopper.bmp \
-l /data/local/tmp/labels.txt -a 1 -f 1"
```
then you should see something like the followings:
```
Loaded model /data/local/tmp/mobilenet_v1_1.0_224.tflite
resolved reporter
INFO: Initialized TensorFlow Lite runtime.
INFO: Created TensorFlow Lite delegate for NNAPI.
Applied NNAPI delegate.
invoked average time:10.348 ms
0.905401: 653 military uniform
0.0379589: 907 Windsor tie
0.00735866: 466 bulletproof vest
0.00605307: 458 bow tie
0.00422573: 514 cornet
```

To run a model with the Hexagon Delegate, assuming we have followed the
[Hexagon Delegate Guide](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/performance/hexagon_delegate.md)
and installed Hexagon libraries in `/data/local/tmp`. Run it `adb shell
"/data/local/tmp/label_image \ -m
/data/local/tmp/mobilenet_v1_1.0_224_quant.tflite \ -i
/data/local/tmp/grace_hopper.bmp \ -l /data/local/tmp/labels.txt -j 1"` then you
should see something like the followings: ``` Loaded model
/data/local/tmp/mobilenet_v1_1.0_224_quant.tflite resolved reporter INFO:
Initialized TensorFlow Lite runtime. INFO: Created TensorFlow Lite delegate for
Hexagon. INFO: Hexagon delegate: 31 nodes delegated out of 31 nodes.

remote_handle_control available and used Applied Hexagon delegate.invoked
average time: 8.307 ms 0.729412: 653 military uniform 0.0980392: 907 Windsor tie
0.0313726: 466 bulletproof vest 0.0313726: 458 bow tie 0.0117647: 700 panpipe
and installed Hexagon libraries in `/data/local/tmp`. Run it
```
adb shell "/data/local/tmp/label_image \
-m /data/local/tmp/mobilenet_v1_1.0_224_quant.tflite \
-i /data/local/tmp/grace_hopper.bmp \
-l /data/local/tmp/labels.txt -j 1"
```
then you should see something like the followings:
```
Loaded model /data/local/tmp/mobilenet_v1_1.0_224_quant.tflite
resolved reporter
INFO: Initialized TensorFlow Lite runtime.
INFO: Created TensorFlow Lite delegate for Hexagon.
INFO: Hexagon delegate: 31 nodes delegated out of 31 nodes.

remote_handle_control available and used
Applied Hexagon delegate.invoked
average time: 8.307 ms
0.729412: 653 military uniform
0.0980392: 907 Windsor tie
0.0313726: 466 bulletproof vest
0.0313726: 458 bow tie
0.0117647: 700 panpipe
```

Run the model with the XNNPACK delegate (`-x 1`),
```
adb shell "/data/local/tmp/label_image \
-m /data/local/tmp/mobilenet_v1_1.0_224.tflite \
-i /data/local/tmp/grace_hopper.bmp \
-l /data/local/tmp/labels.txt -x 1"
```
then you should see something like the followings:
```
Loaded model /data/local/tmp/mobilenet_v1_1.0_224.tflite
resolved reporter
INFO: Initialized TensorFlow Lite runtime.
Applied XNNPACK delegate.invoked
average time: 11.0237 ms
0.90707: 653 military uniform
0.0372418: 907 Windsor tie
0.0073376: 466 bulletproof vest
0.00592856: 458 bow tie
0.00414093: 514 cornet
```

See the `label_image.cc` source code for other command line options.
21 changes: 20 additions & 1 deletion tensorflow/lite/examples/label_image/label_image.cc
Expand Up @@ -37,6 +37,7 @@ limitations under the License.

#include "absl/memory/memory.h"
#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
#include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
#include "tensorflow/lite/examples/label_image/bitmap_helpers.h"
#include "tensorflow/lite/examples/label_image/get_top_n.h"
#include "tensorflow/lite/kernels/register.h"
Expand Down Expand Up @@ -101,6 +102,19 @@ TfLiteDelegatePtrMap GetDelegates(Settings* s) {
}
}

if (s->xnnpack_delegate) {
TfLiteXNNPackDelegateOptions xnnpack_options =
TfLiteXNNPackDelegateOptionsDefault();
xnnpack_options.num_threads = s->number_of_threads;

auto delegate = evaluation::CreateXNNPACKDelegate(&xnnpack_options);
if (!delegate) {
LOG(INFO) << "XNNPACK acceleration is unsupported on this platform.";
} else {
delegates.emplace("XNNPACK", std::move(delegate));
}
}

return delegates;
}

Expand Down Expand Up @@ -350,6 +364,7 @@ void display_usage() {
<< "--threads, -t: number of threads\n"
<< "--verbose, -v: [0|1] print more information\n"
<< "--warmup_runs, -w: number of warmup runs\n"
<< "--xnnpack_delegate, -x: xnnpack delegate\n"
<< "\n";
}

Expand All @@ -376,13 +391,14 @@ int Main(int argc, char** argv) {
{"warmup_runs", required_argument, nullptr, 'w'},
{"gl_backend", required_argument, nullptr, 'g'},
{"hexagon_delegate", required_argument, nullptr, 'j'},
{"xnnpack_delegate", required_argument, nullptr, 'x'},
{nullptr, 0, nullptr, 0}};

/* getopt_long stores the option index here. */
int option_index = 0;

c = getopt_long(argc, argv,
"a:b:c:d:e:f:g:i:j:l:m:p:r:s:t:v:w:", long_options,
"a:b:c:d:e:f:g:i:j:l:m:p:r:s:t:v:w:x:", long_options,
&option_index);

/* Detect the end of the options. */
Expand Down Expand Up @@ -450,6 +466,9 @@ int Main(int argc, char** argv) {
s.number_of_warmup_runs =
strtol(optarg, nullptr, 10); // NOLINT(runtime/deprecated_fn)
break;
case 'x':
s.xnnpack_delegate = optarg;
break;
case 'h':
case '?':
/* getopt_long already printed an error message. */
Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/examples/label_image/label_image.h
Expand Up @@ -31,6 +31,7 @@ struct Settings {
bool allow_fp16 = false;
bool gl_backend = false;
bool hexagon_delegate = false;
bool xnnpack_delegate = false;
int loop_count = 1;
float input_mean = 127.5f;
float input_std = 127.5f;
Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/tools/evaluation/BUILD
Expand Up @@ -43,6 +43,7 @@ cc_library(
"//tensorflow/lite:context",
"//tensorflow/lite:framework",
"//tensorflow/lite/delegates/nnapi:nnapi_delegate",
"//tensorflow/lite/delegates/xnnpack:xnnpack_delegate",
] + select({
"//tensorflow:android": [
"//tensorflow/lite/delegates/gpu:delegate",
Expand Down
14 changes: 14 additions & 0 deletions tensorflow/lite/tools/evaluation/utils.cc
Expand Up @@ -164,5 +164,19 @@ Interpreter::TfLiteDelegatePtr CreateHexagonDelegate(
#endif // defined(__ANDROID__)
}

Interpreter::TfLiteDelegatePtr CreateXNNPACKDelegate() {
TfLiteXNNPackDelegateOptions xnnpack_options =
TfLiteXNNPackDelegateOptionsDefault();
return CreateXNNPACKDelegate(&xnnpack_options);
}

Interpreter::TfLiteDelegatePtr CreateXNNPACKDelegate(
const TfLiteXNNPackDelegateOptions* xnnpack_options) {
auto xnnpack_delegate = TfLiteXNNPackDelegateCreate(xnnpack_options);
return Interpreter::TfLiteDelegatePtr(
xnnpack_delegate,
[](TfLiteDelegate* delegate) { TfLiteXNNPackDelegateDelete(delegate); });
}

} // namespace evaluation
} // namespace tflite
5 changes: 5 additions & 0 deletions tensorflow/lite/tools/evaluation/utils.h
Expand Up @@ -29,6 +29,7 @@ limitations under the License.

#include "tensorflow/lite/context.h"
#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h"
#include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h"
#include "tensorflow/lite/model.h"

namespace tflite {
Expand Down Expand Up @@ -64,6 +65,10 @@ Interpreter::TfLiteDelegatePtr CreateGPUDelegate(
Interpreter::TfLiteDelegatePtr CreateHexagonDelegate(
const std::string& library_directory_path, bool profiling);

Interpreter::TfLiteDelegatePtr CreateXNNPACKDelegate();
Interpreter::TfLiteDelegatePtr CreateXNNPACKDelegate(
const TfLiteXNNPackDelegateOptions* options);

} // namespace evaluation
} // namespace tflite

Expand Down
14 changes: 14 additions & 0 deletions third_party/cpuinfo/BUILD.bazel
Expand Up @@ -101,6 +101,8 @@ MACH_ARM_SRCS = [
cc_library(
name = "cpuinfo_impl",
srcs = select({
":linux_aarch64": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM64_SRCS,
":linux_arm": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS,
":linux_x86_64": COMMON_SRCS + X86_SRCS + LINUX_SRCS + LINUX_X86_SRCS,
":macos_x86_64": COMMON_SRCS + X86_SRCS + MACH_SRCS + MACH_X86_SRCS,
":android_armv7": COMMON_SRCS + ARM_SRCS + LINUX_SRCS + LINUX_ARM32_SRCS + ANDROID_ARM_SRCS,
Expand Down Expand Up @@ -159,6 +161,18 @@ cc_library(

############################# Build configurations #############################

config_setting(
name = "linux_aarch64",
values = {"cpu": "aarch64"},
visibility = ["//visibility:public"],
)

config_setting(
name = "linux_arm",
values = {"cpu": "arm"},
visibility = ["//visibility:public"],
)

config_setting(
name = "linux_x86_64",
values = {"cpu": "k8"},
Expand Down