Repository navigation
[Triton/Gluon] Explicit tl.fma in blockscale GEMM kernels for Triton 3.9 - #7544
Conversation
…_blockscale_a16w16
Two changes in the fp8 K loop:
1. Use tl.range(num_stages=2) for the loop. With num_stages on the loop,
the Triton pipeliner also pipelines loads that do not feed tl.dot.
The a_scale and b_scale loads are then issued one iteration ahead.
Before, the wait for the scales also waited for the A/B prefetch.
2. Compute the scale outer product first and use tl.fma for the
accumulation. Triton 3.9 does not fuse mul and add by default, so
the old expression gave separate mul and add instructions.
gfx950, M=4096 N8=512 N16=256 K=7168, median of 5 (us):
3.9 fp8 3.9 both 3.8 fp8 3.8 both
before 129.9 141.5 122.8 131.6
after 77.8 120.3 76.9 119.7
Co-Authored-By: Claude Opus 5.5 <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
WAITING ON AUTHORPR Self-ReviewRevision Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision. Questions to answer in the PR description
Edit the Missing or invalid answers
Shadow mode reports what would block but keeps the status successful. Set |
There was a problem hiding this comment.
🟡 Changes recommended
The kernel hard-codes a loop-stage tuning value contrary to the JSON-only rule and multiplies FP16 scales before promotion, which can underflow and incorrectly produce zero.
2 open issues
What changed in this PR
This PR optimizes the FP8 K loop in the fused block-scaled FP8 and 16-bit GEMM kernel.
Changes:
- Uses a two-stage
tl.rangeloop to pipeline scale loads. - Computes the scale outer product first and uses explicit fused multiply-add accumulation.
| File | Description |
|---|---|
aiter/ops/triton/_triton_kernels/gemm/fused/fused_gemm_a8w8_blockscale_a16w16.py |
Updates FP8 loop pipelining and scaled accumulation. |
Review effort: Balanced
…proj Triton 3.9 disables implicit FP fusion by default (#11270, #12479), turning every fused multiply-add into separate mul + add instructions. This causes ~7% regression in gemm_a8w8_blockscale and 12-15% in gather_kv_b_proj. Replace `accumulator += tl.dot(a, b) * scale` with explicit `tl.fma(tl.dot(a, b), scale, accumulator)` to restore FMA codegen without relying on environment variables. Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Both GEMM kernels multiply scales before FP32 promotion, causing FP16 underflow that turns valid nonzero outputs into zero; the scale casts need correction and an FP16 regression test is missing.
3 open issues
Review effort: Balanced
… K=8192 Triton 3.9 does not fuse mul and add by default, so `accumulator += tl.dot(a, b) * scale` in the non-preshuffle _gemm_a16w8_blockscale_kernel became separate mul + add. Use tl.fma for both the PREQUANT and the plain branch, as was done for gemm_a8w8_blockscale. The preshuffle kernel showed no regression and is unchanged. The explicit FMA made M=32/64 at N=1280 K=8192 (the default benchmark shape) 4-6% slower with the DEFAULT config. Add a gfx950 N=1280-K=8192 config that copies DEFAULT.json and only changes M_LEQ_32 / M_LEQ_64 to NUM_KSPLIT=8, num_warps=4 (80 -> 160 blocks). gfx950, N=1280 K=8192, kernel time with L2 flushed, vs 3.8 (us): M 3.8 3.9 before 3.9 after 32 19.7 19.5 8.5 64 19.7 19.8 10.7 1024 79.8 86.2 78.8 2048 87.4 94.0 88.8 16384 489.3 522.8 487.9 On N=7168 K=2048 (DeepSeek shape with its own config) the change is within +-2% of 3.8 for all M. test_gemm_a16w8_blockscale.py passes on Triton 3.8.0 (git7cb7b059) and 3.9.0 (gitf1bf9132). Co-Authored-By: Claude <[email protected]>
… 3.9 Two changes in the K loop path of _batched_gemm_a8w8_a_per_token_group_prequant_w_per_batched_tensor_quant_kernel: 1. Use tl.fma for `accumulator += tl.dot(a, b) * a_scale`. Triton 3.9 does not fuse mul and add by default, so the old expression gave separate mul and add instructions (24 FMA vs. 60 on 3.8). 2. With NUM_KSPLIT == 1, skip the split-K range math added in ROCm#6727. SPLITK_BLOCK_SIZE >= K in that case, so k_span is always K and the result does not change. On 3.9 the extra scalar code moved the K loop by 20 bytes, so the loop spans 26 instead of 25 64-byte fetch blocks. With this change the loop is at the same place as before ROCm#6727. gfx950, bench default shapes (B=16, N=1280, K=8192), TFLOPS: M 3.8.0 3.9 before 3.9 after 64 226.8 224.6 243.8 128 456.5 441.7 478.5 192 619.3 570.2 605.1 256 508.6 494.9 518.4 All other default shapes are 2.8% to 8.1% faster than 3.8.0 on 3.9. On 3.8.0 the change is neutral (-0.9% to +0.1%). M=192 is still 2.3% below 3.8.0; this gap also shows before ROCm#6727 with FP fusion on. Co-Authored-By: Claude <[email protected]>
…to separate PR Move the N=1280 K=8192 gfx950 config to a separate PR to keep one problem per PR. Add prequant=True parametrization to test_gemm so the PREQUANT accumulation path with tl.fma has test coverage. Co-Authored-By: Claude <[email protected]>
There was a problem hiding this comment.
🔵 Needs a closer look
Unavailable GPU validation leaves the fused pipeline changes and reassociated FP32 scale products requiring human correctness and base/head latency checks on supported GPUs with Triton 3.8 and 3.9.
4 open issues
Review effort: Balanced
Add a shape-specific config for N=1280 K=8192 on gfx950. With the explicit tl.fma change (PR ROCm#7544), M=32/64 at this shape regressed 4-6% with DEFAULT. The new config uses NUM_KSPLIT=8, num_warps=4 for small M buckets, recovering performance to match Triton 3.8. gfx950, N=1280 K=8192, kernel time (us): M 3.8 3.9+fma+retune 32 19.7 8.5 64 19.7 10.7 1024 79.8 78.8 2048 87.4 88.8 16384 489.3 487.9 Co-Authored-By: Claude <[email protected]>
… config (#8106) * [Tune] gfx950 gemm_a16w8_blockscale: add N=1280 K=8192 config Add a shape-specific config for N=1280 K=8192 on gfx950. With the explicit tl.fma change (PR #7544), M=32/64 at this shape regressed 4-6% with DEFAULT. The new config uses NUM_KSPLIT=8, num_warps=4 for small M buckets, recovering performance to match Triton 3.8. gfx950, N=1280 K=8192, kernel time (us): M 3.8 3.9+fma+retune 32 19.7 8.5 64 19.7 10.7 1024 79.8 78.8 2048 87.4 88.8 16384 489.3 487.9 * [Triton] [Test] Add M=64 shape for N=1280 K=8192 split-K path



Triton 3.8 enables implicit FP contraction by default, but Triton 3.9 disables implicit FP contraction by default.
Expressions like
accumulator += tl.dot(a, b) * scaleare no longer lowered to a single FMA instruction — the compiler emits separate mul and add instead, causing performance regression.Two changes in the fp8 K loop:
gfx950, M=4096 N8=512 N16=256 K=7168, median of 5 (us):
before 129.9 141.5 122.8 131.6
after 77.8 120.3 76.9 119.7
The same explicit tl.fma fix is applied to four more kernels that had
the same Triton 3.9 regression:
split-K range math when NUM_KSPLIT==1 to keep the K loop aligned.
Test: test_gemm_a16w8_blockscale.py now parametrizes prequant={False, True}.
The N=1280 K=8192 gfx950 retune config is in PR #8106
Summary
Use explicit
tl.fmafor scaled accumulation and pipeline scale loads in blockscale GEMM kernels to recover the Triton 3.9 FP contraction regression. Addprequant=Truetest coverage forgemm_a16w8_blockscale.Motivation
Triton 3.9 no longer implicitly fuses multiply-add into FMA instructions. Expressions like
acc += tl.dot(a, b) * scalenow emit separatemul+add, which costs 7-15% throughput on gfx950 blockscale GEMM kernels compared to Triton 3.8. Usingtl.fma(dot_result, scale, acc)restores the fused instruction.In the FP8 fused kernel (
fused_gemm_a8w8_blockscale_a16w16), scale loads also block the A/B prefetch pipeline. Wrapping the K loop withtl.range(num_stages=2)lets the pipeliner issue scale loads one iteration ahead.Changes
fused_gemm_a8w8_blockscale_a16w16.py: pipeline scale loads withtl.range(num_stages=2), compute scale outer product first, usetl.fmafor accumulation.gemm_a8w8_blockscale.py(and preshuffle variant): explicittl.fmafor scaled dot accumulation.gemm_a16w8_blockscale.py: explicittl.fmain both PREQUANT and plain branches.gather_kv_b_proj: explicittl.fma.batched_gemm_a8w8_a_per_token_group_prequant: explicittl.fmaand skip split-K range math whenNUM_KSPLIT==1.test_gemm_a16w8_blockscale.py: parametrizeprequant={False, True}.Performance (if applicable)
fused_gemm_a8w8_blockscale_a16w16 (gfx950, M=4096, N8=512, N16=256, K=7168):
batched_gemm_a8w8 (gfx950, B=16, N=1280, K=8192, TFLOPS):
Testing
Documentation
Config-only and kernel-internal change, no API change.
Dependencies
Breaking Changes
None
PR Self-Review
Reviewed revision
7cdda42
Behavior and intent
Triton 3.9 drops implicit FP contraction, so scaled dot-product accumulation (
acc += dot * scale) runs as two separate instructions. Each affected kernel now usestl.fma(dot, scale, acc)to emit a single FMA. The fused FP8 kernel additionally pipelines scale loads withtl.range(num_stages=2)so the scale wait does not block A/B prefetch.Validation and unverified scope
Benchmarked all changed kernels on gfx950 with their existing benchmarks. The fused FP8 kernel is 40% faster than the 3.9 regression and matches 3.8. Batched GEMM is 5-8% faster than both 3.8 and 3.9-before. The N=1280 K=8192 gfx950 retune config was split to separate PR #8106.
PR-specific question
N/A
Changed op tests
Added
prequant=Trueparametrization totest_gemm_a16w8_blockscale.pyto cover the PREQUANT branch that received thetl.fmachange.