การใช้หน่วยประมวลผลกราฟิก (GPU) เพื่อเรียกใช้โมเดลแมชชีนเลิร์นนิง (ML) ของคุณสามารถปรับปรุงประสิทธิภาพของโมเดลของคุณและประสบการณ์ผู้ใช้ของแอปพลิเคชันที่เปิดใช้งาน ML ได้อย่างมาก บนอุปกรณ์ Android คุณสามารถเปิดใช้งานการใช้โมเดลของคุณที่เร่งด้วย GPU ได้โดยใช้ ผู้รับมอบสิทธิ์ ผู้รับมอบสิทธิ์ทำหน้าที่เป็นไดรเวอร์ฮาร์ดแวร์สำหรับ TensorFlow Lite ซึ่งช่วยให้คุณเรียกใช้โค้ดของรุ่นของคุณบนโปรเซสเซอร์ GPU ได้
หน้านี้อธิบายวิธีเปิดใช้งานการเร่งความเร็ว GPU สำหรับรุ่น TensorFlow Lite ในแอป Android สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้ผู้แทน GPU สำหรับ TensorFlow Lite รวมถึงแนวทางปฏิบัติที่ดีที่สุดและเทคนิคขั้นสูง โปรดดูที่หน้า ผู้แทน GPU
ใช้ GPU กับ API ไลบรารีงาน
TensorFlow Lite Task Libraries มีชุด API เฉพาะงานสำหรับสร้างแอปพลิเคชันการเรียนรู้ของเครื่อง ส่วนนี้อธิบายวิธีใช้ผู้รับมอบสิทธิ์ GPU accelerator กับ API เหล่านี้
เพิ่มการพึ่งพาโครงการ
เปิดใช้งานการเข้าถึง API ผู้รับมอบสิทธิ์ GPU ด้วย TensorFlow Lite Task Libraries โดยการเพิ่มการพึ่งพาต่อไปนี้ อัปเดตไฟล์ build.gradle
โครงการพัฒนาของคุณเพื่อรวม tensorflow-lite-gpu-delegate-plugin
ตามที่แสดงในตัวอย่างโค้ดต่อไปนี้:
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}
เปิดใช้งานการเร่งความเร็ว GPU
เปิดใช้งานตัวเลือกผู้รับมอบสิทธิ์ GPU สำหรับคลาสโมเดล Task API ของคุณด้วยคลาส BaseOptions
ตัวอย่างเช่น คุณสามารถตั้งค่า GPU ใน ObjectDetector
ตามที่แสดงในตัวอย่างโค้ดต่อไปนี้:
Kotlin
import org.tensorflow.lite.task.core.BaseOptions import org.tensorflow.lite.task.gms.vision.detector.ObjectDetector val baseOptions = BaseOptions.builder().useGpu().build() val options = ObjectDetector.ObjectDetectorOptions.builder() .setBaseOptions(baseOptions) .setMaxResults(1) .build() val objectDetector = ObjectDetector.createFromFileAndOptions( context, model, options)
Java
import org.tensorflow.lite.task.core.BaseOptions import org.tensorflow.lite.task.gms.vision.detector.ObjectDetector BaseOptions baseOptions = BaseOptions.builder().useGpu().build(); ObjectDetectorOptions options = ObjectDetectorOptions.builder() .setBaseOptions(baseOptions) .setMaxResults(1) .build(); val objectDetector = ObjectDetector.createFromFileAndOptions( context, model, options);
ใช้ GPU กับ Interpreter API
TensorFlow Lite Interpreter API จัดเตรียมชุด API สำหรับใช้งานทั่วไปสำหรับการสร้างแอปพลิเคชันการเรียนรู้ของเครื่อง ส่วนนี้อธิบายวิธีใช้ผู้รับมอบสิทธิ์ GPU accelerator กับ API เหล่านี้
เพิ่มการพึ่งพาโครงการ
เปิดใช้งานการเข้าถึง API ผู้รับมอบสิทธิ์ GPU โดยการเพิ่มการพึ่งพาต่อไปนี้ อัปเดตไฟล์ build.gradle
โครงการพัฒนาของคุณเพื่อรวม org.tensorflow:tensorflow-lite-gpu
ตามที่แสดงในตัวอย่างโค้ดต่อไปนี้:
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite'
implementation 'org.tensorflow:tensorflow-lite-gpu'
}
เปิดใช้งานการเร่งความเร็ว GPU
จากนั้นเรียกใช้ TensorFlow Lite บน GPU ด้วย TfLiteDelegate
ใน Java คุณสามารถระบุ GpuDelegate
ผ่าน Interpreter.Options
Kotlin
import org.tensorflow.lite.Interpreter import org.tensorflow.lite.gpu.CompatibilityList import org.tensorflow.lite.gpu.GpuDelegate val compatList = CompatibilityList() val options = Interpreter.Options().apply{ if(compatList.isDelegateSupportedOnThisDevice){ // if the device has a supported GPU, add the GPU delegate val delegateOptions = compatList.bestOptionsForThisDevice this.addDelegate(GpuDelegate(delegateOptions)) } else { // if the GPU is not supported, run on 4 threads this.setNumThreads(4) } } val interpreter = Interpreter(model, options) // Run inference writeToInput(input) interpreter.run(input, output) readFromOutput(output)
Java
import org.tensorflow.lite.Interpreter; import org.tensorflow.lite.gpu.CompatibilityList; import org.tensorflow.lite.gpu.GpuDelegate; // Initialize interpreter with GPU delegate Interpreter.Options options = new Interpreter.Options(); CompatibilityList compatList = CompatibilityList(); if(compatList.isDelegateSupportedOnThisDevice()){ // if the device has a supported GPU, add the GPU delegate GpuDelegate.Options delegateOptions = compatList.getBestOptionsForThisDevice(); GpuDelegate gpuDelegate = new GpuDelegate(delegateOptions); options.addDelegate(gpuDelegate); } else { // if the GPU is not supported, run on 4 threads options.setNumThreads(4); } Interpreter interpreter = new Interpreter(model, options); // Run inference writeToInput(input); interpreter.run(input, output); readFromOutput(output);
ผู้แทน GPU ยังสามารถใช้กับการเชื่อมโยงโมเดล ML ใน Android Studio สำหรับข้อมูลเพิ่มเติม โปรดดูที่ สร้างอินเทอร์เฟซของโมเดลโดยใช้ข้อมูลเมตา
รองรับ GPU ขั้นสูง
ส่วนนี้ครอบคลุมการใช้งานขั้นสูงของผู้แทน GPU สำหรับ Android รวมถึง C API, C++ API และการใช้แบบจำลองเชิงปริมาณ
C/C++ API สำหรับ Android
ใช้ผู้แทน TensorFlow Lite GPU สำหรับ Android ใน C หรือ C++ โดยสร้างผู้รับมอบสิทธิ์ด้วย TfLiteGpuDelegateV2Create()
และทำลายมันด้วย TfLiteGpuDelegateV2Delete()
ดังแสดงในโค้ดตัวอย่างต่อไปนี้:
// Set up interpreter.
auto model = FlatBufferModel::BuildFromFile(model_path);
if (!model) return false;
ops::builtin::BuiltinOpResolver op_resolver;
std::unique_ptr<Interpreter> interpreter;
InterpreterBuilder(*model, op_resolver)(&interpreter);
// NEW: Prepare GPU delegate.
auto* delegate = TfLiteGpuDelegateV2Create(/*default options=*/nullptr);
if (interpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) return false;
// Run inference.
WriteToInputTensor(interpreter->typed_input_tensor<float>(0));
if (interpreter->Invoke() != kTfLiteOk) return false;
ReadFromOutputTensor(interpreter->typed_output_tensor<float>(0));
// NEW: Clean up.
TfLiteGpuDelegateV2Delete(delegate);
ตรวจสอบโค้ดอ็อบเจ็กต์ TfLiteGpuDelegateOptionsV2
เพื่อสร้างอินสแตนซ์ผู้รับมอบสิทธิ์พร้อมตัวเลือกที่กำหนดเอง คุณสามารถเริ่มต้นตัวเลือกเริ่มต้นด้วย TfLiteGpuDelegateOptionsV2Default()
แล้วแก้ไขตามความจำเป็น
ตัวแทน TensorFlow Lite GPU สำหรับ Android ใน C หรือ C ++ ใช้ระบบบิลด์ Bazel คุณสามารถสร้างผู้รับมอบสิทธิ์โดยใช้คำสั่งต่อไปนี้:
bazel build -c opt --config android_arm64 tensorflow/lite/delegates/gpu:delegate # for static library
bazel build -c opt --config android_arm64 tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so # for dynamic library
เมื่อเรียก Interpreter::ModifyGraphWithDelegate()
หรือ Interpreter::Invoke()
ผู้เรียกต้องมี EGLContext
ในเธรดปัจจุบัน และ Interpreter::Invoke()
ต้องถูกเรียกจาก EGLContext
เดียวกัน หากไม่มี EGLContext
ผู้รับมอบสิทธิ์จะสร้างหนึ่งรายการภายใน แต่คุณต้องตรวจสอบให้แน่ใจว่า Interpreter::Invoke()
ถูกเรียกจากเธรดเดียวกันที่มีการเรียก Interpreter::ModifyGraphWithDelegate()
เสมอ
โมเดลเชิงปริมาณ
ไลบรารีผู้รับมอบสิทธิ์ Android GPU รองรับโมเดลเชิงปริมาณโดยค่าเริ่มต้น คุณไม่จำเป็นต้องทำการเปลี่ยนแปลงโค้ดใดๆ เพื่อใช้โมเดลเชิงปริมาณกับผู้รับมอบสิทธิ์ GPU ส่วนต่อไปนี้จะอธิบายวิธีปิดใช้งานการสนับสนุนเชิงปริมาณสำหรับวัตถุประสงค์ในการทดสอบหรือการทดลอง
ปิดใช้งานการสนับสนุนโมเดลเชิงปริมาณ
รหัสต่อไปนี้แสดงวิธี ปิดใช้งาน การสนับสนุนสำหรับโมเดลเชิงปริมาณ
Java
GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false)); Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
C++
TfLiteGpuDelegateOptionsV2 options = TfLiteGpuDelegateOptionsV2Default(); options.experimental_flags = TFLITE_GPU_EXPERIMENTAL_FLAGS_NONE; auto* delegate = TfLiteGpuDelegateV2Create(options); if (interpreter->ModifyGraphWithDelegate(delegate) != kTfLiteOk) return false;
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการเรียกใช้โมเดลเชิงปริมาณด้วยการเร่งความเร็ว GPU โปรดดูที่ภาพรวมการ มอบสิทธิ์ GPU