TensorFlow Lite Task Library 在抽象化 TensorFlow 的相同基礎架構之上,提供預先建構的原生/Android/iOS API。如果現有的 Task Library 不支援您的模型,您可以擴充 Task API 基礎架構以建立自訂 API。
總覽
Task API 基礎架構具有雙層結構:底層 C++ 層封裝原生 TFLite 執行階段,頂層 Java/ObjC 層透過 JNI 或原生包裝函式與 C++ 層通訊。
僅在 C++ 中實作所有 TensorFlow 邏輯,可將成本降至最低、最大化推論效能,並簡化跨平台的整體工作流程。
若要建立 Task 類別,請擴充 BaseTaskApi,以提供 TFLite 模型介面和 Task API 介面之間的轉換邏輯,然後使用 Java/ObjC 公用程式建立對應的 API。隱藏所有 TensorFlow 詳細資訊後,您就可以在應用程式中部署 TFLite 模型,而無需任何機器學習知識。
TensorFlow Lite 為最常見的視覺和 NLP 工作提供了一些預先建構的 API。您可以使用 Task API 基礎架構,為其他工作建立自己的 API。
使用 Task API 基礎架構建立您自己的 API
C++ API
所有 TFLite 詳細資訊都在原生 API 中實作。使用其中一個工廠函式建立 API 物件,並透過呼叫介面中定義的函式取得模型結果。
範例用法
以下範例使用 C++ BertQuestionAnswerer
搭配 MobileBert。
char kBertModelPath[] = "path/to/model.tflite";
// Create the API from a model file
std::unique_ptr<BertQuestionAnswerer> question_answerer =
BertQuestionAnswerer::CreateFromFile(kBertModelPath);
char kContext[] = ...; // context of a question to be answered
char kQuestion[] = ...; // question to be answered
// ask a question
std::vector<QaAnswer> answers = question_answerer.Answer(kContext, kQuestion);
// answers[0].text is the best answer
建構 API
若要建構 API 物件,您必須透過擴充 BaseTaskApi
提供以下資訊
判斷 API I/O - 您的 API 在不同平台上應公開類似的輸入/輸出。例如,
BertQuestionAnswerer
接受兩個字串(std::string& context, std::string& question)
作為輸入,並輸出可能的答案向量和機率,格式為std::vector<QaAnswer>
。這是透過在BaseTaskApi
的範本參數中指定對應的類型來完成。指定範本參數後,BaseTaskApi::Infer
函式將具有正確的輸入/輸出類型。API 用戶端可以直接呼叫此函式,但最好將其包裝在模型專用函式內,在本例中為BertQuestionAnswerer::Answer
。class BertQuestionAnswerer : public BaseTaskApi< std::vector<QaAnswer>, // OutputType const std::string&, const std::string& // InputTypes > { // Model specific function delegating calls to BaseTaskApi::Infer std::vector<QaAnswer> Answer(const std::string& context, const std::string& question) { return Infer(context, question).value(); } }
提供 API I/O 與模型的輸入/輸出張量之間的轉換邏輯 - 指定輸入和輸出類型後,子類別也需要實作類型化的函式
BaseTaskApi::Preprocess
和BaseTaskApi::Postprocess
。這兩個函式提供 TFLiteFlatBuffer
的輸入和輸出。子類別負責將值從 API I/O 指派給 I/O 張量。請參閱BertQuestionAnswerer
中的完整實作範例。class BertQuestionAnswerer : public BaseTaskApi< std::vector<QaAnswer>, // OutputType const std::string&, const std::string& // InputTypes > { // Convert API input into tensors absl::Status BertQuestionAnswerer::Preprocess( const std::vector<TfLiteTensor*>& input_tensors, // input tensors of the model const std::string& context, const std::string& query // InputType of the API ) { // Perform tokenization on input strings ... // Populate IDs, Masks and SegmentIDs to corresponding input tensors PopulateTensor(input_ids, input_tensors[0]); PopulateTensor(input_mask, input_tensors[1]); PopulateTensor(segment_ids, input_tensors[2]); return absl::OkStatus(); } // Convert output tensors into API output StatusOr<std::vector<QaAnswer>> // OutputType BertQuestionAnswerer::Postprocess( const std::vector<const TfLiteTensor*>& output_tensors, // output tensors of the model ) { // Get start/end logits of prediction result from output tensors std::vector<float> end_logits; std::vector<float> start_logits; // output_tensors[0]: end_logits FLOAT[1, 384] PopulateVector(output_tensors[0], &end_logits); // output_tensors[1]: start_logits FLOAT[1, 384] PopulateVector(output_tensors[1], &start_logits); ... std::vector<QaAnswer::Pos> orig_results; // Look up the indices from vocabulary file and build results ... return orig_results; } }
建立 API 的工廠函式 - 初始化
tflite::Interpreter
需要模型檔案和OpResolver
。TaskAPIFactory
提供公用程式函式來建立 BaseTaskApi 執行個體。您也必須提供與模型相關聯的任何檔案。例如,
BertQuestionAnswerer
也可能有額外檔案用於其 tokenizer 的詞彙表。class BertQuestionAnswerer : public BaseTaskApi< std::vector<QaAnswer>, // OutputType const std::string&, const std::string& // InputTypes > { // Factory function to create the API instance StatusOr<std::unique_ptr<QuestionAnswerer>> BertQuestionAnswerer::CreateBertQuestionAnswerer( const std::string& path_to_model, // model to passed to TaskApiFactory const std::string& path_to_vocab // additional model specific files ) { // Creates an API object by calling one of the utils from TaskAPIFactory std::unique_ptr<BertQuestionAnswerer> api_to_init; ASSIGN_OR_RETURN( api_to_init, core::TaskAPIFactory::CreateFromFile<BertQuestionAnswerer>( path_to_model, absl::make_unique<tflite::ops::builtin::BuiltinOpResolver>(), kNumLiteThreads)); // Perform additional model specific initializations // In this case building a vocabulary vector from the vocab file. api_to_init->InitializeVocab(path_to_vocab); return api_to_init; } }
Android API
透過定義 Java/Kotlin 介面並將邏輯委派給 C++ 層 (透過 JNI),即可建立 Android API。Android API 需要先建構原生 API。
範例用法
以下範例使用 Java BertQuestionAnswerer
搭配 MobileBert。
String BERT_MODEL_FILE = "path/to/model.tflite";
String VOCAB_FILE = "path/to/vocab.txt";
// Create the API from a model file and vocabulary file
BertQuestionAnswerer bertQuestionAnswerer =
BertQuestionAnswerer.createBertQuestionAnswerer(
ApplicationProvider.getApplicationContext(), BERT_MODEL_FILE, VOCAB_FILE);
String CONTEXT = ...; // context of a question to be answered
String QUESTION = ...; // question to be answered
// ask a question
List<QaAnswer> answers = bertQuestionAnswerer.answer(CONTEXT, QUESTION);
// answers.get(0).text is the best answer
建構 API
與原生 API 類似,若要建構 API 物件,用戶端需要透過擴充 BaseTaskApi
來提供以下資訊,此 API 為所有 Java Task API 提供 JNI 處理。
判斷 API I/O - 這通常會反映原生介面。例如,
BertQuestionAnswerer
接受(String context, String question)
作為輸入,並輸出List<QaAnswer>
。實作會呼叫具有類似簽名的私有原生函式,但它有一個額外參數long nativeHandle
,這是從 C++ 傳回的指標。class BertQuestionAnswerer extends BaseTaskApi { public List<QaAnswer> answer(String context, String question) { return answerNative(getNativeHandle(), context, question); } private static native List<QaAnswer> answerNative( long nativeHandle, // C++ pointer String context, String question // API I/O ); }
建立 API 的工廠函式 - 這也會反映原生工廠函式,但 Android 工廠函式也需要取得
Context
以進行檔案存取。實作會呼叫TaskJniUtils
中的其中一個公用程式來建構對應的 C++ API 物件,並將其指標傳遞給BaseTaskApi
建構函式。class BertQuestionAnswerer extends BaseTaskApi { private static final String BERT_QUESTION_ANSWERER_NATIVE_LIBNAME = "bert_question_answerer_jni"; // Extending super constructor by providing the // native handle(pointer of corresponding C++ API object) private BertQuestionAnswerer(long nativeHandle) { super(nativeHandle); } public static BertQuestionAnswerer createBertQuestionAnswerer( Context context, // Accessing Android files String pathToModel, String pathToVocab) { return new BertQuestionAnswerer( // The util first try loads the JNI module with name // BERT_QUESTION_ANSWERER_NATIVE_LIBNAME, then opens two files, // converts them into ByteBuffer, finally ::initJniWithBertByteBuffers // is called with the buffer for a C++ API object pointer TaskJniUtils.createHandleWithMultipleAssetFilesFromLibrary( context, BertQuestionAnswerer::initJniWithBertByteBuffers, BERT_QUESTION_ANSWERER_NATIVE_LIBNAME, pathToModel, pathToVocab)); } // modelBuffers[0] is tflite model file buffer, and modelBuffers[1] is vocab file buffer. // returns C++ API object pointer casted to long private static native long initJniWithBertByteBuffers(ByteBuffer... modelBuffers); }
為原生函式實作 JNI 模組 - 所有 Java 原生方法都是透過從 JNI 模組呼叫對應的原生函式來實作。工廠函式會建立原生 API 物件,並傳回其指標 (作為 long 類型) 給 Java。在後續呼叫 Java API 時,long 類型指標會傳遞回 JNI 並轉換回原生 API 物件。然後,原生 API 結果會轉換回 Java 結果。
例如,以下說明 bert_question_answerer_jni 的實作方式。
// Implements BertQuestionAnswerer::initJniWithBertByteBuffers extern "C" JNIEXPORT jlong JNICALL Java_org_tensorflow_lite_task_text_qa_BertQuestionAnswerer_initJniWithBertByteBuffers( JNIEnv* env, jclass thiz, jobjectArray model_buffers) { // Convert Java ByteBuffer object into a buffer that can be read by native factory functions absl::string_view model = GetMappedFileBuffer(env, env->GetObjectArrayElement(model_buffers, 0)); // Creates the native API object absl::StatusOr<std::unique_ptr<QuestionAnswerer>> status = BertQuestionAnswerer::CreateFromBuffer( model.data(), model.size()); if (status.ok()) { // converts the object pointer to jlong and return to Java. return reinterpret_cast<jlong>(status->release()); } else { return kInvalidPointer; } } // Implements BertQuestionAnswerer::answerNative extern "C" JNIEXPORT jobject JNICALL Java_org_tensorflow_lite_task_text_qa_BertQuestionAnswerer_answerNative( JNIEnv* env, jclass thiz, jlong native_handle, jstring context, jstring question) { // Convert long to native API object pointer QuestionAnswerer* question_answerer = reinterpret_cast<QuestionAnswerer*>(native_handle); // Calls the native API std::vector<QaAnswer> results = question_answerer->Answer(JStringToString(env, context), JStringToString(env, question)); // Converts native result(std::vector<QaAnswer>) to Java result(List<QaAnswerer>) jclass qa_answer_class = env->FindClass("org/tensorflow/lite/task/text/qa/QaAnswer"); jmethodID qa_answer_ctor = env->GetMethodID(qa_answer_class, "<init>", "(Ljava/lang/String;IIF)V"); return ConvertVectorToArrayList<QaAnswer>( env, results, [env, qa_answer_class, qa_answer_ctor](const QaAnswer& ans) { jstring text = env->NewStringUTF(ans.text.data()); jobject qa_answer = env->NewObject(qa_answer_class, qa_answer_ctor, text, ans.pos.start, ans.pos.end, ans.pos.logit); env->DeleteLocalRef(text); return qa_answer; }); } // Implements BaseTaskApi::deinitJni by delete the native object extern "C" JNIEXPORT void JNICALL Java_task_core_BaseTaskApi_deinitJni( JNIEnv* env, jobject thiz, jlong native_handle) { delete reinterpret_cast<QuestionAnswerer*>(native_handle); }
iOS API
透過將原生 API 物件包裝到 ObjC API 物件中,即可建立 iOS API。建立的 API 物件可用於 ObjC 或 Swift。iOS API 需要先建構原生 API。
範例用法
以下範例使用 ObjC TFLBertQuestionAnswerer
搭配 Swift 中的 MobileBert。
static let mobileBertModelPath = "path/to/model.tflite";
// Create the API from a model file and vocabulary file
let mobileBertAnswerer = TFLBertQuestionAnswerer.mobilebertQuestionAnswerer(
modelPath: mobileBertModelPath)
static let context = ...; // context of a question to be answered
static let question = ...; // question to be answered
// ask a question
let answers = mobileBertAnswerer.answer(
context: TFLBertQuestionAnswererTest.context, question: TFLBertQuestionAnswererTest.question)
// answers.[0].text is the best answer
建構 API
iOS API 是原生 API 之上的簡單 ObjC 包裝函式。按照以下步驟建構 API
定義 ObjC 包裝函式 - 定義 ObjC 類別,並將實作委派給對應的原生 API 物件。請注意,由於 Swift 無法與 C++ 互通,因此原生依附元件只能出現在 .mm 檔案中。
- .h 檔案
@interface TFLBertQuestionAnswerer : NSObject // Delegate calls to the native BertQuestionAnswerer::CreateBertQuestionAnswerer + (instancetype)mobilebertQuestionAnswererWithModelPath:(NSString*)modelPath vocabPath:(NSString*)vocabPath NS_SWIFT_NAME(mobilebertQuestionAnswerer(modelPath:vocabPath:)); // Delegate calls to the native BertQuestionAnswerer::Answer - (NSArray<TFLQAAnswer*>*)answerWithContext:(NSString*)context question:(NSString*)question NS_SWIFT_NAME(answer(context:question:)); }
- .mm 檔案
using BertQuestionAnswererCPP = ::tflite::task::text::BertQuestionAnswerer; @implementation TFLBertQuestionAnswerer { // define an iVar for the native API object std::unique_ptr<QuestionAnswererCPP> _bertQuestionAnswerwer; } // Initialize the native API object + (instancetype)mobilebertQuestionAnswererWithModelPath:(NSString *)modelPath vocabPath:(NSString *)vocabPath { absl::StatusOr<std::unique_ptr<QuestionAnswererCPP>> cQuestionAnswerer = BertQuestionAnswererCPP::CreateBertQuestionAnswerer(MakeString(modelPath), MakeString(vocabPath)); _GTMDevAssert(cQuestionAnswerer.ok(), @"Failed to create BertQuestionAnswerer"); return [[TFLBertQuestionAnswerer alloc] initWithQuestionAnswerer:std::move(cQuestionAnswerer.value())]; } // Calls the native API and converts C++ results into ObjC results - (NSArray<TFLQAAnswer *> *)answerWithContext:(NSString *)context question:(NSString *)question { std::vector<QaAnswerCPP> results = _bertQuestionAnswerwer->Answer(MakeString(context), MakeString(question)); return [self arrayFromVector:results]; } }