[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-24 (世界標準時間)。"],[],[],null,["# GPU acceleration delegate with Interpreter API\n\nUsing graphics processing units (GPUs) to run your machine learning (ML) models\ncan dramatically improve the performance and the user experience of your\nML-enabled applications. On Android devices, you can enable a\n[*delegate*](../performance/delegates) and one of the following APIs:\n\n- Interpreter API - this guide\n- Native (C/C++) API - [guide](./gpu_native)\n\nThis page describes how to enable GPU acceleration for LiteRT models in\nAndroid apps using the Interpreter API. For more information about using the GPU\ndelegate for LiteRT, including best practices and advanced techniques,\nsee the [GPU delegates](../performance/gpu) page.\n\nUse GPU with LiteRT with Google Play services\n---------------------------------------------\n\nThe LiteRT [Interpreter\nAPI](../../api/tflite/java/org/tensorflow/lite/InterpreterApi) provides a set of\ngeneral purpose APIs for building a machine learning applications. This section\ndescribes how to use the GPU accelerator delegate with these APIs with\nLiteRT with Google Play services.\n\n[LiteRT with Google Play services](./play_services) is the recommended\npath to use LiteRT on Android. If your application is targeting devices\nnot running Google Play, see the [GPU with Interpreter API and standalone\nLiteRT](#standalone) section.\n\n### Add project dependencies (with .toml version catalog)\n\n1. Update your project's `libs.versions.toml` file\n\n [libraries]\n ...\n tflite-gpu = { module = \"com.google.ai.edge.litert:litert-gpu\", version = \"2.X.Y\" }\n tflite-gpu-api = { module = \"com.google.ai.edge.litert:litert-gpu-api\", version = \"2.X.Y\" }\n ...\n\n1. Add project dependencies in the app's `build.gradle.kts`\n\n dependencies {\n ...\n implementation(libs.tflite.gpu)\n implementation(libs.tflite.gpu.api)\n ...\n }\n\n### Add project dependencies\n\nTo enable access to the GPU delegate, add\n`com.google.android.gms:play-services-tflite-gpu` to your app's `build.gradle`\nfile: \n\n dependencies {\n ...\n implementation 'com.google.android.gms:play-services-tflite-java:16.0.1'\n implementation 'com.google.android.gms:play-services-tflite-gpu:16.1.0'\n }\n\n### Enable GPU acceleration\n\nThen initialize LiteRT with Google Play services with the GPU support: \n\n### Kotlin\n\n```kotlin\nval useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context)\n\nval interpreterTask = useGpuTask.continueWith { useGpuTask -\u003e\n TfLite.initialize(context,\n TfLiteInitializationOptions.builder()\n .setEnableGpuDelegateSupport(useGpuTask.result)\n .build())\n }\n \n```\n\n### Java\n\n```java\nTask\u003cboolean\u003e useGpuTask = TfLiteGpu.isGpuDelegateAvailable(context);\n\nTask\u003cOptions\u003e interpreterOptionsTask = useGpuTask.continueWith({ task -\u003e\n TfLite.initialize(context,\n TfLiteInitializationOptions.builder()\n .setEnableGpuDelegateSupport(true)\n .build());\n});\n \n```\n\nYou can finally initialize the interpreter passing a `GpuDelegateFactory`\nthrough `InterpreterApi.Options`: \n\n### Kotlin\n\n\u003cbr /\u003e\n\n```kotlin\n val options = InterpreterApi.Options()\n .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)\n .addDelegateFactory(GpuDelegateFactory())\n\n val interpreter = InterpreterApi(model, options)\n\n // Run inference\n writeToInput(input)\n interpreter.run(input, output)\n readFromOutput(output)\n \n```\n\n\u003cbr /\u003e\n\n### Java\n\n\u003cbr /\u003e\n\n```java\n Options options = InterpreterApi.Options()\n .setRuntime(TfLiteRuntime.FROM_SYSTEM_ONLY)\n .addDelegateFactory(new GpuDelegateFactory());\n\n Interpreter interpreter = new InterpreterApi(model, options);\n\n // Run inference\n writeToInput(input);\n interpreter.run(input, output);\n readFromOutput(output);\n \n```\n\n\u003cbr /\u003e\n\n| **Note:** The GPU delegate must be created on the same thread that runs it. Otherwise, you may see the following error, `TfLiteGpuDelegate Invoke:\n| GpuDelegate must run on the same thread where it was initialized.`\n\nThe GPU delegate can also be used with ML model binding in Android Studio. For\nmore information, see [Generate model interfaces using\nmetadata](./metadata/codegen#acceleration).\n\nUse GPU with standalone LiteRT\n------------------------------\n\nIf your application is targets devices which are not running Google Play, it is\npossible to bundle the GPU delegate to your application and use it with the\nstandalone version of LiteRT.\n\n### Add project dependencies\n\nTo enable access to the GPU delegate, add\n`com.google.ai.edge.litert:litert-gpu-delegate-plugin` to your app's\n`build.gradle` file: \n\n dependencies {\n ...\n implementation 'com.google.ai.edge.litert:litert'\n implementation 'com.google.ai.edge.litert:litert-gpu'\n implementation 'com.google.ai.edge.litert:litert-gpu-api'\n }\n\n### Enable GPU acceleration\n\nThen run LiteRT on GPU with `TfLiteDelegate`. In Java, you can specify\nthe `GpuDelegate` through `Interpreter.Options`. \n\n### Kotlin\n\n\u003cbr /\u003e\n\n```kotlin\n import org.tensorflow.lite.Interpreter\n import org.tensorflow.lite.gpu.CompatibilityList\n import org.tensorflow.lite.gpu.GpuDelegate\n\n val compatList = CompatibilityList()\n\n val options = Interpreter.Options().apply{\n if(compatList.isDelegateSupportedOnThisDevice){\n // if the device has a supported GPU, add the GPU delegate\n val delegateOptions = compatList.bestOptionsForThisDevice\n this.addDelegate(GpuDelegate(delegateOptions))\n } else {\n // if the GPU is not supported, run on 4 threads\n this.setNumThreads(4)\n }\n }\n\n val interpreter = Interpreter(model, options)\n\n // Run inference\n writeToInput(input)\n interpreter.run(input, output)\n readFromOutput(output)\n \n```\n\n\u003cbr /\u003e\n\n### Java\n\n\u003cbr /\u003e\n\n```java\n import org.tensorflow.lite.Interpreter;\n import org.tensorflow.lite.gpu.CompatibilityList;\n import org.tensorflow.lite.gpu.GpuDelegate;\n\n // Initialize interpreter with GPU delegate\n Interpreter.Options options = new Interpreter.Options();\n CompatibilityList compatList = CompatibilityList();\n\n if(compatList.isDelegateSupportedOnThisDevice()){\n // if the device has a supported GPU, add the GPU delegate\n GpuDelegate.Options delegateOptions = compatList.getBestOptionsForThisDevice();\n GpuDelegate gpuDelegate = new GpuDelegate(delegateOptions);\n options.addDelegate(gpuDelegate);\n } else {\n // if the GPU is not supported, run on 4 threads\n options.setNumThreads(4);\n }\n\n Interpreter interpreter = new Interpreter(model, options);\n\n // Run inference\n writeToInput(input);\n interpreter.run(input, output);\n readFromOutput(output);\n \n```\n\n\u003cbr /\u003e\n\n### Quantized models\n\nAndroid GPU delegate libraries support quantized models by default. You do not\nhave to make any code changes to use quantized models with the GPU delegate. The\nfollowing section explains how to disable quantized support for testing or\nexperimental purposes.\n\n#### Disable quantized model support\n\nThe following code shows how to ***disable*** support for quantized models. \n\n### Java\n\n\u003cbr /\u003e\n\n```java\nGpuDelegate delegate = new GpuDelegate(new GpuDelegate.Options().setQuantizedModelsAllowed(false));\n\nInterpreter.Options options = (new Interpreter.Options()).addDelegate(delegate);\n \n```\n\n\u003cbr /\u003e\n\nFor more information about running quantized models with GPU acceleration, see\n[GPU delegate](./gpu#quantized_models) overview."]]