LiteRT สำหรับ Android

API รันไทม์ LiteRT ต่อไปนี้พร้อมใช้งานสำหรับการพัฒนา Android

  • CompiledModel API: มาตรฐานที่ทันสมัยสำหรับการอนุมานที่มีประสิทธิภาพสูง ซึ่งเพิ่มประสิทธิภาพการเร่งฮาร์ดแวร์ใน CPU/GPU/NPU ดูข้อมูลเพิ่มเติม เกี่ยวกับเหตุผลที่ควรเลือก CompiledModel API
  • Interpreter API: API การอนุมานพื้นฐานที่ดูแลรักษาเพื่อความเข้ากันได้แบบย้อนกลับ

เริ่มต้นใช้งาน CompiledModel API

เวอร์ชันและ API ของ Android ที่รองรับ

เวอร์ชัน LiteRT สถานะ API ที่รองรับ ระดับ SDK ขั้นต่ำ เวอร์ชัน NDK ขั้นต่ำ (หากใช้) วันที่เผยแพร่
v2.1.0 ✅ ล่าสุด CompiledModel
Interpreter(CPU เท่านั้น)
23 (Android 6 Marshmallow) r26a 19-12-2025
v2.0.3 ⚠️ เดิม CompiledModel 26 (Android 8 Oreo) r26a 08-11-2025
v1.4.1 ✅ ล่าสุด Interpreter 21 (Android 5 Lollipop) r26a 07-11-2025
v1.4.0 ⚠️ เดิม Interpreter 26 (Android 8 Oreo) r26a 25-06-2025
v1.3.0 ⚠️ เดิม Interpreter 21 (Android 5 Lollipop) r26a 19-05-2025
v1.2.0 ⚠️ เดิม Interpreter 21 (Android 5 Lollipop) r26a 13-03-2025

สำคัญ: โปรดอัปเดตการขึ้นต่อกันให้เป็นเวอร์ชันล่าสุดเพื่อให้มั่นใจว่าเข้ากันได้กับ ฟีเจอร์ล่าสุดและการอัปเดตความปลอดภัย

คู่มือเริ่มใช้งาน CompiledModel API ฉบับย่อ

เพิ่มแพ็กเกจ Maven ของ LiteRT ลงในโปรเจ็กต์ Android โดยทำดังนี้

dependencies {
  ...
  implementation `com.google.ai.edge.litert:litert:2.1.0`
}

ผสานรวมโมเดล .tflite กับ API CompiledModel ข้อมูลโค้ดต่อไปนี้ แสดงการติดตั้งใช้งานพื้นฐานใน Kotlin และ C++

Kotlin

// Load model and initialize runtime
val compiledModel = CompiledModel.create(
    "/path/to/mymodel.tflite",
    CompiledModel.Options(Accelerator.CPU))

// Preallocate input/output buffers
val inputBuffers = compiledModel.createInputBuffers()
val outputBuffers = compiledModel.createOutputBuffers()

// Fill the input buffer
inputBuffers.get(0).writeFloat(input0)
inputBuffers.get(1).writeFloat(input1)

// Invoke
compiledModel.run(inputBuffers, outputBuffers)

// Read the output
val output = outputBuffers.get(0).readFloat()

C++

// Load model and initialize runtime
LITERT_ASSIGN_OR_RETURN(auto env, GetEnvironment());
LITERT_ASSIGN_OR_RETURN(auto options, GetOptions());
LITERT_ASSIGN_OR_RETURN(
    auto compiled_model,
    CompiledModel::Create(env, "/path/to/mymodel.tflite", options));

// Preallocate input/output buffers
LITERT_ASSIGN_OR_RETURN(auto input_buffers,compiled_model.CreateInputBuffers(signature_index));
LITERT_ASSIGN_OR_RETURN(auto output_buffers,compiled_model.CreateOutputBuffers(signature_index));

// Fill the input buffer
LITERT_ABORT_IF_ERROR(input_buffers[0].Write(input0));
LITERT_ABORT_IF_ERROR(input_buffers[1].Write(input1));

// Invoke
LITERT_ABORT_IF_ERROR(compiled_model.Run(signature_index, input_buffers, output_buffers));

// Read the output
LITERT_ABORT_IF_ERROR(output_buffers[0].Read(output0));