Repository navigation
[OPUS] [JIT] Default RMSNorm BF16 stores to optimized RNE - #6300
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
f5a096d to
08f58c7
Compare
|
@aiter-bot review |
|
aiter-bot — queued behind 1 review(s); expect to start in about 50 min. The box reviews one PR at a time. |
ROCm/aiter PR #6300 — [OPUS] [JIT] Default RMSNorm BF16 stores to optimized RNEFlips the default fp32→bf16 store rounding of aiter's RMSNorm kernels (plain norm, residual-add, fused QK norm+quant) on gfx942 from truncation toward zero to a new optimized software round-to-nearest-even (mode 5), selectable at JIT build time via the OPUS_FP32_to_BF16_DEFAULT env var. Review (advisory):
|
|
Where that review came from — I ran the bot on your PR to show what it does. It is not an automatic hook; nothing reviews your PR unless somebody asks. Any reviewer on the aiter allowlist can ask for one, on any PR including their own. The trigger is a comment containing the words below — shown quoted here, because quoted lines deliberately do not trigger it:
Feedback on the review itself is welcome; it is how the rules get better. |
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 #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 #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]>
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]>
Problem and change
BF16 RMSNorm stores currently truncate FP32 values on architectures without hardware BF16 conversion, including gfx942. This rounds inexact values toward zero, including
residual_out, and can accumulate bias in the residual stream. This PR addresses the correctness problem described in PR #6076 using an optimized software round-to-nearest-even (RNE) converter to reduce the cost of switching away from truncation.The optimized converter is appended as mode 5 in the existing
OPUS_FP32_to_BF16_DEFAULTselection. Mode 0 keeps the original software RNE implementation and its legacy NaN behavior. Existing mode numbers are unchanged. The separateOPUS_FP32_to_BF16_RNE_IMPLselector is removed.RMSNorm now defaults to mode 5 in these three JIT modules:
module_rmsnormmodule_rmsnorm_quantmodule_fused_qk_rmsnorm_group_quantThe standalone
rmsnorm_opus_kernel.hppalso defaults to mode 5. The sharedopus.hppdefault remains mode 2 for other callers. On gfx950/gfx1250/gfx12 with Clang >=20, the existing hardware conversion path remains in use.Existing environment override, including OPUS RMSNorm
No environment variable is needed to select optimized RNE for RMSNorm. The existing override works for all three modules, including OPUS
module_rmsnorm:This is a compile-time setting. Set it before starting Python and rebuild the affected JIT modules, or use a fresh
AITER_JIT_DIR; existing cached shared libraries do not switch at runtime. Modes 1 (truncation with NaN handling) and 3 (assembly RNE) are unchanged. Mode 4 is mentioned by the existing RTA comment but has no implementation in this header.Conversion behavior and validation
The optimized converter computes the rounding bias unconditionally and selects a canonical NaN with an integer comparison. Under strict compilation it matches original RNE for finite values, signed zeros, subnormals and infinities. It canonicalizes NaNs to positive BF16
0x7fff; mode 0 retains the original sign/payload behavior. Existing fast-math flags are unchanged. In a-ffast-mathprobe, the compiler removed optimized NaN handling, so special-value compatibility is not guaranteed for fast-math builds.Validation on MI308X (gfx942), ROCm 7.2.4, PyTorch 2.10.0:
test_rmsnorm2d.py, 32x4096: residual-add, model-sensitive/T5 and Gemma checks passed with freshly compiled mode-5 modules.git diff --checkpassed.These tests do not establish model-level rollout/trainer log-probability equivalence, and the full AITER test suite and GPU execution on other architectures have not been run.
Detailed performance
Same final source for all variants, with separate JIT caches and mode overrides: 2 = original truncation, 0 = original software RNE, 5 = optimized software RNE. Measured on MI308X (gfx942), ROCm 7.2.4, PyTorch 2.10.0, with BF16 inputs.
Each CUDA Graph contains 128 calls; each sample times 12 graph replays. Seven samples per case are reduced to a median; the table reports the median of three runs per mode. Mode 2/5 order was 2,5,5,2,2,5; mode 0 was measured afterward. Inputs are reused, so these are hot-cache GPU timings excluding Python launch overhead. GPU clocks were not locked or otherwise changed.
hip_norm/hip_addmeasure the common HIP path for 2D BF16, hidden <=8192, non-model-sensitive mode.opus_norm/opus_addexplicitly measure the OPUS path; public dispatch uses OPUS for larger hidden sizes and model-sensitive cases.hip_add_fp8uses residual-add group quantization with group size 128. QK cases use strided Q/K split inputs of widths 1536/512, residual input, BF16 unquantized output, and optional FP8 group quantization.Relative to truncation, latency increases range from approximately 0-7.1% for HIP norm, 0-11.0% for HIP add, 0-3.9% for HIP add+FP8, 2.5-35.1% for OPUS norm, 14.2-45.4% for OPUS add, and 1.6-6.7% for QK add+FP8. Optimized RNE is faster than original software RNE in representative OPUS cases by about 10-13%, but remains more expensive than truncation. These are kernel latency changes, not end-to-end model throughput changes.
All latencies below are in microseconds. Positive percentages mean higher latency; negative values mean lower latency. Mode 0 was not measured for QK cases.