Skip to content

[OPUS] [JIT] Default RMSNorm BF16 stores to optimized RNE - #6300

Merged
valarLip merged 3 commits into
mainfrom
jun/opus_f32tobf16
Oct 13, 2026
Merged

valarLip merged 3 commits into
mainfrom
jun/opus_f32tobf16

Conversation

@junhaha666

@junhaha666 junhaha666 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

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_DEFAULT selection. Mode 0 keeps the original software RNE implementation and its legacy NaN behavior. Existing mode numbers are unchanged. The separate OPUS_FP32_to_BF16_RNE_IMPL selector is removed.

RMSNorm now defaults to mode 5 in these three JIT modules:

Module Use
module_rmsnorm OPUS RMSNorm, including larger hidden sizes and model-sensitive paths
module_rmsnorm_quant Common HIP RMSNorm and residual-add paths, and quantization
module_fused_qk_rmsnorm_group_quant Fused Q/K RMSNorm with group/per-token quantization and BF16 outputs

The standalone rmsnorm_opus_kernel.hpp also defaults to mode 5. The shared opus.hpp default 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:

# Default optimized RNE
AITER_REBUILD=1 python your_script.py

# Original software RNE
OPUS_FP32_to_BF16_DEFAULT=0 AITER_REBUILD=1 python your_script.py

# Previous truncation behavior
OPUS_FP32_to_BF16_DEFAULT=2 AITER_REBUILD=1 python your_script.py

# Explicit optimized RNE
OPUS_FP32_to_BF16_DEFAULT=5 AITER_REBUILD=1 python your_script.py

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-math probe, 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:

  • Standalone GPU conversion probe: 1,327,680 bit patterns, covering every BF16 prefix with lower-bit values 0, 0x7fff, 0x8000, 0x8001 and 0xffff, plus one million random patterns. Modes 0, 2 and 5 matched their bit-level references under strict compilation, including ties, NaNs and infinities.
  • BF16 test_rmsnorm2d.py, 32x4096: residual-add, model-sensitive/T5 and Gemma checks passed with freshly compiled mode-5 modules.
  • FP8 residual-add group quantization, 32x4096, group size 128: modes 0 and 5 produced bit-identical quantized output, residual and scales on the same finite inputs. The existing test reported about 3.1% of quantized outputs outside its strict PyTorch comparison tolerance; this comparison does not establish exact PyTorch equivalence.
  • Fused QK group quantization, M=32, Q/K widths 1536/512: residual and non-residual cases, with and without quantization. HIP and Triton outputs agreed; BF16 output/residual checks passed. Quantized comparisons against PyTorch reported small differences with identical HIP/Triton error rates.
  • gfx950 hardware-path compilation passed. JIT flags were checked for default 5 and overrides 0/2/5 in all three modules. git diff --check passed.

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_add measure the common HIP path for 2D BF16, hidden <=8192, non-model-sensitive mode. opus_norm / opus_add explicitly measure the OPUS path; public dispatch uses OPUS for larger hidden sizes and model-sensitive cases. hip_add_fp8 uses 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.

Case Original truncate (us) Fast RNE (us) Latency increase Original RNE (us) Fast vs original RNE
hip_add:1x4096 2.617 2.701 +3.2% 4.516 -40.2%
hip_add:1x8192 3.156 3.504 +11.0% 7.024 -50.1%
hip_add:2048x4096 25.549 25.561 +0.0% 31.116 -17.9%
hip_add:2048x8192 48.274 48.692 +0.9% 59.356 -18.0%
hip_add:256x4096 4.954 5.163 +4.2% 8.017 -35.6%
hip_add:256x8192 8.605 8.601 -0.0% 13.995 -38.5%
hip_add:32x4096 2.807 2.932 +4.5% 4.726 -38.0%
hip_add:32x8192 3.636 3.852 +5.9% 7.387 -47.9%
hip_add_fp8:1x4096 3.323 3.416 +2.8% 4.365 -21.7%
hip_add_fp8:1x8192 3.981 4.136 +3.9% 5.202 -20.5%
hip_add_fp8:2048x4096 24.161 24.101 -0.2% 32.204 -25.2%
hip_add_fp8:2048x8192 44.753 44.763 +0.0% 59.086 -24.2%
hip_add_fp8:256x4096 6.540 6.770 +3.5% 8.310 -18.5%
hip_add_fp8:256x8192 10.514 10.633 +1.1% 12.712 -16.4%
hip_add_fp8:32x4096 3.594 3.711 +3.3% 4.632 -19.9%
hip_add_fp8:32x8192 4.368 4.488 +2.8% 5.627 -20.2%
hip_norm:1x4096 2.520 2.516 -0.1% 3.323 -24.3%
hip_norm:1x8192 2.690 2.825 +5.0% 4.534 -37.7%
hip_norm:2048x4096 14.983 15.418 +2.9% 20.462 -24.6%
hip_norm:2048x8192 27.448 27.613 +0.6% 33.841 -18.4%
hip_norm:256x4096 4.059 4.346 +7.1% 5.678 -23.5%
hip_norm:256x8192 5.518 5.809 +5.3% 8.406 -30.9%
hip_norm:32x4096 2.675 2.703 +1.0% 3.517 -23.2%
hip_norm:32x8192 3.135 3.167 +1.0% 4.896 -35.3%
opus_add:1x16384 11.136 14.288 +28.3% 20.490 -30.3%
opus_add:1x4096 4.891 5.588 +14.2% 7.602 -26.5%
opus_add:1x8192 6.588 8.057 +22.3% 11.990 -32.8%
opus_add:2048x16384 88.537 128.524 +45.2% 132.860 -3.3%
opus_add:2048x4096 30.730 39.938 +30.0% 43.843 -8.9%
opus_add:2048x8192 48.020 66.503 +38.5% 73.551 -9.6%
opus_add:256x16384 19.668 27.263 +38.6% 29.965 -9.0%
opus_add:256x4096 7.091 8.862 +25.0% 10.256 -13.6%
opus_add:256x8192 10.386 14.335 +38.0% 16.881 -15.1%
opus_add:32x16384 16.796 24.419 +45.4% 27.208 -10.3%
opus_add:32x4096 6.803 8.609 +26.6% 9.828 -12.4%
opus_add:32x8192 9.720 13.421 +38.1% 15.703 -14.5%
opus_norm:1x16384 8.381 10.170 +21.3% 13.846 -26.5%
opus_norm:1x4096 4.371 4.480 +2.5% 5.849 -23.4%
opus_norm:1x8192 5.284 5.959 +12.8% 8.484 -29.8%
opus_norm:2048x16384 61.916 83.662 +35.1% 85.791 -2.5%
opus_norm:2048x4096 23.673 29.592 +25.0% 30.798 -3.9%
opus_norm:2048x8192 35.147 43.990 +25.2% 49.017 -10.3%
opus_norm:256x16384 13.731 16.694 +21.6% 18.780 -11.1%
opus_norm:256x4096 6.128 6.824 +11.4% 7.532 -9.4%
opus_norm:256x8192 7.867 9.046 +15.0% 10.952 -17.4%
opus_norm:32x16384 12.115 15.132 +24.9% 17.259 -12.3%
opus_norm:32x4096 5.930 6.305 +6.3% 7.265 -13.2%
opus_norm:32x8192 7.612 8.747 +14.9% 10.512 -16.8%
qk_add_fp8:1x1536 3.184 3.352 +5.3% - -
qk_add_fp8:2048x1536 19.708 20.019 +1.6% - -
qk_add_fp8:256x1536 4.628 4.937 +6.7% - -
qk_add_fp8:32x1536 3.432 3.598 +4.9% - -
qk_add_noquant:1x1536 2.773 2.919 +5.3% - -
qk_add_noquant:2048x1536 17.435 17.797 +2.1% - -
qk_add_noquant:256x1536 4.448 4.425 -0.5% - -
qk_add_noquant:32x1536 2.959 3.160 +6.8% - -

@junhaha666
junhaha666 requested a review from a team October 9, 2026 08:50
@github-actions github-actions Bot added the OPUS label Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 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
ci:multi-gpu 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 6300 --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.

@junhaha666 junhaha666 changed the title [OPUS] Reduce software FP32-to-BF16 RNE overhead on gfx942 [OPUS] Reduce software FP32-to-BF16 RNE overhead on MI308X (gfx942) Oct 9, 2026
@junhaha666 junhaha666 changed the title [OPUS] Reduce software FP32-to-BF16 RNE overhead on MI308X (gfx942) [OPUS] Reduce software FP32-to-BF16 RNE overhead on gfx942 Oct 9, 2026
@zufayu
zufayu self-requested a review October 10, 2026 01:39
@zufayu zufayu self-assigned this Oct 10, 2026
zufayu
zufayu previously approved these changes Oct 10, 2026
@zufayu
zufayu marked this pull request as draft October 10, 2026 05:58
@zufayu
zufayu marked this pull request as ready for review October 10, 2026 05:59
@junhaha666 junhaha666 changed the title [OPUS] Reduce software FP32-to-BF16 RNE overhead on gfx942 [OPUS] [JIT] Default RMSNorm BF16 stores to optimized RNE Oct 12, 2026
@github-actions github-actions Bot added the JIT label Oct 12, 2026
@zufayu

zufayu commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 1 review(s); expect to start in about 50 min. The box reviews one PR at a time.

@aiter-bot

Copy link
Copy Markdown

ROCm/aiter PR #6300 — [OPUS] [JIT] Default RMSNorm BF16 stores to optimized RNE

Flips 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): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — the PR changes runtime code that executes at run time (the rmsnorm kernels every model calls) but ships no test target for the validator to run; the missing target is reported as a finding below.
Perf (advisory): NOT RUN — the PR ships no benchmark target for the perf stage and this box has no torch/aiter environment to hand-run one; the PR's own same-box table (7 samples per case, median of 3 runs, MI308X/ROCm 7.2.4) reports the new default at +2.5–45.4% kernel latency vs the previous truncation default on gfx942.

⚠️ [verified] The default flip changes bit-exact numerics for every bf16 RMSNorm call on gfx942 — module_rmsnorm is the sole OPUS rmsnorm backend and module_rmsnorm_quant serves the common bf16/fp16 hidden≤8192 2-D case (aiter/ops/rmsnorm.py:388,408), both redefined in csrc/include/opus/rmsnorm_opus_kernel.hpp and aiter/jit/optCompilerConfig.json — while the PR's own table prices the switch at +14.2–45.4% kernel latency on the OPUS add path and up to +11.0% on the common HIP add path, and the PR itself states model-level logprob equivalence was not established; a default numeric-semantics change on a path every downstream model family calls needs the path owner's explicit sign-off, not CI alone. Author must get sign-off from the de-facto owner of the changed headers (@carlushuang, top committer of csrc/include/opus/opus.hpp and sole committer of csrc/include/opus/rmsnorm_opus_kernel.hpp) with the latency cost stated, and provide model-level accuracy evidence for the default change — or keep truncation as the default and let deployments opt into RNE with OPUS_FP32_to_BF16_DEFAULT=5.
⚠️ [verified] No in-repo test can fail on a rounding-mode regression: the PR ships no test, and the existing checks (op_tests/test_rmsnorm2d.py:94 atol=0.03; op_tests/test_rmsnorm2dFusedAddQuant.py:249 rtol=0, atol=1) pass under the old truncation default too, so the mode-5 contract added to csrc/include/opus/opus.hpp is pinned only by the author's uncommitted 1,327,680-pattern probe — a later edit that breaks mode 5 (a wrong rounding bias, or fast-math eating the NaN select) would pass CI silently. Author must add an in-repo bit-exactness test (mode 5 vs mode 0 over ties, subnormals and NaNs, or vs torch's RNE) so the new default is falsifiable.
📝 [verified] Existing deployments silently keep the biased truncation after upgrade: aiter/jit/core.py:2102 loads cached module .so files by stable name and only builds on ModuleNotFoundError, so the new default in aiter/jit/optCompilerConfig.json is a no-op for warm JIT caches until AITER_REBUILD=1 or a fresh AITER_JIT_DIR, with no signal that the fix did not apply. Reviewer should ask for the rebuild requirement to be carried in the release notes so upgrading users know the default does not take effect without a rebuild.
📝 [verified] The residual-stream round-toward-zero bias this PR removes persists outside RMSNorm: csrc/kernels/mhc_kernels.cu:3079 still stores its bf16 next-residual through store_vector → opus::cast → fp32_to_bf16 with the truncating default that csrc/include/opus/opus.hpp:41 leaves at mode 2, so a model mixing mhc and rmsnorm residual paths gets mixed rounding after this PR. Reviewer should ask whether mhc's bf16 residual stores are in scope for the same RNE treatment, or confirm the truncation there is intentional.
📝 [verified] The PR body's claims diverge from the net diff in two places: "The separate OPUS_FP32_to_BF16_RNE_IMPL selector is removed" describes nothing (grep of merge target and head finds no such selector in either tree), and while the body discloses that fast-math builds may drop mode-5 NaN handling, it does not say that two of the three redefined modules in aiter/jit/optCompilerConfig.json (module_rmsnorm_quant, module_fused_qk_rmsnorm_group_quant) compile with -ffast-math — so on the common path a NaN input may no longer propagate as NaN. Author must correct the description and state the NaN-propagation behavior for the fast-math modules.

@valarLip
valarLip merged commit 4f925b7 into main Oct 13, 2026
@valarLip
valarLip deleted the jun/opus_f32tobf16 branch October 13, 2026 09:12
@zufayu

zufayu commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

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:

@aiter-bot review

  • Reviews the PR at its current head. Comment again after pushing fixes for a fresh pass; a repeat on the same commit is deduped.
  • One review runs at a time on one box, roughly 20 minutes. If work is ahead of you the bot replies with your place in the queue, and if the runner is down it says so rather than leaving you waiting for a review that will never start.
  • The card is advisory. It never blocks a merge, 🔴 included — it is there to be argued with.

Feedback on the review itself is welcome; it is how the rules get better.

vgokhale pushed a commit that referenced this pull request Oct 19, 2026
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]>
junhaha666 pushed a commit that referenced this pull request Dec 1, 2026
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]>
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.

4 participants