Repository navigation
Description
Problem / Background
PR #405 (fix for #401) added a sliding_prefill_mask(seq_len, sliding_offset, window) helper to both src/models/gemma3.rs and src/models/gemma4.rs. The helper wraps the new create_causal_mask_with_window_full (in src/lib/mlxcel-core/src/utils.rs): it selects the uncapped full windowed mask in the over-window prefill case and otherwise falls through to the existing clamped create_causal_mask_with_window path (with effective_offset = sliding_offset.min((window - seq_len).max(0))).
Both the pr-reviewer and pr-security-checker flagged the duplication as a LOW, non-blocking note: the helper should live in a single shared location so the gating logic is defined once. This matters because the gate encodes a subtle correctness invariant — the sliding_offset == 0 condition that avoids a shape mismatch with the trimmed rotating cache — and two independent copies will drift.
They already have. The two copies are not byte-identical today:
src/models/gemma3.rs(lines 72-79):fn sliding_prefill_mask(seq_len: i32, sliding_offset: i32, window: i32) -> UniquePtr<MlxArray> { if seq_len > window && sliding_offset == 0 { create_causal_mask_with_window_full(seq_len, sliding_offset, Some(window)) } else { let effective_offset = sliding_offset.min((window - seq_len).max(0)); create_causal_mask_with_window(seq_len, effective_offset, Some(window)) } }
src/models/gemma4.rs(lines 2088-2095):fn sliding_prefill_mask(l: i32, sliding_offset: i32, window: i32) -> UniquePtr<MlxArray> { if l > window { create_causal_mask_with_window_full(l, sliding_offset, Some(window)) } else { let sliding_effective_offset = sliding_offset.min((window - l).max(0)); create_causal_mask_with_window(l, sliding_effective_offset, Some(window)) } }
The gemma4 copy is missing the && sliding_offset == 0 guard that gemma3 carries. That is exactly the drift this consolidation is meant to prevent, and it means the refactor cannot be a pure mechanical move: it has to reconcile the two gates into one. The gemma3 form carries the documented correctness invariant (see its doc comment and the existing tests sliding_prefill_mask_fresh_over_window_* / sliding_prefill_mask_nonfresh_over_window_matches_trimmed_cache), so the consolidated helper should adopt the guarded gate unless gemma4 deliberately needs the unguarded form — in which case the divergence must be made explicit rather than implied by a copy.
Proposed Solution
Extract a single sliding_prefill_mask helper to one shared location (for example mlxcel-core::utils, next to create_causal_mask_with_window_full, or a shared models helper module) and have both gemma3.rs and gemma4.rs call it.
- Reconcile the gate into one definition. Prefer gemma3's
seq_len > window && sliding_offset == 0form (the one with the documented trimmed-rotating-cache invariant) and confirm gemma4's existing call sites (forward_with_speculative_sinks, the pipeline-stageexecute_hidden) remain correct under it. If gemma4 genuinely requires the unguarded gate, document why and keep that distinction explicit instead of relying on two copies. - Add the
// Used by: Gemma 3, Gemma 4comment above the shared helper per the shared-function convention indocs/code-guidelines.md, so a future sliding-window model that adopts it reuses the single helper. - Remove the two per-model copies.
Acceptance Criteria
- A single shared
sliding_prefill_mask(or equivalently named) helper exists in one location. - Both
gemma3.rsandgemma4.rscall the shared helper; the per-model copies are deleted. - The two gates are reconciled into one definition, with the chosen behavior justified (the
sliding_offset == 0guard kept or its absence documented). - The shared helper carries a
// Used by: Gemma 3, Gemma 4comment perdocs/code-guidelines.md. - Existing gemma3 and gemma4 sliding-window tests still pass unchanged (the over-window / within-window / trimmed-cache cases).
- No observable behavior change for the cases the existing tests cover.
Technical Considerations
create_causal_mask_with_window_fullandcreate_causal_mask_with_windowboth live insrc/lib/mlxcel-core/src/utils.rs, so co-locating the wrapper there keeps the windowed-mask family together.- Validate against a real Gemma 3 and Gemma 4 checkpoint with a prompt longer than the sliding window (the regime that originally degenerated to
<pad>in fix(models): gemma4 sliding-window single-pass prefill degenerates to <pad> beyond the window #401), not just unit tests.
Activity
inureyes commentedon Jun 23, 2026
Clarification on the gate divergence flagged above: the difference is deliberate and reviewer-confirmed, not accidental drift, so a consolidation must preserve it rather than "correct" gemma4's gate.
l > window, no offset condition) is safe because gemma4 setsno_chunked_prefill: its prefill is single-pass (sliding_offset == 0) and decode isl == 1, so thel > window && sliding_offset > 0case is never reached. gemma4 also routes throughtrim_mask_to_keys, which re-crops any over-length mask to the key axis (so no shape mismatch even if it were reached).&& sliding_offset == 0guard because it has no trim step (it passes the mask straight toattention_from_ptr) and can legitimately seesliding_offset > 0withl > window(server chunked prefill with chunk > window, or multi-turn cache reuse). That was the HIGH bug fixed in PR fix(models): correct gemma sliding-window prefill beyond the window #405's review (commit4cb2c18a1).So a single shared helper should either (a) keep the gemma3 guarded gate AND have gemma4 keep its
trim_mask_to_keysstep (the guard is then a harmless no-op for gemma4's actual usage), or (b) make the divergence explicit via a parameter (e.g.caller_trims: bool). Do NOT silently add the guard to gemma4 and call it "no behavior change" without confirming gemma4's call sites, and do NOT drop gemma3's guard. The deeper "what if gemma4 ever enables chunked prefill" question is tracked separately in #408 (sliding-window prefill hardening); coordinate with it.