เริ่มต้นใช้งาน LiteRT Next

LiteRT Next API ใช้งานร่วมกับ LiteRT API ไม่ได้ แอปพลิเคชันที่ใช้ LiteRT จึงต้องย้ายข้อมูลไปยัง LiteRT Next โดยสมบูรณ์เพื่อใช้ประโยชน์จากฟีเจอร์และความสามารถที่ API ใหม่มอบให้ แอปพลิเคชันใช้ TF Lite Interpreter API และ Compiled Model API ร่วมกันไม่ได้

LiteRT Next มี API สำหรับ Kotlin และ C++ แอปพลิเคชันที่ใช้ LiteRT SDK ในภาษาอื่นๆ ควรใช้ LiteRT ต่อไป

Dependency ของ Android

หากต้องการย้ายข้อมูลแอปพลิเคชัน Android โดยใช้ LiteRT ให้แทนที่การพึ่งพาจาก com.google.ai.edge.litert เป็น com.google.ai.edge.litert:litert:2.0.0-alpha

เมื่อใช้ LiteRT ตัวเร่ง GPU จะพร้อมใช้งานในฐานะผู้รับมอบสิทธิ์ในไลบรารีแยกต่างหาก (com.google.ai.edge.litert:litert-gpu) เมื่อใช้ LiteRT Next ตัวเร่ง GPU จะรวมอยู่ในแพ็กเกจ LiteRT Next ดูข้อมูลเพิ่มเติมได้ใน GPU ที่มี LiteRT Next

คุณเพิ่มแพ็กเกจ LiteRT Next ลงในbuild.gradle Dependency ได้ดังนี้

dependencies {
  ...
  implementation `com.google.ai.edge.litert:litert:2.0.0-alpha`
}

การเปลี่ยนแปลงโค้ด

แอปพลิเคชันที่ใช้ LiteRT จะต้องแทนที่โค้ดที่ใช้ TFLite interpreter API ด้วยโค้ดที่ใช้ Compiled Model API ข้อมูลต่อไปนี้แสดงการเปลี่ยนแปลงที่สำคัญซึ่งจําเป็นต่อการย้ายข้อมูลไปยัง LiteRT Next โปรดดูรายละเอียดเพิ่มเติมที่ข้อมูลอ้างอิง LiteRT Next API

การเปลี่ยนแปลงโค้ดใน C++

หากต้องการย้ายข้อมูลแอปพลิเคชันที่ใช้ C++ ให้แทนที่ข้อมูลโค้ดต่อไปนี้

LiteRT (โปรแกรมแปลภาษา TFLite) LiteRT Next (CompiledModel)
โหลดโมเดล FlatBufferModel::BuildFromFile() InterpreterBuilder(...) Model::CreateFromFile("mymodel.tflite")
หมายเหตุ: ไม่มีขั้นตอนการสร้างแยกต่างหาก
เริ่มต้นรันไทม์ builder(&interpreter), interpreter->AllocateTensors() CompiledModel::Create(env, model, kLiteRtHwAcceleratorCpu)
หมายเหตุ: ไม่มีขั้นตอนการจัดสรรหน่วยความจำด้วยตนเอง
ใช้ Accelerator interpreter->ModifyGraphWithDelegate(...) CompiledModel::Create(env, model, kLiteRtHwAcceleratorGpu)
เรียกใช้รูปแบบ interpreter->Invoke() compiled_model->Run(inputs, outputs)

การเปลี่ยนแปลงโค้ดใน Kotlin

หากต้องการย้ายข้อมูลแอปพลิเคชันโดยใช้ Kotlin ให้ทําตามขั้นตอนสําคัญต่อไปนี้

ตั้งค่าโมเดลและรันไทม์

เมื่อใช้ LiteRT คุณจะโหลดโมเดล ตั้งค่าการเร่ง และเริ่มต้นรันไทม์ในขั้นตอนต่างๆ ดังนี้

// Load the model
val modelBuffer: MappedByteBuffer =
  FileUtil.loadMappedFile(appContext, "model.tflite")

// Initialize runtime
val options = Interpreter.Options()
val interpreter = Interpreter(modelBuffer, options)
interpreter.allocateTensors()

// Use accelerators
aval gpuDelegate = GpuDelegate()
options.addDelegate(gpuDelegate)

เมื่อใช้ LiteRT Next คุณจะโหลดโมเดล ระบุการเร่งความเร็ว และเริ่มต้นรันไทม์ได้พร้อมกัน

val model =
CompiledModel.create(
  context.assets,
  "model.tflite",
  CompiledModel.Options(Accelerator.GPU)
)

เรียกใช้การอนุมาน

วิธีเรียกใช้โมเดลด้วย LiteRT

val input = FloatBuffer.allocate(data_size)
val output = FloatBuffer.allocate(data_size)
interpreter.run(input, output)

วิธีเรียกใช้โมเดลด้วย LiteRT Next

val inputBuffers = model.createInputBuffers()
val outputBuffers = model.createOutputBuffers()
model.run(inputBuffers, outputBuffers)

ห้องสมุดอื่นๆ

LiteRT Next API พร้อมให้บริการใน Kotlin และ C++ เท่านั้น แอปพลิเคชันที่ใช้ LiteRT SDK ในภาษาอื่นๆ จะย้ายข้อมูลไปยัง LiteRT Next ไม่ได้

แอปพลิเคชันที่ใช้ LiteRT ในรันไทม์ของบริการ Play จะย้ายข้อมูลไปยัง LiteRT Next ไม่ได้ และควรใช้รันไทม์ play-services-tflite ต่อไป ไลบรารีคลังงานและเครื่องมือสร้างโมเดลจะย้ายข้อมูลไปยัง LiteRT Next ไม่ได้ และควรใช้ TensorFlow Lite API ต่อไป