feat(gemma): optional maxInferenceLen on load() to cap KV cache on constrained devices (#178)#180
Merged
Merged
Conversation
) The eager network sizes its KV cache + RoPE tables for maxInferenceLen (= min(contextLength, 4096) by default). On the 1.9 GB SL2610 that ~0.4 GB KV cache (allocated at the first forward) OOMs the board even after the packed Q8_0 lm_head dropped the weight footprint to ~1.06 GB resident. Thread an optional `maxInferenceLen: Int? = null` through load() -> applyWeightsToNetwork -> applyWeightsToNetworkNonReified -> gemmaNetwork so a constrained-device consumer can cap the context (e.g. 32 for a short tool-call prompt), shrinking the KV cache ~100x. Default null preserves the existing min(contextLength, 4096) behaviour. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #179. Adds an optional
maxInferenceLentoGemmaNetworkLoader.load()(threaded throughapplyWeightsToNetwork[NonReified]→gemmaNetwork).Why
The eager network sizes its KV cache + RoPE tables for
maxInferenceLen(defaultmin(contextLength, 4096)). On the 1.9 GB SL2610, after #179 dropped the weight footprint to ~1.06 GB resident (packed Q8_0 lm_head), the first forward still allocates the ~0.4 GB KV cache for a 4096-token context and OOMs the board — even though a tool-call prompt is ~13 tokens.Capping
maxInferenceLen(e.g. 32) shrinks the KV cache ~100×, so the eager decode fits. Defaultnullpreserves existing behaviour.On-board evidence
A composite build of #736+#737+#179 loaded FunctionGemma to a stable 1.06 GB on the SL2610 (vs the prior 1.5 GB OOM-at-load), confirming the packed-Q8_0-lm_head fix works on hardware; the remaining OOM was the uncapped KV cache this param addresses.
Part of #178.
🤖 Generated with Claude Code