Repository navigation
feat(cuda): make fused add+RMSNorm byte-identical to MLX rms_norm - #3512
Conversation
The CUDA fused kernel swept a row with its own geometry (ceil(dim/4) lanes of four elements), so the fp32 sum of squares was reduced in another order than MLX's rms_norm and a normed element could land one or two bf16 ulps away, enough to break an exact logit tie the other way with MLXCEL_FUSED_ADD_RMSNORM=1 (#3501). The CUDA body now takes MLX's geometry: compute dtype promote_types(activation, weight), vectors of 16 / sizeof(compute) elements, the block width of dispatch_num_chunks and the eval_gpu threshold (fused_norm_cuda_block), and the per-thread strided vector order. This tree's promotion table makes bf16 with f32 bf16, so that pair computes in bf16, not f32 as the issue assumed. The geometry is chosen by the CUDA build flag because the port-dispatch check forbids comparing the backend kind in a launcher. Metal and ROCm are unchanged. The cuda-oxide kernel follows the same contract and its artifact is regenerated (PTX ed470a19c4848893, cubin 390fcc2833e532ef). Manifests record fused_norm_cuda_block(dim, n_reads) and each entry's n_reads, and the adapter refuses an artifact built for the old rule or another vector width. The cubin ceiling in the binary-assets check goes from 80 to 100 KB. cuda_byte_identity.rs replaces cuda_near_tie.rs: eight dtype pairs, 21 widths over every dispatch branch, the #3501 seeds, the Gemma fold, signed zeros and non-finite rows. It fails on the previous kernel (270 cases) and passes now. The oxide matrix test requires bit identity with the kernel on all 130 cases and with the graph on finite rows. Both env.sh files now work when sourced from zsh. Refs #3510
After the kernel change the graph, fused and cuda-oxide arms give one token digest per cell on the six #2834 cells of llama-3.2-1b-instruct-bf16 (B=1 / 128 is 153e97b6c47aae0d on all three, where the fused arms used to stop after 98 tokens), the probe's prefill dumps of the two arms are byte-identical, and its op diff reports no differing element on any layer. The rows are in docs/benchmark_results/cuda-fused-norm-byte-identity-gb10-2026-11-28/, and the spike's compare and bench results are from the regenerated artifact. The evidence documents of #2832, #2833 and #2834 get dated notes with the new artifact digests instead of rewritten tables, the environment-variable reference and the CHANGELOG state that opting in on CUDA no longer changes output, and the dated #2834 and #3501 result directories are untouched. Refs #3510
…hange The #2834 M4 method (nsys, CUDA graphs off, bf16 width 2048) on GB10 shows the cost of matching MLX's summation order: with MLX's 128-thread block the fused kernel and the cuda-oxide kernel take 1.3 to 2 times their 2026-11-27 time at the five model shapes (3.4 against 2.2 us at one row, 186 against 140 us at 2048 rows). The graph path's kernels read the same as before, so the change is the kernel's, and the graph path is now faster at every shape. One end-to-end round shows no difference above 1.5 %. The CSVs and the table are in the new results directory, and the benchmark evidence note, the results index and the CHANGELOG state the regression. A CI job overlapped the stage and it was not repeated on an idle host. Refs #3510
Implementation Review SummaryIntent
Findings AddressedNo CRITICAL or HIGH findings; no fixes pushed. Remaining Items
Verification
Checked against the MLX source at the pin: |
Security and Performance ReviewNo CRITICAL or HIGH findings; no fixes pushed. Checked
Remaining Items
|
The adapter evidence page stated the pre-regeneration PTX digest and the JIT module name built from it in present tense; they now read as the 2026-11-26 values next to the current digest `ed470a19...`. Comments in the byte-identity test and the spike pointed at `kernels/fused_norm.cpp` for `fused_norm_cuda_block`, which lives in `kernels/fused_norm_cuda.h`. `host_model` in the spike now says it is a tolerance model, not a summation-order model. The spike edits are comment-only and the committed PTX carries no `.loc` or `.file` directives, so the artifact bytes are unaffected. The Rust artifact validator is compiled only under `MLXCEL_BUILD_CUDA_OXIDE_FUSED_NORM=1`, so the compute_dtype check was not added here. Validation: cargo fmt --all --check; clippy -p mlxcel-core --features cuda --lib --tests -D warnings; verify-kernel-dtype-keys, verify-kernel-port-dispatch, verify-binary-assets, verify-env-var-docs; check_benchmark_raw_logs.py. Refs #3510
…da-fused-norm-byte-identity # Conflicts: # CHANGELOG.md
Summary
fused_add_rms_normnow returns the bytes of the defaultadd+fast_rms_normpair for every dtype pair and row width, soMLXCEL_FUSED_ADD_RMSNORM=1no longer changes greedy text (fix(cuda): Greedy output differs between graph path and fused add+RMSNorm on Llama 3.2 1B #3501). The kernel reads a row as MLX'srms_normdoes: compute dtypepromote_types(activation, weight), vectors of16 / sizeof(compute)elements, the block ofdispatch_num_chunks(64, 128, or 1024 for the looped kernel), threadtsumming vectorst,t + block, ... in order.ed470a19c4848893, cubin390fcc2833e532ef). Manifests recordfused_norm_cuda_block(dim, n_reads)andn_reads; the adapter refuses an artifact built for the old rule or another vector width.FUSED_ADD_RMSNORM_DEFAULTis unchanged. The Metal and ROCm bodies, geometry and template args are unchanged.scripts/env.shin both spike packages can be sourced from zsh.Differences from the issue text
n_reads8, weight rounded to bf16), not f32: this tree's promotion table makes bf16 with f32 bf16. The same holds for f32 under a bf16 weight.MLXCEL_BRIDGE_CUDA_BACKEND, becauseverify-kernel-port-dispatchforbidsgpu_kernel_backend() ==in a launcher.Kernel time
The matching geometry is slower. With the #2834 M4 method (bf16, width 2048) the fused and cuda-oxide kernels take 1.3 to 2 times their earlier time (3.4 against 2.2 µs at one row, 12.7 against 6.5 µs at 128 rows, 186 against 140 µs at 2048 rows), while the graph path's kernels are unchanged, so the graph path is now faster at all five shapes. One end-to-end round shows no difference above 1.5 %. A CI job overlapped the stage and it was not repeated on an idle host. Rows and table:
docs/benchmark_results/cuda-fused-norm-byte-identity-gb10-2026-11-28/.Changes during review
ed470a19...), points two comments atkernels/fused_norm_cuda.hforfused_norm_cuda_block, and documentshost_modelas a tolerance model rather than a summation-order model. These spike edits are comment-only; the committed PTX has no.locor.filedirectives, so the artifact bytes are unchanged.Known limits, not addressed here
n_readsbut notcompute_dtype(the Python tool checks both). It compiles only in the opted-in oxide build, and the PTX digest pin covers the committed artifact.grid.z, so an opted-in launch with more than 65535 rows fails with a launch error. This predates the PR; changing it means editing the kernel body and regenerating the artifact.rms_norm_smalldoes) was not attempted.Test plan
cuda_byte_identity(8 dtype pairs, 21 widths at 1 and 33 rows, the fix(cuda): Greedy output differs between graph path and fused add+RMSNorm on Llama 3.2 1B #3501 seeds, Gemma fold, eps 1e-5, spread row scales, signed zeros, non-finite rows): 270 failing cases on main, passes now.fused_norm_parity_testswithMLXCEL_FUSED_ADD_RMSNORMunset,=0,=1: 13 passed each, release, GB10.cuda_oxidein an opted-in build: 33 passed; matrixcases=130 failed=0 bitwise_identical_to_fused_cuda=130, graph equality on finite rows.build.sh,run.sh(130 cases, launch check),compare.sh(130 bitwise),bench.sh,check-lint.sh,check-clean-build.sh. The oracle ran from the release-built example, not throughoracle.sh.tokens_sha256_16per cell acrossgraph,fused,oxide, routes matching each arm; B=1 / 128 is153e97b6c47aae0don all three.--compareof the two arms' prefill dumps byte-identical,--op-diff0 differing elements on 16 layers.cargo clippy -p mlxcel-core --features cuda --all-targets -- -D warningswith and without the oxide build flag,cargo fmt --check,verify-kernel-dtype-keys,verify-kernel-port-dispatch,verify-binary-assets.Closes #3510