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

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

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

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

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

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

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

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

जीपीयू डेलिगेट का ऐक्सेस चालू करने के लिए, जोड़ें आपके ऐप्लिकेशन के 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 की मदद से, 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);
      

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

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

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

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

जीपीयू डेलिगेट का ऐक्सेस चालू करने के लिए, जोड़ें 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'
}

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

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

क्वांटाइज़्ड मॉडल के साथ काम करने की सुविधा बंद करें

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

Java

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

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

जीपीयू ऐक्सेलरेशन के साथ क्वांटाइज़्ड मॉडल चलाने के बारे में ज़्यादा जानकारी के लिए, देखें GPU डेलिगेट की खास जानकारी.