Repository navigation
Description
Problem / Background
The dense-KVCache sliding-window models (cohere2, gemma3n, olmo3) build their multi-token prefill attention masks (both the global create_causal_mask(l, ga_offset) and the sliding create_sliding_window_prefill_mask_dense(l, sliding_offset, window)) from the cache's MONOTONIC offset. PR #418 (issue #413) made the dense sliding mask a full [l, l + sliding_offset] mask over all retained keys (create_sliding_window_prefill_mask_dense in src/lib/mlxcel-core/src/utils.rs:710), which is correct for the normal dense path where the cache returns all offset keys.
Mask-build sites that read the monotonic offset directly:
- cohere2:
src/models/cohere2.rs:426-438and:488-500(ga_offset/swa_offset = caches[..].offset). - gemma3n:
src/models/gemma3n.rs:1199-1214and:1376-1391(global_offset/sliding_offset = caches[..].offset). - olmo3:
src/models/olmo3.rs:463-471(ga_offset/swa_offset = caches[..].offset).
The bug
Under the server --max-kv-size path, enforce_max_kv_size_for (src/server/batch/scheduler.rs:2082, trim at :2113) calls KVCache::trim_front (src/lib/mlxcel-core/src/cache.rs:2408), which advances live_start while offset keeps growing monotonically (to preserve RoPE relative positions). The dense KVCache::update_and_fetch Fp16/Int8 path (cache.rs:2515, live_len = buffer_idx() at :2535, sliced at :2629-2638) then returns only the live window of live_len = offset - live_start keys, NOT offset keys. On a later multi-token continuation chunk (l > 1), a mask sized from the monotonic offset is WIDER than the live K/V the cache actually returns, so the mask key axis and the K/V key axis disagree.
This is PREEXISTING and not introduced by #418: the untouched global-attention path in the same models builds create_causal_mask(l, ga_offset) from the same monotonic offset, and the global layer carries window_size == 0 (cohere2 :283-287), so the attention layer's "slice K/V to the mask key axis" branch (cohere2.rs:193, gemma3n.rs:529, gated on self.window_size > 0) never fires for the global layer. It thus already mismatches under the exact same config (--max-kv-size + a dense sliding-window model + a multi-token continuation chunk). So the end-to-end config is already broken on the global path regardless of #418; the sliding path now carries the same offset-vs-live_len assumption.
Note on gemma3n: its attention layer already reads cache.live_len() to slice the KV-shared K/V buffer (gemma3n.rs:482-489) and its comment explicitly anticipates the --max-kv-size trim, but the model-level mask at gemma3n.rs:1199-1200/1376-1377 is still built from the monotonic cache.offset. So even gemma3n, which is partly trim-aware on the K/V side, still builds a mask whose key axis can exceed the live K/V after a trim.
Proposed direction
Align dense-cache prefill mask construction (both global and sliding) with cache.live_len() (the actual returned key count) rather than the monotonic offset, for the --max-kv-size / trimmed path. Because the masks are byte-identical when live_start == 0 (no trim has happened — live_len == offset), this can be an isolated, behavior-preserving change for the untrimmed common case. A sliding-only partial fix is insufficient: the global path must be aligned too for the config to work end-to-end.
Acceptance criteria
- A multi-token continuation chunk on a dense sliding-window model (cohere2 / gemma3n / olmo3) under
--max-kv-size(after at least onetrim_front) produces coherent output, with the mask key axis matching the live K/V key axis on both the global and sliding layers. - The untrimmed path (
live_start == 0) remains byte-identical (greedy output unchanged). - A regression test exercises the trimmed (
live_start > 0) continuation case.
Technical Considerations
--max-kv-sizeis an opt-in operator flag (not a default), and the mismatch additionally requires a dense sliding-window family plus a multi-token continuation chunk after at least one trim, so the trigger is config-specific rather than something every server hits.- Turbo* cache modes are out of scope here:
trim_frontis a no-op for them (cache.rs:2433) and the scheduler already warns that--max-kv-sizeis not honored on the V side for those modes.
References
- Surfaced in the PR fix(models): dense-cache sliding-window prefill uses full mask over all retained keys (#413) #418 security/performance review (follow-up to fix(models): dense-cache sliding-window models still degenerate on rolled-over over-window prefill (offset>0 && seq>window) #413). PR fix(models): dense-cache sliding-window prefill uses full mask over all retained keys (#413) #418: fix(models): dense-cache sliding-window prefill uses full mask over all retained keys (#413) #418
Activity
fix(models): size dense prefill masks from live_len under trim (#419) (…
fix(models): size dense prefill masks from live_len for mistral4/nemo…
fix(models): size dense prefill masks from live_len for mistral4/nemo…
fix: size gemma3/gemma4/exaone_moe prefill mask from live_len under -…
fix(models): size dense prefill masks from live_len under trim (lablu…
fix(models): size dense prefill masks from live_len for mistral4/nemo…
fix: size gemma3/gemma4/exaone_moe prefill mask from live_len under -…