Skip to content

fix: validate remaining sliding windows and test the XLA mask - #2800

Merged
inureyes merged 9 commits into
mainfrom
fix/2798-validate-sliding-windows-xla-mask
Nov 1, 2026
Merged

inureyes merged 9 commits into
mainfrom
fix/2798-validate-sliding-windows-xla-mask

Conversation

@inureyes

@inureyes inureyes commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Closes #2798

Two LOW residuals of #2792.

Sliding-window validation

After #2792, nine families validated their window. Every other family that turns a config sliding_window into a cache size or a mask still cast with as i32. What a bad value does depends on the consumer: zero panics the rotating-cache ring arithmetic, silently means full attention for the dense-cache and mask-only families, and masks every key to -1e30 in the OpenXLA band; a value above i32::MAX wraps to a narrower or negative window.

validate_sliding_window and cache_window moved unchanged into mlxcel_core::cache beside RotatingKVCache, and src/models/model_owned.rs re-exports cache_window, so the nine existing callers are untouched and there is no second copy. Each of the 19 families in the issue now calls the helper once at load with the sliding-layer predicate its layer builder already uses, stores the returned i32, and the casts at the listed sites are gone. DeepSeek V4, IQuest Loop Coder and the Muse assistant drafter replaced their zero-only checks. Nothing is clamped: a config with a sliding layer fails the load, and an all-global config still loads with any window. One test per family asserts both bounds.

Each predicate matches its consumer rather than its own reading of the field: Gemma 3n and OLMo 3 treat every kind that is not full_attention as sliding, a period of 0 makes every Gemma 3 embedding layer slide, and Baichuan matches by in-range layer index, so a list of out-of-range indices has no sliding layer. EmbeddingGemma 2 reads the raw JSON value so a positive integer above i64::MAX reaches the validator instead of falling back to the default; a JSON null or a non-integer still keeps the default as before.

Gemma 3 mask parity

The parity test only passed on CUDA with MLXCEL_XLA_PRECISION=f32, but f16 contractions are the device default. It now runs two arms: f32 at the strict 1e-3 bound, which catches a small masking difference, and the device default with 5e-3 * max(1, spread) per row plus a top-1 match.

Validation

fmt and clippy on mlxcel, mlxcel-core and mlxcel-xla (lib and tests, -D warnings) pass, as do the model suite (2529 passed), mlxcel-core drafter:: (233) and cache:: (588), mlxcel-xla emitter:: (199), the four ignored xla_prepared_prefill tests on local-task, and the two-arm run on MLXCEL_XLA_DEVICE=cuda. On CUDA the f32 arm is 4.8e-7 to 2.6e-6 and the production arm 1.0e-3 to 5.35e-3, with every row's top-1 matching.

Mutation checks: neutralizing the helper fails 18 model tests, both drafter tests and the emitter test; a flat 1e-3 production bound fails on CUDA (rows 5 to 11, 1.1e-3 to 5.4e-3); the pre-#2789 bidirectional mask fails the f32 arm (6.1e-2 to 4.4e-1) and the production arm (6.2e-2 to 4.4e-1, plus a differing top-1 at row 11).

Two deviations from the issue's command block: -p mlxcel-core --lib drafter:: cache:: cannot take two filters before --, so those ran as two commands, and this host builds against IREE_CUDA_HOME from scripts/iree/setup-cuda.sh --env rather than an IREE_DIST tree.

Both validators were `pub(crate)` in `src/models/model_owned.rs`, so the drafters in `mlxcel-core` and the OpenXLA emitter could not reach them and every new caller would have re-implemented the check. They now live as `pub fn` in `mlxcel_core::cache` beside `RotatingKVCache`, whose ring arithmetic divides by zero on a zero window and whose size is `i32`.

The model layer re-exports `cache_window`, so the nine existing callers are unchanged. The helper unit test moved with the function, and a second test covers the all-global config whose window stays unused.

Refs #2798
Families that turn a config window into a cache size or a mask still cast with `as i32` and never checked it. A zero silently means full attention for the dense-cache and mask-only families, panics the rotating-cache ring arithmetic, and masks every key to `-1e30` in the OpenXLA band; a value above `i32::MAX` wraps.

Each family now calls `cache_window` once at load with the sliding-layer predicate its layer builder already uses, stores the returned `i32`, and the casts are gone. Families that only rejected zero use the helper, which also rejects values above `i32::MAX`. One test per family asserts that both bounds fail the load with the family named in the message.

Refs #2798
The Muse Glimmer and Gemma 4 assistant drafters built their context windows from `config.sliding_window as i32` and only rejected zero, so a value above `i32::MAX` wrapped to a small or negative window and narrowed the draft context silently.

Both validate through `mlxcel_core::cache::cache_window` at load, store the result, and their config tests cover both bounds.

Refs #2798
The emitter's local-band predicate reads the window as `i32`. A zero masks every key to `-1e30`, so softmax spreads uniformly over the context and the logits are silently wrong; a wrapped value narrows the band.

Gemma 2, Gemma 3, OLMo3 and Cohere2 now validate the window where the config is parsed and return the helper's error, so `Config::from_json_str` and `Config::from_json` reject a config the graph cannot honor. A config that omits the field still parses.

Refs #2798
The parity test only passed on CUDA with `MLXCEL_XLA_PRECISION=f32`, but f16 contractions are the device default and what deployments run. It now runs two arms in one test: f32 at the strict 1e-3 absolute bound, which is what catches a small masking difference, and the device default with a bound scaled to each row's logit spread plus a top-1 match, which the #2789 mask bugs would still exceed by an order of magnitude.

Both arms print their per-row error and spread. Each arm loads its own engine because the precision is read at emit time and the vmfb cache is keyed per precision.

Refs #2798
The validation ran after the embedding and layer loads, so an unusable `attention_window_size` surfaced as a missing-weight error first and the window never reached the rotating cache. It now runs right after the block-type expansion, before any weight is read.

Refs #2798
The validator now reports the shared sliding-window error for `sliding_window` rather than the array-dimension one. The hostile-value loop keeps the other dimensions, and a new assertion pins the band radius to the helper's message.

Refs #2798
The failure messages carried a derived Debug dump whose fields the lint could not see as used, and the production arm did not show the bound it missed. Both arms now format the row, error, spread, bound and top-1 explicitly, which removes the dead-code warning and makes a failure readable.

Refs #2798
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:low Low priority area:models Model architectures, weights, loading, metadata area:core mlxcel-core: MLX FFI, primitives, KV cache, layers labels Oct 31, 2026
The window validation added for issue #2798 used predicates that disagreed with what the layer builders and mask paths actually do, so four configs still accepted a window the consumer could not take:

- Baichuan looks the layer index up in `sliding_window_layers`, so a list of out-of-range indices has no sliding layer. Such a config now loads with an unused window instead of failing.
- Gemma 3 embedding: `is_full_attention` treats a period of 0 as every layer sliding (`is_multiple_of(0)` is false), so the check is `pattern != 1`, not `pattern > 1`.
- Gemma 3n and OLMo 3 treat every kind that is not `full_attention` as sliding in their mask paths, so an unknown kind can no longer skip the check.

EmbeddingGemma 2 now reads the raw JSON value through `as_u64` first, so a positive integer above `i64::MAX` reaches the validator instead of silently falling back to the 512 default. A JSON null or a non-integer still keeps the default, as `i32_field` did before.

Tests cover the unknown-kind cases for Gemma 3n and OLMo 3, the zero period for Gemma 3 embedding, an out-of-range Baichuan layer list, and a 2^63 window for EmbeddingGemma 2.

Verification: cargo fmt; clippy -D warnings on mlxcel (cuda), mlxcel-core (cuda) and mlxcel-xla; models:: 2529 passed, drafter:: 233, cache:: 588, emitter:: 199.
@inureyes
inureyes merged commit 6f1c56a into main Nov 1, 2026
34 checks passed
@inureyes
inureyes deleted the fix/2798-validate-sliding-windows-xla-mask branch November 1, 2026 03:28
@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

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:models Model architectures, weights, loading, metadata priority:low Low priority type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Validate remaining sliding windows and test the XLA mask at f32 and f16

1 participant