Skip to content

fix(cuda): drop the broken RMSNorm overlay, restore DeepSeek-V2 graphs - #1037

Merged
inureyes merged 1 commit into
mainfrom
fix/issue-831-cuda-graph-decode
Aug 5, 2026
Merged

inureyes merged 1 commit into
mainfrom
fix/issue-831-cuda-graph-decode

Conversation

@inureyes

@inureyes inureyes commented Aug 5, 2026

Copy link
Copy Markdown
Member

Summary

Root-causes the DeepSeek-V2 "CUDA-graph-captured incremental decode collapse" from #824/#829/#831: it was not a graph-capture hazard but mlxcel's own RMSNorm overlay (patches/mlx/backend/cuda/rms_norm.cu, upstream pre-#3792), whose two-stage reduction reads past its shared scratch on the exact kv_a_layernorm axis (kv_lora_rank == 512 at bf16). Deletes the overlay, removes DeepSeek-V2 and DeepSeek-VL2 from maybe_disable_cuda_graphs_for_model so they run with CUDA graphs again, and pins the band with a numeric regression test. Gemma 4 stays on the lever: its norm axes never touch the broken dispatch geometry, so #688 is a separate, still-open bug, and #831's premise that the two families shared one hazard class is wrong.

The decisive experiment

2x2 matrix on GB10 at 13741ae2: deepseek-v2-lite-4bit, greedy --temp 0, 48 tokens, fresh process per rep, 6 reps per cell per prompt, two prompts (1-token "Hi", ~90-token summarization). "Identical" is by content hash of the generated text.

Cell Kernel MLX_USE_CUDA_GRAPHS Short prompt Long prompt
A overlay 1700b39 0 6/6 coherent, identical 6/6 coherent, identical
B overlay 1700b39 1 4/6 coherent, all four token-divergent; 2/6 collapse into repeated ! 0/6: every rep collapses after one generated token
C pinned b7c3dd6d 0 6/6 coherent, identical 6/6 coherent, identical
D pinned b7c3dd6d 1 6/6 coherent, identical, byte-identical to C 6/6 coherent, identical, byte-identical to C

Cells C and D being byte-identical means graph capture has no observable effect on this family once the kernel is correct. The overlay's single-row failure mode (normalizer depends on residual shared memory) explains every recorded symptom: greedy flakiness, coherent-prefix-then-collapse, prompt-length dependence, and MLXCEL_FORCE_SYNC=1 not helping.

The cell C/D binary was proven to contain the pinned kernel (the configure_file(COPYONLY) overlay mechanism makes a silent no-op rebuild possible): post-build source diff against b7c3dd6d, rms_norm.cu.o recompile mtime, and cuobjdump/strings mangling showing 128 rms_norm_small instantiations with #3850's bool ALIGNED parameter (Lb0E/Lb1E) and zero of the overlay's four-int form.

What changed

  • src/lib/mlx-cpp/patches/mlx/backend/cuda/rms_norm.cu: deleted. The pinned kernel is correct on the affected axis and carries the #3850 forward speedup.
  • src/loading/mod.rs: ModelType::DeepSeekV2 | ModelType::DeepSeekVL2 removed from the hazard match; function docs rewritten to record the corrected attribution. Gemma 4 arms unchanged.
  • src/server/startup.rs: main-thread hoist comment updated to the Gemma-4-only story.
  • src/lib/mlxcel-core/src/rms_norm_small_axis_tests.rs (new, registered in lib.rs): pins fast::rms_norm against a float64 host reference across the broken band, 16-bit (256, 512] and f32 (128, 256], rows 8/3/2/1. Verified non-vacuous: with the overlay kernel linked back in, both tests fail at ~3e-1 worst-row relative error; on the pin they pass at rounding scale.
  • docs/upstream/mlx-cuda-rmsnorm-small-axis-regression.md: resolution section with the matrix, the binary proof, the non-reproduction of fix(cuda): restore DeepSeek-V2 output by reverting the regressed RMSNorm kernel #829's 0/4 baseline, and the local checkpoints that sat in the band (five MLA models at kv_lora_rank == 512 plus falcon-h1-tiny whose hidden_size itself is 512).

Test plan

  • cargo fmt --all -- --check clean
  • cargo clippy --release --features cuda -p mlxcel-core --lib --tests and -p mlxcel --bins: no lints
  • cargo test --release --features cuda -p mlxcel-core --lib rms_norm_small_axis: 2/2 (and 0/2 against the overlay kernel, restored afterwards)
  • cargo test --release --features cuda -p mlxcel-core --lib fused_norm_parity: 8/8
  • cargo test --release --features cuda --test deepseek_v2_greedy_parity: 2/2, token-exact vs mlx-lm 0.31.3 with graphs now on by default
  • Smoke: deepseek-v2-lite-4bit coherent with no env override (no disable log), deepseek-vl2-small-4bit coherent, gemma-4-e2b-it-4bit still logs the bug(gemma4/cuda): decode collapses to <pad> after 1-3 tokens (single-query maskless attention + quantized GEMV); mlx_lm decodes coherently #688 graph disable
  • 200-token decode: graphs-on 85.0-93.3 tok/s vs graphs-off 86.5-88.1 (n=3 each; GB10 single-run decode is bimodal, coherence is the gate)

Closes #831

DeepSeek-V2's "graph-capture" decode collapse (#824, #829, #831) was never a graph hazard: it was mlxcel's own RMSNorm overlay at patches/mlx/backend/cuda/rms_norm.cu, the pre-#3792 upstream kernel, which in the dispatch band (256, 512] at 16-bit launches 128 threads against shared scratch sized for 64. The second reduction stage reads past the scratch: with multiple rows it folds the sibling row's partials into the normalizer, and at single-row decode it reads whatever the previous kernel left in shared memory. kv_a_layernorm normalizes over kv_lora_rank == 512 at bf16, 27 times per deepseek-v2-lite forward, and CUDA graph capture changes which kernels precede it, which is why toggling capture flipped decode between coherent and garbage.

Evidence: a 2x2 matrix (overlay present/removed x MLX_USE_CUDA_GRAPHS 0/1) on GB10 at 13741ae, deepseek-v2-lite-4bit greedy, six reps per cell per prompt, fresh process per rep, short and long prompts. Overlay with graphs on reproduces the recorded flakiness exactly (short prompt 4/6 coherent but token-divergent, 2/6 collapse into repeated "!"; long prompt 0/6). The pinned-kernel build is deterministic, coherent, and byte-identical with graphs on versus off (12/12 each). The pinned-kernel binary was verified by symbol mangling: 128 rms_norm_small instantiations carrying #3850's bool ALIGNED template parameter, zero of the overlay's four-integer form.

Changes: delete the overlay (the pinned b7c3dd6d kernel is correct on the axis and carries the #3850 forward speedup); remove DeepSeekV2 and DeepSeekVL2 from maybe_disable_cuda_graphs_for_model so the family runs with CUDA graphs again, keeping Gemma 4 on the lever since its norm axes (head_dim 256, hidden 2816/3840/5376) never select the broken dispatch geometry and #688 remains a separate bug; add rms_norm_small_axis_tests pinning fast::rms_norm across the band against a float64 host reference, verified non-vacuous by linking the overlay kernel back in (both tests then fail at ~3e-1 worst-row error) and re-verifying green on the pin; record the resolution in docs/upstream/mlx-cuda-rmsnorm-small-axis-regression.md.

Validation: cargo fmt --all clean; scoped clippy clean (mlxcel-core lib+tests, mlxcel bins); cargo test --release --features cuda: rms_norm_small_axis 2/2, fused_norm_parity 8/8, deepseek_v2_greedy_parity 2/2 token-exact against the mlx-lm 0.31.3 reference with graphs now on by default; end-to-end smoke coherent on deepseek-v2-lite-4bit and deepseek-vl2-small-4bit, gemma-4-e2b still logs the graph disable. 200-token decode throughput on the new default: 85.0-93.3 tok/s graphs-on vs 86.5-88.1 graphs-off (n=3 each; GB10 single-run decode is bimodal, coherence is the gate).

Refs #824, #829, #830, #688. Fixes the DeepSeek-V2 half of #831.
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review status:done Completed and removed status:review Under review labels Aug 5, 2026
@inureyes
inureyes merged commit f1b9499 into main Aug 5, 2026
4 checks passed
@inureyes
inureyes deleted the fix/issue-831-cuda-graph-decode branch August 5, 2026 06:32
@inureyes inureyes self-assigned this Aug 31, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
lablup#1037)

## Summary

Root-causes the DeepSeek-V2 "CUDA-graph-captured incremental decode collapse" from lablup#824/lablup#829/lablup#831: it was not a graph-capture hazard but mlxcel's own RMSNorm overlay (`patches/mlx/backend/cuda/rms_norm.cu`, upstream pre-#3792), whose two-stage reduction reads past its shared scratch on the exact `kv_a_layernorm` axis (`kv_lora_rank == 512` at bf16). Deletes the overlay, removes DeepSeek-V2 and DeepSeek-VL2 from `maybe_disable_cuda_graphs_for_model` so they run with CUDA graphs again, and pins the band with a numeric regression test. Gemma 4 stays on the lever: its norm axes never touch the broken dispatch geometry, so lablup#688 is a separate, still-open bug, and lablup#831's premise that the two families shared one hazard class is wrong.

## The decisive experiment

2x2 matrix on GB10 at `13741ae2`: deepseek-v2-lite-4bit, greedy `--temp 0`, 48 tokens, fresh process per rep, 6 reps per cell per prompt, two prompts (1-token "Hi", ~90-token summarization). "Identical" is by content hash of the generated text.

| Cell | Kernel | `MLX_USE_CUDA_GRAPHS` | Short prompt | Long prompt |
|---|---|---|---|---|
| A | overlay `1700b39` | 0 | 6/6 coherent, identical | 6/6 coherent, identical |
| B | overlay `1700b39` | 1 | 4/6 coherent, all four token-divergent; 2/6 collapse into repeated `!` | 0/6: every rep collapses after one generated token |
| C | pinned `b7c3dd6d` | 0 | 6/6 coherent, identical | 6/6 coherent, identical |
| D | pinned `b7c3dd6d` | 1 | 6/6 coherent, identical, byte-identical to C | 6/6 coherent, identical, byte-identical to C |

Cells C and D being byte-identical means graph capture has no observable effect on this family once the kernel is correct. The overlay's single-row failure mode (normalizer depends on residual shared memory) explains every recorded symptom: greedy flakiness, coherent-prefix-then-collapse, prompt-length dependence, and `MLXCEL_FORCE_SYNC=1` not helping.

The cell C/D binary was proven to contain the pinned kernel (the `configure_file(COPYONLY)` overlay mechanism makes a silent no-op rebuild possible): post-build source diff against `b7c3dd6d`, `rms_norm.cu.o` recompile mtime, and `cuobjdump`/`strings` mangling showing 128 `rms_norm_small` instantiations with #3850's `bool ALIGNED` parameter (`Lb0E`/`Lb1E`) and zero of the overlay's four-int form.

## What changed

- `src/lib/mlx-cpp/patches/mlx/backend/cuda/rms_norm.cu`: deleted. The pinned kernel is correct on the affected axis and carries the #3850 forward speedup.
- `src/loading/mod.rs`: `ModelType::DeepSeekV2 | ModelType::DeepSeekVL2` removed from the hazard match; function docs rewritten to record the corrected attribution. Gemma 4 arms unchanged.
- `src/server/startup.rs`: main-thread hoist comment updated to the Gemma-4-only story.
- `src/lib/mlxcel-core/src/rms_norm_small_axis_tests.rs` (new, registered in `lib.rs`): pins `fast::rms_norm` against a float64 host reference across the broken band, 16-bit `(256, 512]` and f32 `(128, 256]`, rows 8/3/2/1. Verified non-vacuous: with the overlay kernel linked back in, both tests fail at ~3e-1 worst-row relative error; on the pin they pass at rounding scale.
- `docs/upstream/mlx-cuda-rmsnorm-small-axis-regression.md`: resolution section with the matrix, the binary proof, the non-reproduction of lablup#829's 0/4 baseline, and the local checkpoints that sat in the band (five MLA models at `kv_lora_rank == 512` plus falcon-h1-tiny whose `hidden_size` itself is 512).

## Test plan

- [x] `cargo fmt --all -- --check` clean
- [x] `cargo clippy --release --features cuda -p mlxcel-core --lib --tests` and `-p mlxcel --bins`: no lints
- [x] `cargo test --release --features cuda -p mlxcel-core --lib rms_norm_small_axis`: 2/2 (and 0/2 against the overlay kernel, restored afterwards)
- [x] `cargo test --release --features cuda -p mlxcel-core --lib fused_norm_parity`: 8/8
- [x] `cargo test --release --features cuda --test deepseek_v2_greedy_parity`: 2/2, token-exact vs mlx-lm 0.31.3 with graphs now on by default
- [x] Smoke: deepseek-v2-lite-4bit coherent with no env override (no disable log), deepseek-vl2-small-4bit coherent, gemma-4-e2b-it-4bit still logs the lablup#688 graph disable
- [x] 200-token decode: graphs-on 85.0-93.3 tok/s vs graphs-off 86.5-88.1 (n=3 each; GB10 single-run decode is bimodal, coherence is the gate)

Closes lablup#831
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:medium Medium 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(cuda): make incremental decode graph-capture-safe for DeepSeek-V2 and Gemma 4 to re-enable CUDA graphs

1 participant