Skip to content

fix(xla): update Gemma 3n emitter to the shared-layer query offset - #2513

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-2466-xla-gemma3n-offset
Oct 21, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-2466-xla-gemma3n-offset

Conversation

@inureyes

Copy link
Copy Markdown
Member

Closes #2466

Summary

The emitter 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. This PR ports exactly that to both graphs; the rope table shrinks from 2 * context_capacity to context_capacity rows because every position now stays below context_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.

## 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
@inureyes inureyes added status:done Completed area:models Model architectures, weights, loading, metadata priority:low Low priority type:bug Bug fixes, error corrections, or issue resolutions labels Oct 21, 2026
@inureyes
inureyes merged commit d013373 into main Oct 21, 2026
23 of 28 checks passed
@inureyes
inureyes deleted the fix/issue-2466-xla-gemma3n-offset branch October 30, 2026 13:35
@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:models Model architectures, weights, loading, metadata priority:low Low priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(xla): update Gemma 3n emitter to the shared-layer query offset

1 participant