Repository navigation
fix(xla): update Gemma 3n emitter to the shared-layer query offset - #2513
Merged
Merged
Conversation
## Summary The mlxcel-xla Gemma 3n emitter still built shared-KV-layer query positions the way the eager path did before #2457 (`positions + real_len` on the prefill graph, `position + 1` on the decode graph), so MLX-vs-XLA Gemma 3n parity diverged. The maintained MLX path reads `rope_offset` once per forward before any layer runs (mlx-lm's `cache[0].offset`), so a shared-KV layer rotates its queries with the same positions as its owner layer; the emitter now mirrors that on both graphs and the rope table shrinks from `2 * context_capacity` to `context_capacity` rows. - The `#[ignore]`d pinned E2B prefix-decode top1 (236798) predates #2457 and is refreshed to 854, re-captured on main at 551039e (MLX pin bff10220) with the mlx-vlm 0.7.8 CPU oracle agreeing on the same fixture; the full-prefill and greedy pins are unchanged and the comments record why. - `tests/xla_gemma3n_prefill.rs` fixes the stale comment about the extended rope table. ## Verification (GB10, --profile test-fast, MLX_ENABLE_TF32=0) Fail-before proof on the pre-change emitter: token-prefill logits max_abs 1.9375 against the 1.5 envelope, and the one-row prefix-decode carrier top1 xla=236798 vs mlx=854, exactly the pre-#2457 MLX value the stale offset reproduces. After the fix the same stage comparison resolves the prefill divergence the issue targets: token-prefill logits max_abs 0.8203 (within 1.5) with matching top1, the shared-layer stages improve (layer_last_active_plane max_abs 9 to 3), and the greedy trajectory matches ([236798, 236789] both sides). Remaining pinned-envelope failures are pre-existing drift, not introduced here: scaled_embeddings (0.03125 vs envelope 0), layer0_ple_injected (2 vs 0.25) and the layer-mid planes (15 vs 3) measure byte-identical on the pre-change emitter, and the prefix-decode comparison stays outside the envelope both before (2.2617, cosine 0.9432) and after (2.7266, cosine 0.8710); these stages predate both #2457 and this PR (the pin was last captured 2026-07-24 and the MLX pin has since moved), and per the #932 decision recorded drift is not gating. cargo test --profile test-fast --features cuda,xla-diagnostics -p mlxcel --lib gemma3n -- --test-threads=1: 40 passed, 0 failed. cargo test --profile test-fast --features cuda,xla-diagnostics -p mlxcel --test xla_gemma3n_prefill -- --test-threads=1 --ignored (MLXCEL_XLA_DEVICE=cuda): 1 passed. cargo fmt --all -- --check: the four changed files are clean (main carries pre-existing fmt diffs in src/models/inkling/mlp.rs and src/models/switch_layers.rs, untouched here). Scoped clippy -p mlxcel -p mlxcel-xla --lib -D warnings: green. Closes #2466
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2466
Summary
The emitter built shared-KV-layer query positions the way the eager path did before #2457 (
positions + real_lenon the prefill graph,position + 1on the decode graph), so MLX-vs-XLA Gemma 3n parity diverged. The maintained MLX path readsrope_offsetonce per forward before any layer runs (mlx-lm'scache[0].offset), so a shared-KV layer rotates its queries with the same positions as its owner layer. This PR ports exactly that to both graphs; the rope table shrinks from2 * context_capacitytocontext_capacityrows because every position now stays belowcontext_capacity.The
#[ignore]d pinned E2B prefix-decode top1 (236798) predates #2457 and is refreshed to 854, re-captured on main at 551039e (MLX pin bff10220) with the mlx-vlm 0.7.8 CPU oracle agreeing on the same fixture; the full-prefill and greedy pins are unchanged, and the test comments record why.Fail-before proof (GB10, pre-change emitter, --profile test-fast, MLX_ENABLE_TF32=0)
Measured against the current MLX pin: token-prefill logits max_abs 1.9375 against the 1.5 envelope, and the one-row prefix-decode carrier top1 xla=236798 vs mlx=854, exactly the pre-#2457 MLX value the stale offset reproduces.
Post-fix
The same stage-level comparison resolves the prefill divergence the issue targets: token-prefill logits max_abs 0.8203 (within 1.5) with matching top1, the shared-layer stages improve (layer_last_active_plane max_abs 9 to 3), and the greedy trajectory matches ([236798, 236789] both sides).
Remaining pinned-envelope failures are pre-existing drift, not introduced here: scaled_embeddings (0.03125 vs envelope 0), layer0_ple_injected (2 vs 0.25) and the layer-mid planes (15 vs 3) measure byte-identical on the pre-change emitter, and the prefix-decode comparison stays outside the envelope both before (2.2617, cosine 0.9432) and after (2.7266, cosine 0.8710). These stages predate both #2457 and this PR (the pin was last captured 2026-07-24 and the MLX pin has since moved); per the #932 decision recorded drift is not gating.
Validation
cargo test --profile test-fast --features cuda,xla-diagnostics -p mlxcel --lib gemma3n -- --test-threads=1: 40 passed, 0 failed. cargo test --profile test-fast --features cuda,xla-diagnostics -p mlxcel --test xla_gemma3n_prefill -- --test-threads=1 --ignored (MLXCEL_XLA_DEVICE=cuda): 1 passed. cargo fmt --all -- --check: the four changed files are clean (main carries pre-existing fmt diffs in src/models/inkling/mlp.rs and src/models/switch_layers.rs, untouched here). Scoped clippy -p mlxcel -p mlxcel-xla --lib --no-deps -D warnings: green.