API รันไทม์ LiteRT ต่อไปนี้พร้อมใช้งานสำหรับการพัฒนา Android
CompiledModelAPI: มาตรฐานที่ทันสมัยสำหรับการอนุมานประสิทธิภาพสูง ซึ่งช่วยเพิ่มประสิทธิภาพการทำงานของฮาร์ดแวร์ใน CPU/GPU/NPU ดูข้อมูลเพิ่มเติม เกี่ยวกับ เหตุผลที่ควรเลือก CompiledModel API.InterpreterAPI: API การอนุมานพื้นฐานที่ยังคงไว้เพื่อความเข้ากันได้แบบย้อนกลับ
เริ่มต้นใช้งาน CompiledModel API
สำหรับโมเดล ML แบบคลาสสิก โปรดดูแอปสาธิตต่อไปนี้
- แอป Kotlin สำหรับการแบ่งส่วนรูปภาพ: การอนุมาน CPU/GPU/NPU
- แอป C++ สำหรับการแบ่งส่วนรูปภาพ: การอนุมาน CPU/GPU/NPU ด้วย การดำเนินการแบบไม่พร้อมกัน
สำหรับโมเดล GenAI โปรดดูแอปสาธิตต่อไปนี้
- แอป C++ สำหรับความคล้ายคลึงเชิงความหมายของ EmbeddingGemma: การอนุมาน CPU/GPU/NPU
เวอร์ชันและ API ของ Android ที่รองรับ
| เวอร์ชัน LiteRT | สถานะ | API ที่รองรับ | ระดับ SDK ขั้นต่ำ | เวอร์ชัน NDK ขั้นต่ำ (หากใช้) | วันที่เผยแพร่ |
|---|---|---|---|---|---|
v2.1.5 |
✅ ล่าสุด | CompiledModel Interpreter(CPU เท่านั้น) |
23 (Android 6 Marshmallow) |
r26a |
2026-05-15 |
v2.1.4 |
⚠️ เดิม | CompiledModel Interpreter(CPU เท่านั้น) |
23 (Android 6 Marshmallow) |
r26a |
2026-04-10 |
v2.1.3 |
⚠️ เดิม | CompiledModel Interpreter(CPU เท่านั้น) |
23 (Android 6 Marshmallow) |
r26a |
2026-03-16 |
v2.1.1 |
⚠️ เดิม | CompiledModel Interpreter(CPU เท่านั้น) |
23 (Android 6 Marshmallow) |
r26a |
2026-01-27 |
v2.1.0 |
⚠️ เดิม | CompiledModel Interpreter(CPU เท่านั้น) |
23 (Android 6 Marshmallow) |
r26a |
2025-12-19 |
v2.0.3 |
⚠️ เดิม | CompiledModel |
26 (Android 8 Oreo) |
r26a |
2025-11-08 |
v1.4.2 |
✅ ล่าสุด | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2026-03-16 |
v1.4.1 |
⚠️ เดิม | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-11-07 |
v1.4.0 |
⚠️ เดิม | Interpreter |
26 (Android 8 Oreo) |
r26a |
2025-06-25 |
v1.3.0 |
⚠️ เดิม | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-05-19 |
v1.2.0 |
⚠️ เดิม | Interpreter |
21 (Android 5 Lollipop) |
r26a |
2025-03-13 |
สำคัญ: โปรดอัปเดตข้อมูลทรัพยากร Dependency ให้เป็นเวอร์ชันล่าสุดเพื่อให้แน่ใจว่าเข้ากันได้กับฟีเจอร์ล่าสุดและการอัปเดตด้านความปลอดภัย
คู่มือเริ่มใช้งาน CompiledModel API ฉบับย่อ
เพิ่มแพ็กเกจ LiteRT Maven ลงในโปรเจ็กต์ Android โดยทำดังนี้
dependencies {
...
implementation `com.google.ai.edge.litert:litert:2.1.0`
}
ผสานรวมโมเดล .tflite กับ CompiledModel API ข้อมูลโค้ดต่อไปนี้แสดงการใช้งานพื้นฐานใน 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));