Repository navigation
[HIP] [JIT] [Bugfix] Round mHC outputs to nearest even on gfx942 - #8653
Conversation
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 ROCm#6300 did for RMSNorm and ROCm#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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
|
@aiter-bot review |
|
aiter-bot — queued behind 3 review(s); expect to start in about 52 min. The box reviews one PR at a time. |
aiter-bot
left a comment
There was a problem hiding this comment.
ROCm/aiter PR #8653 — [HIP] [JIT] [Bugfix] Round mHC outputs to nearest even on gfx942
Review (advisory):
Validation (deterministic): NOT RUN — REVIEW_AUTO_VALIDATE=0 and no GPU access in this review session (/dev/kfd permission denied); triage found 2 candidate targets (op_tests/test_mhc.py, op_tests/test_mhc_seam.py) and picked none — environment gap, not a PR defect
Perf (advisory): NOT RUN — no GPU access in this session; the PR's own number is head-only (50.3 us, m=2048 hidden 5120, beside a loaded server) and cannot show a regression
Size: 101 lines / 6 files
Design (advisory): fix the rounding mode at the compile-flag level for the two CUDA mHC modules while leaving the prebuilt gfx942 ASM seam truncating, accepting a documented ≤1-ulp fused-vs-unfused residual_out divergence until the seam kernels are rebuilt
Human: recommend Andreas Karatzas review whether the mega_mhc gfx942 H=5120 bf16 route through the truncating ASM seam (aiter/ops/mega_mhc.py:562 → _mega_mhc_asm) keeps PR #8653's residual-bias fix off the DeepSeek-V4.1 serving path on MI300X, and when the seam kernels get the same rounding
aiter/op_tests/test_mhc_seam.py
Line 826 in d9861a4
📝 [verified] The JIT .so cache is keyed by module filename only (aiter/jit/core.py:2015), so the OPUS_FP32_to_BF16_DEFAULT value aiter/jit/optCompilerConfig.json evaluates is in no cache key and the PR body's revert path ("=2 restores the old truncation") silently reuses the cached RNE .so on a warm cache — the same exposure #6300/#6640 shipped. Reviewer should ask the author to state the cache-clear requirement next to the env override.
|
Updated the body with the requested data. No code change: the only code-level issue was the body's revert claim. Perf (base vs head, MI300X gfx942, idle card 0c:00.0, H=5120, hc=4, PR-base kernel, kernel-only CUDA-graph timing, A/A <=3.2 %): Path: SGLang main on gfx942 calls Cache: confirmed |
|
@junhaha666 @zufayu — the three author items from the aiter-bot review of
Remaining reviewer side, per the same review: the default PR CI builds gfx950 |
Summary
Build
module_mhcandmodule_fused_ar_mhcwith-DOPUS_FP32_to_BF16_DEFAULT=5, so their FP32 -> BF16 conversions round to nearest even on gfx942 instead of truncating.test_mhc_postgains a check that catches truncation, andtest_mhc_seam.pychecks the gfx942 ASM seam (which keeps truncating) against the truncation ofmhc_post's value instead of bit-exactness withmhc_post.Motivation
On gfx942, every BF16 value the mHC kernels write is rounded toward zero.
mhc_kernels.cuandmhc_post_pre_gram.cuconvert withopus::cast, and on architectures without a hardware BF16 convert that cast usesOPUS_FP32_to_BF16_DEFAULT, which defaults to 2, truncation (csrc/include/opus/opus.hpp). The mega_mhc modules already pass=5; the two modules that buildmhc_kernels.cudo not.Truncation stays inside
test_mhc.py's tolerance, but it is a bias, not noise:mhc_postshrinks the residual streams by 0.3-0.4 % per call. DeepSeek-V4.1 runsmhc_posttwice per layer (80 calls per forward), the shrink compounds, and on MI300X the model's teacher-forced NLL sits 0.1-0.3 nat above the same checkpoint on MI355X. With this change the two match.The same bias was fixed for RMSNorm in #6300 and for the custom all-reduce in #6640, with the same flag and env override.
Which MI300X DeepSeek-V4.1 path this reaches
The NLL numbers above were measured on the SGLang path, which is
module_mhcon gfx942. The prebuilt gfx942 ASM seam is a different path and this PR does not change it:module_mhc.deepseek_v4_mhc.post()falls through toaiter.ops.mhc.mhc_poston HIP (python/sglang/srt/models/deepseek_v4_mhc.py:698-729), and the fused attn/MoE boundary callsaiter.ops.mhc.mhc_fused_post_pre(deepseek_v4_fused_mhc.py:300-328). Both aremodule_mhckernels. SGLang'smega_mhcpath requires_is_gfx95_supportedand is off by default, so gfx942 never takes it (deepseek_v4_mhc.py:789-800,environ.py:2173). The service logs of the two cx57-4 rows (cards 0-3 and 4-7) showmodule_mhcimported and nomodule_mhc_seam_asm/mega_mhcreference.rocm_aiter_ops.is_mega_mhc_available()is CDNA4-only (vllm/_aiter_ops.py:2974-2983on origin/main78f20a5a3,vllm/platforms/rocm.py:374-383), so the gfx942 DSV4.1 path does not takemega_mhc(vllm/models/deepseek_v41/amd/model.py:334-339).atom/model_ops/deepseek_v41/mhc_pre_delayed.py:54-60,214-218usesmega_mhcfor a seam with a post whenevermhc_fused_post_pre_delayed_asm_supported(H=5120, hc=4)holds andATOM_DSV41_MHC_BACKEND != "triton"(the default isauto; its unit test expectsauto->mega). That runs_mega_mhc_asm(aiter/ops/mega_mhc.py:561-569), whoseresidual_outstill truncates. ATOM is therefore the one serving stack on MI300X where the residual-bias fix does not reach the model. Follow-up: rebuild thehsa/gfx942/mhc_seamcode objects with RNE forresidual_outand restore the bit-exact seam test; until then ATOM can run withATOM_DSV41_MHC_BACKEND=triton. I will file/track that separately; it is not part of this PR.Changes
aiter/jit/optCompilerConfig.json:module_mhc(mhc_kernels.cu,mhc_post_pre_gram.cu) andmodule_fused_ar_mhcget-DOPUS_FP32_to_BF16_DEFAULT={OPUS_FP32_to_BF16_DEFAULT, default 5}, exactly as the mega_mhc modules.op_tests/test_mhc.py:mhc_post_ref_fp32is split out ofmhc_post_ref;test_mhc_postasserts that the signed rounding error relative to the FP32 result is at most 5e-4 and reports it asround_bias. Truncation gives about -2.8e-3; round to nearest gives 0 with a spread of 5e-5 at the smallest case (m=1, hidden=512).op_tests/test_mhc_seam.py: the gfx942 ASM seam (hsa/gfx942/mhc_seam, prebuilt) keeps its truncatingresidual_out, which the test asserted bit-exact againstmhc_post. It now checks that every element is the truncation ofmhc_post's value (equal, or one BF16 ulp smaller in magnitude, same sign), and checkslayer_inputand the gates against the seam's ownresidual_out, which is what the kernels compute them from. Its docstrings andasm_mhc_seam.cu's header say so. Making the seam round too needs its kernels rebuilt, which this PR does not do.mhc_pre's outputs,mhc_post_pre's next residual and BF16 GEMM operands inmhc_kernels.cu/mhc_post_pre_gram.cu, and the all-reduce stores built intomodule_fused_ar_mhc(consistent withmodule_custom_all_reducesince [JIT] Round custom all-reduce BF16 stores to nearest even #6640). Comments inmhc_kernels.cuthat said these stores truncate are updated.Performance (if applicable)
Measured on an idle MI300X (gfx942, BDF
0000:0c:00.0/renderD128, rowcx57-4:32300taken out of service and restored), hidden 5120, hc 4. The PR-basemhc_kernels.cu(024bf6b8...) was compiled twice with-DOPUS_FP32_to_BF16_DEFAULT=2(base) and=5(head); the rest of the flags, compiler and source are identical. PR head differs from base only in comments in that file (git diff ab0223ec4..d9861a4a5 -- csrc/kernels/mhc_kernels.cu).mhc_postand 7.8 % formhc_post_reduce(one outlier; typical <2 %). Two independent runs (rebuild, and prebuilt.sore-run) agree within 2.1 percentage points exceptmhc_post_reduceat m=8192 (+11.1 % vs +15.7 %); both show the same direction.-2.81e-3, head-4.8e-6, confirming the flag actually took effect.mhc_postmhc_postmhc_postmhc_postmhc_postmhc_post_reducemhc_post_reducemhc_post_reducemhc_post_reducemhc_post_reduceCold single-launch deltas at m = 1 / 64 / 512 / 2048 / 8192 (includes launch and L2-miss time, so the relative cost is compressed):
mhc_post-1.3 / -1.5 / +7.2 / +1.2 / +0.5 %;mhc_post_reduce-1.1 / -2.1 / +7.3 / +10.0 / +8.0 %.RNE is not free: the fast software RNE adds three integer ops per element, and the kernel is not fully bandwidth-bound at decode/mid-size m. At m=2048 this is +3.3 us per
mhc_post; at 80 posts per forward that is an upper bound of +0.27 ms per forward, and end-to-end impact was not measured here. The previous head-only 50.3 us number was taken beside a loaded server and is replaced by this table.Testing
All on MI300X (gfx942):
test_mhc.py(whole file, default sweep)test_mhc_seam.py(whole file)multigpu_tests/test_fused_ar_mhc_post_only.py -t 2v_bfe_u32+v_add3_u32) permhc_post_kernelinstantiation, main'smhc_kernels.cubuilt for gfx942 withmodule_mhc's flags (28 instantiations)mhc_poston one DeepSeek-V4.1 layer's real residual (T = 2000): scale of output vs FP32 resultCI note: the default PR CI runs on gfx950 only (
.github/workflows/aiter-test.yaml:22), where this change is a no-op, andop_tests/test_mhc_seam.py:826skips off gfx942. Without labels this fix and its new check merge with no CI exercise. A maintainer should addci:mi300x(andci:multi-gpuformodule_fused_ar_mhc) before merge. The multi-GPU test is author-verified at TP2 only; TP4/8 remain unverified.Documentation
A compile flag and a test check; no API or behavior change beyond the rounding mode.
Dependencies
Breaking Changes
None for a default build.
OPUS_FP32_to_BF16_DEFAULT=2requests the old truncation, but it only takes effect on a fresh module build: the JIT.socache is keyed by module name only (aiter/jit/core.py:2015,:1319; build target:1488), so a warm cache reuses the RNE.soand the override silently does nothing. Before switching, removemodule_mhc.so/module_fused_ar_mhc.soand theirbuild/directories, or setAITER_REBUILD=1for the next import. Verified on gfx942: env=5 fresh build bias -4.8e-6; env=2 on the same cache bias -4.8e-6 with the same.somtime; env=2 after clearing the cache bias -2.8e-3.PR Self-Review
Reviewed revision
d9861a4
Behavior and intent
On gfx942 the mHC modules' BF16 outputs change from truncation to round-to-nearest-even; nothing changes on architectures with a hardware BF16 convert. Truncation is a systematic shrink of the residual streams that compounds across layers.
Validation and unverified scope
Validated as in Testing. All op tests ran on MI300X on aiter 8c964fa from a ROCm SGLang image (same opus default, module flags and mHC test files as main, with this PR's flag and test changes applied), because main's
import aiterin that image needs a newer Triton (gl.amd.cdna5); main's ownmhc_kernels.cuwas checked at the ISA level. The perf table above is a separate build of the PR-base kernel source with the two flag values on an idle MI300X card. Remains unverified: a run of main's package on gfx942 (CI covers it), MI250X,module_fused_ar_mhcat TP 4/8, the gfx942 ASM seam with rounding (out of scope; ATOM affected), end-to-end serving impact of the RNE cost, and the other JIT modules that compile opus without the flag (not audited).PR-specific question
Which architecture, dtype and shape boundaries select the changed path: only the BF16 conversions compiled into
module_mhcandmodule_fused_ar_mhcon architectures without a hardware BF16 convert (gfx942 and older), because opus usesOPUS_FP32_to_BF16_DEFAULTonly there; gfx950, gfx12 and gfx1250 take the hardware conversion and ignore the mode (csrc/include/opus/opus.hpp,fp32_to_bf16). FP16 outputs are unaffected. No shape boundary changes: every shape these modules run takes the same conversion. The invariant is the compile flag itself (the same one #6300 and #6640 use), with the env overrideOPUS_FP32_to_BF16_DEFAULTfor a build that wants the old truncation; because the JIT cache is keyed by module name only, the override requires clearing the module.so/build dir (orAITER_REBUILD=1) first. The new test check is skipped for such a build; it cannot tell a fresh truncating build from a stale RNE one when the env asks for truncation. The prebuilt gfx942 ASM seam is not rebuilt and keeps truncating; its test now states that contract.Changed op tests
op_tests/test_mhc.py:test_mhc_postnow asserts the sign of the rounding error (BF16, non-truncating builds). The existing tolerance check cannot see truncation, so this is the only check that would catch the bug fixed here.op_tests/test_mhc_seam.py: the seam'sresidual_outcontract changes from "bit-exact withmhc_post" to "the truncation ofmhc_post's value", becausemhc_postnow rounds and the prebuilt seam kernels do not; without this the gfx942 seam test would fail.