← Back to Pull requests

Replace the gfx950 blockwise FP8 running-scale accumulator #744

Closed
unknown closed this pull request without merging on Oct 19, 2026 02:58 UTC
View on GitHub
unknown unknown commented on Sep 26, 2026 19:19 UTC
Author

[agent reply]

Glossary

Three terms are used throughout and none of them is standard, so they are pinned down here.

| term | meaning |
|---|---|
| running scale | the accumulation form micro_tk used before this change: one FP32 accumulator carried across all K-blocks holding T_k / s_k, rescaled every block by s_{k-1} / s_k. |
| scale-then-accumulate | the form this change puts in its place: each K-block's product is formed in a temporary, multiplied by that block's own scales, and added. The accumulator only ever holds a partial sum in true units. |
| 1D×2D / 1D×1D | which recipe supplies the weight scale. 1D×2D blocks the weights 128×128 (one scalar per wave per K-block); 1D×1D blocks them 1×128 (one value per output column). Float8BlockScaling uses 1D×2D for fprop and dgrad and 1D×1D for wgrad. |

What this changes

micro_tk — the gfx950 FP32-scale blockwise FP8 GEMM, selected when NVTE_FP8_BLOCK_SCALING_FP32_SCALES=1 and K is a multiple of 128 and at least 256 — is replaced with a scale-then-accumulate kernel.

The motivation is numerical. s_{k-1} / s_k is a quotient of two data-dependent quantiser outputs and is unbounded in both directions, which is where the inf reported against this path comes from. Under scale-then-accumulate the accumulator can overflow only if the true result is itself unrepresentable in FP32, so the two empirical floors that were guarding the ratio are no longer load-bearing and kMinScaleInv is removed with them.

It is also faster. On MI355X, against the kernel it replaces, measured under the harness described below:

| shape | running scale | this PR | |
|---|---|---|---|
| 4096³ | 939.4 | 1739.4 | 1.85× |
| 6144³ | 849.2 | 1542.1 | 1.82× |
| 8192³ | 1134.7 | 1954.0 | 1.72× |
| 16384³ | 1165.8 | 2088.2 | 1.79× |

TFLOP/s, 1D×1D recipe, FP32 scales. The 1D×2D recipe reaches 1945 / 1788 / 2343 / 2448 on the same shapes.

The speedup does not come from the scale change itself. It comes from two things the scale change made possible:

  • a transposed accumulator. Swapping the MFMA operands puts the GEMM's N on the tile rows, so a lane holds four consecutive N at one M. The 1×128 activation scale varies along M, so it becomes one value per lane per tile instead of four — 16 scale registers per accumulator become 4.

  • a hand-scheduled MFMA cluster. Each v_mfma_f32_16x16x128_f8f6f4 occupies the matrix pipe for 32 cycles; the fold that consumes tile k-1's result is placed in tile k's shadow so the MFMA issue cadence stays at 32. The MFMA→VALU hazard on this tier is satisfied by elapsed cycles rather than by instruction count, which is why the s_nop counts in the fold are what they are and are written out rather than left to the scheduler.
  • What this does NOT change

  • micro_tk_partial_k is untouched — zero lines. It remains the path for K % 128 != 0 or K < 256, which this kernel does not accept. It was already scale-then-accumulate and had no ratio, so it never had the overflow this PR is about.

  • micro_tk_pow2 is untouched. The default path, when the env var is unset, is unaffected and bit-identical.

  • gfx942 (cdna3/) is untouched. The arch split already runs through BlockwiseGemmBackend::get_cdna3() / get_cdna4(); every source change here is under cdna4/.

  • No change to the quantiser, to amax_epsilon, or to any framework-level file.
  • Files

    | file | |
    |---|---|
    | cdna4/blockwise_fp8_gemm.cpp | micro_tk's body replaced; signature, name and dispatch unchanged |
    | cdna4/blockwise_fp8_gemm_helper.cuh | six running-scale-only helpers deleted; kMinScaleInv and floor_scale_inv deleted; the transposed-accumulator family and apply_epilogue_t appended under a section banner |
    | tests/cpp/operator/test_cublaslt_gemm.cu | BlockwiseGEMMTestSuite extended — see below |

    The helper header carries two families of device helpers, separated by a section banner rather than by a file boundary. They differ in the accumulator layout -- whether a lane holds four consecutive M at one N, or the transpose of that -- which changes which index a scale varies along, how many values a lane needs, and whether four elements of C are adjacent or a row apart. That is a property of one kernel, not of a compilation unit: micro_tk uses the transposed half, micro_tk_pow2 and micro_tk_partial_k the other.

    kMinScaleInv

    Removed, along with floor_scale_inv and its six call sites. It existed to keep the ratio's denominator away from zero; there is no ratio now. Flooring a scale_inv at 1e-13 in a multiply-then-accumulate path is a distortion with nothing to buy, so it goes rather than staying as a no-op.

    The two test cases whose comments named it (// pins kMinScaleInv from below / from above) still pass without it.

    Epilogue

    apply_epilogue_t is the transposed counterpart of apply_epilogue: same arithmetic, same order, mirrored traversal. The order matters and is preserved — the accumulate term rounds to the output type before adding the prior value, so that the sum matches what an add performed after the store would produce, and moving bias across that rounding would change the result.

    The traversal flip moves cost between the three terms:

  • bias is indexed by n alone. Untransposed, a lane's four accumulator entries share one column and one scalar read serves them; transposed they are four consecutive columns. They are adjacent, so they are read as one contiguous run — the load count per tile matches apply_epilogue's again, and only the bytes differ.

  • gelu_aux and c_in are indexed by mN + n, so the four a lane touches are adjacent rather than a row apart: four strided reads become one contiguous run.
  • gelu_aux is deliberately not gathered into a local. Its index carries the row base, so a gathered copy is live per tile rather than per column and the compiler hoists all eight tiles' worth above the loop. Measured, that costs 588 bytes/lane of scratch and ~800 VGPR spills in the GELU_AUX_BETA instantiation, against zero without it.

    Resource usage for every instantiation, from -Rpass-analysis=kernel-resource-usage:

    | | VGPR | SGPR | scratch | occupancy | spills |
    |---|---|---|---|---|---|
    | 1D×2D, all six epilogues | 256 | 67 | 0 | 2 waves/SIMD | 0 |
    | 1D×1D, all six epilogues | 256 | 65 | 64–68 | 2 waves/SIMD | 15–16 |

    Occupancy is 2 waves/SIMD throughout; no epilogue changes it. The 1D×1D scratch is from the #pragma unroll 2 on the K loop and predates the epilogue.

    e5m2 operands

    cbsz and blgp select the MFMA's per-operand format. Because this kernel feeds the MFMA b before a — the operand swap that transposes the accumulator — the two codes swap with them: the template's CBSZ, which names the caller's A, has to be passed in the builtin's second modifier slot. Passing them straight through would decode the weights with the activations' format, which is silent: same 8 bits, different exponent bias.

    The generated ISA confirms it. The three instantiations are identical instruction for instruction and register for register, differing only in one modifier:

    CBSZ=0 BLGP=0   v_mfma_f32_16x16x128_f8f6f4 v[10:13], v[18:25], v[58:65], 0
    CBSZ=1 BLGP=0   v_mfma_f32_16x16x128_f8f6f4 v[10:13], v[18:25], v[58:65], 0 blgp:1
    CBSZ=0 BLGP=1   v_mfma_f32_16x16x128_f8f6f4 v[10:13], v[18:25], v[58:65], 0 cbsz:1
    

    e5m2 therefore costs nothing.

    Tests

    BlockwiseGEMMTestSuite went from 12 cases to 31.

    Coverage is stated as kernel instantiations actually dispatched, joined from rocpd_kernel_dispatch against rocpd_info_kernel_symbol in a rocprofv3 --kernel-trace capture — not as a count of parameter rows, and not from the symbol table, which lists every instantiation the code object contains whether or not it ran. micro_tk's full cross-product is 3 output types × 2 cbsz × 2 blgp × 2 recipes × 6 epilogues = 144.

    | | instantiations reached |
    |---|---|
    | before | 2 — both DEFAULT, both e4m3×e4m3, both bf16 out |
    | after | 18 |

    Per axis, after:

    out dtype   bf16 15/48   float 2/48   f16 1/48
    cbsz        0 17/72      1 1/72
    blgp        0 15/72      1 3/72
    recipe      1Dx2D 6/72   1Dx1D 12/72
    epilogue    DEFAULT 7/24  BIAS 4/24  GELU_AUX 2/24  BETA 2/24  BIAS_BETA 1/24  GELU_AUX_BETA 2/24
    

    Every axis is non-zero where it was zero before. The suite had no bias, gelu_aux or accumulate fields at all, so the fused epilogue had no C++ coverage; a te.Linear(bias=True) forward pass runs the BIAS instantiation on every step.

    To support that the reference gained the same three terms in the same order the kernel applies them, and the product is now computed in FP32 and converted after the epilogue rather than before it. The reference is still an independent FP32 loop over dequantised operands, so a kernel that applies the epilogue to the wrong element still fails — bias is given a profile whose period divides neither the four elements a lane holds nor the 16 or 128 of a tile, because a constant or tile-periodic bias would pass even if the epilogue put it on the wrong column.

    One thing the new cases establish that is worth recording: the GEMM requires m to be a multiple of 16 and the kernel indexes bias by m, so the merged four-element read is always in range and its guarded scalar fallback is unreachable through this entry point.

    Results

    Run on MI355X (gfx950), ROCm 7.15, built with NVTE_FRAMEWORK=pytorch.

    | | |
    |---|---|
    | BlockwiseGEMMTestSuite, default path | 31 / 31 passed |
    | BlockwiseGEMMTestSuite, NVTE_FP8_BLOCK_SCALING_FP32_SCALES=1 | 31 / 31 passed |
    | test_operator --gtest_filter=
    Blockwise* | 516 passed, 24 skipped — unchanged from the base commit |
    | tests/pytorch/test_float8_blockwise_gemm_exact.py | 938 passed, 641 skipped |
    | the same with NVTE_FP8_BLOCK_SCALING_FP32_SCALES=1 | 938 passed, 641 skipped |
    | tests/pytorch/test_float8_blockwise_scaling_exact.py | 258 passed |

    Every count matches the base commit 03afd8f81 measured before the change.

    JAX was not built, and no JAX test exercises this GEMM.

    Benchmark method

    The numbers above are medians over 21 timed reps of 10 launches, on an idle GPU, with:

  • rotating operand buffers sized so the working set exceeds the LLC. One set re-run in a loop reports a bandwidth the kernel never sees.

  • a wall-clock warmup to DVFS steady state, not a fixed iteration count.

  • arms alternated inside the timed region, with the order flipped every rep. This is not a detail: running one arm to completion and then the other attributes every drift — clock, thermals, a co-tenant arriving — to whichever arm ran during it. A speedup first measured at 1.11–1.19× over this baseline fell to 1.03–1.05× once the arms were interleaved, and the error was in the baseline.

  • operands filled by bit pattern rather than by rounding a float distribution. fp8 data content moves the clock on a power-limited part; filling via rand()/10 produced enough subnormals and zeros to lower switching activity and raise the clock, which reversed one shape's verdict.
  • The reported spread (min/max) was within 1% on every arm; a wide spread is the symptom of a co-tenant and was checked before the medians were believed.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Micky774 Review duration: 32138m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #744"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%