מתן גישה להאצה של GPU עם Translationer API

השימוש ביחידות עיבוד גרפיות (GPU) להפעלת מודלים של למידת מכונה (ML) יכול לשפר משמעותית את הביצועים ואת חוויית המשתמש של האפליקציות שתומכות ב-ML. במכשירי Android אפשר להפעיל הענקת גישה ואחד מממשקי ה-API הבאים:

  • Translate API – מדריך זה
  • Native (C/C++ ) API – מדריך

בדף הזה נסביר איך להפעיל האצה של GPU בדגמי TensorFlow Lite באפליקציות ל-Android באמצעות Translationer API. למידע נוסף על השימוש בהענקת גישה ל-GPU ב-TensorFlow Lite, כולל שיטות מומלצות וטכניקות מתקדמות, קראו את הדף משתמשים שקיבלו הרשאה ל-GPU.

שימוש ב-GPU עם TensorFlow Lite ב-Google Play Services

Translateer API של TensorFlow Lite מספק קבוצה של ממשקי API לשימוש כללי לצורך פיתוח אפליקציות של למידת מכונה. בקטע הזה מוסבר איך להשתמש בהאצלה של GPU עם ממשקי ה-API האלה עם TensorFlow Lite עם Google Play Services.

מומלץ להשתמש ב-TensorFlow Lite עם Google Play Services ב-Android. אם האפליקציה שלכם מטרגטת מכשירים שלא מריצים את Google Play, עיינו בקטע GPU עם Translate API ו-TensorFlow Lite עצמאי.

הוספת יחסי תלות של פרויקט (עם קטלוג גרסאות .toml)

  1. מעדכנים את קובץ libs.versions.toml של הפרויקט
[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. מוסיפים יחסי תלות של פרויקטים ב-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

לאחר מכן מפעילים את TensorFlow Lite בשירותי 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 עם TensorFlow Lite

אם האפליקציה שלכם מטרגטת מכשירים שלא מריצים בהם Google Play, אפשר לאגד את הגישה ל-GPU לאפליקציה שלכם ולהשתמש בה בגרסה העצמאית של TensorFlow Lite.

הוספת יחסי תלות של פרויקט

כדי להפעיל את הגישה להרשאה ל-GPU, צריך להוסיף את org.tensorflow:tensorflow-lite-gpu-delegate-plugin לקובץ build.gradle של האפליקציה:

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

הפעלת שיפור המהירות באמצעות GPU

לאחר מכן מריצים את TensorFlow Lite ב-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);
      

מודלים מדומים

ספריות הענקת גישה ל-Android GPU תומכות במודלים קוונטיים כברירת מחדל. לא צריך לבצע שינויים בקוד כדי להשתמש במודלים קוונטיים עם הענקת גישה ל-GPU. בקטע הבא מוסבר איך להשבית תמיכה כמותית למטרות בדיקה או ניסוי.

השבתת התמיכה במודלים מוגבלים

הקוד הבא מראה איך להשבית את התמיכה במודלים מכווננים.

Java

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

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

למידע נוסף על הפעלת מודלים קונטיים עם האצה של GPU, ראו את הסקירה הכללית בנושא הענקת גישה ל-GPU.