使用 translateer API 進行 GPU 加速委派

使用圖形處理器 (GPU) 執行機器學習 (ML) 模型 大幅改善產品效能和使用者體驗 支援機器學習的應用程式在 Android 裝置上,你可以啟用 委派和下列其中一個 API:

  • Translateer API - 本指南
  • 原生 (C/C++) API - 指南

本頁面說明如何在以下位置啟用 LiteRT 模型的 GPU 加速功能: 使用 解譯 er API 的 Android 應用程式。進一步瞭解如何使用 GPU LiteRT 的委派代表,包括最佳做法和進階技術 請參閱「GPU 委派」頁面。

搭配 Google Play 服務使用 GPU 和 LiteRT

LiteRT 翻譯工具 API 會提供一組 以及一般用途 API這個區段 說明如何搭配這些 API 使用 GPU 加速器委派項目 內建 Google Play 服務的 LiteRT。

建議使用 Google Play 服務的 LiteRT 在 Android 上使用 LiteRT 的路徑。如果您的應用程式指定的是裝置 未執行 Google Play,請參閱 GPU with Translateer API 和獨立式 GPU LiteRT 區段,。

新增專案依附元件 (含 .toml 版本目錄)

  1. 更新專案的 libs.versions.toml 檔案
[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. 在應用程式的 build.gradle.kts 中新增專案依附元件
dependencies {
  ...
  implementation(libraries.tflite.gpu)
  implementation(libraries.tflite.gpu.api)
  ...
}

新增專案依附元件

如要啟用 GPU 委派功能的存取權,請新增 com.google.android.gms:play-services-tflite-gpu至應用程式的 build.gradle 檔案:

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'
}

啟用 GPU 加速功能

然後使用 GPU 支援的 Google Play 服務初始化 LiteRT:

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());
});
        

您終於可以初始化傳遞 GpuDelegateFactory 的解譯器了。 到 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);
      

GPU 委派項目也可以與 Android Studio 中的機器學習模型繫結搭配使用。適用對象 詳情請參閱「使用以下程式碼產生模型介面: 中繼資料

搭配獨立的 LiteRT 使用 GPU

如果您的應用程式指定的不是執行 Google Play 的裝置,則 將 GPU 委派附加至應用程式,並與 獨立的 LiteRT 版本

新增專案依附元件

如要啟用 GPU 委派功能的存取權,請新增 對您的應用程式的 com.google.ai.edge.litert:litert-gpu-delegate-plugin build.gradle 檔案:

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

啟用 GPU 加速功能

然後使用 TfLiteDelegate 在 GPU 上執行 LiteRT。在 Java 中,您可以指定 GpuDelegateInterpreter.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);
      

量化模型

Android GPU 委派程式庫預設支援量化模型。你不需要 變更任何程式碼,才能搭配 GPU 委派使用量化模型。 下一節將說明如何停用量化支援功能,以用於測試、 實驗用途

停用量化模型支援功能

以下程式碼顯示如何停用量化模型的支援功能。

Java

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

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

如要進一步瞭解如何使用 GPU 加速功能執行量化模型,請參閱 GPU 委派總覽。