Modern Apps git

← Modern-Apps.git

ml: gemma4 sampling falls back to full logits when top-k fails

Commit
050f0daf3e38963561c8c09cd8c60356a2813853
Author
vayun-mathur <[email protected]>
When
2026-10-26 00:51 UTC (1 month ago)
Parents
ede048c

Diff

library/ml/src/main/java/com/vayunmathur/library/ml/Gemma4Handle.kt
diff --git a/library/ml/src/main/java/com/vayunmathur/library/ml/Gemma4Handle.kt b/library/ml/src/main/java/com/vayunmathur/library/ml/Gemma4Handle.ktindex d749993f6b..5edc0453f7 100644--- a/library/ml/src/main/java/com/vayunmathur/library/ml/Gemma4Handle.kt+++ b/library/ml/src/main/java/com/vayunmathur/library/ml/Gemma4Handle.kt@@ -345,9 +345,18 @@ class Gemma4Handle private constructor(internal val directory: File) : AutoClose         val token = when {             sampling == null -> MlNative.stepGemma4(handle, next)             sampling.topK in 1..GEMMA4_TOP_K_CANDIDATES -> {-                val candidates =-                    MlNative.topKGemma4(handle, next, GEMMA4_TOP_K_CANDIDATES) ?: return null-                sampleGemma4Candidates(candidates, sampling, step)+                val before = MlNative.positionGemma4(handle)+                val candidates = MlNative.topKGemma4(handle, next, GEMMA4_TOP_K_CANDIDATES)+                when {+                    candidates != null -> sampleGemma4Candidates(candidates, sampling, step)+                    // Only when the failed call did not consume the token: feeding it again+                    // after it reached the cache would put it there twice.+                    MlNative.positionGemma4(handle) == before -> {+                        val logits = MlNative.logitsGemma4(handle, next) ?: return null+                        sampleGemma4Logits(logits, sampling, step)+                    }+                    else -> return null+                }             }             else -> {                 val logits = MlNative.logitsGemma4(handle, next) ?: return null