มอบสิทธิ์การเร่งความเร็วของ GPU ด้วย Mediation API

การใช้หน่วยประมวลผลกราฟิก (GPU) เพื่อเรียกใช้โมเดลแมชชีนเลิร์นนิง (ML) จะช่วยปรับปรุงประสิทธิภาพและประสบการณ์ของผู้ใช้ แอปพลิเคชันที่รองรับ ML ในอุปกรณ์ Android คุณสามารถเปิดใช้ ตัวแทนและ API อย่างใดอย่างหนึ่งต่อไปนี้

  • Interpreter API - คู่มือนี้
  • API ดั้งเดิม (C/C++) - คำแนะนำ

หน้านี้อธิบายวิธีเปิดใช้การเร่ง GPU สำหรับโมเดล LiteRT ใน แอป Android ที่ใช้ Interpreter API สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้ GPU สำหรับ LiteRT รวมถึง แนวทางปฏิบัติที่ดีที่สุดและเทคนิคขั้นสูง ดูหน้าผู้รับมอบสิทธิ์ GPU

ใช้ GPU กับ LiteRT ด้วยบริการ Google Play

โหมดล่ามLiteRT API จะมีชุดของ API สำหรับวัตถุประสงค์ทั่วไปในการสร้างแอปพลิเคชันแมชชีนเลิร์นนิง ส่วนนี้ อธิบายวิธีใช้การมอบสิทธิ์ตัวเร่ง GPU กับ API เหล่านี้ด้วย LiteRT ด้วยบริการ Google Play

ขอแนะนำให้ใช้ LiteRT ที่มีบริการ Google Play สู่การใช้ LiteRT บน Android หากแอปพลิเคชันของคุณกำหนดเป้าหมายอุปกรณ์ ในกรณีที่เรียกใช้ Google Play โปรดดู GPU ที่มี Interpreter API และแบบสแตนด์อโลน LiteRT

เพิ่มทรัพยากร Dependency ของโปรเจ็กต์ (ด้วยแคตตาล็อกเวอร์ชัน .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. เพิ่มทรัพยากร Dependency ของโปรเจ็กต์ใน build.gradle.kts ของแอป
dependencies {
  ...
  implementation(libraries.tflite.gpu)
  implementation(libraries.tflite.gpu.api)
  ...
}

เพิ่มทรัพยากร Dependency ของโปรเจ็กต์

ในการเปิดใช้การเข้าถึงตัวแทนของ 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

จากนั้นเริ่มต้น LiteRT ด้วยบริการ Google Play ที่รองรับ 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());
});
        

ในที่สุดก็เริ่มต้นล่ามผ่าน 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 ยังใช้กับการเชื่อมโยงโมเดล ML ใน Android Studio ได้ด้วย สำหรับ สำหรับข้อมูลเพิ่มเติม โปรดดูการสร้างอินเทอร์เฟซโมเดลโดยใช้ ข้อมูลเมตา

ใช้ GPU กับ LiteRT แบบสแตนด์อโลน

หากแอปพลิเคชันของคุณกำหนดเป้าหมายอุปกรณ์ที่ไม่ได้เรียกใช้ Google Play รวมกลุ่มผู้รับมอบสิทธิ์ GPU เข้ากับแอปพลิเคชันของคุณและใช้กับ LiteRT เวอร์ชันแบบสแตนด์อโลน

เพิ่มทรัพยากร Dependency ของโปรเจ็กต์

ในการเปิดใช้การเข้าถึงตัวแทนของ 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

จากนั้นเรียกใช้ LiteRT ใน 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 ของ Android รองรับโมเดลที่เล็กลงโดยค่าเริ่มต้น คุณไม่ได้ ต้องทำการเปลี่ยนแปลงโค้ดเพื่อใช้โมเดลที่ควอนซ์กับการมอบสิทธิ์ GPU ส่วนต่อไปนี้จะอธิบายวิธีปิดใช้การสนับสนุนที่เล็กลงสำหรับการทดสอบหรือ เพื่อการทดลอง

ปิดใช้การรองรับโมเดลที่เล็กลง

โค้ดต่อไปนี้แสดงวิธีปิดใช้การรองรับโมเดลที่เล็กลง

Java

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

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

ดูข้อมูลเพิ่มเติมเกี่ยวกับการเรียกใช้โมเดลที่เล็กลงด้วยการเร่ง GPU ได้ที่ ภาพรวมผู้รับมอบสิทธิ์ GPU