应用体积更小、GPU 推理更快:Google Play 服务版 LiteRT 迎来重大升级
英文标题:Smaller apps and faster GPU inference with LiteRT in Google Play services
Google 宣布在 Google Play 服务中为 LiteRT(原 TensorFlow Lite)引入基于 ML Drift 驱动的全新 GPU 推理引擎与 CompiledModel API,GPU 推理速度较传统 Delegate 提升约 1.4 倍。开发者将打包在 APK 中的运行时切换为 Google Play 服务共享运行时后,应用体积可缩减约 8 MB,并能享受无需重新发版即可分批推送的自动运行时更新。此外,LiteRT 支持从 Android 7 到 Wear OS 4+ 及车载系统等多端部署,Android Studio 亦同步上线了 AI Agent 迁移技能以简化重构流程。
86.9 KB
203.6 KB
43.4 KB
68.0 KB
中文
Google Play 服务中的 LiteRT 是 Android 面向自定义模型的端侧推理运行时,每天在数十亿台设备上处理超过一万亿次推理。今天,它迎来了有史以来最大的升级:由 ML Drift 驱动的 GPU 推理速度比传统的 GPU Delegate 快约 1.4 倍,并可通过全新推出的 CompiledModel API 调用——这是 LiteRT 对传统 Interpreter API 的现代化演进替代品。
在此之前,要利用这一更快的 GPU 技术栈,必须将 LiteRT 打包到你的应用内部。在我们的测试中,迁移到 Google Play 服务中 LiteRT 的应用体积减少了约 8 MB,这使应用下载更快,并且对用户的设备存储更轻量。这是因为包括 GPU 加速器在内的运行时只需随 Play 服务分发一次,而无需内置在每个应用中。如今有超过 10 万个 Android 应用捆绑了自有的 LiteRT 或 TensorFlow Lite 副本,因此随着应用逐步迁移,节省的存储资源将迅速累加。
如果你的 Android 应用在 CPU 或 GPU 上运行自定义机器学习模型,我们强烈建议你迁移到 Google Play 服务中的 LiteRT。 你可以在 Google AI Edge Gallery 应用中查看实际效果,该应用中 Scrapbook 功能的图像分割就是基于 Google Play 服务中的 LiteRT 运行的。
为什么使用 Google Play 服务中的 LiteRT
采用 Google Play 服务版 LiteRT 的开发者可以立即享受到以下好处:
- 更小的应用体积,释放更多存储空间:由于无需将机器学习运行时打包进 APK,你的应用体积更小、下载更快。此外,由于多个应用共享同一运行时,随着迁移应用的增加,存储和内存的节省效果也会越发显著。
- 自动更新:性能改进、问题修复以及安全补丁都会通过 Play 服务分阶段推送,在无需开发者发布应用更新的情况下及早发现特定设备上的问题。
- 覆盖更多运行环境:Play 服务中的 LiteRT 支持最低至 Android 7(API 级别 24)的手机,现在还扩展支持 Wear OS 4+ 和 Android Automotive OS(Android 11+)。
- 经过大规模检验:ML Kit 的视觉和文本 API,例如主体分割(Subject Segmentation)与文档扫描仪(Document Scanner),已经在数十亿台 Android 设备上依托 Google Play 服务中的 LiteRT 规模化运行。通过采用 Google Play 服务中的 LiteRT,你可以在自己的应用中直接复用同样经过严苛实战检验的基础设施。
CompiledModel API
LiteRT 是 TensorFlow Lite 的演进版本。它保留了你所依赖的 .tflite 模型格式,并新增了 CompiledModel API,使跨碎片化硬件的加速更加可靠:
- 更简单:统一 CPU 和 GPU 的加速工作流,不再需要为每个加速器维护不同的 delegate 和配置路径。LiteRT 在可用时使用 OpenCL,其他情况下使用 OpenGL。如果你同时请求了 GPU 和 CPU,在没有可用 GPU 的设备上它会自动降级回退到 CPU。
- 更快速:由 ML Drift 驱动,这是 LiteRT 最新的开源 GPU 引擎,已在 Google 各大应用中每天应用于数百万台设备上。GPU 推理速度比传统的 TensorFlow Lite GPU delegate 快约 1.4 倍。
- 可移植:跨平台支持,同一份
.tflite模型可以在 Android、iOS、桌面端和 Web 端运行。 - 高灵活性:CPU 上仍旧支持传统的 Interpreter API,因此你可以分阶段逐步迁移。
上手入门与升级
无论你是全新开始还是从内置的 LiteRT 迁移,在 Google Play 服务中使用 LiteRT 只需几行代码:
- 添加
com.google.android.gms:play-services-litert:17.0.0(或更高版本)。如果你目前内置了 LiteRT,请移除旧的litert依赖。 - 从 Play 服务获取
LiteRT环境:
kotlin val liteRtEnvironment = LiteRt.initialize( context, LiteRtDsl.initializationOptions { setEnableGpu(true) } ).await()
- 在创建
CompiledModel时传入 LiteRTenvironment。其余代码保持不变。
如果你已经在 Google Play 服务中使用了旧版的 Interpreter API,切换到 play-services-litert 后,你的 Interpreter 代码在 CPU 上仍可继续工作。若要在 GPU 上运行,请迁移到 CompiledModel API。
让你的编码 Agent 代劳。 在 Agent 模式下,Android Studio 的全新 LiteRT 技能(Skill)可以自动化迁移到 Google Play 服务的 LiteRT 以及 CompiledModel API。你可以对它说“将我的 TFLite 代码迁移到 CompiledModel”,或者输入 @litert-runtime,它就会将你的应用切换到 play-services-litert,重写你的 Interpreter 调用,并配置具有 CPU 回退机制的 GPU 加速。它还可以用于搭建全新的 LiteRT 应用脚手架。建议将其作为高效的初稿:检查 diff 变更、编译并在真实设备上比对输出结果。在 GitHub 上获取该技能。
从旧版 API 升级? 传统的 GPU delegate 和自定义 delegate API 已被弃用,建议改用 CompiledModel GPU 加速以及 Compiler Plugin 和 Dispatch API;NNAPI 也将从 2027 年开始彻底移除。较旧的 SDK 目前仅接收严重安全修复。你已发布的旧版应用仍可正常运行,但建议现在就规划迁移到 CompiledModel API:使用已弃用 API 的代码将无法基于最新的 SDK 进行编译,而停留在旧版 SDK 可能会随着其他依赖库的升级而引发冲突。详情请参阅发布说明。
未来展望
展望未来,Google Play 服务中的 LiteRT 将成为我们在 Android 上运行特定任务自定义模型并实现 CPU 与 GPU 加速的基石。
- LiteRT 将持续获得定期更新,包括更强大的 GPU 加速和更广泛的模型覆盖。
- Play 服务中的 NPU 支持目前正在积极开发中。
准备好开始了吗?
我们迫不及待地想看到你所构建的应用。
English / 原文
LiteRT in Google Play services is Android's on-device inference runtime for custom models, handling more than a trillion inferences a day across billions of devices. Today, it gets itsbiggest upgrade yet: GPU inference that runs about 1.4x faster than the legacy GPU delegate , powered by ML Drift and available through the new CompiledModel API, LiteRT’s modern successor to the legacy Interpreter API.
Until now, taking advantage of this faster GPU stack required bundling LiteRT in your app. In our testing, apps that switched to LiteRT in Google Play services are about 8 MB smaller , making them faster to download and lighter on users' devices , because the runtime, including the GPU accelerator, ships once with Play services rather than inside every app. More than 100K Android apps bundle their own copy of LiteRT or TensorFlow Lite today, so the savings add up quickly as apps switch.
If your Android app runs custom ML models on CPU or GPU, we encourage you to switch to LiteRT in Google Play services. You can see it in action in the Google AI Edge Gallery app, where the Scrapbook feature's image segmentation runs on LiteRT in Google Play services.
Why use LiteRT in Google Play services
Developers that adopt LiteRT in Google Play Services enjoy immediate benefits:
- Smaller apps, more free storage : As no ML runtime is bundled into your APK, your app is smaller and faster to download. In addition, as apps share one runtime, the storage and memory savings grow with each app that switches.
- Automatic updates : performance improvements, bug fixes, and security patches arrive through Play services in staged rollouts that catch device-specific issues early, without you having to ship an app update.
- More places to run : LiteRT in Play services runs on phones back to Android 7 (API level 24), and now also on Wear OS 4+ and Android Automotive OS (Android 11+).
- Tested at scale : ML Kit's Vision and Text APIs, like Subject Segmentation and Document Scanner, are already powered by LiteRT in Google Play services at scale across billions of Android devices. By adopting LiteRT in Google Play Services you are using the same, battle-tested infrastructure in your own app.
CompiledModel API
LiteRT is the evolution of TensorFlow Lite. It keeps the same .tflite model format you rely on and adds the CompiledModel API, which makes acceleration across fragmented hardware reliable:
- Simpler : one workflow for CPU and GPU acceleration instead of a different delegate and setup path for each accelerator. LiteRT uses OpenCL where available and OpenGL otherwise. If you request both GPU and CPU, it falls back to CPU on devices without a usable GPU.
- Faster : powered by ML Drift, LiteRT's latest, open-source GPU engine already used by Google apps on millions of devices daily. GPU inference runs about 1.4x faster than the legacy TensorFlow Lite GPU delegate.
- Portable : cross-platform support, so the same
.tflitemodel runs on Android, iOS, desktop, and web. - Flexible : the legacy Interpreter API is still supported on CPU, so you can migrate in phases.
Getting started and upgrading
Whether you're starting fresh or switching from bundled LiteRT, using LiteRT in Google Play services takes a few lines of code:
- Add
com.google.android.gms:play-services-litert:17.0.0(or a later version). If you bundle LiteRT today, remove thelitertdependency. - Get the
LiteRTenvironment from Play services:
val liteRtEnvironment =
LiteRt.initialize(
context,
LiteRtDsl.initializationOptions { setEnableGpu(true) }
).await()
Kotlin
- Pass the LiteRT
environmentin when you create yourCompiledModel. The rest of your code stays the same.
If you're already using the legacy Interpreter API in Google Play services , your Interpreter code keeps working on CPU when you switch to play-services-litert. To run on the GPU, move to the CompiledModel API.
Let your coding agent do the work. In Agent Mode, the new LiteRT skill for Android Studio automates the move to LiteRT in Google Play services and the CompiledModel API. Ask it to “migrate my TFLite code to CompiledModel,” or type @litert-runtime, and it switches your app to play-services-litert, rewrites your Interpreter calls, and sets up GPU acceleration with CPU fallback. It can also scaffold a new LiteRT app. Treat it as a strong first pass: review the diff, build, and compare outputs on a real device. Get the skill on GitHub.
Upgrading from legacy APIs? The legacy GPU delegate and custom delegate APIs are deprecated in favor of CompiledModel GPU acceleration and the Compiler Plugin and Dispatch APIs, and NNAPI will be removed starting in 2027. Older SDKs now receive critical security fixes only. Apps you've already shipped keep working, but plan your move to the CompiledModel API now: code that uses deprecated APIs won't compile against the latest SDK, and staying on an older SDK can cause conflicts as your other dependencies move forward. See the release notes for details.
What's next
Looking ahead, LiteRT in Google Play services is our foundation for running your own task-specific models on Android, with CPU and GPU acceleration.
- LiteRT will keep getting regular updates, including better GPU acceleration and wider model coverage.
- NPU support in Play services is actively being worked on.
Ready to start**?**
We can't wait to see what you build.