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

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

  • API Phiên 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 TensorFlow Lite trong ứng dụng Android bằng API Phiên dịch. Để biết thêm thông tin về cách sử dụng uỷ quyền GPU cho TensorFlow Lite, bao gồm cả 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 TensorFlow Lite với Dịch vụ Google Play

API Thông dịch của TensorFlow Lite cung cấp một bộ API dùng chung cho việc xây dựng các ứng dụng học máy. Phần này mô tả cách sử dụng thực thể đại diện của trình tăng tốc GPU với các API này với TensorFlow Lite với Dịch vụ Google Play.

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

Thêm phần phụ thuộc của dự án (bằng 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 = "org.tensorflow:tensorflow-lite-gpu", version = "2.X.Y" }
tflite-gpu-api = { module = "org.tensorflow:tensorflow-lite-gpu-api", version = "2.X.Y" }
...
  1. Thêm các phần phụ thuộc của 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 của GPU, hãy thêm com.google.android.gms:play-services-tflite-gpu vào tệp build.gradle của ứng dụng:

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 đó, khởi chạy TensorFlow Lite với các dịch vụ của 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 thông 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ể dùng tính năng uỷ quyền GPU với liên kết mô hình học máy trong Android Studio. Để biết thêm thông tin, hãy xem bài viết Tạo giao diện mô hình bằng siêu dữ liệu.

Sử dụng GPU với TensorFlow Lite độ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ì bạn có thể gói uỷ quyền GPU cho ứng dụng và sử dụng nó với phiên bản TensorFlow Lite độc lập.

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

Để cho phép truy cập vào thực thể đại diện của GPU, hãy thêm org.tensorflow:tensorflow-lite-gpu-delegate-plugin vào tệp build.gradle của ứng dụng:

dependencies {
    ...
    implementation 'org.tensorflow:tensorflow-lite'
    implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin'
}

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

Sau đó, chạy TensorFlow Lite trên GPU bằng 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);
      

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

Theo mặc định, các 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 cần phải thay đổi mã để sử dụng các mô hình lượng tử hoá với thực thể đại diện GPU. Phần sau đây giải thích cách tắt tính năng hỗ trợ lượng tử cho mục đích kiểm thử hoặc thử nghiệm.

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

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 nội dung tổng quan về uỷ quyền GPU.