Skip to content

[Bugfix] Apply scale_a and scale_b in BF16 tgemm.mm instead of ignoring them - #7884

Draft
amd-bartgips wants to merge 1 commit into
mainfrom
users/bartgips/fix-tgemm-ignored-scales
Draft

amd-bartgips wants to merge 1 commit into
mainfrom
users/bartgips/fix-tgemm-ignored-scales

Conversation

@amd-bartgips

@amd-bartgips amd-bartgips commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

tgemm.mm (and gemm_a16w16) with BF16 or FP16 inputs accepts scale_a and scale_b but 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:

Shape (M, N, K) Weight Backend Before After
1, 256, 4096 plain skinny (after: torch) 1.0 6.0
512, 4096, 4096 plain torch 1.0 6.0
512, 4096, 4096 preshuffled asm 1.0 raises asm_gemm does not support scaling

Why each backend gets it wrong:

  • torch_gemm applies scales only for FP8.
  • skinny_gemm and asm_gemm accept 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_gemm sends every scaled call to _torch_scaled_gemm, which now also handles BF16/FP16. It does an FP32 torch.mm, multiplies by scale_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_gemm and asm_gemm assert that no scale is passed.
  • The untuned skinny default requires 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 with scale_a alone, 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

  • FP8 through tgemm.mm: already correct. The torch path and hipBLASLt apply the scales, and skinny and ASM, which dropped them, now refuse them.
  • a8w8, a4w4, blockscale and batched GEMMs: scales are required inputs passed to every backend, so they cannot be dropped this way.
  • Related bug, a follow-up: gemm_a4w4 silently drops bias, alpha and beta when a tuned row picks its CK blockscale kernel (aiter/ops/gemm_op_a4w4.py), and its gfx1250 path warns and ignores them.

Testing

  • Unit tests added or updated
  • Tested on MI355X (gfx950)

Documentation

  • No documentation change is needed: the scales were already part of the signature.

Dependencies

  • No new third-party dependencies added

Breaking Changes

  • A scaled BF16/FP16 call now returns the scaled (correct) result.
  • A scaled call that lands on skinny or ASM now raises instead of returning the unscaled product. That happens with a preshuffled weight on gfx950, or when a tuned row forces those backends.

Merge note: this PR and #7883 each add a name to the default -t list in op_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.mm honours scale_a and scale_b on 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 main 8b13d52:

  • python3 op_tests/test_gemm_a16w16.py (default -t normal scaled): passes.
  • Motivation table: unpatched main against this branch, same script and data.
  • FP8 tgemm.mm with 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 same torch_gemm path 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_scaled fails at its first shape: the result is 1/6 of the reference. The existing -sa / -sb options of test_gemm are 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 scaled check, added to the default -t list so the CI python3 run executes it.

…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.
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 Radeon R9700 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 7884 --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.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant