Skip to content

fix(models): size sliding masks from visible_len, not seq_len - #1776

Merged
inureyes merged 3 commits into
mainfrom
fix/issue-1764-rotating-live-len
Sep 10, 2026
Merged

inureyes merged 3 commits into
mainfrom
fix/issue-1764-rotating-live-len

Conversation

@inureyes

@inureyes inureyes commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Summary

AnyKVCache::live_len (exaone_moe) and both Gemma 4 sliding lookups read RotatingKVCache::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_place grows the buffer by 256 slots) or a trim (truncated snapshot restore, rollback of an unbuffered cache), the buffer runs ahead of offset, while update_concat returns only visible_len() prior keys for an FP16 rotating cache.

  • All three sites now read visible_len(), and their comments no longer claim seq_len() reports the live window.
  • Gemma4StageModel had a private copy of the lookup. It now calls the free first_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_keys and first_cache_live_len_sliding_matches_returned_keys now decode once before the multi-token append and assert physical > offset and offset < window - 1. The second condition matters: once offset >= window - 1 the 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_band crops the seq_len()-sized mask with trim_mask_to_keys and compares it value for value with a band built from logical positions. It covers both ways of reaching physical > offset (decode, trim) and appends inside the window and across it. The band depends only on k - q - kept_prior, and trim_mask_to_keys keeps the trailing columns, so the crop equals the visible_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 return idx. 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 of offset keeps 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 unless enable_speculative_buffer fails 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 and trim_mask_to_keys rebuilds 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 models::exaone_moe::exaone_moe_mask_tests::sliding_cache_live_len_matches_returned_keys ... FAILED
test models::gemma4::gemma4_unified_mask_tests::first_cache_live_len_sliding_matches_returned_keys ... FAILED
assertion `left == right` failed: AnyKVCache::live_len() = 262 sized a sliding mask with 265 key columns, but the append returned 10 keys
assertion `left == right` failed: first_cache_live_len = 262 sized a sliding mask with 265 key columns, but the append returned 10 keys
test result: FAILED. 13 passed; 2 failed

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 commit
  • cargo clippy --lib --tests --profile test-fast --features metal,accelerate -- -D warnings, cargo fmt --all -- --check
  • Workspace gate on 824b01e: 123 binaries, 11016 passed, 0 failed, 359 ignored; workspace clippy -D warnings, fmt, make verify-versions / verify-kernel-dtype-keys / verify-llama-compat clean. The follow-up commit is comment-only (clippy and the two mask modules re-run clean).
  • Multi-turn greedy run on gemma-4-12b-it-4bit through mlxcel-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

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
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) arch:moe Sparse mixture-of-experts decoder status:review Under review labels Sep 10, 2026
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
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 10, 2026
@inureyes
inureyes merged commit 9355d9e into main Sep 10, 2026
13 checks passed
@inureyes
inureyes deleted the fix/issue-1764-rotating-live-len branch September 10, 2026 20:34
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:moe Sparse mixture-of-experts decoder area:inference Generation, sampling, decoding (incl. speculative, DRY) area:models Model architectures, weights, loading, metadata priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(exaone_moe): AnyKVCache::live_len reports seq_len() for a rotating cache, the same expression #1752 disproved

1 participant