Skip to content

fix: gemma3/gemma4/exaone_moe size prefill mask from monotonic offset, remote-crash under --max-kv-size (missed by #419-#422) #430

Closed

Description

@inureyes

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)

  1. enforce_max_kv_size_for (src/server/batch/scheduler.rs:2082-2115) iterates EVERY cache in the sequence and trims any whose live_len() > max_kv_size via KVCache::trim_front, which advances live_start while offset keeps growing (the RoPE invariant). After a trim, update_and_fetch returns only live_len = offset - live_start keys. The global (full-attention) Standard cache is the unbounded one this is designed to trim.
  2. These three size their multi-token prefill mask from the monotonic offset, not live_len:
    • gemma3 src/models/gemma3.rs:766 — let global_offset = caches[global_idx].as_interface().offset(); then create_causal_mask(seq_len, global_offset) at :767 (and the sliding mask at :769 also uses offset()). 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) returns Cache::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_offset from the AnyKVCache::Standard cache's .offset(), used in create_causal_mask(seq_len, ga_offset) (and the sliding swa_offset from the Rotating cache at :684).
  3. After the global Standard cache is trimmed, offset > live_len, so create_causal_mask(seq_len, offset) is wider than the live K/V (which is live_len), the attention score broadcast mismatches, MLX throws std::invalid_argument, and the non-Result cxx boundary turns it into std::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:

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-size set, 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_target is 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

  1. added
    type:bug
    status:ready
    priority:high
    area:models
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Jun 24, 2026
  2. inureyes commented on Jun 24, 2026

    @inureyes
    MemberAuthor

    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_mut returns nothing) and enforce_max_kv_size_for never trims their caches, leaving offset == live_len. exaone_moe's trait forward recreates offset-0 AnyKVCache per call (src/models/exaone_moe.rs:756), so a trimmed pool cache's live_start never 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.

  3. added
    priority:medium
    and removed
    priority:high
    on Jun 24, 2026
  4. added
    status:done
    and removed
    status:review
    on Jun 24, 2026
  5. self-assigned this
    on Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelspriority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions