نماینده شتاب GPU با Interpreter API

استفاده از واحدهای پردازش گرافیکی (GPU) برای اجرای مدل‌های یادگیری ماشینی (ML) می‌تواند عملکرد و تجربه کاربری برنامه‌های دارای ML را به‌طور چشمگیری بهبود بخشد. در دستگاه‌های Android، می‌توانید یک نماینده و یکی از APIهای زیر را فعال کنید:

  • Interpreter API - این راهنما
  • Native (C/C++) API - راهنمای

این صفحه نحوه فعال کردن شتاب GPU را برای مدل‌های LiteRT در برنامه‌های Android با استفاده از Interpreter API شرح می‌دهد. برای اطلاعات بیشتر در مورد استفاده از نماینده GPU برای LiteRT، از جمله بهترین شیوه‌ها و تکنیک‌های پیشرفته، به صفحه نمایندگان GPU مراجعه کنید.

از GPU با LiteRT با خدمات Google Play استفاده کنید

LiteRT Interpreter API مجموعه‌ای از APIهای عمومی را برای ساخت برنامه‌های یادگیری ماشین ارائه می‌کند. این بخش نحوه استفاده از نماینده شتاب دهنده GPU با این APIها با LiteRT با خدمات Google Play را توضیح می دهد.

LiteRT با خدمات Google Play مسیر توصیه شده برای استفاده از LiteRT در اندروید است. اگر برنامه شما دستگاه هایی را هدف قرار می دهد که از Google Play استفاده نمی کنند، به GPU با Interpreter 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)
  ...
}

وابستگی های پروژه را اضافه کنید

برای فعال کردن دسترسی به نماینده 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 راه اندازی کنید:

کاتلین

val useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)

val interpreterTask = useGpuTask.continueWith { useGpuTask ->
  TfLite.initialize(context,
      TfLiteInitializationOptions.builder()
      .setEnableGpuDelegateSupport(useGpuTask.result)
      .build())
  }
        

جاوا

Task<boolean> useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context);

Task<Options> interpreterOptionsTask = useGpuTask.continueWith({ task ->
  TfLite.initialize(context,
  TfLiteInitializationOptions.builder()
    .setEnableGpuDelegateSupport(true)
    .build());
});
        

در نهایت می توانید مفسری را که یک GpuDelegateFactory را از طریق InterpreterApi.Options ارسال می کند، مقداردهی اولیه کنید:

کاتلین


    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)
      

جاوا


    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 استفاده کنید.

وابستگی های پروژه را اضافه کنید

برای فعال کردن دسترسی به نماینده 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 اجرا کنید. در جاوا، می‌توانید GpuDelegate از طریق Interpreter.Options مشخص کنید.

کاتلین

      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)
      

جاوا

      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 اندروید به طور پیش فرض از مدل های کوانتیزه شده پشتیبانی می کنند. برای استفاده از مدل های کوانتیزه شده با نماینده GPU، نیازی به تغییر کد ندارید. بخش زیر نحوه غیرفعال کردن پشتیبانی کوانتیزه شده برای اهداف آزمایشی یا آزمایشی را توضیح می دهد.

پشتیبانی از مدل کوانتیزه را غیرفعال کنید

کد زیر نحوه غیرفعال کردن پشتیبانی از مدل های کوانتیزه را نشان می دهد.

جاوا

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

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

برای اطلاعات بیشتر در مورد اجرای مدل های کوانتیزه شده با شتاب GPU، به نمای کلی نماینده GPU مراجعه کنید.