Skip to content

[JIT] Round custom all-reduce BF16 stores to nearest even - #6640

Merged
vgokhale merged 1 commit into
ROCm:mainfrom
AndreasKaratzas:akaratza/custom-ar-bf16-rne
Oct 19, 2026
Merged

vgokhale merged 1 commit into
ROCm:mainfrom
AndreasKaratzas:akaratza/custom-ar-bf16-rne

Conversation

@AndreasKaratzas

Copy link
Copy Markdown
Contributor

Motivation

On gfx942, every bf16 value that module_custom_all_reduce writes 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_out and 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 truncated residual_out then 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_fusion in vllm/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 2 on MI300X:

import torch, torch.distributed as dist
from aiter.dist.device_communicators.custom_all_reduce import CustomAllreduce

dist.init_process_group("nccl")
r = dist.get_rank()
torch.cuda.set_device(r)
ca = CustomAllreduce(dist.new_group(backend="gloo"), torch.device(f"cuda:{r}"))
x = torch.full((4096,), [1.0078125, 0.0048828125][r], device="cuda", dtype=torch.bfloat16)
y = ca.all_reduce(x)  # exact sum 1.0126953125; nearest bf16 is 1.015625
if r == 0:
    print(y[0].item())  # main: 1.0078125, this PR: 1.015625
ca.close()
dist.destroy_process_group()

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 uses OPUS_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 not module_custom_all_reduce.

This PR adds the same flag and env override to module_custom_all_reduce in aiter/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_reduce rebuilt 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:

    • plain all-reduce at 1 KB to 64 MB, which covers the LL, LL128 and 1-stage kernels;
    • fused all-reduce + RMSNorm, 1-stage and 2-stage, hidden 4096 and 8192, 1 to 2048 tokens.

    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 --regression and op_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:

    • gsm8k 5-shot, all 1319 questions, seed 1234, 2 runs per arm;
    • vllm bench latency 512/256;
    • vllm bench serve on random 1024/256 data, 3 interleaved runs per arm, with fusion on and off.

Test Result

Bit-exactness:

output main == RNE this PR == RNE
all-reduce, 1 KB - 64 MB 73-75% (100% == truncation) 100%
fused residual_out 59% 100%
fused normed output 48% 99.999% (the rest are 1 ulp off, from rsqrt and reduction order)

The 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, the fused_err at (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:

build run 1 run 2
unfused (all-reduce, then RMSNorm), main 0.7036 0.7028
fused, main 0.6649 0.6725
fused, this PR 0.7043 (p = 7.0e-6) 0.6990 (p = 1.2e-3)

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:

case main this PR ratio
all-reduce 1 KB, graph 3.15 (3.12-3.33) 3.20 (3.17-3.46) 1.016
all-reduce 256 KB, graph 9.82 (9.79-9.98) 10.15 (10.02-10.35) 1.034
all-reduce 1 MB, kernel 29.29 (28.42-140.62) 29.30 (28.73-193.20) 1.000
all-reduce 64 MB, kernel 1421.28 (1406.25-1948.95) 1445.91 (1437.51-2882.34) 1.017
fused 1-stage, 1 tok x 4096, graph 5.96 (5.93-6.14) 6.55 (6.50-6.58) 1.099
fused 1-stage, 64 tok x 4096, graph 16.95 (16.93-16.98) 17.55 (17.38-17.69) 1.035
fused 2-stage, 256 tok x 4096, kernel 58.94 (57.29-88.54) 59.71 (55.97-113.27) 1.013
fused 2-stage, 8192 tok x 4096, kernel 1508.11 (1499.57-2461.07) 1517.90 (1513.19-4043.66) 1.006

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):
main this PR
fused, 400 prompts, rate inf 5623 (5375-5666) 5477 (5004-5611)
fused, concurrency 4 884 (880-887) 873 (872-888)
fused, concurrency 32 3054 (2710-3176) 3243 (3224-3247)
unfused, 400 prompts, rate inf 5678 (5504-5714) 5378 (4788-5684)
unfused, concurrency 4 886 (883-887) 885 (881-887)
unfused, concurrency 32 3229 (3208-3240) 3219 (3204-3237)

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:

  • gfx950/MI355. It uses the hardware convert and should not be affected, but I have not run it.
  • Models other than Llama-3.1-8B, and TP > 2.

Submission Checklist

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]>
@github-actions

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:windows Windows JIT op smoke test on the gfx1201 ROCm runner
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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 6640 --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.

@TennyWang1223 TennyWang1223 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ChuanLi1101 ChuanLi1101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vgokhale
vgokhale merged commit 50786a8 into ROCm:main Oct 19, 2026
@AndreasKaratzas
AndreasKaratzas deleted the akaratza/custom-ar-bf16-rne branch October 19, 2026 21:20
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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants