Repository navigation
[Bugfix] Apply scale_a and scale_b in BF16 tgemm.mm instead of ignoring them - #7884
amd-bartgips wants to merge 1 commit into
Conversation
…ng them tgemm.mm with BF16/FP16 inputs and scale_a/scale_b returned the unscaled product: torch_gemm applied scales only for FP8, and skinny_gemm and asm_gemm accepted them without using them. No tuned BF16 row has scaleAB, so every such call took one of those three. torch_gemm now scales BF16/FP16 in the FP32 accumulator, the skinny default is skipped for scaled calls, and skinny_gemm and asm_gemm refuse scales like the FlyDSL, OPUS and Triton backends already do.
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
Summary
tgemm.mm(andgemm_a16w16) with BF16 or FP16 inputs acceptsscale_aandscale_bbut returns the unscaled product, without an error. This PR applies the scales in the torch path, where every such call lands. The skinny and ASM backends cannot apply scales, so they now refuse them, as FlyDSL, OPUS and Triton already do.Motivation
On unpatched
main(MI355X),tgemm.mm(a, b, scale_a=2, scale_b=3)with BF16 inputs. Ratio is the median of out / (a @ b.T); it should be 6:asm_gemm does not support scalingWhy each backend gets it wrong:
torch_gemmapplies scales only for FP8.skinny_gemmandasm_gemmaccept the arguments and never use them.No shipped BF16 tuned row sets
scaleAB, so a scaled BF16 call always takes the untuned default (skinny, ASM for preshuffled weights on gfx950, otherwise torch). No in-tree or vLLM caller passes BF16 scales today, so the bug is latent.Changes
aiter/tuned_gemm.py:torch_gemmsends every scaled call to_torch_scaled_gemm, which now also handles BF16/FP16. It does an FP32torch.mm, multiplies byscale_a * scale_b, then casts and adds bias, in the same order as the FP8 path. FP8 output is bit-identical before and after.skinny_gemmandasm_gemmassert that no scale is passed.not scaleAB, so a scaled skinny shape goes to torch instead of hitting the assert.op_tests/test_gemm_a16w16.py:test_gemm_scaled(-t scaled, added to the default run that CI uses) covers 5 shapes (skinny, bias, FP32 output, FP16), with both scales and withscale_aalone, and checks that skinny and ASM refuse scales.Unscaled calls take the same path as before. A scaled call now pays for the scaling: at 4096 x 8192 x 8192 it takes 464 us against 338 us unscaled, because of the FP32 output, one in-place multiply and a cast.
Other GEMM families
tgemm.mm: already correct. The torch path and hipBLASLt apply the scales, and skinny and ASM, which dropped them, now refuse them.gemm_a4w4silently dropsbias,alphaandbetawhen a tuned row picks its CK blockscale kernel (aiter/ops/gemm_op_a4w4.py), and its gfx1250 path warns and ignores them.Testing
Documentation
Dependencies
Breaking Changes
Merge note: this PR and #7883 each add a name to the default
-tlist inop_tests/test_gemm_a16w16.py, so whichever merges later needs a one-line merge.PR Self-Review
Reviewed revision
5855e8a
Behavior and intent
BF16/FP16
tgemm.mmhonoursscale_aandscale_bon the default path, and the two backends that cannot apply them refuse them instead of silently dropping them. Unscaled calls and FP8 calls are unchanged.Validation and unverified scope
MI355X (gfx950), this branch on
main8b13d52:python3 op_tests/test_gemm_a16w16.py(default-t normal scaled): passes.mainagainst this branch, same script and data.tgemm.mmwith scales at 1 x 4096 x 4096 and 512 x 4096 x 4096: identical output before and after.Unverified: per-row and per-column scales (
[M, 1],[1, N]) broadcast in the same expression as FP8's, but the test uses per-tensor scales only. gfx942 and gfx1250 take the sametorch_gemmpath but were not run on that hardware.PR-specific question
What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
On
main,test_gemm_scaledfails at its first shape: the result is 1/6 of the reference. The existing-sa/-sboptions oftest_gemmare never set in CI. With BF16 they also run ASM and Triton, which ignore or refuse scales, so they cannot serve as this check.Changed op tests
op_tests/test_gemm_a16w16.py: new-t scaledcheck, added to the default-tlist so the CIpython3run executes it.