縮減 LiteRT 二進位檔大小

總覽

為裝置端機器學習 (ODML) 應用程式部署模型時,請務必留意行動裝置的可用記憶體有限。模型二進位檔大小與模型中使用的作業數量密切相關。LiteRT 可讓您使用選擇性建構作業,縮減模型二進位檔大小。選擇性建構會略過模型集中未使用的作業,並產生精簡的程式庫,其中只包含在行動裝置上執行模型所需的執行階段和作業核心。

選擇性建構適用於下列三個作業程式庫。

  1. LiteRT 內建作業程式庫
  2. LiteRT 自訂作業
  3. 選取 TensorFlow 運算程式庫

下表說明在一些常見用途中,選擇性建構的影響:

模型名稱 網域 目標架構 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 模型,可以將下列標記新增至建構指令,藉此建構模型:

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,並視需要為目前目錄中的 Select TensorFlow 作業產生 AAR 檔案 tensorflow-lite-select-tf-ops.aar

--cache_dir 會指定快取目錄。如未提供,指令碼會在目前的工作目錄下建立名為 bazel-build-cache 的目錄,用於快取。

將 AAR 檔案新增至專案

直接將 AAR 匯入專案,或將自訂 AAR 發布至本機 Maven 存放區,即可新增 AAR 檔案。請注意,如果產生 AAR 檔案,您也必須新增 tensorflow-lite-select-tf-ops.aar 的 AAR 檔案。

iOS 選擇性建構

請參閱「在本機建構」一節,設定建構環境及設定 TensorFlow 工作區,然後按照指南使用 iOS 的選擇性建構指令碼。