Ủy quyền tăng tốc GPU bằng API Phiên dịch

Sử dụng đơn vị xử lý đồ hoạ (GPU) để chạy các mô hình học máy (ML) có thể cải thiện đáng kể hiệu suất và trải nghiệm người dùng của ứng dụng các ứng dụng hỗ trợ ML. Trên thiết bị Android, bạn có thể bật uỷ quyền và một trong các API sau:

  • API Thông dịch – hướng dẫn này
  • API gốc (C/C++) – hướng dẫn

Trang này mô tả cách bật tính năng tăng tốc GPU cho các mô hình LiteRT trong Ứng dụng Android dùng API Phiên dịch. Để biết thêm thông tin về cách sử dụng GPU đại diện cho LiteRT, bao gồm các phương pháp hay nhất và kỹ thuật nâng cao, hãy xem trang Uỷ quyền GPU.

Sử dụng GPU với LiteRT với Dịch vụ Google Play

Thông dịch viên LiteRT API 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 uỷ quyền của trình tăng tốc GPU bằng những API này với LiteRT với Dịch vụ Google Play.

Bạn nên dùng LiteRT với Dịch vụ Google Play để sử dụng LiteRT trên Android. Nếu ứng dụng của bạn đang nhắm đến thiết bị không chạy Google Play, hãy xem GPU với API phiên dịch và phiên bản độc lập LiteRT.

Thêm phần phụ thuộc dự án (với danh mục phiên bản .toml)

  1. Cập nhật tệp libs.versions.toml của dự án
[libraries]
...
tflite-gpu = { module = "com.google.ai.edge.litert:litert-gpu", version = "2.X.Y" }
tflite-gpu-api = { module = "com.google.ai.edge.litert:litert-gpu-api", version = "2.X.Y" }
...
  1. Thêm phần phụ thuộc dự án vào build.gradle.kts của ứng dụng
dependencies {
  ...
  implementation(libraries.tflite.gpu)
  implementation(libraries.tflite.gpu.api)
  ...
}

Thêm phần phụ thuộc của dự án

Để cho phép truy cập vào tính năng uỷ quyền GPU, hãy thêm com.google.android.gms:play-services-tflite-gpu cho build.gradle của ứng dụng của bạn tệp:

dependencies {
    ...
    implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'
    implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'
}

Bật tính năng tăng tốc GPU

Sau đó, hãy khởi chạy LiteRT bằng Dịch vụ Google Play có hỗ trợ GPU:

Kotlin

val useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)

val interpreterTask = useGpuTask.continueWith { useGpuTask ->
  TfLite.initialize(context,
      TfLiteInitializationOptions.builder()
      .setEnableGpuDelegateSupport(useGpuTask.result)
      .build())
  }
        

Java

Task<boolean> useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context);

Task<Options> interpreterOptionsTask = useGpuTask.continueWith({ task ->
  TfLite.initialize(context,
  TfLiteInitializationOptions.builder()
    .setEnableGpuDelegateSupport(true)
    .build());
});
        

Cuối cùng, bạn có thể khởi chạy trình phiên dịch truyền GpuDelegateFactory thông qua InterpreterApi.Options:

Kotlin

    val options = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
      .addDelegateFactory(GpuDelegateFactory())

    val interpreter = InterpreterApi(model, options)

    // Run inference
    writeToInput(input)
    interpreter.run(input, output)
    readFromOutput(output)
      

Java

    Options options = InterpreterApi.Options()
      .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)
      .addDelegateFactory(new GpuDelegateFactory());

    Interpreter interpreter = new InterpreterApi(model, options);

    // Run inference
    writeToInput(input);
    interpreter.run(input, output);
    readFromOutput(output);
      

Bạn cũng có thể sử dụng tính năng uỷ quyền GPU với tính năng liên kết mô hình học máy trong Android Studio. Cho để biết thêm thông tin, hãy xem bài viết Tạo giao diện mô hình bằng cách sử dụng siêu dữ liệu.

Sử dụng GPU với LiteRT độc lập

Nếu ứng dụng của bạn nhắm mục tiêu đến các thiết bị không chạy Google Play, thì có thể gói uỷ quyền GPU vào ứng dụng của bạn và sử dụng nó với phiên bản độc lập của LiteRT.

Thêm phần phụ thuộc của dự án

Để cho phép truy cập vào tính năng uỷ quyền GPU, hãy thêm com.google.ai.edge.litert:litert-gpu-delegate-plugin cho ứng dụng của bạn Tệp build.gradle:

dependencies {
    ...
    implementation 'org.tensorflow:tensorflow-lite'
    implementation 'com.google.ai.edge.litert:litert-gpu-delegate-plugin'
}

Bật tính năng tăng tốc GPU

Sau đó, chạy LiteRT trên GPU với TfLiteDelegate. Trong Java, bạn có thể chỉ định GpuDelegate đến 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);
      

Mô hình lượng tử hoá

Theo mặc định, thư viện uỷ quyền GPU của Android hỗ trợ các mô hình lượng tử hoá. Bạn không phải thực hiện bất kỳ thay đổi nào về mã để sử dụng các mô hình lượng tử hoá với bộ uỷ quyền GPU. Chiến lược phát hành đĩa đơn phần sau đây giải thích cách tắt tính năng hỗ trợ lượng tử hoá cho việc kiểm thử hoặc cho mục đích thử nghiệm.

Tắt tính năng hỗ trợ mô hình lượng tử hoá

Đoạn mã sau đây cho biết cách tắt tính năng hỗ trợ cho các mô hình lượng tử hoá.

Java

GpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false));

Interpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);
      

Để biết thêm thông tin về cách chạy các mô hình lượng tử hoá bằng tính năng tăng tốc GPU, hãy xem Tổng quan về Uỷ quyền GPU.