LiteRT ใน Google Play Services C และ C++ API

LiteRT ในรันไทม์ของบริการ Google Play ช่วยให้คุณเรียกใช้โมเดลแมชชีนเลิร์นนิง (ML) ได้โดยไม่ต้องรวมไลบรารี LiteRT ไว้ในแอปแบบคงที่ คู่มือนี้จะแสดงวิธีใช้ C หรือ C++ API สำหรับบริการ Google Play

ก่อนใช้งาน LiteRT ใน C API หรือ C++ API ของบริการ Google Play ให้ตรวจสอบว่าคุณได้ติดตั้งเครื่องมือสร้าง CMake แล้ว

อัปเดตการกำหนดค่าบิลด์

(1) เพิ่ม Dependency ต่อไปนี้ลงในโค้ดโปรเจ็กต์แอปเพื่อเข้าถึง Playservices API สำหรับ LiteRT

implementation "com.google.android.gms:play-services-tflite-java:16.4.0"

โปรดทราบว่าแม้ว่าชื่อแพ็กเกจจะลงท้ายด้วย -java แต่แพ็กเกจดังกล่าวก็มี API ของ C และ C++ ด้วย

(2) จากนั้นเปิดใช้ฟีเจอร์ Prefab เพื่อเข้าถึง C API จากสคริปต์ CMake โดยอัปเดตบล็อก android ของไฟล์ build.gradle ของโมดูล ดังนี้

buildFeatures {
  prefab = true
}

(3) [C++ API เท่านั้น] หากคุณใช้ C++ API ให้คัดลอก tflite-java-extract-cpp-sdk.gradle ลงในโปรเจ็กต์ในไดเรกทอรี app แล้วเพิ่มบรรทัดต่อไปนี้ไว้ที่จุดเริ่มต้นของสคริปต์ gradle ของแอป (เช่น app/build.gradle)

apply from: 'tflite-java-extract-cpp-sdk.gradle'

ซึ่งประกอบด้วยโค้ด Gradle เพื่อแตกไฟล์ SDK ของ C++ จากไฟล์ AAR สำหรับ play-services-tflite-java โดยอัตโนมัติ

(4) [C++ API เท่านั้น] หากคุณใช้ C++ API ให้ค้นหาไดเรกทอรีที่มีไฟล์กำหนดค่า CMake ของแอป (ปกติจะเป็น CMakeLists.txt) โดยปกติแล้วไดเรกทอรีดังกล่าวจะเป็นไดเรกทอรี app/src/main/cpp จากนั้นคัดลอก Findtflite_cc_api.cmake ไปยังโปรเจ็กต์ของคุณในไดเรกทอรีย่อย Modules ใหม่ของไดเรกทอรีนั้น ไฟล์นี้มีโค้ดที่ค้นหา SDK ของ C++ ที่แกะไฟล์โดยสคริปต์ Gradle ในขั้นตอนก่อนหน้า

(5) สุดท้ายคุณต้องเพิ่มแพ็กเกจ tensorflowlite_jni_gms_client และแพ็กเกจ tflite_cc_api สำหรับ C++ API ซึ่งทั้ง 2 รายการจะนำเข้าจาก AAR เพื่อเป็น Dependency ในสคริปต์ CMake

C

find_package(tensorflowlite_jni_gms_client REQUIRED CONFIG)

# Set up C/C++ compiler flags to enable use of TFLite in Play services
# (rather than regular TFLite bundled with the app).
add_compile_definitions(TFLITE_IN_GMSCORE)
add_compile_definitions(TFLITE_WITH_STABLE_ABI)

target_link_libraries(tflite-jni # your JNI lib target
        tensorflowlite_jni_gms_client::tensorflowlite_jni_gms_client
        android # other deps for your target
        log)
      

C++

# Set up TFLite in Play services C API (tensorflowlite_jni_gms_client) dependency.

find_package(tensorflowlite_jni_gms_client REQUIRED CONFIG)

# Set up TFLite in Play services C++ API (tflite_cc_api) dependency.

list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Modules")

find_package(tflite_cc_api REQUIRED MODULE)
include_directories(${tflite_cc_api_INCLUDE_DIR})
add_subdirectory(${tflite_cc_api_DIR} tflite_cc_api_build)

# Set up C/C++ compiler flags to enable use of TFLite in Play services
# (rather than regular TFLite bundled with the app).
add_compile_definitions(TFLITE_IN_GMSCORE)
add_compile_definitions(TFLITE_WITH_STABLE_ABI)

target_link_libraries(tflite-jni # your JNI lib target
        tflite_cc_api::tflite_cc_api
        tensorflowlite_jni_gms_client::tensorflowlite_jni_gms_client
        android # other deps for your target
        log)
      

เริ่มต้นรันไทม์ LiteRT

ก่อนเรียกใช้ LiteRT C หรือ C++ API คุณต้องเริ่มต้นรันไทม์ TfLiteNative ในโค้ด Java หรือ Kotlin

Java

Task tfLiteInitializeTask = TfLiteNative.initialize(context);
      

Kotlin

val tfLiteInitializeTask: Task = TfLiteNative.initialize(context)
        

TfLiteNative.initializeใช้ Task API ของบริการ Google Play เพื่อโหลดรันไทม์ TFLite จากบริการ Google Play ไปยังกระบวนการรันไทม์ของแอปแบบไม่พร้อมกัน ใช้ addOnSuccessListener() เพื่อให้แน่ใจว่างาน TfLite.initialize() เสร็จสมบูรณ์ก่อนที่จะเรียกใช้โค้ดที่เข้าถึง LiteRT API เมื่องานเสร็จสมบูรณ์แล้ว คุณจะเรียกใช้ TFLite Native API ทั้งหมดที่มีอยู่ได้

การติดตั้งใช้งานโค้ดเนทีฟ

หากต้องการใช้ LiteRT ในบริการ Google Play กับโค้ด C/C++ คุณจะทำอย่างใดอย่างหนึ่ง (หรือทั้ง 2 อย่าง) ต่อไปนี้ได้

  • ประกาศฟังก์ชัน JNI ใหม่เพื่อเรียกใช้ฟังก์ชัน C หรือ C++ จากโค้ด Java
  • เรียกใช้ LiteRT Native API จากโค้ด C หรือ C++ ที่มีอยู่

ฟังก์ชัน JNI

คุณสามารถประกาศฟังก์ชัน JNI ใหม่เพื่อให้รันไทม์ LiteRT ที่ประกาศในโค้ด C/C++ เข้าถึงโค้ด Java/Kotlin ได้ ดังนี้

Java

package com.google.samples.gms.tflite.c;

public class TfLiteJni {
  static {
    System.loadLibrary("tflite-jni");
  }
  public TfLiteJni() { /**/ };
  public native void loadModel(AssetManager assetManager, String assetName);
  public native float[] runInference(float[] input);  // For example.
}
      

Kotlin

package com.google.samples.gms.tflite.c

class TfLiteJni() {
  companion object {
    init {
      System.loadLibrary("tflite-jni")
    }
  }
  external fun loadModel(assetManager: AssetManager, assetName: String)
  external fun runInference(input: FloatArray): FloatArray  // For example.
}
        

การจับคู่ฟังก์ชัน loadModel และ runInference C หรือ C++ ต่อไปนี้

#ifdef __cplusplus
extern "C" {
#endif

void Java_com_google_samples_gms_tflite_c_loadModel(
  JNIEnv *env, jobject tflite_jni, jobject asset_manager, jstring asset_name){
  //...
}

jfloatArray Java_com_google_samples_gms_tflite_c_TfLiteJni_runInference(
  JNIEnv* env, jobject tfliteJni, jfloatArray input) {
  //...
}

#ifdef __cplusplus
}  // extern "C".
#endif

จากนั้นคุณจะเรียกใช้ฟังก์ชัน C/C++ จากโค้ด Java/Kotlin ได้โดยทำดังนี้

Java

tfLiteHandleTask.onSuccessTask(unused -> {
    TfLiteJni jni = new TfLiteJni();
    jni.loadModel(getAssets(), "add.bin");
    //...
});
    

Kotlin

tfLiteHandleTask.onSuccessTask {
    val jni = TfLiteJni()
    jni.loadModel(assets, "add.bin")
    // ...
}
      

LiteRT ในโค้ดเนทีฟ

รวมไฟล์ส่วนหัว API ที่เหมาะสมเพื่อรวม LiteRT กับ Google Play Services API

C

#include "tensorflow/lite/c/c_api.h"
      

C++

#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/model_builder.h"
      

จากนั้นคุณจะใช้ LiteRT C หรือ C++ API ปกติได้

C

TfLiteModel* model = TfLiteModelCreate(model_asset, model_asset_length);
// ...
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
// ...
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
      

C++

  // Load the model.
  auto model = tflite::FlatBufferModel::VerifyAndBuildFromBuffer(
      model_asset, model_asset_length);
  ...
  // Initialize the interpreter.
  BuiltinOpResolver op_resolver;
  InterpreterBuilder interpreter_builder(*model, op_resolver);
  interpreter_builder(&interpreter);
  std::unique_ptr<Interpreter>` interpreter;
  interpreter_builder(&interpreter);
      

API ที่รองรับ

C

LiteRT ที่มีส่วนหัว C API ของบริการ Google Play มี API เดียวกับ LiteRT C API ปกติ ยกเว้นฟีเจอร์ที่เลิกใช้งานหรืออยู่ในขั้นทดลอง ขณะนี้มีฟังก์ชันและประเภทจากส่วนหัวต่อไปนี้

TensorFlow Lite API สําหรับการโหลดและเรียกใช้โมเดล

tensorflow/lite/c/c_api.h
tensorflow/lite/c/c_api_types.h
      

TensorFlow Lite Extension API สําหรับการกําหนดการดำเนินการและตัวแทนที่กําหนดเอง (เช่น สําหรับการเร่งด้วยฮาร์ดแวร์)

tensorflow/lite/c/c_api_opaque.h
tensorflow/lite/c/common.h
tensorflow/lite/c/builtin_op_data.h
tensorflow/lite/builtin_ops.h
      

API ของปลั๊กอินที่รับมอบสิทธิ์สำหรับการใช้ผู้รับมอบสิทธิ์ที่มีอยู่

tensorflow/lite/acceleration/configuration/c/gpu_plugin.h
tensorflow/lite/acceleration/configuration/c/xnnpack_plugin.h
      

โปรดทราบว่าระบบไม่รองรับฟังก์ชันจากส่วนหัว c_api_experimental.h

คุณสามารถใช้ฟังก์ชันเฉพาะของ LiteRT กับบริการ Google Play ได้โดยใส่ส่วนหัวต่อไปนี้

tensorflow/lite/abi/tflite.h

C++

ส่วนหัว LiteRT ที่มี C++ API ของบริการ Google Play มี API เดียวกับ LiteRT C++ API ปกติ ยกเว้นฟีเจอร์ที่เลิกใช้งานหรืออยู่ในขั้นทดลอง และมีข้อยกเว้นเล็กน้อยที่ระบุไว้ในส่วนนี้ ฟังก์ชันการทำงานจากส่วนหัวต่อไปนี้พร้อมใช้งาน

tensorflow/lite/model_builder.h
tensorflow/lite/interpreter_builder.h
tensorflow/lite/interpreter.h
tensorflow/lite/signature_runner.h
tensorflow/lite/acceleration/configuration/delegate_registry.h
tensorflow/lite/kernels/builtin_op_kernels.h
tensorflow/lite/kernels/register.h
tensorflow/lite/tools/verifier.h
      

สำหรับ tensorflow/lite/interpreter.h API ที่รองรับกับบริการ Play จะยกเว้นสมาชิกบางรายของ tflite::Interpreter ที่ LiteRT ไม่มี ABI ที่เสถียร ดังนี้

Interpreter::variables()
Interpreter::nodes_size()
Interpreter::node_and_registration(int node_index)
Interpreter::kTensorsReservedCapacity
Interpreter::kTensorsCapacityHeadroom
Interpreter::OpProfilingString(const TfLiteRegistration&, const TfLiteNode*)
Interpreter::SetExternalContext(TfLiteExternalContextType type, TfLiteExternalContext* ctx)