अनुवादक एपीआई की मदद से जीपीयू ऐक्सेलरेशन का डेलिगेट

मशीन लर्निंग (एमएल) मॉडल को चलाने के लिए, ग्राफ़िक प्रोसेसिंग यूनिट (जीपीयू) का इस्तेमाल करने से, मशीन लर्निंग (एमएल) की सुविधा वाले ऐप्लिकेशन की परफ़ॉर्मेंस और उनका उपयोगकर्ता अनुभव काफ़ी बेहतर हो सकता है. Android डिवाइसों पर, डेलीगेट और इनमें से किसी एक एपीआई को चालू किया जा सकता है:

  • अनुवादक एपीआई - यह गाइड
  • Native (C/C++) एपीआई - गाइड

इस पेज पर बताया गया है कि Android ऐप्लिकेशन में 'अनुवादक एपीआई' का इस्तेमाल करके, TensorFlow Lite मॉडल के लिए जीपीयू से तेज़ी लाने का तरीका कैसे चालू किया जाता है. TensorFlow Lite के लिए जीपीयू डेलिगेट का इस्तेमाल करने, साथ ही सबसे सही तरीके और बेहतर तकनीकों के बारे में ज़्यादा जानने के लिए, जीपीयू डेलिगेट पेज देखें.

Google Play services के साथ TensorFlow Lite के साथ जीपीयू का इस्तेमाल करें

TensorFlow Lite अनुवादक एपीआई, मशीन लर्निंग ऐप्लिकेशन बनाने के लिए, सामान्य तौर पर इस्तेमाल किए जाने वाले एपीआई का सेट उपलब्ध कराता है. इस सेक्शन में बताया गया है कि Google Play services के साथ TensorFlow Lite की मदद से, इन एपीआई की मदद से जीपीयू ऐक्सेलरेटर डेलिगेट को कैसे इस्तेमाल किया जाता है.

Android पर TensorFlow Lite इस्तेमाल करने के लिए, Google Play services के साथ TensorFlow Lite का सुझाव दिया जाता है. अगर आपका ऐप्लिकेशन ऐसे डिवाइसों को टारगेट कर रहा है जिन पर Google Play नहीं चल रहा है, तो अनुवादक एपीआई के साथ जीपीयू और स्टैंडअलोन 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)
  ...
}

प्रोजेक्ट डिपेंडेंसी जोड़ें

जीपीयू डेलिगेट का ऐक्सेस देने के लिए, अपने ऐप्लिकेशन की build.gradle फ़ाइल में com.google.android.gms:play-services-tflite-gpu जोड़ें:

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

यह विकल्प जीपीयू से तेज़ी लाने की सुविधा चालू करता है

इसके बाद, जीपीयू के साथ काम करने वाली Google Play services की मदद से TensorFlow Lite की शुरुआत करें:

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

आखिर में, इंटरप्रेटर को InterpreterApi.Options के ज़रिए GpuDelegateFactory पास करना शुरू किया जा सकता है:

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

Android Studio में एमएल मॉडल बाइंडिंग के साथ भी जीपीयू डेलिगेट का इस्तेमाल किया जा सकता है. ज़्यादा जानकारी के लिए, मेटाडेटा का इस्तेमाल करके मॉडल इंटरफ़ेस जनरेट करना देखें.

स्टैंडअलोन TensorFlow Lite के साथ जीपीयू का इस्तेमाल करें

अगर आपका ऐप्लिकेशन उन डिवाइसों को टारगेट करता है जिन पर Google Play नहीं चल रहा है, तो आपके ऐप्लिकेशन में जीपीयू डेलिगेट को बंडल किया जा सकता है और उसे TensorFlow Lite के स्टैंडअलोन वर्शन के साथ इस्तेमाल किया जा सकता है.

प्रोजेक्ट डिपेंडेंसी जोड़ें

जीपीयू डेलिगेट का ऐक्सेस देने के लिए, अपने ऐप्लिकेशन की build.gradle फ़ाइल में org.tensorflow:tensorflow-lite-gpu-delegate-plugin जोड़ें:

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

यह विकल्प जीपीयू से तेज़ी लाने की सुविधा चालू करता है

इसके बाद, TfLiteDelegate की मदद से जीपीयू पर TensorFlow Lite चलाएं. Java में, Interpreter.Options के ज़रिए GpuDelegate के बारे में बताया जा सकता है.

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 जीपीयू डेलिगेट की गई लाइब्रेरी, डिफ़ॉल्ट रूप से संख्या वाले मॉडल के साथ काम करती हैं. जीपीयू डेलिगेट के साथ संख्या वाले मॉडल इस्तेमाल करने के लिए, आपको किसी कोड में बदलाव करने की ज़रूरत नहीं है. नीचे दिए गए सेक्शन में, टेस्टिंग या प्रयोग के लिए, संख्या के आधार पर मिलने वाली सहायता को बंद करने का तरीका बताया गया है.

संख्या वाले मॉडल की सुविधा बंद करें

इस कोड में, संख्या वाले मॉडल के लिए सहायता बंद करने का तरीका बताया गया है.

Java

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

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

जीपीयू की रफ़्तार के साथ संख्या वाले मॉडल चलाने के बारे में ज़्यादा जानकारी के लिए, GPU प्रतिनिधि की खास जानकारी देखें.