Repository navigation
fix(cuda): explain the fused add+RMSNorm greedy divergence as a near-tie - #3507
Conversation
On GB10, llama-3.2-1b-instruct-bf16 at B=1 on the 128-token prompt generates other text with MLXCEL_FUSED_ADD_RMSNORM=1. The new examples/fused_norm_divergence_probe.rs reproduces both arms' CLI token digests, teacher-forces both on the graph arm's tokens, dumps every layer's tensors at a chosen step, diffs two dumps, and feeds one arm's join inputs to graph_add_rms_norm and the fused launcher in one process. The only argmax flip is step 11, where the graph's top two are an exact bf16 tie (margin 0; fused margin 0.125, accumulated logit deviation at most 0.125). The arms first part in the prefill at layer 1's normed. On the same input new_residual is byte-identical and normed differs on 2 of 2048 prefill rows, each side reproduced by one fp32 normalizer, the float64 reference agreeing with each side once. Simulated in fp32, MLX's rms_norm_small<bf16, 128, 2, _, 8> and the fused kernel's 512 lanes of 4 sum the squares one fp32 ulp apart on both rows. The intermediate differs by one bf16 ulp, the output by up to two. No kernel change: making it byte-identical needs MLX's launch geometry and a regenerated cuda-oxide artifact. A CUDA test pins the relationship, the fused_add_rmsnorm_enabled doc, the evidence doc and the stale UNVALIDATED header cite it, and raw results are recorded as CSV. Refs #3501
docs/benchmark_results/README.md says maintained harnesses belong in scripts/ and result folders hold data, which is where #2834 put its oxide_fused_norm tools. The fit script that explains the two differing prefill rows now lives at scripts/fused_norm_normalizer_fit.py with the shebang and Apache header of its siblings, and the results README points at the new path. Re-ran it on the probe's layer 1 and layer 11 op-diff dumps: output matches normalizer-fit-prefill.csv. Refs #3501
Implementation Review SummaryIntent
Findings Addressed
Remaining Items
Verification
|
Security and Performance ReviewNo CRITICAL or HIGH findings, so nothing was changed or pushed. Every probe and script input ( Remaining Items
Measured
|
The results README, evidence doc, fused_norm.cpp header comment and cuda_near_tie.rs doc wrote `rms_norm_small<bf16, 128, 2, true, 8>` as if the ALIGNED argument had been observed. Only the block size, chunk count and read width were read from MLX's dispatcher; ALIGNED selects the load function, not the summation order, so those sites now say it was not observed and does not change the conclusion. The layers.rs comment now says the rounded intermediate bf16(x * inv) is one ulp away and the post-gain normed output up to two. The evidence doc and README state that the two-ulp output exceeds the issue's one-ulp budget and why the near-tie verdict still holds (one fp32 normalizer per side reproduces each row, a float64 reference splits). The README notes that the normalizer fit ranges are clipped at the script's 24-ulp search limit. The new results folder is added to docs/benchmark_results/README.md. Comment and doc changes only; the kernel string is untouched. Refs #3501
…cript read_dump now checks each manifest entry before from_bytes trusts it: the tensor name cannot hold a path separator or `..`, the dtype is one the probe dumps, no dimension is negative, and element count times element size equals the .bin size. --op-diff reads the dump once and looks tensors up by name instead of re-reading it per lookup. The probe rejects --prompt-tokens 0 and --expect-route values other than graph, fused or oxide at parse time, checks --tokens ids against the logits width, prints the route for --op-diff and honours --expect-route there, and rejects --expect-route with --compare. fused_norm_normalizer_fit.py checks the safetensors header length, entry dtype (BF16) and data offsets, validates argv, and exits with a message for any width other than the 2048 geometry it models. cuda_near_tie.rs runs six seeds instead of four (5 of 24576 rows differ, 3.0 s) and its zero-rows failure message now tells the reader to compare MLX's dim-2048 dispatch with fused_norm_threads before switching to a byte-identity pin. Validation: clippy -D warnings on the probe example and mlxcel-core --lib --tests, cargo fmt --check, fused_norm_parity_tests 11/11 on GB10 with the switch unset and =1 under gpu-lock, probe smoke run (teacher-forced prefill dump, --op-diff) and the benchmark and used-by and env-var checks. Refs #3501
Summary
The greedy divergence between the graph path and
MLXCEL_FUSED_ADD_RMSNORM=1onllama-3.2-1b-instruct-bf16(GB10, B=1, 128-token prompt) is a near-tie from the fp32 summation order of the RMSNorm sum of squares, not a kernel defect. This PR adds the probe that measures it, pins the measured relationship in a CUDA test, and corrects the docs. The kernel is unchanged.Findings
153e97b6c47aae0dgraph,ca3dde687d118faffused) with route counters checked. Teacher-forced, the only argmax flip in 128 steps is step 11: graph 287 and 430 tie exactly at 18.5 (margin 0); fused 430 at 18.5 over 287 at 18.375 (margin 0.125). Accumulated logit deviation at that step is at most 0.125.normedin the prefill (19 of 262,144 elements, 1 bf16 ulp);new_residualnever diverges first.new_residualis byte-identical everywhere;normeddiffers on 2 of 2048 prefill rows and on no step-11 row. Each side's row is reproduced exactly by one fp32 normalizer, and a float64 reference agrees with the fused kernel on one row and the graph on the other.rms_norm_small<bf16, 128, 2, _, 8>at dim 2048 (ALIGNEDdepends on runtime pointers). Simulated in fp32 in that order and in the fused kernel's 512 lanes of 4, the two rows' sums of squares are one fp32 ulp apart, in the direction the fitted normalizers predict.bf16(x * inv)is within one ulp. Two output elements are two ulps apart, because the gain multiply carries the intermediate's change across a binade. So the issue's literal one-ulp criterion on the output is not met. I still took the near-tie branch, because a single per-row normalizer explains both sides.What changed
examples/fused_norm_divergence_probe.rs(+tensors.rs,op_diff.rs): steps,--dump-layers,--compare,--op-diff, and a route check that refuses a mismatched arm (--expect-routeis checked at parse time and honoured by--op-diff;--comparerejects it). Dump manifests are validated before any tensor is built (no path components in names, known dtype, non-negative dims, element count times dtype size equal to the file size).fused_norm_parity_tests/cuda_near_tie.rs: on CUDA at bf16 dim 2048 over 6 seeds of 4096 rows (5 of 24,576 rows differ, about 3 s),new_residualis byte-identical, differingnormedrows are at most 2 ulps apart, and each is reproduced by one fp32 normalizer within 32 ulps. It also asserts that some rows differ, so it fails once the kernel becomes byte-identical. It replaces the "within one ulp" test the brief asked for, because the measured output bound is two. A mutated formula (roundingw * xfirst) was checked to fail it.fused_add_rmsnorm_enabledcomment, the evidence doc next to "One observation outside the gate", and the stale UNVALIDATED header infused_norm.cpp. Raw CSVs are indocs/benchmark_results/issue-3501-fused-norm-divergence-gb10-2026-11-28/; the analysis script isscripts/fused_norm_normalizer_fit.py.Changes during review
scripts/, per the results README.--prompt-tokens 0, token ids against the vocabulary, safetensors header bounds and dtype, unsupported widths), and--op-diffreads a dump once.ALIGNEDwas not observed and selects only the load function; the intermediate is within one ulp and the output within two, which exceeds the issue's literal budget; the fit ranges are clipped at the script's 24-ulp search limit.Follow-up, not done here: byte identity needs MLX's per-dim geometry (
dispatch_num_chunks, 16-byte chunked loads, the looped kernel past 8192) and a regenerated cuda-oxide artifact. That artifact is a bitwise port of the current kernel, and its toolchain is not on this host.Test plan
fused_norm_parity_tests(11 tests) pass on GB10 withMLXCEL_FUSED_ADD_RMSNORMunset,=0and=1(--test-threads=1, under gpu-lock)m1-end-to-end.csvon both arms; layer walk gives byte-identicalsteps.csvto the plain forward-D warningsonmlxcel-core --lib --testsand the probe example;cargo fmt --check;check_benchmark_raw_logs.py,check_kernel_dtype_keys.py,check_kernel_port_dispatch.py,check_benchmark_csv_schema.py,check_used_by.py,check_env_var_docs.py--op-diffreports layer 1; a malformed dump manifest is rejectedCloses #3501