Repository navigation
Alizaidy/bf16 gemm tuning 081426 - #4749
Merged
Merged
Conversation
…e buffer as the ds_load without waiting for all the ds_loads to drain. we leave one buffer gap between tdm load and lds read now
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
# Conflicts: # aiter/configs/model_configs/dsv4_bf16_tuned_gemm.csv
valarLip
approved these changes
Aug 18, 2026
nsakkine
pushed a commit
to nsakkine/aiter
that referenced
this pull request
Aug 21, 2026
* Use triton for DSv4 Pro bf16 GEMM * Add bf16 GEMM tuning for DSv4 Pro * Tune triton BF16 GEMMs more for DSv4 * tuned m=64,128 * tuned m=1,32 * tuned m=256 * tuned m=16k * black formatting fix * fixes WAR hazard in kernel rn due to compiler issuing tdm load to same buffer as the ds_load without waiting for all the ds_loads to drain. we leave one buffer gap between tdm load and lds read now * Add time, tf, BW * Resolve conflicts with main * Add DSv4 Flash configs * Fix configs * Restore opus on shapes where it performs better * Adjust configs --------- Co-authored-by: ahmed-bsod <[email protected]>
nsakkine
pushed a commit
to nsakkine/aiter
that referenced
this pull request
Aug 24, 2026
* Use triton for DSv4 Pro bf16 GEMM * Add bf16 GEMM tuning for DSv4 Pro * Tune triton BF16 GEMMs more for DSv4 * tuned m=64,128 * tuned m=1,32 * tuned m=256 * tuned m=16k * black formatting fix * fixes WAR hazard in kernel rn due to compiler issuing tdm load to same buffer as the ds_load without waiting for all the ds_loads to drain. we leave one buffer gap between tdm load and lds read now * Add time, tf, BW * Resolve conflicts with main * Add DSv4 Flash configs * Fix configs * Restore opus on shapes where it performs better * Adjust configs --------- Co-authored-by: ahmed-bsod <[email protected]>
junhaha666
pushed a commit
that referenced
this pull request
Aug 26, 2026
* opus: pre-compiled (.co) a16w16 kid family for gfx1250 The 4wave_compute pipeline needs __builtin_amdgcn_pin_vgpr, amdgpu_num_vgpr and -mllvm -amdgpu-expert-scheduling-mode. A release ROCm toolchain has none of them, and aiter's hip_flag_checker SILENTLY DROPS flags the compiler rejects -- so a JIT build would not fail, it would quietly produce a differently-scheduled kernel. Only the device compile is bypassed: the kernel is built ahead of time into gen_co/<arch>/*.co and loaded at runtime, while the host launcher, manifest, tune lookup, dispatch and CSV schema all come from the ordinary codegen. Moving the family back to JIT later is a branch in codegen/gen_instances_gfx1250.py. gen_co/co_kernels.json is the single source of truth: opus_gemm_common.py turns it into the kid table that drives codegen, and build_co.py re-reads it to produce the images, so the .co filename, its extern "C" symbol and the launcher symbol are one string by construction. Two pipelines. The reference one is a verbatim port of the standalone kernel (128x256x128 only). The _wl_ one generalises it: the wave layout (4x1 / 2x2 / 1x4) is a template parameter, the msb split is derived rather than fixed at 4, the pin bases follow the accumulator block instead of sitting at a constant, and the macro register lists are templates so their length can follow the tile. Layout turns out to be a real per-shape tuning axis, not just a small-tile enabler: on 128x256 it swings -3.9% to +11.4% by shape. Tuning: candidate_kids_for_shape / kid_rejects_shape / candidate_splitK in opus_gemm_tune.py learn about the family, so gemm_a16w16_tune.py --libtype opus picks it up. On the PR #4749 M=2048 K=7168 slice it wins 4 of 6 N values, up to 2039 TFlops at N=32320. 143 variants ship, each verified for numerics, out-of-bounds writes, zero spills and 100% pin retention. Three known defects are written up with reproducers in gen_co/KNOWN_ISSUES.md; two are blocked by static_assert and the third (a race at B_N <= 128 on large shapes, present in the reference pipeline too) is handled by leaving the 60 affected variants out of the table. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * opus co: force 1 WG/CU via LDS padding, fixing a race at small tiles Any variant whose A/B LDS segments fit twice in the 320 KB budget (<= 160 KB) let a second workgroup co-reside on a CU, which oversubscribes the per-CU TDM request budget. The result was non-deterministic wrong output at large grids: same seed and data, the wrong-element count changed every run, including runs that came out clean. The predicate separated the population exactly -- of 203 variants, every one at LDS <= 160 KB raced and every one above it was clean (50/50 at B_N=64, 10/51 at B_N=128, 0/51 at B_N>=192). Both pipelines were affected, reference included, so this predates the wave-layout work rather than coming from it. Pad LDS_BYTES past 160 KB when the real footprint is below it, the same trick opus_cluster_tdm_splitk_ws_traits_gfx1250 already uses in this header for the same reason; the pad tail is never accessed. build_co.py mirrors the formula host-side and had to learn about the pad -- its group_segment_fixed_size == traits check is what caught the drift instead of shipping it. The 60 variants dropped in the previous commit are restored: 143 -> 204, all verified over 9 shapes x 2 repeats covering large grids, N < B_N / M < B_M, and M/N/K tails, with sentinel checks for out-of-bounds writes. Zero spills, zero lost pins. Two things that made this hard to see are written up in gen_co/KNOWN_ISSUES.md: a tolerance check is a poor detector when the tolerance is the unknown (an outlier comparison across kids, which all compute identical math, found it), and a clean standalone replay proves nothing about a race. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * opus co: state what the 1-WG/CU evidence actually shows The previous message asserted the mechanism -- "oversubscribes the per-CU TDM request budget" -- which was borrowed from the split-K pipeline's comments and never measured for this one. What the data supports is narrower and now written down as such. VGPR and LDS both scale with the tile, so the raw LDS<=160KB correlation cannot separate them; the population contains the control that can. 71 variants whose registers would admit two waves per SIMD but whose LDS does not are all correct, against 61 where both admit two and all are wrong. Those two groups differ only in the LDS-driven occupancy, so the variable is workgroups-per-CU. Why 2 WG/CU breaks it stays open, with the candidates and the experiment that would separate them recorded. The fix rests on "correct at 1 WG/CU", which the control establishes, not on the mechanism. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * opus co: rule out synchronisation as the cause of the 2 WG/CU corruption Occupancy cannot by itself break a correct kernel, so the natural reading of the previous commit was that 2 WG/CU merely exposes a latent sync hole. It does not. With the LDS pad disabled so the kernel really runs at 2 WG/CU, neither skipping the -3 cluster barrier when the cluster is 1x1, nor draining every outstanding TDM and LDS read and taking a full s_barrier() before every single tile, changes the failure at all -- the wrong-element counts stay in the same range and stay non-deterministic. No ordering fix inside the workgroup closes it, so the fault is not between this workgroup's own waves. What remains is something shared per CU and not covered by barriers. The leading suspect, recorded as a hypothesis rather than a finding, is the LDS address in the TDM descriptor: it is workgroup-relative, and if the TDM engine does not add the workgroup's LDS base then a second co-resident workgroup aims its transfers into the first one's LDS -- immune to barriers, corrupting both, and vanishing at 1 WG/CU where the base is zero. Confirming it needs an ATT capture or the TDM ISA description. No functional change: the debug switches used for these experiments are removed and the padded build is re-verified. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * opus splitk: make the partial workspace dtype a per-kid choice The gfx1250 _ws split-K families declared an fp32 partial in the traits (D_C) and then wrote a bf16 one, because the pipeline took its element type from a build-wide OPUS_WS_BF16 macro instead of the traits. The reduce, the explicit instantiations and the host allocator each carried their own copy of that assumption, so -DOPUS_WS_BF16=0 moved only some of them and produced NaN. D_C is what the traits already documented the slot as, so let it be the one source: the pipeline stores Traits::DataC, the reduce is instantiated with the same D_C, the dispatch table and the explicit instantiations use the kid's splitk_workspace_dtype, and the host sizes the buffer from the same field. Both partial widths are instantiated since the choice is now per instance. The generated launcher checks workspace.element_size() against sizeof(D_C): a .so stale with respect to the kid table used to be a GPU page fault with nothing pointing at the cause. Default is bf16. It clears the gate the tuner actually applies -- at rtol=atol=5e-2 for a bf16 output, err_ratio is 0.004-0.012 against a 0.05 line, flat from split_k=1 to 16 -- at half the workspace and half the reduce's read traffic. Candidate selection drops fp32-partial kids unless OPUS_TUNE_FP32_WS=1, and the 4 GiB workspace cap now measures in the kid's own element width rather than assuming 4 bytes. Two things that only bite during a sweep: _get_opus_workspace was @functools.cache'd, pinning one buffer per (device, shape, dtype) for the process lifetime. Bounded for inference, which sees a handful of shapes; the tuner walks every (tile padding x split_k) pair, which is 1065 distinct buffers totalling 450 GiB on a 432 GiB part. Nothing needs them to outlive the call. BF16WS_EXACT_REDUCE_SHAPES is a gfx942 whitelist of N values, all powers of two, gated only on "uses a bf16 workspace". That was equivalent to "is a gfx942 kid" only while the gfx1250 kids declared fp32, so flipping the default subjected them to it and erased the whole _ws family at N=384 / 32320 / 129280. Scoped to the family it was written for. Co-authored-by: Cursor <[email protected]> * opus co: fix the 4wave write-after-read and wire the family into tuning Two structural bugs in the 4wave pipelines, both of which the earlier 1-WG/CU LDS pad only hid: A step refills the slot it is reading -- slot g%P is consumed by step g and reloaded at its end for step g+P -- so the handshake carries a write-after-read as well as the fill it was written for. That needs the reads retired, not merely issued: a barrier orders waves, not their in-flight LDS traffic. Drain with s_wait_dscnt(0) before the handshake, and arrive-and-wait as one s_barrier: split across kBarrierAhead WMMAs, a wave that is ahead could signal for the next step into this step's count. A step past the last is not a no-op transfer. Measured on gfx1250, one whose origin is at or past either extent zero-fills its whole tile in LDS, and C stages in a slot one of those is aimed at. The tensorcnt drain has to happen before the staging rather than after the peeled step. .co artifacts rebuilt for all 204 variants. build_co.py can now append -DOPUS_CO_NO_1WG_PAD through --device-flag with _expected_lds following suit, so the pad can be measured off without editing the table. Tuning: the candidate filter nominates .co kids (top-N tiles by grid-occupancy fit x top-N cluster dims, no split-K to minimise over), and kid_rejects_shape answers both .co tags rather than only a16w16_4wave_co. The wl variant used to fall through to the gfx942 bf16-workspace whitelist, which rejected all 140 of its kids at any N outside that table -- invisibly, since a candidate that was never nominated reads the same as one that lost. At M=256 N=384 that left 6 of 106 candidates, all one tile and no split-K, which looked like a fixed ~25us floor; with the family restored the same shape tunes to 7.0us. dsv4_bf16_tuned_gemm.csv carries the re-tuned winners for the 84 M>=256 DSV4 shapes, opus vs triton measured in one session. opus takes 72 of them, 45 on 4wave_wl_co. Co-authored-by: Cursor <[email protected]> * opus co: add the B_K=256 tiles the wl family was missing Of the 84 DSV4 shapes, 38 tuned to a JIT _ws kid. Checking each of those winning tiles against the .co pipeline's own constraints says why: 25 of them are legal in that pipeline today and simply had no entry, and the bulk of those want B_K = 256, which the family had none of at any tile. Only 10 are actually blocked by a static_assert, and 0 by the B_K>128 one -- every winning deep-K tile already has kNSub == 1. So this is 15 entries and no code: 64x64x256 in the w1x4 and w2x2 layouts, 32x64x256 in w1x4 (w2x2 would need kExpM == 1), five clusters each. They build clean -- no spills, LDS 198 KB and 161 KB against the 320 KB budget, wmma_dst_hi non-zero so the VGPR pin still takes -- and all 15 verify numerically. Re-tuning the 12 shapes those tiles had been losing moves 8 of them to wl_co, 1.156x geomean and up to 1.331x, and takes the group from 7/12 to 12/12 against triton. Over all 84 that is 77 wins at 1.331x geomean. The four it does not take are the informative ones: all four run at splitK 2-5, and no .co family supports split-K, while the eight it takes are at splitK 0 or 1. A .co entry can only win a shape that does not want split-K, which is also how to price the remaining static_assert before restructuring anything for it -- the tiles it guards are the smallest in the set, and small tiles are what lean hardest on split-K. Docs follow: the README covered only the compute family and predates the wl one entirely, so it gains both families, the wave_layout field, the --device-flag passthrough, and that split-K boundary. KNOWN_ISSUES records the tuner-side defect that silently withheld the whole wl family from nomination, and the measurement above in place of the guess that fixing the two asserts was where the performance was. Co-authored-by: Cursor <[email protected]> * opus co: re-tune the shapes the new tiles reached, and price issue 3 Adding the B_K=256 entries only helped the 12 shapes that were re-tuned against them. Ten more could reach the same tiles and had not been, which is its own trap: a new candidate does nothing for a shape nobody re-tunes, and the tuned CSV gives no sign that it is out of date. All ten improve -- 1.083x geomean, up to 1.322x on 8192x64x4096, which had been running a 64x64 tile at half occupancy -- and the group goes 8/10 to 10/10 against triton. Over all 84 that is 79 wins at 1.344x geomean, wl_co now taking 58. The remaining gap is 8 shapes that no legal .co tile fills without split-K but B_M=16 would, and B_M=16 is what issue 3's static_assert refuses. The occupancy case for lifting it is real; the scheduling budget is what kills it. A K step only has (B_K/64) * kNumNSub * kExpM * kExpNPerSub * kExpKHalf WMMAs to hide the ds_reads, the handshake and the TDM issue in, and every .co kernel winning a shape today has at least 16 of them. That floor is now measured rather than guessed. 32x64x128 is legal today and has the same grid as 32x64x256, so it differs only in K depth and budget -- 8 against 16. Added as a probe and tuned head-to-head on the five shapes 32x64x256 owns, it won none of them, inside noise at 0.975-1.017x. B_M=16 lands at 8 or 4, and widening B_N to buy budget halves the grid and gives the occupancy straight back. The probe entries are removed again; KNOWN_ISSUES records the numbers so the restructure is not attempted on the occupancy argument alone. Co-authored-by: Cursor <[email protected]> * opus co: reorganise the .co docs and bring the branch back under black/ruff The style checks were the urgent part. At the merge-base with main these files are black-clean and carry 3 ruff findings; on this branch black wanted 5 of them reformatted and ruff was up to 11. CI runs psf/black@stable over the whole tree and pins ruff 0.16.0, so the branch was red on both. Reformatted the nine Python files the branch touches and cleared their ruff findings: subprocess.run in build_co.py now says check=False explicitly -- it reads returncode itself so that CalledProcess Error does not swallow hipcc's diagnostics -- and the file gets the exec bit its shebang implies. The five findings left repo-wide are all in files this branch never touched. black is clean across all 1063 files. KNOWN_ISSUES was still shaped like a list of four live defects when two are fixed and the other two have been measured and declined. Open constraints now lead, each carrying its verdict; the resolved pair moves below and issue 1 loses the forensic narrative it no longer needs, keeping the reproducer, the occupancy control group and the damage geometry that identified the write-after-read. The dead ends and the detection techniques stay where they were -- they are the reusable part. Numbering is unchanged so existing references still resolve. README gains the two numbers that decide whether a tile is worth adding at all: the grid it produces, which with no split-K is the only parallelism available, and its WMMA budget, which is what the pipeline has to hide the ds_reads, the handshake and the TDM issue in. Everything that wins a shape today has at least 16 of the latter, and a tile with 8 won none of the five it was aimed at -- so the additions worth making raise B_K, which buys budget without costing grid. Co-authored-by: Cursor <[email protected]> * remove triton dsv4pro duplicate config Signed-off-by: binglue.lyu <[email protected]> * opus splitk: scope the bf16 partial to the gfx1250 _ws families The bf16 split-K partial was applied by flipping the default on OpusGemmInstance, which is every kid on every arch, not just the two gfx1250 families it was measured on. gfx942's codegen reads splitk_workspace_dtype to pick its own workspace type, so the flip retargeted that arch's split-K families as well, and em3en4_lds1_pgr2_sk static_asserts an fp32 workspace: static assertion failed due to requirement 'std::is_same_v<__bf16, float>' EM3EN4 LDS1/PGR2 splitK main kernel writes fp32 workspace Invisible locally because everything here is built for gfx1250; the wheel job builds gfx942;gfx950 and nothing else, so this only ever appeared in CI. Default goes back to fp32 and the two _ws tags opt in by name after the table is assembled, which also leaves the three gfx942 variants that were already bf16 (kbuf1_sk, kbuf2v_bk128_sk, quad_mfma32_kbuf1_sk) exactly as they were. Verified by building module_deepgemm_opus with GPU_ARCHS="gfx942;gfx950" -- the CI configuration -- which now compiles clean, and by re-checking gfx1250: kid 20417 still declares a bf16 partial and the golden sweep is 6/6. Co-authored-by: Cursor <[email protected]> * opus: condense the comments added to the gfx1250 headers The comments introduced with the WAR fix and the per-kid partial dtype ran five and six lines, in files whose own comments are one to three and stay inside 89 columns. Same points in 23 lines instead of 54, nothing over 86 columns. Comments only; the golden sweep is 6/6. Co-authored-by: Cursor <[email protected]> * opus: expose the gfx1250 matrix_a_reuse/matrix_b_reuse WMMA hints wmma::operator() and the swap_ab adaptor take reuse_a/reuse_b as template parameters and forward them to the builtins' last two arguments, which until now were hardcoded false. Both default to false, so every existing call site emits exactly the instruction it did before. The hint asserts that this WMMA's operand repeats the previous one's and may be read from the source cache rather than VGPR RAM; it is undefined if the operand does not in fact repeat, so a caller has to be able to prove it. matrix_a_reuse is a no-op on MI450, which is why the swap_ab adaptor forwards them crossed -- under that adaptor `a` lands in SRC1, so reuse_a is the one that reaches the bit that works. No caller passes true yet. Co-authored-by: Cursor <[email protected]> * config: add the tuned 512x129280x7168 gfx1250 row N=129280 had only been tuned at K=4096, so this shape had no row at all. opus takes it at 555.84 us against triton's 1803.94 us (3.25x), both from the same tuning session; the winner is 4wave_wl_co 128x128x256x128 w2x2 c4x4 at splitK=0, err_ratio 0.0. Only the new row is added. 512x32320x7168 was re-tuned alongside it and is left alone -- it already carries a faster measurement of the same kid -- and every other row is byte-identical. Co-authored-by: Cursor <[email protected]> * config: move the tuned 512x129280x7168 row to dsv3 512x129280x7168 is a dsv3 shape, not a dsv4 one -- it was added to the dsv4 table in error. dsv3 already carried a placeholder row for it (triton, us=0, kernelName=auto), so this replaces that with the measured result rather than adding a row: opus at 555.84 us, 4wave_wl_co 128x128x256x128 w2x2 c4x4, splitK=0, err_ratio 0.0. Triton measured 1803.94 us on the same shape in the same session. dsv4 goes back byte-for-byte to what it was before that row landed. Co-authored-by: Cursor <[email protected]> --------- Signed-off-by: binglue.lyu <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: binglue.lyu <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GEMM A16W16 tuned kernel time (µs)
DSv4 Pro K=7168
N=64, K=7168
N=384, K=7168
N=512, K=7168
N=1024, K=7168
N=2048, K=7168
N=32320, K=7168
DSv4 Flash K=4096
N=64, K=4096
N=256, K=4096
N=512, K=4096
N=1024, K=4096
N=2048, K=4096
N=129280, K=4096