Repository navigation
Add LiteRT CompiledModel snippets for the litert-runtime skill - #1186
smilingday wants to merge 2 commits into
Conversation
|
Here is the summary of changes. You are about to add 7 region tags.
This comment is generated by snippet-bot.
|
Adds LiteRtRuntimeSkillSnippets.kt to the ai module with region-tagged snippets backing the developer.android.com litert-runtime Android agent skill: imports, the Classifier helper (CompiledModel, TensorBuffer reuse, warm-up, close order), on-disk CompiledModel.create(filePath, ...), UiState/MainViewModel with GPU-to-CPU fallback, MainScreen, and preprocess. Adds the LiteRT 2.3.0 litert and litert-gpu dependencies.
8829f93 to
e396bf3
Compare
|
Hi @kkuan2011 @ksemenova @JolandaVerhoef, could one of you take a look when you have a chance? GitHub didn't auto-request reviewers on this PR (it comes from a fork). This adds the code snippets for the LiteRT
Thanks! |
|
The snippets are compile-only here, so I ran them on a phone. I built this head (3d8f5fc) and called its five code regions (all but the imports and the dependency lines) from a local instrumented test on a Pixel 8a (Android 16) with LiteRT 2.3.0. The test serves the model through a test-only asset loader and leaves the snippet file unchanged. All 23 tests passed:
|
kkuan2011
left a comment
There was a problem hiding this comment.
Looks good from a snippets repo perspective, but I'll defer to the Intelligent Experiences team to review the code as they'll be more familiar with this subject matter area. Thanks Shuangfeng for adding these snippets!
|
|
||
| package com.example.snippets.ai | ||
|
|
||
| // [START android_litert_runtime_skill_imports] |
There was a problem hiding this comment.
you actually don't need to add region tags around import statements (because ordering can change as you add more code to the file), so feel free to leave those hardcoded in the markdown file
| implementation(libs.firebase.ondevice) | ||
| implementation(libs.guava.android) | ||
| implementation(libs.mlkit.genai.prompt) | ||
| // [START android_litert_runtime_skill_dependencies] |
There was a problem hiding this comment.
same here, you don't need to add dependency block code snippets to the snippets repo (since the list of dependencies could change as the ai module grows), feel free to leave these dependencies hardcoded in the markdown file and remove the region tags here
Add LiteRT CompiledModel snippets for the litert-runtime skill
Summary
Adds compilable Kotlin snippets for the LiteRT
CompiledModelAPI(
com.google.ai.edge.litert:litert/litert-gpu2.3.0) used by thedeveloper.android.com Android agent skill litert-runtime
("Build an Android app with the LiteRT CompiledModel API",
/agents/skills/device-ai/litert/litert-runtime). The skill currently inlinesthis code; once this PR is merged the DAC page can include these regions instead
of hardcoded blocks.
ai/src/main/java/com/example/snippets/ai/LiteRtRuntimeSkillSnippets.kt(package
com.example.snippets.ai).gradle/libs.versions.toml: newlitert = "2.3.0"version andlitert,litert-gpulibrary entries.ai/build.gradle.kts:implementation(libs.litert)andimplementation(libs.litert.gpu)(wrapped in a region), plus the existingcatalog entries
androidx-lifecycle-runtime-composeandandroidx-lifecycle-viewModelComposeforcollectAsStateWithLifecycleandviewModel().Region tags
android_litert_runtime_skill_importsandroid_litert_runtime_skill_dependencies(ai/build.gradle.kts)android_litert_runtime_skill_file_pathCompiledModel.create(filePath, CompiledModel.Options(accelerator), environment)android_litert_runtime_skill_classifierClassifier:CompiledModel,TensorBufferreuse, warm-up ininit, close order)android_litert_runtime_skill_viewmodelUiState,MainViewModel, single-thread executor, GPU to CPU fallback onLiteRtException)android_litert_runtime_skill_screenMainScreen)android_litert_runtime_skill_preprocesspreprocess)Notes
small
createCompiledModelFromFile(filePath, accelerator, environment)function so the Step 1 on-disk
CompiledModel.create(filePath, ...)varianthas a compilable, self-contained home; callers pass the process-wide
Environment.minSdk = 24Gradle block is intentionally not ported: thisrepo's
minSdkis 36, so that block stays inline in the skill.aiapp doesnot reference these classes at runtime).
aidependency changes version, and none isremoved (
debugRuntimeClasspathdiffed againstmain). The new entries comeonly from LiteRT:
litert,litert-gpu,litert-api, plus the transitivedependencies of
litert-api2.3.0 for loading models from Play AI packs(
com.google.android.play:ai-delivery0.1.1-alpha01 and itsasset-delivery,core-common,work-runtime,room-*, andsqlite*).This PR doesn't add those directly.
Verification
./gradlew :ai:assembleDebug— BUILD SUCCESSFUL (JDK 21, AGP from the repo,compileSdk 37).
./gradlew :ai:spotlessApply :ai:spotlessKotlinCheck :ai:assembleDebug—BUILD SUCCESSFUL.
./gradlew :ai:clean :ai:assembleDebug :ai:testDebugUnitTest --rerun-tasks—BUILD SUCCESSFUL; recompiles every existing
aisnippet, andcheckDebugDuplicateClasses/mergeDebugNativeLibspass (no duplicateclasses or native libraries).