ภาพรวม
เมื่อติดตั้งใช้งานโมเดลสำหรับแอปพลิเคชันแมชชีนเลิร์นนิงในอุปกรณ์ (ODML) คุณควรทราบว่าอุปกรณ์เคลื่อนที่มีหน่วยความจำจำกัด ขนาดไบนารีของโมเดลมีความสัมพันธ์อย่างใกล้ชิดกับจำนวนการดำเนินการที่ใช้ในโมเดล LiteRT ช่วยให้คุณลดขนาดไบนารีของโมเดลได้โดยใช้การบิลด์แบบเลือก การสร้างแบบเลือกจะข้ามการดำเนินการที่ไม่ได้ใช้ในชุดโมเดลและสร้าง ไลบรารีขนาดกะทัดรัดที่มีเฉพาะรันไทม์และเคอร์เนลของ Op ที่จำเป็นสำหรับโมเดล เพื่อเรียกใช้ในอุปกรณ์เคลื่อนที่
การสร้างแบบเลือกจะมีผลกับไลบรารีการดำเนินการ 3 รายการต่อไปนี้
- ไลบรารีการดำเนินการในตัวของ LiteRT
- การดำเนินการที่กำหนดเองของ LiteRT
- เลือกไลบรารีการดำเนินการของ 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 ไบต์) |
สร้าง LiteRT ด้วย Bazel แบบเลือก
ส่วนนี้ถือว่าคุณได้ดาวน์โหลดซอร์สโค้ด TensorFlow และตั้งค่าสภาพแวดล้อมการพัฒนาภายในสำหรับ Bazel แล้ว
สร้างไฟล์ AAR สำหรับโปรเจ็กต์ Android
คุณสร้าง 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
คำสั่งข้างต้นจะสร้างไฟล์ AAR bazel-bin/tmp/tensorflow-lite.aar
สำหรับตัวดำเนินการในตัวและตัวดำเนินการที่กำหนดเองของ LiteRT และอาจสร้างไฟล์ AAR
bazel-bin/tmp/tensorflow-lite-select-tf-ops.aar หากโมเดลมีตัวดำเนินการ TensorFlow ของ Select
โปรดทราบว่าการดำเนินการนี้จะสร้าง AAR แบบ "Fat" ที่มีสถาปัตยกรรมที่แตกต่างกันหลายแบบ หากไม่ต้องการทั้งหมด ให้ใช้ชุดย่อยที่เหมาะสมกับสภาพแวดล้อมการติดตั้งใช้งาน
สร้างด้วยการดำเนินการที่กำหนดเอง
หากพัฒนาโมเดล 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 จะมีทรัพยากร Dependency สำหรับสร้างไฟล์ต้นฉบับเหล่านั้น โปรดทราบว่าการอ้างอิงเหล่านี้ต้องอยู่ในที่เก็บ 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",
],
)
สำหรับโมเดลที่มี Select TF ops ให้ทำดังนี้
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++ ที่กำหนดเอง
หากต้องการสร้างออบเจ็กต์ที่ใช้ร่วมกัน C/C++ ของ TFLite ที่กำหนดเองสำหรับโมเดลที่ระบุ ให้ทำตามขั้นตอนด้านล่าง
สร้างไฟล์ BUILD ชั่วคราวโดยเรียกใช้คำสั่งต่อไปนี้ที่ไดเรกทอรีรากของซอร์สโค้ด TensorFlow
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
สร้าง LiteRT ด้วย Docker แบบเลือก
ส่วนนี้ถือว่าคุณได้ติดตั้ง Docker ในเครื่องของคุณและดาวน์โหลด Dockerfile ของ LiteRT ที่นี่แล้ว
หลังจากดาวน์โหลด Dockerfile ข้างต้นแล้ว คุณจะสร้างอิมเมจ Docker ได้โดย เรียกใช้คำสั่งต่อไปนี้
docker build . -t tflite-builder -f tflite-android.Dockerfile
สร้างไฟล์ AAR สำหรับโปรเจ็กต์ Android
ดาวน์โหลดสคริปต์สำหรับการสร้างด้วย 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
จากนั้นคุณจะสร้าง AAR ของ LiteRT ที่กำหนดเองได้โดยระบุเส้นทางไฟล์โมเดลดังนี้
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 ที่คุณต้องการ
เช็คเอาต์ก่อนสร้างไลบรารี โดยค่าเริ่มต้นจะเป็นสาขาการเผยแพร่ล่าสุด
คำสั่งข้างต้นจะสร้างไฟล์ AAR
tensorflow-lite.aar สำหรับการดำเนินการในตัวและที่กำหนดเองของ LiteRT และอาจสร้างไฟล์ AAR
tensorflow-lite-select-tf-ops.aar สำหรับการดำเนินการ TensorFlow ที่เลือกในไดเรกทอรีปัจจุบัน
--cache_dir ระบุไดเรกทอรีแคช หากไม่ได้ระบุไว้ สคริปต์จะสร้างไดเรกทอรีชื่อ bazel-build-cache ในไดเรกทอรีที่ใช้งานปัจจุบันเพื่อแคช
เพิ่มไฟล์ AAR ลงในโปรเจ็กต์
เพิ่มไฟล์ AAR โดยนำเข้า AAR ลงใน
โปรเจ็กต์โดยตรง หรือเผยแพร่
AAR ที่กำหนดเองไปยังที่เก็บ Maven ในเครื่อง โปรดทราบ
ว่าคุณต้องเพิ่มไฟล์ AAR สำหรับ tensorflow-lite-select-tf-ops.aar ด้วย
หากสร้างไฟล์ดังกล่าว
การบิลด์แบบเลือกสำหรับ iOS
โปรดดูส่วนการสร้างในเครื่องเพื่อตั้งค่า สภาพแวดล้อมการสร้างและกำหนดค่าพื้นที่ทำงาน TensorFlow จากนั้นทำตาม คำแนะนำเพื่อใช้สคริปต์การสร้างแบบเลือก สำหรับ iOS