Google Play services के रनटाइम में LiteRT की मदद से, मशीन लर्निंग (एमएल) मॉडल को चलाया जा सकता है. इसके लिए, LiteRT लाइब्रेरी को अपने ऐप्लिकेशन में स्टैटिक तौर पर बंडल करने की ज़रूरत नहीं होती. इस गाइड में, Google Play services के लिए C या C++ एपीआई इस्तेमाल करने के बारे में निर्देश दिए गए हैं.
Google Play services C API या C++ API में LiteRT का इस्तेमाल करने से पहले, पक्का करें कि आपने CMake बिल्ड टूल इंस्टॉल किया हो.
अपने बिल्ड कॉन्फ़िगरेशन को अपडेट करना
(1) LiteRT के लिए Play services API को ऐक्सेस करने के लिए, अपने ऐप्लिकेशन प्रोजेक्ट कोड में ये डिपेंडेंसी जोड़ें:
implementation "com.google.android.gms:play-services-tflite-java:16.4.0"
ध्यान दें कि पैकेज का नाम -java पर खत्म होता है. हालांकि, इस पैकेज में C और C++ एपीआई भी शामिल हैं.
(2) इसके बाद, अपनी CMake स्क्रिप्ट से C API को ऐक्सेस करने के लिए, Prefab सुविधा चालू करें. इसके लिए, अपने मॉड्यूल की build.gradle फ़ाइल के android ब्लॉक को अपडेट करें:
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'
इसमें play-services-tflite-java के लिए, AAR फ़ाइल से C++ SDK को अपने-आप अनपैक करने वाला Gradle कोड होता है.
(4) [सिर्फ़ C++ API के लिए] अगर C++ API का इस्तेमाल किया जा रहा है, तो उस डायरेक्ट्री को ढूंढें जिसमें आपके ऐप्लिकेशन की CMake कॉन्फ़िगरेशन फ़ाइल मौजूद है. आम तौर पर, यह CMakeLists.txt होती है. यह डायरेक्ट्री आम तौर पर आपकी app/src/main/cpp डायरेक्ट्री होती है. इसके बाद, Findtflite_cc_api.cmake को अपने प्रोजेक्ट में कॉपी करें. इसके लिए, उस डायरेक्ट्री की नई Modules सबडायरेक्ट्री में जाएं.
इसमें ऐसा कोड होता है जो पिछले चरण में Gradle स्क्रिप्ट से अनपैक किए गए C++ SDK को ढूंढता है.
(5) आखिर में, आपको पैकेज tensorflowlite_jni_gms_client जोड़ना होगा. साथ ही, C++ API के लिए पैकेज tflite_cc_api भी जोड़ना होगा. इन दोनों को AAR से इंपोर्ट किया जाता है. इन्हें CMake स्क्रिप्ट में डिपेंडेंसी के तौर पर जोड़ा जाता है:
C
find_package(tensorflowlite_jni_gms_client REQUIRED CONFIG) # Set up C/C++ compiler flags to enable use of LiteRT in Play services # (rather than regular LiteRT 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 LiteRT in Play services C API (tensorflowlite_jni_gms_client) dependency. find_package(tensorflowlite_jni_gms_client REQUIRED CONFIG) # Set up LiteRT 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 LiteRT in Play services # (rather than regular LiteRT 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 को कॉल करने से पहले, आपको अपने Java या Kotlin कोड में TfLiteNative रनटाइम को शुरू करना होगा.
Java
Task tfLiteInitializeTask = TfLiteNative.initialize(context);
Kotlin
val tfLiteInitializeTask: Task= TfLiteNative.initialize(context)
Google Play services Task API का इस्तेमाल करके, TfLiteNative.initializeGoogle Play services से LiteRT रनटाइम को आपके ऐप्लिकेशन की रनटाइम प्रोसेस में एसिंक्रोनस तरीके से लोड करता है. addOnSuccessListener() का इस्तेमाल करके यह पक्का करें कि LiteRT API को ऐक्सेस करने वाले कोड को लागू करने से पहले, TfLite.initialize() टास्क पूरा हो गया हो. टास्क पूरा होने के बाद, उपलब्ध सभी LiteRT Native API को चालू किया जा सकता है.
नेटिव कोड लागू करना
Google Play services में C/C++ कोड के साथ LiteRT का इस्तेमाल करने के लिए, इनमें से कोई एक या दोनों काम किए जा सकते हैं:
- अपने Java कोड से C या C++ फ़ंक्शन को कॉल करने के लिए, नए JNI फ़ंक्शन का एलान करें
- अपने मौजूदा C या C++ कोड से, LiteRT Native API को कॉल करें.
JNI फ़ंक्शन
C/C++ कोड में एलान किए गए LiteRT रनटाइम को अपने Java/Kotlin कोड में ऐक्सेस करने के लिए, नए JNI फ़ंक्शन का एलान किया जा सकता है. इसके लिए, यह तरीका अपनाएं:
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
इसके बाद, अपने Java/Kotlin कोड से C/C++ फ़ंक्शन को कॉल किया जा सकता है:
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
Google Play सेवाओं के एपीआई के साथ LiteRT को शामिल करने के लिए, सही एपीआई हेडर फ़ाइल शामिल करें:
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);
काम करने वाले एपीआई
C
Google Play services के C API हेडर के साथ LiteRT, 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
मौजूदा डेलिगेट का इस्तेमाल करने के लिए, डेलिगेट प्लग-इन एपीआई:
tensorflow/lite/acceleration/configuration/c/gpu_plugin.h tensorflow/lite/acceleration/configuration/c/xnnpack_plugin.h
कृपया ध्यान दें कि c_api_experimental.h हेडर के फ़ंक्शन काम नहीं करते हैं.
Google Play services के साथ LiteRT के लिए खास तौर पर बनाए गए फ़ंक्शन का इस्तेमाल किया जा सकता है. इसके लिए, आपको यह हेडर शामिल करना होगा:
tensorflow/lite/abi/tflite.h
C++
Google Play services के साथ काम करने वाले LiteRT C++ 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 के लिए, Play services के साथ काम करने वाले एपीआई में tensorflow/lite/interpreter.h के कुछ सदस्यों को शामिल नहीं किया गया है. ऐसा इसलिए है, क्योंकि LiteRT इनके लिए स्टेबल एबीआई उपलब्ध नहीं कराता:tflite::Interpreter
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)