Repository navigation
perf(cuda): recover fused add+RMSNorm kernel time with byte identity - #3534
Conversation
The byte-identical kernel of #3510 read one element at a time and read `new_residual` back for its second pass, which made it 1.3 to 2 times slower than before and slower than the graph path's two kernels at all five #2834 shapes. Only data movement changes here: the summation order, the FMA, the two butterflies and every cast point are as they were. - NVRTC kernel: rows move through MLX's `load_vector` / `store_vector` (run-time alignment test, element-wise fallback), a thread keeps its `Chunks` vectors of residual sums in registers on the small path, a thread with one or two chunks requests its weight vectors with the row's, and the looped path re-reads `new_residual` as MLX's looped `rms_norm` re-reads its input. - Rows above 65535: rows are walked grid-stride on x from at most 65535 blocks, with the count in a 1-element int64 input, CUDA only. A launch past 65535 rows used to fail at evaluation. - cuda-oxide kernel: the same split at run time, with whole vectors moved as 64-bit words and two vectors kept in registers (eight need 72 registers, over the 64 a 1024-lane block allows). Artifact regenerated; cubin 92,000 to 123,464 bytes, so the asset ceiling goes to 128 KB. - Validator: `Launch` carries `compute_dtype` and it is checked against `Lane::compute_under`, now the one Rust statement of the promotion table, which a test pins to MLX's own `promote_types`. - The launcher refuses any activation or weight dtype other than f32, f16 and bf16, and `fused_add_rms_norm_eligible` answers false for them first. Validated on GB10 (sm_121): `fused_norm_parity_tests` 17 passed with `MLXCEL_FUSED_ADD_RMSNORM` unset, 0 and 1; 35 `cuda_oxide` tests with the adapter matrix at 130 of 130 bitwise; spike `run.sh` 130 cases and `compare.sh` 130 bitwise against the fused kernel and the graph; clippy on mlxcel-core in both build variants. Refs #3518
Adds `docs/benchmark_results/cuda-fused-norm-kernel-time-gb10-2026-11-29/` with the #2834 M4 kernel rows, a per-shape summary against both bars of #3518, and the host state of each run of the stage. The fused kernel is below the graph path's two kernels at all five shapes (2.30 against 2.46 µs at one row, 140.5 against 145.8 at 2048 rows) and at or below its time before #3510 at 4, 128 and 512 rows. Both runs of the stage overlapped a CI job on the host, which the README and the changelog state; the idle-host run the issue asks for is still owed. Also updates the changelog, the `MLXCEL_FUSED_ADD_RMSNORM` entry, the three cuda-oxide evidence documents and the spike README for the new data movement and artifact digests, and re-records `compare-sm_121.json` against the implementation commit. Refs #3518
The two runs of the M4 stage recorded in the previous commit each overlapped a CI job. Two later runs had no CI job and no other `gpu-lock` holder for their whole length, so the results directory now holds those six captures and the earlier ones are discarded. The medians moved by at most 4 %: 2.30, 2.27, 2.82, 6.82 and 140.1 µs for the fused kernel against 2.46, 2.91, 3.52, 8.42 and 143.8 for the graph path. The CPUs were busy with an unrelated process during the captures, which the README states. `fused_norm_m4_summarize.py` writes LF line endings and adds a `paired` row per shape: the captures in which the fused kernel was below that capture's own graph pair (6 of 6 at every shape). The README marks the causes that were not isolated as likely. Refs #3518
Implementation Review SummaryIntent
Findings AddressedNone needed: no CRITICAL or HIGH finding. Remaining Items
Checked
Verification
|
Security and Performance ReviewNo CRITICAL or HIGH finding; nothing was changed on the branch. Sanitizer runs (GB10, CUDA 13.0 compute-sanitizer 2025.3.1, under gpu-lock)
Filtered means Reviewed by reading
Coverage limits (LOW, no fix requested)
|
The summarizer paired captures by label alone, so the two stages of the committed m4-nsys-kernels.csv, which reuse mlx-graphs-off-rep1..3, collapsed into 3 pairs; it now keys on (stage, label) and reproduces the committed m4-summary.csv byte for byte (6 of 6 pairs). The results README now says what host-state.csv records: the first stage started after 45 idle seconds, the second 5 seconds after the first ended, both with zero busy samples. It also notes that the compare step does not use the GPU, which is why compare-sm_121.json has gpu_lock_kind unspecified. A new test runs 70000 rows at width 2048 and 66000 rows at width 8200 (bf16/bf16) so the Chunks 2+ bodies and the looped body walk a second row per block; it passes byte-identical against the graph. A comment in the oxide kernel states the n <= 2 * per_word packing limit (comment only; the PTX has no .loc or .file directives and the manifests digest PTX and cubin, unchanged). Validation: - gpu-lock run fused_norm_parity_tests --test-threads=1: 18 passed - cargo fmt --all --check, cargo clippy --release -p mlxcel-core --features cuda --lib --tests -D warnings, make verify-*, check_benchmark_raw_logs.py: pass Refs #3518
Summary
The byte-identical CUDA fused add+RMSNorm kernel of #3510 read one element at a time and read its residual sum back, which left it slower than the graph path's two kernels at every #2834 shape. This PR changes how both kernels move data and no value they compute, and closes the four hardening gaps listed in #3518.
load_vector/store_vector(run-time alignment test, element-wise fallback), keeps a thread's vectors of sums in registers on the small path, and requests the weight with the row when a thread owns one or two vectors. The cuda-oxide kernel moves whole vectors as 64-bit words and keeps two vectors in registers; its ABI and launch rule are unchanged, soLaunchgained no field beyond item 3's.compute_dtype(item 3) is part ofLaunchand checked againstLane::compute_under.fused_add_rms_norm_eligibleanswers false first.Lane::compute_underis the Rust source,n_reads_underderives from it, andlane_promotion_matches_mlx_promote_typespins it to MLX at run time.Kernel time
#2834 M4 method, bf16, width 2048, µs, median of six captures (results):
The fused kernel is below the graph path at all five shapes, in each of the six captures, and at or below its time before #3510 at 4, 128 and 512 rows.
FUSED_ADD_RMSNORM_DEFAULTis unchanged.Host state. Two runs of the stage (six captures) had no CI job and no other
gpu-lockholder, sampled once a second (host-state.csv); two earlier runs that a CI job overlapped were discarded. The CPUs were not idle: another project's test process held about 16 of the 20 cores (load average 20 to 21). The durations are GPU timestamps and the untouched graph kernels read as on 2026-11-28, but a run with idle CPUs was not obtained. At one row and at 2048 rows the kernel misses its time before #3510 (2.30 against 2.21, 140.1 against 139.7) and passes on the graph bar, by 6 % and 3 %.Validation (GB10, sm_121)
fused_norm_parity_tests: 18 passed withMLXCEL_FUSED_ADD_RMSNORMunset,=0,=1. The feat(cuda): make the fused add+RMSNorm kernel byte-identical to MLX rms_norm and regenerate the cuda-oxide artifact #3510 byte-identity tests are unchanged; new: unaligned views (8 pairs, widths 2048 / 2049 / 8193, the view address asserted unaligned), 65535 / 65536 / 70000 rows at width 8, 70000 rows at 2048 and 66000 rows at 8200 (looped body), the dtype guard.cuda_oxidetests, matrixcases=130 failed=0 bitwise_identical_to_fused_cuda=130; the layer-route integration test; spikebuild.sh,run.sh(130),compare.sh(130 bitwise, zero deviation from the graph),bench.sh,check-lint.sh,check-clean-build.sh.cargo fmt --all -- --check,make verify-kernel-dtype-keys verify-kernel-port-dispatch,check_binary_assets.py.cuda_byte_identity(graphs off, filtered tofused_add_rms_norm; memcheck also unfiltered), plus the grid-stride test alone under unfiltered racecheck.compute_underreturning f32 for bf16 under f32, the test fails withBf16 under a F32 weight: compute_under says F32, MLX promotes to dtype 12, and the committed artifact stops validating (8 of 15 artifact tests fail).compute_dtyperefusal, int32 guard) were not executed against main.Changes during review
scripts/fused_norm_m4_summarize.pyon(stage, label)so it reproduces the committedm4-summary.csvfrom the committed CSV (6 of 6 pairs), corrects the README's idle-wait wording to whathost-state.csvrecorded, documents the oxide packing limit (a vector is at most two 64-bit words) in a comment, and adds the wide and looped grid-stride test. Artifacts are unchanged (the PTX has no.locor.filedirectives).For review
check_binary_assets.pygoes from 100 KB to 128 KB and the total budget by the same 31,464 bytes. PTX SHA-256fb873d30443256e61b5cdcabbb6a0b0189531c19a2d52968f0c16e4b2b795d0f.ptxasreports 72 registers and a 1024-lane launch fails; with two, 41 to 48.Closes #3518