Skip to content

[OPUS] [JIT] [Bugfix] Round RMSNorm bf16 stores to nearest even instead of truncating - #6076

Closed
Arist12 wants to merge 4 commits into
ROCm:mainfrom
Arist12:fix/rmsnorm-bf16-rne
Closed

Arist12 wants to merge 4 commits into
ROCm:mainfrom
Arist12:fix/rmsnorm-bf16-rne

Conversation

@Arist12

@Arist12 Arist12 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Summary

The RMSNorm kernels behind rmsnorm2d_fwd and rmsnorm2d_fwd_with_add, and fused_qk_rmsnorm_group_quant, store bf16 by truncating fp32 on targets without a hardware bf16 cvt, such as gfx942 (MI300X / MI325X). That rounds every inexact output toward zero, including residual_out (input + residual).

This PR switches them to round-to-nearest-even, which is what PyTorch does and what the hardware cvt does on gfx950 / gfx12. It's a trade: on gfx942, decode-size calls get slower (see Performance).

Motivation

#4059 chose truncation for two reasons: software RNE costs about 6 ops per element on gfx942, and truncation matched the CK/HIP reference bit for bit. But the error from truncation is one-sided, so it accumulates through the residual stream.

We hit this in miles, an RL framework. sglang (using these kernels) generates the rollouts; Megatron / Transformer Engine trains. The metric is the mean per-token |logp_rollout − logp_train|, measured with miles' Qwen2.5-0.5B GRPO test (tests/e2e/short/test_qwen2.5_0.5B_gsm8k_short.py). Hidden size is 896, so only module_rmsnorm_quant runs.

setup mean abs logprob diff
MI355X ~0.008
MI300X 0.021–0.024
MI300X, sglang's native RMSNorm instead of aiter's (everything else aiter) 0.008–0.009
MI300X, module_rmsnorm_quant built with OPUS_FP32_to_BF16_DEFAULT=0 0.007–0.010

Switching only RoPE to native, or attention to triton, leaves the diff unchanged. cc @carlushuang

Changes

Change the default of OPUS_FP32_to_BF16_DEFAULT from 2 (truncate) to 0 (RNE, the existing fp32_to_bf16_rtn_raw) in three places:

module serves where the default lives
module_rmsnorm_quant rmsnorm2d_fwd* for 2-D bf16/fp16 inputs with hidden ≤ 8192, non-T5 build flag in optCompilerConfig.json (env override kept)
module_rmsnorm every other rmsnorm2d_fwd* case (see _use_hip_common in aiter/ops/rmsnorm.py) rmsnorm_opus_kernel.hpp
module_fused_qk_rmsnorm_group_quant fused_qk_rmsnorm with per-token or group quant; affects its bf16 q_out_unquantized, k_out and q_res_out build flag in optCompilerConfig.json (env override kept)
  • The first two modules change together because rmsnorm2d_fwd* picks between them by shape.
  • The third is the fused q/k-norm counterpart used in the same models. It is meant to be bit-identical to aiter.rmsnorm, and op_tests/test_fused_qk_rmsnorm_per_token_quant.py checks its k_out bit-exactly. Left on truncation, q/k norm and the residual stream would round differently.
  • Other kernels that still default to truncation, e.g. the no-quant fused_qk_rmsnorm path, are left for follow-ups.

Behavior change: this affects targets without a hardware bf16 cvt: gfx942, and gfx950 / gfx12 built with clang < 20.

  • Each bf16 store now rounds to nearest even instead of toward zero, so each stored value moves by at most 1 ulp.
  • Values computed from a stored bf16 intermediate can move slightly more, including their quantized outputs. This happens in T5 mode, and in opus rows longer than its register cache.
  • With clang ≥ 20, gfx950 and gfx12 are unchanged.

Performance

MI300X, GPU time per call, measured with HIP graphs:

M × N rms_norm, trunc → RNE fused add, trunc → RNE
1 × 4096 1.8 → 2.3 µs (+28%) 1.9 → 3.1 µs (+63%)
256 × 4096 2.3 → 2.9 µs (+26%) 2.8 → 4.1 µs (+46%)
4096 × 4096 17.9 → 18.7 µs (+4%) 33.0 → 35.9 µs (+9%)
8192 × 8192 62.5 → 65.1 µs (+4%) 126.0 → 131.7 µs (+5%)
1 × 16384 (opus) 5.9 → 9.6 µs (+63%) 7.9 → 14.0 µs (+77%)
4096 × 16384 (opus) 75.4 → 85.9 µs (+14%) 165.5 → 176.9 µs (+7%)
  • fused_qk_rmsnorm_group_quant: +6–10% at m = 8192 / 16384, per the test's own timing. Smaller shapes are within noise.
  • No kernel spills, and VGPR use barely moves. The extra cost is the software rounding sequence, which small launches can't hide.

The compiler's native conversion, static_cast<bf16_t>, is cheaper. opus already uses it on gfx950, and it gives bit-identical RNE results: the 1 × 4096 fused add drops to 2.2 µs and the 1 × 16384 one to 10.1 µs. It changes the shared helper in opus.hpp, though, so we'd put it in a follow-up.

Testing

On MI300X, the three residual checks in op_tests/test_rmsnorm2d.py now require an exact match with PyTorch's input + residual (rtol=0, atol=0, tol_err_ratio=0):

  • Without the fix, they report a mismatch in all 45 bf16 cases.
  • With the fix, all 810 checks pass.
  • fp16 and fp32 were already exact.

Before / after on related tests:

test before after
test_rmsnorm2dFusedAddQuant (vs PyTorch, atol=0.01) 2.9–3.1% of elements over tolerance in every case (under the 5% threshold) < 1e-6 of elements in any case
test_fused_qk_rmsnorm_per_token_quant all pass all pass (only module_rmsnorm_quant flipped: k_out fails)
test_fused_qk_rmsnorm_group_quant all pass all pass (one within-tolerance fp8 warning, triton vs hip, from unseeded random inputs; the HIP quant path doesn't depend on bf16 rounding)
test_gated_rmsnorm_fp8_quant all pass all pass

To reproduce, rebuild with AITER_REBUILD=1. Otherwise a cached module_*.so keeps the old rounding.

Default OPUS_FP32_to_BF16_DEFAULT to 0 (RNE) for module_rmsnorm_quant, module_rmsnorm and
module_fused_qk_rmsnorm_group_quant. Truncation biased bf16 outputs and residuals toward zero on
targets without a hardware bf16 cvt (e.g. gfx942), where RNE runs in software and costs extra
latency on small shapes.
@Arist12
Arist12 requested a review from a team October 2, 2026 03:21
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
multigpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 6076 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@github-actions github-actions Bot changed the title [Bugfix] Round RMSNorm bf16 stores to nearest even instead of truncating [OPUS] [JIT] [Bugfix] Round RMSNorm bf16 stores to nearest even instead of truncating Oct 2, 2026
@zufayu
zufayu requested a review from junhaha666 October 3, 2026 00:36
@lohiaj

lohiaj commented Oct 4, 2026

Copy link
Copy Markdown

Thanks @Arist12. We verified this on MI300X (gfx942) with vLLM 0.29.0: PR head c4c273e5 applied to aiter 0.1.19 from vllm/vllm-openai-rocm:v0.29.0, compared with the stock build and an unpatched rebuild as a control (bit-identical to stock).

Accuracy. Stock rounds ~50% of RMSNorm outputs 1 ulp toward zero (bias -2.8e-3), and the residual stream shrinks 7.1% over Qwen3-1.7B's 56 fused add+norm calls. Patched outputs are 99.999% exact and residual_out is bit-exact RNE. Rollout-vs-trainer mismatch (vLLM vs an HF forward, teacher-forced, KL as a top-20 lower bound):

Qwen3-1.7B KL top-1 flips Qwen3-30B-A3B KL top-1 flips
AITER off 9.82e-4 0.066% 2.35e-3 0.265%
stock 3.18e-3 0.444% 1.07e-2 1.194%
patched 9.76e-4 0.062% 2.32e-3 0.270%

Cost. RMSNorm kernel time at decode sizes (1-1024 rows): +12-21% norm and +27-34% fused add at hidden 2048, +33-64% fused add at hidden 4096, in line with your table. End-to-end vLLM decode on Qwen3-1.7B: -1.1% to -1.9% (batch 1-128). One outlier we haven't explained: +83% for norm at 4096 rows × hidden 4096, where your table shows +4%.

Same truncation as #5151.

@Arist12

Arist12 commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @lohiaj for the independent verification, and for pointing to #5151. @xiaohong42 reported the same bug a month earlier with verl evidence; I'll add Fixes #5151 to the description.

We reran on your setup: vllm/vllm-openai-rocm:v0.29.0 (aiter 0.1.19, torch 2.12) on MI300X, comparing the stock build, an unpatched rebuild, and the PR diff applied to 0.1.19. The rebuild's kernel times match stock (median |Δ| 0.8% over 184 shapes), so stock-vs-patched differences come from the patch.

The 4096 × 4096 outlier. We couldn't reproduce it. At this shape, aiter.rms_norm and rmsnorm2d_fwd both launch add_rmsnorm_quant_kernel<bf16, bf16, 256, 16, ...> (checked with the profiler). Trunc → RNE, median of 3 interleaved rounds:

harness 4096 × 4096 norm
HIP graph replay, one input (hot cache) 17.8 → 18.5 µs (+4%)
aiter.test_common.run_perftest (rotated inputs) 17.0 → 18.1 µs (+7%)
eager, CUDA events (includes launch overhead) 34.6 → 35.8 µs (+3%)
triton.testing.do_bench (includes launch overhead) 23.4 → 23.8 µs (+2%)

Our full graph-replay sweep gives +7% here on both 0.1.19 and aiter main, one of the smaller norm slowdowns. If you can share your script, happy to take a look.

End to end. vLLM 0.29, Qwen3-1.7B, VLLM_ROCM_USE_AITER=1, 128 prompt / 256 generated tokens, 3 interleaved rounds. Generation time: +0.3% (bs 1), +0.9% (8), +0.4% (32), +0.9% (128). That's the same direction as your −1.1% to −1.9% throughput, slightly smaller.

On gating by FUSE_QUANT (#5151). The quantized path still writes residual_out in bf16, and that feeds the next layer. We chained 56 calls (hidden 2048, synthetic inputs), feeding residual_out back. Residual-stream L2 norm after 56 calls, relative to a torch RNE reference:

path trunc this PR
fused add −11.3% 0.00%
fused add + dynamic quant (i8 / fp8) −11.3% 0.00%
fused add + group-128 fp8 quant −11.3% 0.00%

Gating would avoid the RNE overhead on the quant kernels at decode. On aiter main at M ≤ 256, that overhead is +13–49% for i8 / fp8 dynamic quant and +10–27% for group-128 fp8. But FP8 / INT8 models would keep the drift. The bf16 path, which pays more (+22–108%), needs RNE either way. I'd keep RNE on all paths.

Recovering the kernel cost (follow-up). The bf16 conversion in opus.hpp has a round-to-nearest-even branch (rm == 0). Switching it to the compiler's conversion (static_cast<bf16_t>) gives bit-identical output. We checked torch.equal on 108 outputs: norm, fused add, and i8 / fp8 quant, through both modules, including inf / nan / subnormal inputs.

On the HIP path it removes most of the cost: fused add at 1 × 8192 drops from +108% to +32%, and norm at 1 × 4096 from +20% to −1%. On the opus kernels (hidden > 8192) the result is mixed:

  • Better at M = 1 and 256: norm at 1 × 16384 goes from +63% to +24%.
  • Worse at M = 8, 32, 128: norm goes from +39–42% to +56–60%, and the quant opus kernels regress too.

It needs per-kernel work and touches a shared helper, so I'd do it in a separate PR once this one lands.

@junhaha666

Copy link
Copy Markdown
Contributor

Helllo, could you help to test #6300 on top of this PR to check whether rollout-versus-trainer accuracy meets your requirements?
It reduces RNE overhead on MI308X (gfx942), with 18.9% lower RMSNorm latency and 27.3% lower fused-add latency on average for the HIP path. Our finite-input tests are bit-identical to the original RNE, though NaN handling differs. Please rebuild the affected modules before testing.

@junhaha666

Copy link
Copy Markdown
Contributor

PR #6300 has already been merged and addresses the BF16 truncation issue discussed here by switching the affected RMSNorm and residual-add paths to round-to-nearest-even (RNE). It also introduces a faster software RNE conversion to reduce the performance overhead compared with the original RNE implementation. On the common HIP path, the measured overhead relative to truncation is approximately 0–7.1% for RMSNorm and 0–11.0% for fused residual-add. Since the fix has landed in #6300, I’ll close this PR for now. Please rebuild the affected JIT modules with AITER_REBUILD=1 before testing.

@junhaha666 junhaha666 closed this Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants