Skip to content

[ASM] [Bugfix] Make the bf16 and a4w4 ASM GEMMs exact past 4 GiB - #7883

Merged
JohnNikolay84 merged 17 commits into
mainfrom
users/bartgips/fix-asm-bf16gemm-4gib
Nov 25, 2026
Merged

JohnNikolay84 merged 17 commits into
mainfrom
users/bartgips/fix-asm-bf16gemm-4gib

Conversation

@amd-bartgips

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

Copy link
Copy Markdown
Contributor

Summary

Two ASM GEMM ops return wrong output, without an error, once A, B or the output reaches 4 GiB: their kernels address each operand through one buffer descriptor whose 32-bit size and 32-bit lane offsets wrap at 4 GiB. This PR patches the code objects so every tile addresses memory from its own 64-bit base:

  • gemm_a16w16_asm (bf16gemm kernels): all 24 gfx950 kernels and all 22 gfx942 kernels (the gfx950 set without the two 256x256 kernels).
  • gemm_a4w4_asm (f4gemm kernels), gfx950: the two 256x256 kernels. The other 33 f4gemm kernels already use a 64-bit base per tile: I measured all 33 exact with the output past 4 GiB, 15 of them with A and 21 with B past 4 GiB, and checked the rest in their disassembly. main has since added a persistent BpreShuffle_256x256_ps kernel ([ASM] [HIP] [CK] Add persistent gfx950 f4gemm 256x256 kernel (_ps) #6067), which this PR does not change: it is exact with the output past 4 GiB, and its launcher check keeps A and B below 4 GiB, so larger A or B falls back to the patched BpreShuffle_256x256.

Below 4 GiB nothing changes: outputs at normal shapes are bit-identical to the original kernels' (except split-K with atomic accumulation, whose summation order varies from run to run anyway), and kernel times show no consistent change. The launchers are not changed.

The kernels have no sources in the repository, so the patched code objects were produced by disassembling, patching and reassembling the shipped ones. This gist has the per-kernel assembly diffs, the patch script, a script that regenerates all 48 code objects from main byte for byte, and the validation harness; start with 0_README.md.

Motivation

Kernel names. bf16gemm kernels are named bf16gemm_{bf16,fp32bf16}_tn_<tileM>x<tileN>[_variant], f4gemm kernels f4gemm_bf16_per1x32Fp4_{BpreShuffle,noBpreShuffle}_<tileM>x<tileN>. In the variants, bshuffle and bpreshuffle take a preshuffled B; pf3 takes a plain B. *_splitk kernels without _clean run without splitting K. Only *_splitk_clean kernels split K: each K slice adds its partial sum atomically into the output, and the slices coordinate through a 16 × 64 semaphore workspace, which limits the grid to 1024 workgroups. (Columns bPreshuffle, pf and splitK of hsa/<arch>/bf16gemm/bf16gemm_fp32bf16.csv.)

The bug. These kernels access memory only through buffer descriptors. In the affected kernels each A and B descriptor covered the whole operand: its size (num_records, 32 bits) was rows × row stride, computed with s_mul_i32, which keeps only the low 32 bits, and each lane's 32-bit offset included the tile's first row × row stride. Both wrap at 4 GiB. Loads past the wrapped size return zeros, and stores past it are dropped, so the kernel never faults. The output descriptor of some kernels already had a per-tile base, but its size, (M − tile's first row) × row stride, wraps for the first tiles in the same way.

Measured on MI355X (gfx950) with unpatched main, kernel selected by passing its name, every output element compared with an fp32 reference. "At 4 GiB" means the operand is the smallest tile-aligned size of at least 2³² bytes. Fraction of the output that is wrong:

Kernel A at 4 GiB B at 4 GiB bf16 output at 4 GiB FP32 output at 4 GiB
bf16 256x256, 256x256_bpreshuffle 77% 77% 1.6% exact¹
bf16 64x64_pf3_splitk, 160x64_bshuffle_splitk 77% 77% 100% 100%
bf16 32x64_splitk_clean, 128x64_bshuffle_splitk_clean (split-K 4) 68% 68% n/a² n/a²
f4gemm BpreShuffle_256x256³ 92% 92% 1.1%⁴ n/a
f4gemm noBpreShuffle_256x256 96% 99.9% exact⁵ n/a

¹ The 256x256 bf16gemm kernels write FP32 output on a separate store path that already used a per-tile base and size.
² With at most 1024 workgroups, the output of a *_clean kernel stays far below 4 GiB.
³ With log2_k_split 0 (the a4w4 launcher's split-K setting, log₂ of the number of K slices), at M × N × K = 1048576 × 256 × 8192 for A and 256 × 1048576 × 8192 for B; with log2_k_split 1, 75%.
⁴ At M × N × K = 20000 × 107520 × 512 (4.005 GiB of output): the first tile's output size wraps to the 5.8 MB past 4 GiB (27 rows), so that tile's stores from row 27 on are dropped.
⁵ When M is a multiple of 256; see "Existing bugs not fixed here".

On gfx942 (MI300X) the original kernels gave the same fractions (A and B 77%, output 100%, *_clean 68%).

One step below each limit (one row or column less), every kernel is exact, with one exception. When the last M tile is partial, some original kernels already fail just below 4 GiB of output: they compute offsets for the tile's rows past M, and those wrap onto the first rows. For example, at M = 16383, 160x64_bshuffle_splitk returns rows 0–95 wrong, and the kernels with 48- and 96-row tiles return rows 0–31 wrong, on both architectures. The patch fixes this case too.

How these shapes are reached today. tgemm.mm (aiter/tuned_gemm.py) and gemm_a4w4 (aiter/ops/gemm_op_a4w4.py) look up (M, N, K) in a tuned-config CSV (aiter/configs/bf16_tuned_gemm.csv, aiter/configs/a4w4_blockscale_tuned_gemm.csv and the per-model files in aiter/configs/model_configs/). If no row matches M, they retry with M padded (get_padded_m in aiter/ops/gemm_op_common.py); the coarsest padding maps every M above 8192 to 8192. When an ASM launcher gets no kernel name, it picks a kernel by its own heuristic.

  • bf16, preshuffled weights: on gfx950, tgemm.mm with a preshuffled weight and no tuned row uses ASM at any size; no other backend handles that layout.
  • bf16, tuned row reused at a larger M: the shipped llama70B row for N = 57344, K = 8192 (256x256, tuned at M = 8192) also serves M = 40960, where rows from 3510 on come back wrong.
  • a4w4, tuned row reused at a larger M: 15 shipped (N, K) shapes on gfx950 have a BpreShuffle_256x256 row tuned at M = 8192: one in a4w4_blockscale_tuned_gemm.csv and 14 in the per-model files. The other rows at M = 8192 name BpreShuffle_256x256_ps. For example, gemm_a4w4 at N = 59136, K = 8192, M = 36352 (a 4.004 GiB output) uses the M = 8192 row; with the original code object, 0.6% of the output is wrong.
  • a4w4, no tuned row: the launcher's heuristic picks 256x256 at such sizes when _ps cannot take the call, for example with A or B of 4 GiB or more; otherwise _ps replaces it.
  • Tuning: the tuners try every ASM kernel at any size.

Changes

  • Code objects (48): hsa/gfx950/bf16gemm/*.co (24), hsa/gfx942/bf16gemm/*.co (22) and hsa/gfx950/f4gemm/f4gemm_bf16_per1x32Fp4_{BpreShuffle,noBpreShuffle}_256x256.co (2).

  • What each tile now does, for A, B and (where it did not already have a per-tile base) the output:

    • adds its first row × row stride to the 48-bit descriptor base with a 64-bit multiply-add (s_mul_i32 / s_mul_hi_u32, s_add_u32 / s_addc_u32);
    • sizes the descriptor to min(rows - first_row, tile) × row stride, so the hardware still clips the partial last tile;
    • drops the tile term from the lane offsets;
    • saturates the descriptor size at 0 during prefetch. Each prefetch in the prologue and K loop advances the A and B descriptor bases by one K step and lowers their size by the same amount (s_sub_u32 nr, nr, step), so the end of the descriptor stays put. The prefetches continue past the end of K. With a whole-operand size that never went below zero, but a size covering only a last tile of 1–2 rows would wrap and let those prefetches read past the end of A. Each such s_sub_u32 is now followed by s_cselect_b32 nr, 0, nr, which sets the size to 0 on borrow. That clips exactly what the original clipped: everything at or past the end of the operand.

    The f4gemm 224x256 kernel already addresses memory this way. The split-K atomics use the same per-tile base. Kernel descriptors and metadata are unchanged, and no new SGPRs are used.

  • Cost: the split-K kernels gain 26–27 scalar instructions, all before the K loop. The 256x256 kernels gain 25–28, of which 4 are in the K loop body (a body of 205–443 instructions).

  • How the code objects were produced: co_tool.py in the gist disassembles a code object into reassemblable assembly and reassembles it; patch64.py applies the rewrites. Every rewrite matches an exact instruction sequence, with an asserted match count per kernel. It also checks that every scratch register it writes is dead afterwards, and asserts every write and scalar read of each saturated size register. The shipped gfx942 bf16gemm code objects are byte-identical to their gfx950 siblings except the target byte in the ELF header, so the same rewrites apply unchanged.

    Excerpt: f4gemm_bf16_per1x32Fp4_BpreShuffle_256x256, A descriptor, lane offset and one prefetch
    -	s_lshr_b32 s41, s41, 1
    -	s_mul_i32 s52, s41, s43
    -	s_mov_b32 s14, s52
    +	s_lshr_b32 s41, s41, 1
    +	s_mul_i32 s52, s47, 0x100        // first row of this tile
    +	s_mul_i32 s53, s52, s41
    +	s_add_u32 s12, s12, s53          // 64-bit base += first row * stride
    +	s_addc_u32 s13, s13, 0
    +	s_mul_hi_u32 s53, s52, s41
    +	s_add_u32 s13, s13, s53
    +	s_sub_i32 s52, s43, s52
    +	s_min_u32 s52, s52, 0x100        // rows left in this tile
    +	s_mul_i32 s14, s52, s41          // num_records
    ...
    -	s_mul_i32 s53, s47, 0x100
    +	s_mov_b32 s53, 0                 // lane offset no longer includes the tile row
    ...
     	s_sub_u32 s14, s14, s52          // one K step prefetched
    +	s_cselect_b32 s14, 0, s14        // saturate at 0 past the end of K
  • Launchers (csrc/py_itfs_cu/asm_gemm_a16w16.cu, csrc/py_itfs_cu/asm_gemm_a4w4.cu): no change.

  • Tests:
    The tests take their kernels from the launcher tables: every bf16gemm kernel, and every f4gemm kernel with bpreshuffle=1 and ps=0. The out-of-tree validation below covers every kernel and operand.

    • op_tests/test_gemm_a16w16.py -t asm_4g, added to the file's default -t list so the CI run includes it, on gfx950 and gfx942:
      • test_asm_4gib_operands: A and bf16 output just past 4 GiB must be exact in every element for every non-split-K kernel in bf16gemm_fp32bf16.csv, and A (FP32 output, split 2) for every *_splitk_clean kernel.
      • test_asm_last_tile_at_allocation_end: A at the end of a 64 MiB allocation, so a read past it faults, and a last M tile of 1 row (M = tileM + 1), at K = 64 and 128 for every non-split-K kernel, and K = 128 at split 2 for every *_splitk_clean kernel. Every element must be exact.
    • op_tests/test_gemm_a4w4.py, run on gfx950 after the summary table, with the default shapes only (not with -mnk or --csv):
      • check_asm_4gib_operands: output and A just past 4 GiB must be exact for every f4gemm kernel with bpreshuffle=1 and ps=0 (34 on gfx950), at log2_k_split 0. A uses N = 512.
      • check_asm_last_tile_at_allocation_end: the same allocation-end check for those kernels, M = tile_M + 1, N = 512, K = 256 and 512.

Remaining limits. Each descriptor now covers at most one tile, so the limits are per tile: one tile's rows of A, B and the output must each span less than 4 GiB.

  • bf16gemm: tile_M × K × 2 bytes for A, tile_N × K × 2 for B, tile_M × N × (2 or 4) for the output. With 256-row tiles: K < 2²³, and N < 2²³ for bf16 output or 2²² for FP32 output.
  • f4gemm 256x256: 256 × K / 2 bytes for A and B (K < 2²⁵), 256 × N × 2 for the output (N < 2²³). The scale tensors are still addressed with 32-bit offsets and must stay below 4 GiB, so A and B must stay below about 64 GiB.

Performance (if applicable)

Each code object was timed with aiter.test_common.run_perftest in 6 separate processes on one GPU, alternating original and patched. For each shape, the table gives the change in the minimum and in the median of the 6 per-process times.

Hardware Workload/configuration Before After Change
MI355X (gfx950) 24 shapes: 15 shipped tuned rows that select these kernels (4 a4w4 BpreShuffle_256x256, 3 bf16 256x256, 8 bf16 split-K), 3 FP32-output or split-K variants of those, and 6 shapes for kernels no tuned row selects original patched min −1.9% to +1.5% (geomean +0.1%), median −1.0% to +1.1% (geomean +0.1%)
MI300X (gfx942) 17 shapes: 13 shipped gfx942 tuned rows, plus one shape for each of the four bshuffle kernels that no gfx942 tuned row selects original patched min −2.6% to +1.8% (geomean −0.2%), median −3.1% to +5.5% (geomean +0.3%)

There is no consistent direction. The +5.5% median on gfx942 (64x64_splitk_clean, M × N × K = 7168 × 64 × 4096) comes from processes that run in one of two modes: both the original and the patched kernel take about 19.4 µs or 21.4 µs per process, and the patched kernel drew the slow mode in 3 of 6 processes, the original in 1. Its minimum changes by +0.6%. On gfx950 the 1024 × 16384 × 6656 split-K shapes show the same two-mode behaviour.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X
  • Tested on MI355X

Documentation

  • No documentation change is needed: the kernels' interface is unchanged.

Dependencies

  • No new third-party dependencies added

Breaking Changes

None. On gfx950 and gfx942, shapes with A, B or output of 4 GiB or more now compute correctly instead of returning wrong values.

Merge notes.

  • This PR and #7884 each add a name to the default -t list in op_tests/test_gemm_a16w16.py, so whichever merges second needs a one-line merge.
  • Some commits on this branch are superseded by later ones (launcher checks that were added and later removed). Please squash with this message:
[ASM] [Bugfix] Make the bf16 and a4w4 ASM GEMMs exact past 4 GiB

The bf16gemm (gfx950, gfx942) and f4gemm 256x256 (gfx950) ASM kernels gave
each operand one buffer descriptor with a 32-bit size and 32-bit lane offsets
that include the tile's first row, so A, B or C of 4 GiB or more returned
wrong output without an error.

Regenerate the 48 code objects from the shipped ones (disassemble, patch,
reassemble) so each tile adds its first row * stride to the 64-bit descriptor
base, sizes the descriptor to its own rows, drops the tile term from the lane
offsets, and saturates the descriptor size at zero for prefetches past the end
of K. No launcher change. Add op tests past 4 GiB and with A ending an
allocation and a 1-row last tile.

PR Self-Review

Reviewed revision

07742a9

Behavior and intent

The 24 gfx950 and 22 gfx942 bf16gemm code objects and the two gfx950 f4gemm 256x256 code objects now give each tile its own 64-bit descriptor base, so gemm_a16w16_asm (gfx950, gfx942) and gemm_a4w4_asm (gfx950) compute A, B and output of 4 GiB or more correctly, where they used to return wrong output without an error. Below 4 GiB the outputs are unchanged.

Validation and unverified scope

The measurements below ran at 49635be. This revision merges main, which adds BpreShuffle_256x256_ps (#6067), a launcher check for it, retuned a4w4 rows and a split-K test in test_gemm_a4w4.py; the changes of this PR are the same. At this revision, on MI355X: _ps is exact with the output at 4.005 and 4.06 GiB (M × N × K = 20192 and 20480 × 106496 × 512) and through its tuned row at 20192 × 106496 × 16384, and the launcher rejects it with A at 4 GiB. gemm_a4w4 at 36352 × 59136 × 8192 (tuned row BpreShuffle_256x256) is exact with the patched code object, and 12909406 of 2149711872 elements are wrong with the original.

CI. At edce462 (49635be plus a main merge), the CI ran test_gemm_a16w16.py on MI355X (17 asm_4g cases exact) and MI300X (11 exact), and test_gemm_a4w4.py on MI355X (4 exact) and MI300X; all passed. At this revision the tests run 72 asm_4g cases on MI355X, 64 on MI300X and 136 a4w4 cases on MI355X.

Method. Original (main) and patched code objects were run with the same launcher build, one process per case, kernel selected by name, every output element compared with an fp32 reference (computed from the bf16 inputs, or from the dequantized FP4 inputs for a4w4). The harness in the gist, used for the gfx942 runs, also checks that a guard region after the output stays untouched. Allocation-end cases place A or B at the end of an allocation and confirm that the memory after it is unmapped, so a read past it faults.

Reproducing. In the gist: reproduce.sh MAIN_CHECKOUT PR_CHECKOUT regenerates the 48 code objects from main's and compares them with this branch (all 48 identical); make_cases.py and run.py run the bf16gemm correctness, allocation-end and performance cases on either architecture (instructions in 0_README.md). The op tests: python3 op_tests/test_gemm_a16w16.py (default -t list, includes asm_4g) and python3 op_tests/test_gemm_a4w4.py.

Disassembly round trip. Disassembling and reassembling without patches gives back byte-identical code, kernel descriptors and metadata for all 81 shipped code objects of these directories: 59 on gfx950 (24 bf16gemm, 35 f4gemm) and 22 on gfx942.

MI355X (gfx950):

  • bf16gemm, all 24 kernels: 228 of 228 cases exact. A, B and the output (bf16 and FP32), each one row or column below 4 GiB, at 4 GiB, and at about 6 GiB with a partial last tile. For the *_clean kernels: A and B at split-K 4.
  • f4gemm BpreShuffle_256x256: exact with log2_k_split 0 and 1 for A at 4 GiB and at 6 GiB with a partial last M tile, B at 4 and 6 GiB, and output at 4.005, 4.05, 8.0 and 8.01 GiB.
  • f4gemm noBpreShuffle_256x256: exact for A at 4 and 6 GiB, B at 4 and 6 GiB, and output at 4.05 and 8.0 GiB.
  • Operands at the end of an allocation: 854 cases on every kernel, through the heuristic and named, with A or B at the end of an exact-size allocation, small K, and a last tile of 1 to tile_M − 1 rows, or M = 1 or 2.
    • Original kernels: 63 faults, 58 of them with M = 1 or 2.
    • Patched kernels: 9 faults, each also faulting with the original: 8 are the heuristic picking a *_splitk_clean kernel without a semaphore, 1 is noBpreShuffle_256x256 reading past the end of B (both under "Existing bugs not fixed here").
    • With the s_cselect_b32 saturation removed, the patched kernels fault in 170, which is what the allocation-end op tests guard against.
  • Normal shapes, all 26 kernels: outputs bit-identical to the original's, including partial M tiles, except split-K with atomic accumulation, which is exact but differs in summation order.
  • Op tests on this branch: python3 op_tests/test_gemm_a16w16.py passes; -t asm_4g runs 72 cases on MI355X, all exact (36 past 4 GiB, 36 allocation-end). The a4w4 checks run 136 cases, all exact (68 and 68). With the original code objects the allocation-end cases pass and the 4 GiB checks fail on their first case; with the saturation removed, both files fault on their first allocation-end case. test_gemm_a16w16.py can also log two checkAllclose failed lines at 64 × 256 × 5120, also on main: there, bf16-output split-K (split 16) adds each partial sum into the bf16 output, so rounding accumulates; FP32 output is exact.

MI300X (gfx942), with the gfx942 code objects of this revision:

  • 532 of 532 patched cases exact: 204 at and around 4 GiB (the gfx950 bf16gemm shapes) and 328 with A at the end of an allocation (small K, a last tile of 1–3 rows, or M = 1 or 2). The original kernels are wrong past 4 GiB as on gfx950, show the partial-tile wrap just below 4 GiB, and fault in 42 allocation-end cases (M = 1–2 at small K).
  • Normal shapes: 44 of 44 exact and bit-identical to the original's, except split-K with atomic accumulation.
  • Op test: at the previous revision (32d3988), python3 op_tests/test_gemm_a16w16.py -t asm_4g exited 0 with all 30 cases exact. This revision runs 64 cases on gfx942 (32 past 4 GiB, 32 allocation-end), every kernel in the gfx942 table.

Unverified:

  • MI250X (gfx90a): not tested, and not affected: no bf16gemm or f4gemm code objects are shipped for gfx90a.
  • gfx1250 f4gemm *_256x256_4x4_ps kernels: neither measured nor changed; they behave as on main.
  • gfx950 BpreShuffle_256x256_ps: not changed, and measured only with the output past 4 GiB (above); not measured with an operand at the end of an allocation.

Existing bugs not fixed here (also on main, independent of 4 GiB):

  • Without a kernel name, the bf16 launcher can run a *_splitk_clean kernel with a null semaphore pointer, which faults at address 0. This happens in two cases:

    • With K = 64, the heuristic picks such a kernel without splitting K, and the launcher passes a semaphore only when it splits K (seen without preshuffle on gfx950 and with preshuffle on gfx942). Two kernels tie in the heuristic there, and the iteration order of the launcher's kernel table (a std::unordered_map) decides which one wins, so the op tests do not run the heuristic.
    • When the caller passes splitK=1, gemm_a16w16_asm passes an empty semaphore tensor. The heuristic ignores the requested split and can still pick such a kernel at split 2 or more (for example M = 64 or 257, N = 256, K = 128, on gfx950 and gfx942).

    Every shipped ASM tuned row names its kernel, so tuned shapes in tgemm.mm reach neither case.

  • noBpreShuffle_256x256 reads past the end of B when B ends an allocation (its B descriptor size is not lowered in the K loop, so there is nothing to saturate). It also writes whole 256-row tiles when M % 256 ≠ 0, past the output that gemm_a4w4 allocates with M rounded up to a multiple of 32. Either can fault, or the write can overwrite the allocation that follows.

  • Both f4gemm 256x256 kernels give wrong values in some rows when N % 256 ≠ 0 and log2_k_split = 0. Their stores are bounded per tile, not per row, so the partial N tile's columns from N on land in the next row.

PR-specific question

Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?

The patched code objects are the ones gemm_a16w16_asm loads on gfx950 and gfx942 (bf16 input, bf16 or FP32 output) and gemm_a4w4_asm loads on gfx950 (bf16 output), whether the kernel was picked by heuristic, tuned row or name. There is no size gate: each descriptor covers at most one tile, so the only limits left are per tile (see "Remaining limits" under Changes).

Changed op tests

  • op_tests/test_gemm_a16w16.py (-t asm_4g, gfx950 and gfx942): no existing test runs the ASM kernels near 4 GiB or with an operand at the end of an allocation. On main the 4 GiB check fails on its first case (A past 4 GiB, almost all of the output wrong). The allocation-end check guards the saturation: without it the patched kernels fault there.
  • op_tests/test_gemm_a4w4.py (gfx950): the default shapes include 32768 × 106496 × 16384 (a 6.5 GiB output), but its tuned row is a 224x256 kernel, which was already correct, so no existing shape ran 256x256 at 4 GiB. On main the 4 GiB check fails on its first case: rows 27–255 of the first tile come back wrong. The checks select kernels from the f4gemm table by bpreshuffle=1, ps=0, so the heuristic and _ps do not affect them.

The bf16gemm kernels address A, B and out through buffer instructions
with 32-bit byte offsets. With any of them at 4 GiB or more, the output
is wrong (1.6% to 100% of elements) and nothing raises. Below 4 GiB the
kernels are exact.

gemm_a16w16_asm now raises when A, B or out is 4 GiB or larger.
@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 7883 --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.

…n 256x256

The 256x256 kernels write fp32 output through one buffer descriptor per
256-row tile, so a whole-output limit refused shapes they compute correctly
(measured exact up to 8 GiB). The check now runs after kernel selection.
The f4gemm 256x256 kernel returns wrong output without an error once A, B or
out is 4 GiB or more; the other 33 preshuffled kernels are exact there. The
heuristic now skips it at those sizes, and a 256x256 kernel requested by name
(for example a tuned M = 8192 row reused at a larger M) is replaced by the
heuristic's choice with a warning.
@amd-bartgips amd-bartgips changed the title [ASM] [Bugfix] Reject bf16 GEMM operands of 4 GiB or more [ASM] [Bugfix] Stop the bf16 and a4w4 GEMMs returning wrong output at 4 GiB Nov 13, 2026
@JohnNikolay84

Copy link
Copy Markdown
Contributor

I think the real problem is that kernel is using 32bit arithmetic to compute tiles base offset. Then you can move the base ( which is 64 bit ) to the right place. Agents should be able to even patch binaries to do that.

Every buffer descriptor in these kernels covered a whole operand: its size
(32-bit num_records) was rows x row stride from s_mul_i32, and each lane's
32-bit offset included tile_row0 x row stride. Both wrap once A, B or out
reaches 4 GiB, and the kernels then read zeros or drop stores.

Each tile now adds tile_row0 x row stride to the 48-bit descriptor base with
a 64-bit multiply-add, sizes the descriptor to min(rows - tile_row0, tile)
rows, and drops the tile term from the lane offsets. This is the addressing
the f4gemm 224x256 kernel already uses. The K loop, the partial-tile
clipping and the split-K atomics (which use the same base) are unchanged.

Patched: all 24 gfx950 bf16gemm kernels (A, B and out), and
f4gemm_bf16_per1x32Fp4_{BpreShuffle,noBpreShuffle}_256x256 (A and B, plus
the bf16 out tile size of BpreShuffle). The other 33 f4gemm kernels were
already exact past 4 GiB. Code generated from the shipped code objects by a
pattern-checked disassemble/patch/reassemble step; .text outside the patched
instructions, the kernel descriptors and the metadata are unchanged.
gemm_a16w16_asm: the gfx950 kernels now address each tile from its own
64-bit base, so the A/B/out size check only runs on gfx942, whose kernels
are unchanged.

gemm_a4w4_asm: the 256x256 kernels are exact past 4 GiB, so the heuristic
and named requests no longer avoid them (the launcher is back to main).

Tests:
- test_gemm_a16w16.py: test_asm_rejects_4gib_operands becomes
  test_asm_4gib_operands (-t asm_4g, in the default run). On gfx950, A, B
  and out (bf16 and fp32) just past 4 GiB must be exact in every element
  for the heuristic and one kernel per family; on gfx942 they must raise.
- test_gemm_a4w4.py: check_asm_4gib becomes check_asm_4gib_operands, run on
  gfx950. Out, A and B just past 4 GiB must be exact in every element for
  the heuristic, BpreShuffle_256x256 with and without split-K, and (A, B)
  noBpreShuffle_256x256.
@amd-bartgips amd-bartgips changed the title [ASM] [Bugfix] Stop the bf16 and a4w4 GEMMs returning wrong output at 4 GiB [ASM] [Bugfix] Make the gfx950 bf16 and a4w4 ASM GEMMs exact past 4 GiB Nov 13, 2026
The 22 gfx942 bf16gemm split-K kernels have the same 4 GiB wrap as the gfx950
ones: each buffer descriptor's 32-bit size and each lane's 32-bit offset
include tile_row0 x row stride, so A, B or out at or past 4 GiB give wrong
results without faulting.

Each code object is its gfx950 sibling with only the ELF e_flags target byte
changed, so they get the same 64-bit per-tile base: tile_row0 x row stride
added to the 48-bit descriptor base, the descriptor sized to
min(rows - tile_row0, tile) rows, and the tile term dropped from the lane
offsets, for A, B and out (stores and split-K atomics). Each patched code
object is again the patched gfx950 sibling with only e_flags changed. The
kernel descriptors and metadata are unchanged.
…x942

gemm_a16w16_asm: the gfx942 kernels now address each tile from its own
64-bit base too, so the launcher no longer rejects A, B or out of 4 GiB or
more on gfx942 (the file is back to main).

test_gemm_a16w16.py: test_asm_4gib_operands expects exact results on gfx942
as well, for the heuristic and one kernel per gfx942 family
(64x64_pf3_splitk, 64x64_bshuffle_splitk, 64x64_splitk_clean,
64x64_bshuffle_splitk_clean); the 256x256 kernels exist only on gfx950.
The gfx942 kernels compute 32-bit offsets for every row of the last tile,
including the rows past M, and those stores wrap onto the first rows. With
M = 16383 (out 256 KiB below 4 GiB) the 160x64 kernel returned rows 0-95
wrong, so the check now rounds M and N up to whole tiles.
amd-bartgips added a commit that referenced this pull request Nov 16, 2026
#7883 makes the gfx950 ASM GEMMs exact past 4 GiB. On gfx942 no shipped
ASM row can be reused at a shape with a 4 GiB operand: the rows stop at
M=7296 with N, K <= 6144, and getPaddedM only rounds M up by at most 128
or maps it to 8192.
@amd-bartgips amd-bartgips changed the title [ASM] [Bugfix] Make the gfx950 bf16 and a4w4 ASM GEMMs exact past 4 GiB [ASM] [Bugfix] Make the bf16 and a4w4 ASM GEMMs exact past 4 GiB Nov 16, 2026
@amd-bartgips amd-bartgips added the ci:mi300x Run MI300X standard and OPUS CI on PRs label Nov 16, 2026
The K loop moves each A/B descriptor base forward by one K step and lowers
num_records by the same step, so the window end stays put. With a
per-tile size of min(rows - tile_row0, tile) x row stride, a last tile of
one or two rows is smaller than the prefetch distance at small K, so
num_records wrapped below zero and the prefetches past the end of K read
past the end of A or B (a GPU memory fault when the operand ends an
allocation, e.g. 64x64_pf3_splitk M=65 K=64).

Each such s_sub_u32 is now followed by s_cselect_b32 nr, 0, nr, which keeps
num_records at 0 on borrow, so those loads are clipped as they were with the
whole-operand size. SCC is only read by the new instruction, and no SGPR
is added. The split-K kernels lower num_records only before the K loop
(5 or 6 sites); the 256x256 bf16gemm and f4gemm kernels do it 11 to 13
times, 4 per K-loop body of 200 to 440 instructions.

This also removes the original kernels' fault when all of A is one or two
rows at small K. The gfx942 code objects are again their gfx950 siblings
with only the ELF e_flags target byte changed; kernel descriptors and
metadata are unchanged.
test_gemm_a16w16.py: test_asm_last_tile_at_allocation_end (-t asm_4g, in
the default run, gfx942 and gfx950) places A at the end of a 64 MiB
allocation with M = tile + 1 or 2 and K = 64 or 128, and checks every
element for the heuristic, 64x64_pf3_splitk, 64x64_bshuffle_splitk,
64x64_splitk_clean at split 2 and, on gfx950, 256x256 and
256x256_bpreshuffle. A read past the end of A is a GPU memory fault, which
aborts the test process.

test_gemm_a4w4.py: check_asm_last_tile_at_allocation_end does the same on
gfx950 for BpreShuffle_256x256 (split 0 and 1) and noBpreShuffle_256x256
with M = 257 or 258, K = 256 or 512.
There the heuristic picks 32x64_bshuffle_splitk_clean at split 1 without a
semaphore, which faults at address 0 on main as well.
@amd-bartgips

Copy link
Copy Markdown
Contributor Author

@JohnNikolay84 Thanks for the suggestion. I followed it: the kernels now compute each tile's offset in 64 bits and move the 64-bit descriptor base there, and the binaries are patched. The description has the details. In short:

  • All 24 gfx950 and 22 gfx942 bf16gemm kernels, plus the two gfx950 f4gemm 256x256 kernels, are regenerated from the shipped code objects by disassembling, patching and reassembling. There are no launcher changes and no size gate.
  • Each tile now adds its first row × stride to the 64-bit base, sizes its descriptor to its own rows, and drops the tile term from the lane offsets. Shrinking the descriptor exposed one extra issue. The K-loop prefetches lower num_records past the end of K, and with a 1–2-row last tile it wrapped below zero, so the prefetches read past the end of A. Each such decrement is now followed by s_cselect_b32 nr, 0, nr.
  • The kernels are exact past 4 GiB on MI355X and MI300X, outputs below 4 GiB are bit-identical to the original's, and timings show no consistent change.

The per-kernel assembly diffs, the patch script and a script that rebuilds all 48 code objects from main byte for byte are in this gist. Is shipping regenerated binaries like this acceptable for hsa/?

Keep A and out past 4 GiB and a 1-row last tile at the end of an allocation
for each separately built kind (plain-B and preshuffled-B split-K, *_clean,
256x256, f4gemm BpreShuffle_256x256 at log2_k_split 0). Drop the heuristic
cases (the K = 64 pick depends on unordered_map order), B and FP32-output
cases, 2-row tails, noBpreShuffle_256x256 and a4w4 split-K, and the
free-memory skip. Run the a4w4 checks only with the default shapes, after
the summary.
@amd-bartgips
amd-bartgips marked this pull request as ready for review November 17, 2026 11:47
@amd-bartgips
amd-bartgips requested a review from a team November 17, 2026 11:47
@github-actions github-actions Bot added the ASM label Nov 17, 2026
@aiter-bot

aiter-bot commented Nov 17, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 07742a9f1ca7 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

The 24 gfx950 and 22 gfx942 bf16gemm code objects and the two gfx950 f4gemm 256x256 code objects now give each tile its own 64-bit descriptor base, so `gemm_a16w16_asm` (gfx950, gfx942) and `gemm_a4w4_asm` (gfx950) compute A, B and output of 4 GiB or more correctly, where they used to return wrong output without an error. Below 4 GiB the outputs are unchanged.

Validation and unverified scope

The measurements below ran at 49635be44b. This revision merges `main`, which adds `BpreShuffle_256x256_ps` (#6067), a launcher check for it, retuned a4w4 rows and a split-K test in `test_gemm_a4w4.py`; the changes of this PR are the same. At this revision, on MI355X: `_ps` is exact with the output at 4.005 and 4.06 GiB (M × N × K = 20192 and 20480 × 106496 × 512) and through its tuned row at 20192 × 106496 × 16384, and the launcher rejects it with A at 4 GiB. `gemm_a4w4` at 36352 × 59136 × 8192 (tuned row `BpreShuffle_256x256`) is exact with the patched code object, and 12909406 of 2149711872 elements are wrong with the original.

**CI.** At edce462c25 (49635be44b plus a `main` merge), the CI ran `test_gemm_a16w16.py` on MI355X (17 `asm_4g` cases exact) and MI300X (11 exact), and `test_gemm_a4w4.py` on MI355X (4 exact) and MI300X; all passed. At this revision the tests run 72 `asm_4g` cases on MI355X, 64 on MI300X and 136 a4w4 cases on MI355X.

**Method.** Original (`main`) and patched code objects were run with the same launcher build, one process per case, kernel selected by name, every output element compared with an fp32 reference (computed from the bf16 inputs, or from the dequantized FP4 inputs for a4w4). The harness in the gist, used for the gfx942 runs, also checks that a guard region after the output stays untouched. Allocation-end cases place A or B at the end of an allocation and confirm that the memory after it is unmapped, so a read past it faults.

**Reproducing.** In the gist: `reproduce.sh MAIN_CHECKOUT PR_CHECKOUT` regenerates the 48 code objects from `main`'s and compares them with this branch (all 48 identical); `make_cases.py` and `run.py` run the bf16gemm correctness, allocation-end and performance cases on either architecture (instructions in `0_README.md`). The op tests: `python3 op_tests/test_gemm_a16w16.py` (default `-t` list, includes `asm_4g`) and `python3 op_tests/test_gemm_a4w4.py`.

**Disassembly round trip.** Disassembling and reassembling without patches gives back byte-identical code, kernel descriptors and metadata for all 81 shipped code objects of these directories: 59 on gfx950 (24 bf16gemm, 35 f4gemm) and 22 on gfx942.

MI355X (gfx950):

- **bf16gemm, all 24 kernels: 228 of 228 cases exact.** A, B and the output (bf16 and FP32), each one row or column below 4 GiB, at 4 GiB, and at about 6 GiB with a partial last tile. For the `*_clean` kernels: A and B at split-K 4.
- **f4gemm `BpreShuffle_256x256`:** exact with `log2_k_split` 0 and 1 for A at 4 GiB and at 6 GiB with a partial last M tile, B at 4 and 6 GiB, and output at 4.005, 4.05, 8.0 and 8.01 GiB.
- **f4gemm `noBpreShuffle_256x256`:** exact for A at 4 and 6 GiB, B at 4 and 6 GiB, and output at 4.05 and 8.0 GiB.
- **Operands at the end of an allocation:** 854 cases on every kernel, through the heuristic and named, with A or B at the end of an exact-size allocation, small K, and a last tile of 1 to tile_M − 1 rows, or M = 1 or 2.
  - Original kernels: 63 faults, 58 of them with M = 1 or 2.
  - Patched kernels: 9 faults, each also faulting with the original: 8 are the heuristic picking a `*_splitk_clean` kernel without a semaphore, 1 is `noBpreShuffle_256x256` reading past the end of B (both under "Existing bugs not fixed here").
  - With the `s_cselect_b32` saturation removed, the patched kernels fault in 170, which is what the allocation-end op tests guard against.
- **Normal shapes, all 26 kernels:** outputs bit-identical to the original's, including partial M tiles, except split-K with atomic accumulation, which is exact but differs in summation order.
- **Op tests on this branch:** `python3 op_tests/test_gemm_a16w16.py` passes; `-t asm_4g` runs 72 cases on MI355X, all exact (36 past 4 GiB, 36 allocation-end). The a4w4 checks run 136 cases, all exact (68 and 68). With the original code objects the allocation-end cases pass and the 4 GiB checks fail on their first case; with the saturation removed, both files fault on their first alloc

[truncated in readiness report; see the PR description for the full answer]

PR-specific question

*Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?*

The patched code objects are the ones `gemm_a16w16_asm` loads on gfx950 and gfx942 (bf16 input, bf16 or FP32 output) and `gemm_a4w4_asm` loads on gfx950 (bf16 output), whether the kernel was picked by heuristic, tuned row or name. There is no size gate: each descriptor covers at most one tile, so the only limits left are per tile (see "Remaining limits" under Changes).

Changed op tests

- `op_tests/test_gemm_a16w16.py` (`-t asm_4g`, gfx950 and gfx942): no existing test runs the ASM kernels near 4 GiB or with an operand at the end of an allocation. On `main` the 4 GiB check fails on its first case (A past 4 GiB, almost all of the output wrong). The allocation-end check guards the saturation: without it the patched kernels fault there.
- `op_tests/test_gemm_a4w4.py` (gfx950): the default shapes include 32768 × 106496 × 16384 (a 6.5 GiB output), but its tuned row is a 224x256 kernel, which was already correct, so no existing shape ran 256x256 at 4 GiB. On `main` the 4 GiB check fails on its first case: rows 27–255 of the first tile come back wrong. The checks select kernels from the f4gemm table by `bpreshuffle=1, ps=0`, so the heuristic and `_ps` do not affect them.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

JohnNikolay84
JohnNikolay84 previously approved these changes Nov 24, 2026
Comment thread op_tests/test_gemm_a4w4.py Resolved
Comment thread op_tests/test_gemm_a16w16.py Resolved

@JohnNikolay84 JohnNikolay84 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are still unresolved comments

@JohnNikolay84
JohnNikolay84 dismissed their stale review November 24, 2026 10:21

unresolved comments

The asm 4 GiB and allocation-end checks now read the kernel names from
the CSV tables that the launchers read (hsa/<gfx>/bf16gemm and
hsa/<gfx>/f4gemm). The tests no longer name kernels in string literals.

- test_gemm_a16w16.py: the checks run every kernel in the bf16gemm table.
  The splitK column selects the split-K setup and the output dtype.
- test_gemm_a4w4.py: the checks run every f4gemm kernel with
  bpreshuffle=1 and ps=0. N is 512, so 128x512 gets a whole N tile.
- The allocation-end M is tileM + 1 from the table, so each kernel gets
  a 1-row last tile.
@JohnNikolay84
JohnNikolay84 merged commit 35df6ef into main Nov 25, 2026
69 of 70 checks passed
@JohnNikolay84
JohnNikolay84 deleted the users/bartgips/fix-asm-bf16gemm-4gib branch November 25, 2026 13:10
Fyzyukk added a commit to Fyzyukk/aiter that referenced this pull request Nov 26, 2026
Conflict with ROCm#7883 (ASM a4w4 GEMMs exact past 4 GiB) in op_tests/test_gemm_a4w4.py main():
both sets of gfx950 checks run (main's ASM last-tile / 4 GiB checks, then the OPUS
large-offset check, kid sweep and fallback check).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ASM ci:mi300x Run MI300X standard and OPUS CI on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants