인터프리터 API를 사용한 GPU 가속 대리자

그래픽 처리 장치 (GPU)를 사용하여 머신러닝 (ML) 모델 실행 광고의 실적과 사용자 환경을 ML 지원 애플리케이션입니다 Android 기기에서 Delegate 및 다음 API 중 하나를 사용할 수 있습니다.

  • Interpreter API - 이 가이드
  • 네이티브 (C/C++) API - 가이드

이 페이지에서는 다음에서 LiteRT 모델에 GPU 가속을 사용 설정하는 방법을 설명합니다. Interpreter API를 사용하는 Android 앱 GPU 사용에 대한 자세한 내용은 (권장사항 및 고급 기술을 포함한) LiteRT 위임, GPU 대리자 페이지를 참조하세요.

Google Play 서비스에서 LiteRT와 함께 GPU 사용

LiteRT 인터프리터 API는 머신러닝 애플리케이션을 빌드하기 위한 범용 API입니다. 이 섹션 은 Google Play 서비스 LiteRT와 함께 사용할 수 있습니다.

Google Play 서비스와 함께 LiteRT를 사용하는 것이 좋습니다. 사용하는 방법을 설명합니다. 애플리케이션이 기기를 타겟팅하는 경우 자세히 알아보려면 Interpreter API가 포함된 GPU 및 독립형 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 대리자에 대한 액세스를 사용 설정하려면 다음을 추가합니다. 앱의 build.gradlecom.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'
}

GPU 가속 사용 설정

그런 다음 GPU를 지원하는 Google Play 서비스로 LiteRT를 초기화합니다.

Kotlin

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:

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)
      

자바

    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 위임은 Android 스튜디오에서 ML 모델 바인딩과 함께 사용할 수도 있습니다. 대상 자세한 내용은 다음을 사용하여 모델 인터페이스 생성 메타데이터를 참고하세요.

독립형 LiteRT로 GPU 사용

애플리케이션이 Google Play를 실행하지 않는 기기를 대상으로 하는 경우 애플리케이션에 GPU 대리자를 번들로 묶어 사용할 수 있습니다

프로젝트 종속 항목 추가

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 가속 사용 설정

그런 다음 TfLiteDelegate를 사용하여 GPU에서 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)
      

자바

      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 대리자와 함께 양자화 모델을 사용하려면 코드를 변경해야 합니다. 이 다음 섹션에서는 테스트 또는 배포를 위해 양자화 지원을 중지하는 방법을 설명합니다. 있습니다.

양자화 모델 지원 사용 중지

다음 코드는 양자화 모델에 대한 지원을 사용 중지하는 방법을 보여줍니다.

자바

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

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

GPU 가속으로 양자화 모델을 실행하는 방법에 관한 자세한 내용은 GPU 위임 개요