Skip to content

fix(xla): compile the Gemma3n decode without aggressive fusion - #3375

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3363-gemma3n-cuda-decode-fusion
Nov 24, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3363-gemma3n-cuda-decode-fusion

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Gemma3n could not generate on MLXCEL_XLA_DEVICE=cuda because #3048 added --iree-dispatch-creation-enable-aggressive-fusion to every decode compile. In the Gemma3n decode graph the AltUp magnitude and a later RMSNorm square the same activation, IREE shares that x * x, and aggressive fusion pulls it into the RMSNorm reduction dispatch (main_dispatch_10_reduction_2048_f32) as a second, unreduced result that the CUDA VectorDistribute pipeline cannot distribute ('func.func' op failed to distribute).

  • decode_compile_flags adds aggressive fusion only for RuntimeConfig::Dense, the graph perf(xla): hold the decode KV cache in a module global to remove the per-step 32 MB copies #3043 measured it on. Gemma3n keeps the compiler defaults on every device. The doc comment names the failing dispatch and the IREE version so the exemption can be revisited on an upgrade.
  • The shared bundle flags move into bundle_compile_flags unchanged, so the regression test compiles with exactly the production flag set. The Dense decode flag list is byte-identical, so existing cached Dense decode artifacts still hit.

Test plan

  • gemma3n_cuda_decode_compiles: emits the production gemma-3n-e2b decode and compiles it for CUDA with the production flags. It is skipped without the checkpoint, an NVIDIA driver, or a CUDA-capable iree-compile. With the old flags it fails with the reported error on the same MLIR hash as the sweep's cached graph.
  • Unit tests: Dense decode flags include aggressive fusion (cuda, local-task), Gemma3n decode flags do not, prefill flags never do.
  • GB10 real models, eager (MLXCEL_SDPA_DETERMINISTIC=1) vs MLXCEL_BACKEND=xla MLXCEL_XLA_DEVICE=cuda, --temp 0 -n 16, token IDs identical: gemma-3n-e2b text ("Paris"), gemma-3n-e4b text, gemma-3n-e4b audio transcript ("Chapter 1. Mrs. Rachel Lind is Surprised. ...", 16/16 tokens).
  • Dense: qwen2.5-0.5b-bf16 on cuda matches eager, and the run reused the decode vmfb compiled before the change (no new cache entry), so its flags did not change.
  • cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets, cargo clippy -p mlxcel-xla --features iree --lib --tests -D warnings, cargo fmt --check.

Closes #3363

Since #3048 every decode compile passed --iree-dispatch-creation-enable-aggressive-fusion. On the Gemma3n decode graph the AltUp magnitude and a later RMSNorm square the same activation, IREE shares that x * x, and aggressive fusion pulls it into the RMSNorm reduction dispatch as a second, unreduced result. The CUDA VectorDistribute pipeline cannot distribute that store, so iree-compile fails with "'func.func' op failed to distribute" (main_dispatch_10_reduction_2048_f32) and Gemma3n text and audio generation fail on the CUDA device.

The bundle flag assembly moves into bundle_compile_flags, and decode_compile_flags adds aggressive fusion only for the Dense decode graph it was measured on. Gemma3n stays on the compiler defaults on every device. The Dense flag set is unchanged, so its cached decode artifacts are reused.

Tests: unit tests pin the flag split, and gemma3n_cuda_decode_compiles compiles the production e2b decode for CUDA with the production flags (skipped without the checkpoint, an NVIDIA driver, or a CUDA-capable iree-compile); it fails on the old flags with the reported error. On GB10, gemma-3n e2b and e4b text and e4b audio are token-exact with eager at --temp 0 -n 16.

Closes #3363
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 24, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Reviewed against #3363 (body and comments). No CRITICAL, HIGH, or MEDIUM findings; no code changes pushed.

  • Single call site: every decode compile goes through compile_bundle, which now builds flags via bundle_compile_flags and decode_flags via decode_compile_flags. Prefill and embedding variants still use flags without fusion. The other target_flags users (vision, audio, ssm probes) never carried the fusion flag, and the one in tuning.rs is a Dense-only ROCm matcher test.
  • Dense flags are unchanged in content and order (target, cuda-target, ROCm spec, then fusion), so the compile_one cache key (hash of MLIR plus flags) and the cached Dense vmfb stay valid.
  • RuntimeConfig has only Dense and Gemma3n. The match is exhaustive with no wildcard, so a new variant forces a deliberate choice.
  • The all-device Gemma3n exemption is reasonable: the flag was only measured on Dense, and the comment names the failing dispatch and the revisit test.
  • Tests: cargo test --release -p mlxcel-xla --lib --features iree decode_flags_tests passes 3/3 on the GB10 CUDA box, including the real Gemma3n CUDA decode compile. The compile test skips with a message when the checkpoint, NVIDIA driver, or CUDA-capable iree-compile is missing, so CI without a GPU is fine.
  • Security and performance: no new untrusted input, one small Vec clone at compile time only.

LOW, not fixed: the compile test uses a pid-named temp dir and assumes a models link under the repo, with GEMMA3N_MODEL_DIR as override.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 24, 2026
@inureyes
inureyes marked this pull request as ready for review November 24, 2026 02:30
@inureyes
inureyes merged commit 088ff24 into main Nov 24, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3363-gemma3n-cuda-decode-fusion branch November 24, 2026 02:35
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High 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: compile the Gemma3n decode graph for CUDA

1 participant