Skip to content

Commit c2ff8f4

Browse files
Force CPU backend for Gemma 4 LiteRT stability
1 parent 28f5bac commit c2ff8f4

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/feature/multimodal/PhotoReasoningViewModel.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,20 @@ class PhotoReasoningViewModel(
340340
}
341341
ensureLiteRtNativeLoaded()
342342
if (liteRtEngine == null) {
343-
val liteRtBackend = if (backend == InferenceBackend.GPU) Backend.GPU else Backend.CPU
343+
val liteRtBackend = Backend.CPU
344+
if (backend == InferenceBackend.GPU) {
345+
Log.w(
346+
TAG,
347+
"Gemma 4 offline currently forces CPU backend to avoid native crashes on GPU initialization."
348+
)
349+
}
344350
val engineConfig = EngineConfig(
345351
modelPath = modelFile.absolutePath,
346352
backend = liteRtBackend,
347353
cacheDir = context.cacheDir.absolutePath
348354
)
349355
liteRtEngine = Engine(engineConfig).also { it.initialize() }
350-
Log.d(TAG, "Offline model initialized with LiteRT-LM Engine backend=$backend")
356+
Log.d(TAG, "Offline model initialized with LiteRT-LM Engine backend=$liteRtBackend")
351357
}
352358
} else {
353359
if (llmInference == null) {
@@ -456,7 +462,12 @@ class PhotoReasoningViewModel(
456462
}
457463

458464
private fun isOfflineGpuModelLoaded(): Boolean {
459-
return com.google.ai.sample.GenerativeAiViewModelFactory.getCurrentModel().isOfflineModel &&
465+
val currentModel = com.google.ai.sample.GenerativeAiViewModelFactory.getCurrentModel()
466+
if (currentModel == ModelOption.GEMMA_4_E4B_IT) {
467+
// Gemma 4 offline currently runs with CPU backend for stability.
468+
return false
469+
}
470+
return currentModel.isOfflineModel &&
460471
com.google.ai.sample.GenerativeAiViewModelFactory.getBackend() == InferenceBackend.GPU &&
461472
(llmInference != null || liteRtEngine != null)
462473
}

0 commit comments

Comments
 (0)