Repository navigation
Description
Summary
gemma3, gemma4, and exaone_moe build their multi-token (l > 1) prefill attention mask from the cache's monotonic offset instead of live_len()/seq_len(). Under the server --max-kv-size path the global (full-attention) cache is trimmed, so the mask becomes wider than the live K/V, MLX broadcast_shapes throws, and because the op is a non-Result cxx function the throw becomes std::terminate and aborts the whole server. A remote client can crash all sessions with one long prompt plus a multi-token continuation chunk. This is the exact remote-DoS class fixed for the other dense Vec models in #419/#420 and #421/#422, which named cohere2/gemma3n/olmo3 and mistral4/nemotron_nas/qwen-vl but missed these three.
Root cause (static trace, airtight)
enforce_max_kv_size_for(src/server/batch/scheduler.rs:2082-2115) iterates EVERY cache in the sequence and trims any whoselive_len() > max_kv_sizeviaKVCache::trim_front, which advanceslive_startwhileoffsetkeeps growing (the RoPE invariant). After a trim,update_and_fetchreturns onlylive_len = offset - live_startkeys. The global (full-attention) Standard cache is the unbounded one this is designed to trim.- These three size their multi-token prefill mask from the monotonic
offset, notlive_len:- gemma3
src/models/gemma3.rs:766—let global_offset = caches[global_idx].as_interface().offset();thencreate_causal_mask(seq_len, global_offset)at :767 (and the sliding mask at :769 also usesoffset()). This is the seq_len > 1 batched prefill branch (no alternate live_len path). - gemma4
src/models/gemma4.rs—first_cache_offset(caches, "full_attention")(:3279) returnsCache::Standard(c).offset, used to size the global prefill mask at:2750,:2790,:2833,:3786. - exaone_moe
src/models/exaone_moe.rs:674-679—ga_offsetfrom theAnyKVCache::Standardcache's.offset(), used increate_causal_mask(seq_len, ga_offset)(and the slidingswa_offsetfrom the Rotating cache at :684).
- gemma3
- After the global Standard cache is trimmed,
offset > live_len, socreate_causal_mask(seq_len, offset)is wider than the live K/V (which islive_len), the attention score broadcast mismatches, MLX throwsstd::invalid_argument, and the non-Result cxx boundary turns it intostd::terminate.
Contrast the fixed sibling: src/models/mistral4.rs:648 uses create_causal_mask(l, live_len).
Why these were missed
cohere2/gemma3n/olmo3 use the _dense mask builder (create_sliding_window_prefill_mask_dense) that #413/#419 migrated to live_len. gemma3/gemma4 use the older non-dense create_sliding_window_prefill_mask + create_causal_mask with offset, and gemma3.rs/gemma4.rs appear in NONE of the live_len migration commits (#413/#418, #419/#420, #421/#422); their earlier gemma fixes (#405/#412/#415) addressed the sliding-window over-window shape, not the global-mask offset->live_len change. exaone_moe uses the default supports_batching (no override) so it is batched and trimmed, and reads AnyKVCache::Standard.offset().
Audit (the set is exhaustive)
AT-RISK (size mask from monotonic offset AND batch with a trimmable dense cache):
| Model | mask source | file:line |
|---|---|---|
| gemma3 | offset() |
src/models/gemma3.rs:766 (global), :769 (sliding) |
| gemma4 | offset via first_cache_offset |
src/models/gemma4.rs:2750/2790/2833/3786 |
| exaone_moe | offset() via AnyKVCache::Standard |
src/models/exaone_moe.rs:674-679 |
Confirmed SAFE:
- llama4, qwen3_5:
supports_batching() == truebut useModelOwnedSequenceState, whichget_caches_mutreturns empty for, soenforce_max_kv_size_fornever trims them (offset stays == live_len). - minicpm3: its
offsetlocal iscaches.first().seq_len()(== live_len), not the monotonic offset. - supports_batching() == false (never trimmed): gpt_oss, step3p5, ministral3, mellum, exaone4, nemotron_h, jamba, lfm2, plamo2, falcon_h1, granitemoehybrid, qwen3_next, kimi_linear, mamba, mamba2, rwkv7, recurrent_gemma, longcat_flash_ngram, nemotron_nas.
- Already fixed by fix(server): dense sliding-window models build prefill masks from monotonic offset, not live_len, under --max-kv-size #419-fix(models): size dense prefill masks from live_len for mistral4/nemotron_nas/qwen-vl under --max-kv-size (#421) #422: cohere2, gemma3n, olmo3, mistral4, qwen2_vl, qwen3_vl, qwen3_vl_moe.
TO VERIFY during implementation: gemma4_mtp_target (the MTP draft target uses create_causal_mask_with_left_padding; confirm its mask length is live_len-based or apply the same fix).
Fix plan
Mirror #421/#422 exactly: size the multi-token prefill masks from cache.live_len() / seq_len() (== offset - live_start) instead of offset in gemma3, gemma4, and exaone_moe, for BOTH the global causal mask and the sliding-window prefill mask. RoPE, position_ids, and any attention scale keep reading the monotonic offset (that is correct and must not change). Byte-identical behavior when untrimmed (live_start == 0, so live_len == offset).
Tests
Mirror the test approach used by #419-#422. At minimum, assert that the prefill mask key-length is derived from live_len()/seq_len() for each of the three models, and add (or extend) a regression that exercises a trimmed cache (live_len < offset) so the mask width equals the live K/V rather than the monotonic offset.
Acceptance criteria
- With
--max-kv-sizeset, a long prompt that trims the global cache followed by a multi-token continuation chunk no longer aborts the server for gemma3, gemma4, or exaone_moe. - The three models size their prefill masks from
live_len/seq_len; RoPE/position_ids still use the monotonic offset. gemma4_mtp_targetis confirmed safe or fixed.- No behavior change when untrimmed.
Validation note
The fix is device-independent shape logic; the primary gate is cargo test on the mask-sizing plus, ideally, a mlxcel serve --max-kv-size <small> run with a long prompt and a multi-token continuation (this dev host can build and run real models on the GB10 GPU, so the runtime repro is feasible).
Related
Same std::terminate crash class as the conv-layout fix (#428) and the FFI-fallibility follow-up (#427); making the mask/conv MLX ops Result-returning at the cxx boundary (#427, generalized) would convert this entire class from a process abort into a recoverable per-request error.
Activity
inureyes commentedon Jun 24, 2026
Correction after implementation investigation (PR #431): the live remote-DoS framing above is overstated for these three models. gemma3 and gemma4 use
ModelOwnedSequenceState(src/models/gemma3.rs:1152/1160, src/models/gemma4.rs:30), so the scheduler's trimmable pool is empty for them (get_caches_mutreturns nothing) andenforce_max_kv_size_fornever trims their caches, leavingoffset == live_len. exaone_moe's traitforwardrecreates offset-0AnyKVCacheper call (src/models/exaone_moe.rs:756), so a trimmed pool cache'slive_startnever reaches the mask. All three are therefore defensive/consistency-only: not live-crashable today. The offset-sized prefill mask is still an incorrect invariant (mask width should equal the K/V the cache returns), and PR #431 hardens it to mirror the #419-#422 precedent (which likewise included latent/consistency cases) and to guard against a future regression if these models move to the dense scheduler pool. Behavior is byte-identical on the untrimmed path. Downgrading priority:high to priority:medium accordingly.fix(models): size gemma3 stage prefill mask from live_len
docs(changelog): record gemma3/gemma4/exaone_moe prefill-mask consist…
fix: size gemma3/gemma4/exaone_moe prefill mask from live_len under -…
fix: size gemma3/gemma4/exaone_moe prefill mask from live_len under -…