Sử dụng đơn vị xử lý đồ họa (GPU) để chạy các mô hình máy học (ML) của bạn có thể cải thiện đáng kể hiệu suất của mô hình và trải nghiệm người dùng của các ứng dụng hỗ trợ ML của bạn. Trên thiết bị Android, bạn có thể cho phép sử dụng tính năng thực thi được tăng tốc GPU trên các mô hình của mình bằng cách sử dụng một đại diện . Các đại biểu đóng vai trò là trình điều khiển phần cứng cho TensorFlow Lite, cho phép bạn chạy mã mô hình của mình trên bộ xử lý GPU.
Trang này mô tả cách bật tăng tốc GPU cho các mẫu TensorFlow Lite trong ứng dụng Android. Để biết thêm thông tin về cách sử dụng GPU ủy quyền cho TensorFlow Lite, bao gồm các phương pháp hay nhất và kỹ thuật nâng cao, hãy xem trang ủy quyền GPU .
Sử dụng GPU với các API thư viện tác vụ
Thư viện tác vụ TensorFlow Lite cung cấp một tập hợp các API dành riêng cho tác vụ để xây dựng ứng dụng học máy. Phần này mô tả cách sử dụng đại biểu trình tăng tốc GPU với các API này.
Thêm phụ thuộc dự án
Cho phép truy cập vào các API ủy quyền GPU với Thư viện tác vụ TensorFlow Lite bằng cách thêm các phần phụ thuộc sau, cập nhật tệp build.gradle
dự án phát triển của bạn để bao gồm tensorflow-lite-gpu-delegate-plugin
như được hiển thị trong ví dụ mã sau:
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}
Bật tăng tốc GPU
Bật tùy chọn ủy quyền GPU cho lớp mô hình API Tác vụ của bạn với lớp BaseOptions
. Ví dụ: bạn có thể thiết lập GPU trong ObjectDetector
như được hiển thị trong các ví dụ mã sau:
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);
Sử dụng GPU với API phiên dịch
API thông dịch TensorFlow Lite cung cấp một tập hợp các API mục đích chung để xây dựng ứng dụng học máy. Phần này mô tả cách sử dụng đại biểu trình tăng tốc GPU với các API này.
Thêm phụ thuộc dự án
Cho phép truy cập vào các API ủy quyền GPU bằng cách thêm các phần phụ thuộc sau, cập nhật tệp build.gradle
cho các dự án phát triển của bạn để bao gồm org.tensorflow:tensorflow-lite-gpu
như được hiển thị trong ví dụ mã sau:
dependencies {
...
implementation 'org.tensorflow:tensorflow-lite'
implementation 'org.tensorflow:tensorflow-lite-gpu'
}
Bật tăng tốc GPU
Sau đó chạy TensorFlow Lite trên GPU với TfLiteDelegate
. Trong Java, bạn có thể chỉ định GpuDelegate
thông qua 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);
Đại biểu GPU cũng có thể được sử dụng với liên kết mô hình ML trong Android Studio. Để biết thêm thông tin, hãy xem Tạo giao diện mô hình bằng siêu dữ liệu .
Hỗ trợ GPU nâng cao
Phần này bao gồm các cách sử dụng nâng cao của đại biểu GPU dành cho Android, bao gồm API C, API C ++ và sử dụng các mô hình lượng tử hóa.
API C / C ++ dành cho Android
Sử dụng đại biểu GPU TensorFlow Lite cho Android trong C hoặc C ++ bằng cách tạo đại biểu với TfLiteGpuDelegateV2Create()
và hủy nó bằng TfLiteGpuDelegateV2Delete()
, như được hiển thị trong mã ví dụ sau:
// 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);
Xem lại mã đối tượng TfLiteGpuDelegateOptionsV2
để tạo một phiên bản ủy quyền với các tùy chọn tùy chỉnh. Bạn có thể khởi tạo các tùy chọn mặc định với TfLiteGpuDelegateOptionsV2Default()
và sau đó sửa đổi chúng nếu cần.
Đại biểu GPU TensorFlow Lite cho Android trong C hoặc C ++ sử dụng hệ thống xây dựng Bazel . Bạn có thể xây dựng đại biểu bằng cách sử dụng lệnh sau:
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
Khi gọi Interpreter::ModifyGraphWithDelegate()
hoặc Interpreter::Invoke()
, người gọi phải có EGLContext
trong luồng hiện tại và Interpreter::Invoke()
phải được gọi từ cùng một EGLContext
. Nếu một văn bản EGLContext
không tồn tại, đại biểu sẽ tạo một văn bản bên trong, nhưng sau đó bạn phải đảm bảo rằng Interpreter::Invoke()
luôn được gọi từ cùng một luồng mà Interpreter::ModifyGraphWithDelegate()
đã được gọi.
Các mô hình lượng tử hóa
Thư viện đại biểu GPU Android hỗ trợ các mô hình lượng tử hóa theo mặc định. Bạn không phải thực hiện bất kỳ thay đổi mã nào để sử dụng các mô hình được lượng tử hóa với GPU ủy quyền. Phần sau giải thích cách tắt hỗ trợ lượng tử hóa cho mục đích thử nghiệm hoặc thử nghiệm.
Tắt hỗ trợ mô hình lượng tử hóa
Đoạn mã sau đây cho thấy cách tắt hỗ trợ cho các mô hình lượng tử hóa.
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;
Để biết thêm thông tin về cách chạy các mô hình lượng tử hóa với khả năng tăng tốc GPU, hãy xem tổng quan về GPU dành cho đại biểu .