Repository navigation
[JIT] Round custom all-reduce BF16 stores to nearest even - #6640
Conversation
module_custom_all_reduce converts its fp32 accumulators to bf16 through opus::cast, which on gfx942 (no hardware bf16 cvt) falls back to the opus.hpp default OPUS_FP32_to_BF16_DEFAULT=2, i.e. truncation. So every bf16 the module writes is rounded toward zero: the all-reduce result and, in the fused all-reduce + RMSNorm kernels, the rounded all-reduce intermediate, residual_out and the normed output. residual_out feeds the next layer, so the bias does not cancel. This is the same problem ROCm#6300 fixed for the RMSNorm modules, in the path that replaces them when all-reduce and RMSNorm are fused. Build the module with the optimized software RNE (mode 5) that ROCm#6300 introduced, with the same OPUS_FP32_to_BF16_DEFAULT env override. gfx950/gfx12/gfx1250 keep the hardware conversion and are unaffected. Signed-off-by: Andreas Karatzas <[email protected]> Co-Authored-By: Claude <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
ChuanLi1101
left a comment
There was a problem hiding this comment.
gfx942 custom all-reduce was truncating bf16 stores. Mode 5 rounds to nearest even, same as the RMSNorm modules. On Llama-3.1-8B bf16, TP2, with all-reduce and RMSNorm fused, gsm8k goes from 0.665 / 0.673 to 0.704 / 0.699, in line with fusion off. The speed ranges overlap. gfx950 uses the hardware convert and does not read this flag.
module_mhc and module_fused_ar_mhc build csrc/kernels/mhc_kernels.cu without -DOPUS_FP32_to_BF16_DEFAULT, so every FP32 -> BF16 conversion in those kernels (mhc_post's stores, mhc_pre's outputs, mhc_post_pre's next residual, BF16 GEMM operands) takes opus.hpp's default, mode 2: truncate. gfx950, gfx1250 and gfx12 convert in hardware with round-to-nearest-even and ignore the flag, so only gfx942 and older are affected. The mega_mhc modules already pass the flag (5, software RNE); this gives the two modules that build mhc_kernels.cu the same one, as #6300 did for RMSNorm and #6640 for the custom all-reduce. Truncation stays inside test_mhc.py's tolerance, but it rounds every output toward zero, so mhc_post shrinks the residual streams by 0.3-0.4% per call. DeepSeek-V4.1 runs mhc_post twice per layer (80 calls per forward; SGLang calls aiter's mhc_post on ROCm), and the shrink compounds: on MI300X its teacher-forced NLL sat 0.1-0.3 nat above the same checkpoint on MI355X. Tests: - test_mhc.py: test_mhc_post also checks the signed rounding error against the FP32 result, which the tolerance check cannot see (truncation: -2.8e-3; round to nearest: 0 with a spread of 5e-5 at m=1, hidden=512). BF16 only, skipped for a build that asks for truncation (OPUS_FP32_to_BF16_DEFAULT 1 or 2), and guarded against all-zero inputs. - test_mhc_seam.py: the gfx942 ASM seam (hsa/gfx942/mhc_seam) keeps its truncating residual_out, which was asserted bit-exact against mhc_post. It is now checked as the truncation of mhc_post's value (equal, or one BF16 ulp smaller in magnitude), and layer_input and the gates against the seam's own residual_out, which is what the kernels compute from. Comments and the op's docstring say so. Measured on MI300X (gfx942), aiter 8c964fa from a ROCm SGLang image (same opus default, module flags and mHC tests as main) with this change's flags and tests: - test_mhc.py: before, the new check fails (bias -2.81e-3 at m=1, hidden 4096); after, the whole file passes (bias 3.1e-6 / -7.5e-6 / -2.9e-6 at m = 1 / 64 / 2048, hidden 5120). - test_mhc_seam.py: 0 failures before and after. - multigpu_tests/test_fused_ar_mhc_post_only.py -t 2: passes before and after (fused and split paths agree). - Main's mhc_kernels.cu compiled for gfx942 with module_mhc's flags: the RNE pair (v_bfe_u32 + v_add3_u32) per mhc_post_kernel instantiation goes from 48 to 352. - DeepSeek-V4.1-Flash, TP4/EP4, teacher-forced NLL (MI355X on the same checkpoint in brackets): 2,000 tokens 3.65-3.69 -> 3.36-3.37 [3.36]; 1,205 tokens 3.37-3.42 -> 3.17-3.18 [3.18]; 14,207 tokens 1.60 -> 1.48-1.49 [1.49]. Co-authored-by: Jin Pan <[email protected]> Co-authored-by: Jinwei Yao <[email protected]>
Motivation
On gfx942, every bf16 value that
module_custom_all_reducewrites is truncated rather than rounded. That covers the plain all-reduce result and, in the fused all-reduce + RMSNorm kernels, the all-reduce intermediate,residual_outand the normed output. #6300 fixed the same bias in the RMSNorm modules. When vLLM fuses all-reduce with RMSNorm, though, the norm runs inside this module instead. Its truncatedresidual_outthen feeds the next layer, so the error compounds instead of averaging out. vLLM enables this fusion on ROCm whenever AITER is on and TP > 1 (enable_allreduce_rms_fusioninvllm/config/vllm.py, used by the -O2/-O3 defaults), with no gfx942 exception. On Llama-3.1-8B-Instruct bf16 TP=2 with the fusion on, gsm8k strict-match drops by 3-4 points.Repro, run with
torchrun --nproc-per-node 2on MI300X:Technical Details
The module converts fp32 to bf16 with
opus::cast(downcast_s,csrc/include/custom_all_reduce.cuh:86). On architectures without a hardware bf16 convert, that cast usesOPUS_FP32_to_BF16_DEFAULT, which defaults to 2, i.e. truncation (csrc/include/opus/opus.hpp:41). #6300 builds the RMSNorm modules with-DOPUS_FP32_to_BF16_DEFAULT=5, its optimized software RNE, but notmodule_custom_all_reduce.This PR adds the same flag and env override to
module_custom_all_reduceinaiter/jit/optCompilerConfig.json. gfx950, gfx12 and gfx1250 use the hardware conversion (with clang >= 20), which ignores the mode, so they are not affected.Test Plan
All runs used MI300X (gfx942) at TP=2 on the AITER v0.1.25 runtime, with only
module_custom_all_reducerebuilt with this change. "main" in the tables is the unpatched v0.1.25 module. The module also compiles on main for gfx942.Bit-exact comparison against torch RNE (
fp32.to(bf16)) and truncation of the fp32 reference:The check ran 10 times with this PR and twice on main.
Tie and special values: 1 + 2^-8, 1 + 3·2^-8, inf, ±0.
op_tests/test_custom_allreduce.py -t 2 --regressionandop_tests/test_fused_ar_rms.py -t 2 -d bf16 --test fused.Kernel latency, 5 runs each. Small sizes are timed under CUDA-graph replay. Sizes from 1 MB, and the fused 2-stage kernel, use profiled kernel time, because graph timings there swing with xGMI traffic from other jobs.
vLLM, Llama-3.1-8B-Instruct bf16 TP=2:
vllm bench latency512/256;vllm bench serveon random 1024/256 data, 3 interleaved runs per arm, with fusion on and off.Test Result
Bit-exactness:
residual_outThe match rates were identical in all 10 runs with this PR. On main, the mean relative magnitude bias of the fused output is -2.7e-3. With this PR it is below 1e-5. Ties round to even (1 + 2^-8 -> 1, 1 + 3·2^-8 -> 1 + 2^-6), and inf and ±0 pass through unchanged. Both op tests pass. In
test_fused_ar_rms.py, thefused_errat (19, 8192) goes from 6.4e-6 / 1.3e-5 (eager / graph) to 0.gsm8k strict-match, with exact McNemar p against the fused-truncation arm on the same questions:
Fused with this PR vs unfused: p = 1.0 and 0.63. Flexible-extract does not change significantly in any pair (p >= 0.14).
Kernel latency, median of 5 runs (min-max), us:
Plain all-reduce is +0-3.4% at every measured size from 1 KB to 64 MB. The fused 1-stage kernel costs about +0.6 us per call. The fused 2-stage kernel is +1.3% or less.
End to end:
vllm bench latency, fused: median latency at batch 1 goes from 0.803 / 0.804 s to 0.817 / 0.814 s (two runs each, about +1.5%). Batch 32 is unchanged (1.933 / 1.930 vs 1.930 / 1.932 s).vllm bench serve, output tok/s, median of 3 (min-max):The median shifts go both ways (-5.3% to +6.2%), and the min-max ranges overlap in five of the six configs. In the sixth (fused, concurrency 32) this PR is faster. I read this as noise, with no sign of a regression. TPOT p50 is within 1% except in the two noisiest configs (fused concurrency 32: -5.4%, unfused rate inf: +9.4%). None of the 8,400 requests failed.
Not tested:
Submission Checklist