概览
为设备端机器学习 (ODML) 应用部署模型时,务必注意移动设备上可用的内存有限。模型二进制文件大小与模型中使用的操作数密切相关。借助 LiteRT,您可以使用选择性 build 来减小模型二进制文件的大小。选择性 build 会跳过模型集中未使用的操作,并生成一个紧凑的库,其中仅包含运行时和模型在移动设备上运行所需的 op kernel。
选择性 build 适用于以下三个操作库。
下表展示了选择性 build 对一些常见用例的影响:
| 模型名称 | 网域 | 目标架构 | AAR 文件大小 |
|---|---|---|---|
| Mobilenet_1.0_224(float) | 图片分类 | armeabi-v7a | tensorflow-lite.aar(296,635 字节) |
| arm64-v8a | tensorflow-lite.aar(382,892 字节) | ||
| SPICE | 声音音调提取 | armeabi-v7a | tensorflow-lite.aar(375,813 字节) tensorflow-lite-select-tf-ops.aar(1,676,380 字节) |
| arm64-v8a | tensorflow-lite.aar(421,826 字节) tensorflow-lite-select-tf-ops.aar(2,298,630 字节) |
||
| i3d-kinetics-400 | 视频分类 | armeabi-v7a | tensorflow-lite.aar(240,085 字节) tensorflow-lite-select-tf-ops.aar(1,708,597 字节) |
| arm64-v8a | tensorflow-lite.aar(273,713 字节) tensorflow-lite-select-tf-ops.aar(2,339,697 字节) |
使用 Bazel 选择性地构建 LiteRT
本部分假设您已下载 TensorFlow 源代码并设置本地开发环境以使用 Bazel。
为 Android 项目构建 AAR 文件
您可以通过提供模型文件路径来构建自定义 LiteRT AAR,如下所示。
sh tensorflow/lite/tools/build_aar.sh \
--input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
--target_archs=x86,x86_64,arm64-v8a,armeabi-v7a
上述命令将为 LiteRT 内置和自定义操作生成 AAR 文件 bazel-bin/tmp/tensorflow-lite.aar;如果您的模型包含 Select TensorFlow 操作,则可以选择性地生成 AAR 文件 bazel-bin/tmp/tensorflow-lite-select-tf-ops.aar。请注意,这会构建一个包含多种不同架构的“胖”AAR;如果您不需要所有这些架构,请使用适合您的部署环境的子集。
使用自定义操作进行构建
如果您已开发出包含自定义运算的 LiteRT 模型,则可以通过在 build 命令中添加以下标志来构建这些模型:
sh tensorflow/lite/tools/build_aar.sh \
--input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
--target_archs=x86,x86_64,arm64-v8a,armeabi-v7a \
--tflite_custom_ops_srcs=/e/f/file1.cc,/g/h/file2.h \
--tflite_custom_ops_deps=dep1,dep2
tflite_custom_ops_srcs 标志包含自定义操作的源文件,tflite_custom_ops_deps 标志包含用于构建这些源文件的依赖项。请注意,这些依赖项必须存在于 TensorFlow 代码库中。
高级用法:自定义 Bazel 规则
如果您的项目使用 Bazel,并且您想为一组给定的模型定义自定义 TFLite 依赖项,则可以在项目代码库中定义以下规则:
对于仅具有内置运算的模型:
load(
"@org_tensorflow//tensorflow/lite:build_def.bzl",
"tflite_custom_android_library",
"tflite_custom_c_library",
"tflite_custom_cc_library",
)
# A selectively built TFLite Android library.
tflite_custom_android_library(
name = "selectively_built_android_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# A selectively built TFLite C library.
tflite_custom_c_library(
name = "selectively_built_c_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# A selectively built TFLite C++ library.
tflite_custom_cc_library(
name = "selectively_built_cc_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
对于具有选择 TF 操作的模型:
load(
"@org_tensorflow//tensorflow/lite/delegates/flex:build_def.bzl",
"tflite_flex_android_library",
"tflite_flex_cc_library",
)
# A Select TF ops enabled selectively built TFLite Android library.
tflite_flex_android_library(
name = "selective_built_tflite_flex_android_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# A Select TF ops enabled selectively built TFLite C++ library.
tflite_flex_cc_library(
name = "selective_built_tflite_flex_cc_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
高级用法:构建自定义 C/C++ 共享库
如果您想针对给定模型构建自己的自定义 TFLite C/C++ 共享对象,可以按照以下步骤操作:
在 TensorFlow 源代码的根目录下运行以下命令,以创建临时 BUILD 文件:
mkdir -p tmp && touch tmp/BUILD
构建自定义 C 共享对象
如果您想构建自定义 TFLite C 共享对象,请将以下内容添加到 tmp/BUILD 文件:
load(
"//tensorflow/lite:build_def.bzl",
"tflite_custom_c_library",
"tflite_cc_shared_object",
)
tflite_custom_c_library(
name = "selectively_built_c_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# Generates a platform-specific shared library containing the LiteRT C
# API implementation as define in `c_api.h`. The exact output library name
# is platform dependent:
# - Linux/Android: `libtensorflowlite_c.so`
# - Mac: `libtensorflowlite_c.dylib`
# - Windows: `tensorflowlite_c.dll`
tflite_cc_shared_object(
name = "tensorflowlite_c",
linkopts = select({
"//tensorflow:ios": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite/c:exported_symbols.lds)",
],
"//tensorflow:macos": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite/c:exported_symbols.lds)",
],
"//tensorflow:windows": [],
"//conditions:default": [
"-z defs",
"-Wl,--version-script,$(location //tensorflow/lite/c:version_script.lds)",
],
}),
per_os_targets = True,
deps = [
":selectively_built_c_lib",
"//tensorflow/lite/c:exported_symbols.lds",
"//tensorflow/lite/c:version_script.lds",
],
)
新添加的目标可以按如下方式构建:
bazel build -c opt --cxxopt=--std=c++17 \
//tmp:tensorflowlite_c
对于 Android(将 android_arm 替换为 android_arm64 [64 位]):
bazel build -c opt --cxxopt=--std=c++17 --config=android_arm \
//tmp:tensorflowlite_c
构建自定义 C++ 共享对象
如果您想构建自定义 TFLite C++ 共享对象,请将以下内容添加到 tmp/BUILD 文件中:
load(
"//tensorflow/lite:build_def.bzl",
"tflite_custom_cc_library",
"tflite_cc_shared_object",
)
tflite_custom_cc_library(
name = "selectively_built_cc_lib",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
# Shared lib target for convenience, pulls in the core runtime and builtin ops.
# Note: This target is not yet finalized, and the exact set of exported (C/C++)
# APIs is subject to change. The output library name is platform dependent:
# - Linux/Android: `libtensorflowlite.so`
# - Mac: `libtensorflowlite.dylib`
# - Windows: `tensorflowlite.dll`
tflite_cc_shared_object(
name = "tensorflowlite",
# Until we have more granular symbol export for the C++ API on Windows,
# export all symbols.
features = ["windows_export_all_symbols"],
linkopts = select({
"//tensorflow:macos": [
"-Wl,-exported_symbols_list,$(location //tensorflow/lite:tflite_exported_symbols.lds)",
],
"//tensorflow:windows": [],
"//conditions:default": [
"-Wl,-z,defs",
"-Wl,--version-script,$(location //tensorflow/lite:tflite_version_script.lds)",
],
}),
per_os_targets = True,
deps = [
":selectively_built_cc_lib",
"//tensorflow/lite:tflite_exported_symbols.lds",
"//tensorflow/lite:tflite_version_script.lds",
],
)
新添加的目标可以按如下方式构建:
bazel build -c opt --cxxopt=--std=c++17 \
//tmp:tensorflowlite
对于 Android(将 android_arm 替换为 android_arm64 [64 位]):
bazel build -c opt --cxxopt=--std=c++17 --config=android_arm \
//tmp:tensorflowlite
对于具有“选择 TF 操作”的模型,您还需要构建以下共享库:
load(
"@org_tensorflow//tensorflow/lite/delegates/flex:build_def.bzl",
"tflite_flex_shared_library"
)
# Shared lib target for convenience, pulls in the standard set of TensorFlow
# ops and kernels. The output library name is platform dependent:
# - Linux/Android: `libtensorflowlite_flex.so`
# - Mac: `libtensorflowlite_flex.dylib`
# - Windows: `libtensorflowlite_flex.dll`
tflite_flex_shared_library(
name = "tensorflowlite_flex",
models = [
":model_one.tflite",
":model_two.tflite",
],
)
新添加的目标可以按如下方式构建:
bazel build -c opt --cxxopt='--std=c++17' \
--config=monolithic \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tmp:tensorflowlite_flex
对于 Android(将 android_arm 替换为 android_arm64 [64 位]):
bazel build -c opt --cxxopt='--std=c++17' \
--config=android_arm \
--config=monolithic \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tmp:tensorflowlite_flex
使用 Docker 有选择地构建 LiteRT
本部分假设您已在本地机器上安装 Docker,并已在此处下载 LiteRT Dockerfile。
下载上述 Dockerfile 后,您可以通过运行以下命令来构建 Docker 映像:
docker build . -t tflite-builder -f tflite-android.Dockerfile
为 Android 项目构建 AAR 文件
运行以下命令,下载用于通过 Docker 进行构建的脚本:
curl -o build_aar_with_docker.sh \
https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/lite/tools/build_aar_with_docker.sh &&
chmod +x build_aar_with_docker.sh
然后,您可以按如下方式提供模型文件路径,以构建自定义 LiteRT AAR。
sh build_aar_with_docker.sh \
--input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
--target_archs=x86,x86_64,arm64-v8a,armeabi-v7a \
--checkpoint=master \
[--cache_dir=<path to cache directory>]
checkpoint 标志是 TensorFlow 代码库的提交、分支或标记,您希望在构建库之前签出该标志;默认情况下,它是最新的发布分支。上述命令将在当前目录中为 LiteRT 内置和自定义运算生成 AAR 文件 tensorflow-lite.aar,并可选择性地为精选 TensorFlow 运算生成 AAR 文件 tensorflow-lite-select-tf-ops.aar。
--cache_dir 用于指定缓存目录。如果未提供,脚本将在当前工作目录下创建一个名为 bazel-build-cache 的目录以用于缓存。
将 AAR 文件添加到项目
通过以下方式添加 AAR 文件:直接将 AAR 导入到您的项目中,或将自定义 AAR 发布到您的本地 Maven 制品库。请注意,如果您生成 tensorflow-lite-select-tf-ops.aar 的 AAR 文件,也必须添加这些文件。
针对 iOS 进行选择性构建
请参阅本地构建部分,设置构建环境并配置 TensorFlow 工作区,然后按照指南使用适用于 iOS 的选择性构建脚本。