Repository navigation
fix(models): size sliding masks from visible_len, not seq_len - #1776
Merged
Merged
Conversation
PR #1752 fixed Gemma 3's rotating-cache `live_len`, which read `seq_len()` (the physical buffer length) where the prefill mask needs `visible_len()`. The same expression survived in exaone_moe's `AnyKVCache::live_len` and in both Gemma 4 sliding lookups, each under a comment claiming `seq_len()` reports the live window. After a decode step or a `trim`, the buffer runs ahead of `offset`, so the mask came out wider than the K/V the next multi-token append returns. All three sites now read `visible_len()`. `Gemma4StageModel` drops its private copy of the lookup and calls the free `first_cache_live_len`, so one function and one guard cover both Gemma 4 call sites. The two existing guards now decode once before the append and assert `physical > offset` below the window clamp; both fail on the reverted one-liner. A new Gemma 4 test shows that `trim_mask_to_keys`, which keeps the trailing key columns, turns the too-wide mask into exactly the visible-sized band, so Gemma 4 attended with the right band before the fix as well. Refs #1764
3 of 6 tasks
Review of the visible_len fix found the comments slightly overstated what the append returns and named the wrong type. The count is what `update_and_fetch` keeps before its own `window - 1` clamp; the FP16 claim is now scoped to FP16, since a turbo-quantized sliding cache (`update_turbo4_concat`) still returns the whole physical buffer and relies on `trim_mask_to_keys`; a rollback reaches the stale-buffer state only for an unbuffered cache; and the `Used by:` line names `Gemma4TextModel::forward_with_speculative_sinks`. Comment-only; clippy and the two mask test modules pass. Refs #1764
2 of 3 tasks
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.
Summary
AnyKVCache::live_len(exaone_moe) and both Gemma 4 sliding lookups readRotatingKVCache::seq_len(), the physical buffer length, to size the sliding prefill mask. PR #1752 showed for Gemma 3 that this is the wrong accessor: after a decode step (update_in_placegrows the buffer by 256 slots) or atrim(truncated snapshot restore, rollback of an unbuffered cache), the buffer runs ahead ofoffset, whileupdate_concatreturns onlyvisible_len()prior keys for an FP16 rotating cache.visible_len(), and their comments no longer claimseq_len()reports the live window.Gemma4StageModelhad a private copy of the lookup. It now calls the freefirst_cache_live_len(whose parameter becomes&[Cache]), so one function and one guard cover both Gemma 4 call sites.sliding_cache_live_len_matches_returned_keysandfirst_cache_live_len_sliding_matches_returned_keysnow decode once before the multi-token append and assertphysical > offsetandoffset < window - 1. The second condition matters: onceoffset >= window - 1the clamp makes both accessors give the same mask, and the old fixtures' 4-token prefill plus one decode landed exactly there (offset 5 in a 6-slot window), so they could not fail.Gemma 4 outcome
The right-crop already produced the correct band.
pre_fix_sliding_mask_right_crop_is_the_visible_bandcrops theseq_len()-sized mask withtrim_mask_to_keysand compares it value for value with a band built from logical positions. It covers both ways of reachingphysical > offset(decode, trim) and appends inside the window and across it. The band depends only onk - q - kept_prior, andtrim_mask_to_keyskeeps the trailing columns, so the crop equals thevisible_len()mask, and decoded Gemma 4 output does not change on the default paths (confirmed on a real checkpoint below). As a mutation check, cropping from the left instead fails the test (decoded/3: pre-fix crop mask disagrees with the logical band at row 0, key 6: 0 vs -inf, a causal leak).The lookup also feeds
mask_stale_key_gap, which uses left-aligned columns. That runs only on batched MTP verify, where the rotating caches are buffered and both accessors returnidx. One consumer does change behavior: the caller-mask rebuild check in the forward (sliding_returned) now sees the narrower count, so an unbuffered FP16 cache whose buffer runs ahead ofoffsetkeeps a correctly sized caller mask (padding or a draft tree) instead of replacing it with plain causal masks. That is a fix, and nothing reaches it by default: Gemma 4 does not use padded prefill, cold batched prefill runs at offset 0, and tree verify runs on buffered caches unlessenable_speculative_bufferfails on a non-FP16 rotating mode. A turbo-quantized sliding cache (update_turbo4_concat) still returns the whole physical buffer, so after this change its mask is narrower than the keys andtrim_mask_to_keysrebuilds it, which gives the same band as before plus a warning per sliding layer; that append reading the full physical buffer predates this PR and is a follow-up.Revert arm
Both one-liners set back to
seq_len(), same two modules:Test plan
cargo test --profile test-fast --features metal,accelerate --lib -- exaone_moe gemma4 kv_snapshot rotating_live_len: 258 passed, 35 ignored, on the pushed commitcargo clippy --lib --tests --profile test-fast --features metal,accelerate -- -D warnings,cargo fmt --all -- --check-D warnings, fmt,make verify-versions/verify-kernel-dtype-keys/verify-llama-compatclean. The follow-up commit is comment-only (clippy and the two mask modules re-run clean).gemma-4-12b-it-4bitthroughmlxcel-server(temperature 0, seed 0, 128 tokens per turn, assistant text fed back), pre-fix bb146ef vs post-fix binaries, prompt cache on and off. The trace shows T2 and T3 adopting restored prefixes of 311 and 517 tokens, both under the 1023-token clamp, so the changed lookup ran. Pre-fix and post-fix are token-identical on all four turns with the cache on and with it off, and again in a variant whose T3 append runs from 517 to 1261 tokens across the 1024 window. Cache on vs off differ at T3 identically in both binaries (94 vs 121 generated tokens), which is the existing model-owned-cache class tracked in fix(server): the prompt cache donates and adopts KV-less shadow paged entries for model-owned families, so Gemma 3 and Llama 4 answer turn 2 without their history #1346, not this change. exaone_moe has no checkpoint in the store, so the unit guard is its only check.Closes #1764