functiongemma: chunk prefill-with-past graph gemma_prefill_with_past — an utterance against the cache in one call (#410) - #417
Merged
Conversation
…— a fixed 64-token chunk against the dynamic cache in one call (#410) GemmaModel.forwardPrefillWithPast / attnWithPastChunk: heads-first [heads, C, headDim] projections, RoPE from per-position [C, headDim] tables, attention over past+C with a caller-built additive mask [1, 1, C, past+C] per layer type (causal band, padding, and the 512 sliding window all live in the mask, so the graph stays position-agnostic), K/V extended by the chunk, LM head on the one-hot-selected position. Contract: FN_PREFILL_WITH_PAST, DEFAULT_CHUNK = 64, prefillWithPastArgs/Outputs (first-use order), manifest entries; CLI GEMMA_GRAPH=prefill_with_past (GEMMA_CHUNK); dump test.
…oadcast over heads to a dynamic shape is not expressible in static StableHLO
This was referenced Sep 4, 2026
Base automatically changed from
functiongemma-position-selected-head
to
develop
September 4, 2026 20:21
2 tasks done
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.
Part of #410 (the "chunk prefill" item). Stacked on #415 — merge that first.
Why
With the KV contract, a user utterance still costs one
gemma_with_pastcall per token (0.26 s each on the device's Mali GPU with bf16 archives → 9–14 s for a 36–52-token utterance). This graph runs a fixed-size chunk of tokens against the cache in one call.Change (additive)
GemmaModel.forwardPrefillWithPast(tokens, ChunkContext, selectAt, selfKIn, selfVIn, ctx)andattnWithPastChunk: heads-first[heads, C, headDim]projections (reshape + permute), RoPE from per-position[C, headDim]cos/sin tables, attention overpast + Ckeys with a caller-supplied additive mask per layer type[1, nHeads, C, past+C](0 = attend, -1e30 = masked; the mask carries the causal band inside the chunk, the zero padding, and the 512-position sliding window, so the graph stays position-agnostic likegemma_with_past), K/V extended by the chunk, LM head on the one-hot-selected position.[1, 1, C, ?]mask needs a broadcast over heads to a dynamic shape inside the attention add, which staticstablehlo.broadcast_in_dimcannot express (the first export failed there).FunctionGemmaExportHarness.exportPrefillWithPast(gguf, outDir, chunk = 64, …)→gemma-prefill-with-past.{mlir,safetensors}; spec overload; CLIGEMMA_GRAPH=prefill_with_past(GEMMA_CHUNK).FN_PREFILL_WITH_PAST,DEFAULT_CHUNK = 64,prefillWithPastArgs(spec)(first-use order: tokens, then per layer the cos/sin of its base on first use, K, V, and the type's mask on first use; select last — verified against the emitted signature: 44 args, 37 results for the 18-layer spec),prefillWithPastOutputs, manifest entries. Dump test.Validation (device: arm32 Android device, IREE 3.11.0, bf16 archive)
The 852-token tool-catalog prompt split as an 800-position cache (from
gemma_prefill_at) plus a 52-token chunk (C = 64, padded), select on row 51 — must reproduce the token the all-positions redecode graph gives at position 851 (48):Same check with
GEMMA_CHUNK=32(820 cached positions + a 32-token chunk): token 48 ✓ on all three targets, Mali 2.45 s, arm32 5.12 s — the chunk cost scales with C, so the chunk size is a deployment choice driven by the utterance-length distribution.With 0.26 s per decode token, a 15-token tool call with a warm catalog cache projects to ≈ 8.5 s (C = 64) or ≈ 6.4 s (C = 32) on the GPU.