Repository navigation
[ASM] [HIP] [CK] Add persistent gfx950 f4gemm 256x256 kernel (_ps) - #6067
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
a986eaf to
9e6c0da
Compare
9e6c0da to
bd7e387
Compare
|
Thanks for this — I reproduced the speedup on a gfx950 (256 CU) box and it holds up across the board. I also found one silent correctness regression that I think blocks the What I verified A/B on the same machine, same process settings (run_perftest, 51 iters + 5 warmup), forcing kernelName="_ZN5aiter42f4gemm_bf16_per1x32Fp4_BpreShuffle_256x256E", baseline = main, Performance reproduces: 26/26 shapes faster, no regressions. Geomean ratio 0.9015 (1.109x) vs the 0.8768 (1.140x) in the PR description. Large shapes match within 0.5%: geomean: 0.9015 (1.109x) PR description: 0.8768 (1.140x) Two small-shape rows (2048x8192x1024, 4096x4096x8192) show essentially no gain on my box while the PR table claims 5–6%. Worth a re-measure — likely iteration count / clock Accuracy on the normal path is fine: the added test passes on M=1/127/129 (unaligned rows) with err=0, and output checksums across the 26 benchmark shapes differ from baseline by Blocking 1: split-K is broken when the output is a single tile Reverting the PR makes every one of these pass, so it is introduced here. The trigger is exactly tiles == 1 (M ≤ 256 and N ≤ 256) with log2_k_split >= 1. No error is raised — it Root cause, mxfp4_gemm_4w.hip: uint32_t tiles_magic = split ? 0xFFFFFFFFu / tiles + 1 : 0; For tiles == 1, 0xFFFFFFFFu / 1 + 1 wraps to 0 in uint32, so slice is always 0 and tile = work_item. Every CTA computes K slice 0, and three of the four address tiles that do not tiles=1 slices=4 tiles_magic=0x00000000 tiles=2 slices=4 tiles_magic=0x80000000 (correct) The same divisor-of-1 hazard is already handled a few lines below, so the fix can mirror it: // existing, a few lines below: // proposed: Reachability: today's heuristic does not auto-enable split-K for these shapes (I checked the unified aiter.gemm_a4w4 path on M=64/128/192/256 x N=256 x K=16384/32768, all exact), Repro: out = torch.zeros((256, 256), dtype=torch.bfloat16) Please also add a tiles == 1 split-K case to test_gemm_a4w4.py — the split-K path has no coverage at all right now. Blocking 2: bias / beta are silently ignored max|out - (ref + bias)| = 35.4993 Reverting the PR makes every one of these pass, so it is introduced here. The trigger is exactly tiles == 1 (M ≤ 256 and N ≤ 256) with log2_k_split >= 1. No error is raised — it Root cause, mxfp4_gemm_4w.hip: uint32_t tiles_magic = split ? 0xFFFFFFFFu / tiles + 1 : 0; For tiles == 1, 0xFFFFFFFFu / 1 + 1 wraps to 0 in uint32, so slice is always 0 and tile = work_item. Every CTA computes K slice 0, and three of the four address tiles that do not tiles=1 slices=4 tiles_magic=0x00000000 tiles=2 slices=4 tiles_magic=0x80000000 (correct) The same divisor-of-1 hazard is already handled a few lines below, so the fix can mirror it: // existing, a few lines below: // proposed: Reachability: today's heuristic does not auto-enable split-K for these shapes (I checked the unified aiter.gemm_a4w4 path on M=64/128/192/256 x N=256 x K=16384/32768, all exact), Repro: out = torch.zeros((256, 256), dtype=torch.bfloat16) Please also add a tiles == 1 split-K case to test_gemm_a4w4.py — the split-K path has no coverage at all right now. Blocking 2: bias / beta are silently ignored max|out - (ref + bias)| = 35.4993 The code comment says this matches the previous 256x256 kernel, but a comment is not a guard. Please add to the persistent branch: AITER_CHECK(bias == nullptr && beta == 0.f, func, " ", kname, ": bias/beta not supported"); Request 1: please drop hsa/gfx950/f4gemm/src/** We'd like hsa/ to keep containing binaries only, consistent with every other kernel there — no .hip sources and no build.sh in this PR. Ship just the rebuilt .co. Two consequences to handle once the source is out of tree:
Request 2: follow the mla_v4 convention for the persistent flag Please align the CSV and the dispatch with hsa/gfx950/mla_v4/mla_v4_asm.csv (and hsa/gfx950/mla/mla_asm.csv), which already model exactly this case: Concretely:
Non-blocking
|
Address review on ROCm#6067: - The persistent kernel gets its own .co and symbol, f4gemm_bf16_per1x32Fp4_BpreShuffle_256x256_ps; the ps=0 256x256 kernel is restored unchanged. The manifest gets a ps column (mla_v4 convention) and a comment noting that ps=1 kernels read num_cu at kernarg offset 372. - Fix split-K with a single output tile (M, N <= 256): the slice divisor wrapped to 0 for tiles == 1, so every workgroup computed K slice 0. - ps=1 kernels get a separate PsKernelArgs (KernelArgs + num_cu, 376 B); ps=0 kernels get the unchanged 372 B KernelArgs. static_assert on both layouts; KernelArgs is zero-initialized. - bias/beta raise for ps kernels instead of being ignored. - ps_supported() gates the ps kernel per call: a supported ps kernel replaces its ps=0 twin in the heuristic, an unsupported one is skipped; an explicitly named ps kernel still fails loudly. - The host applies the kernel's split-K rule (fewer K slices until every work item has a CU), so the grid equals the work. - hsa/ stays binary-only: the HIP source and build script are removed. - Tuner: reads the manifest with comment="#" and tunes ps and ps=0 kernels as separate candidates. - test_gemm_a4w4.py: the ps kernel runs where ps_supported() holds, plus a split-K test on both 256x256 kernels, including single-tile shapes. Accuracy (rel. err vs fp32, log2_k_split 0/1/2/3, ps kernel): 256x256x8192 1.65e-3 / 2.47e-3 / 3.15e-3 / 4.05e-3 128x128x16384 1.68e-3 / 2.46e-3 / 3.15e-3 / 4.07e-3 2048x2048x8192 1.66e-3 / 2.46e-3 / 3.17e-3 / 3.17e-3 (8 slices > CUs -> 4) Same as the ps=0 kernel on every row, except that the ps=0 kernel is wrong at 128x128x16384 without split K (N % 256 != 0, pre-existing). test_gemm_a4w4.py default list: 138 checks pass. Perf vs the ps=0 256x256, MI350X (gfx950, 256 CUs, 1000 W), run_perftest 51 iters + 5 warmup, both kernels by name, median of 3 alternating runs: 26/26 PR shapes faster, geomean 0.882 (1.134x), best 0.613 (512x32768x512), worst 0.996 (2048x8192x7168). .co: hipcc 7.14.60850, AMD clang 23.0.0git (llvm-project 46fcb339), sha256 8b541b78bff2de71f3cb48db89572f1eb4adc403e09a675be9a8a546a60ff938, .text sha256 91864066e8826818faff395eb15145ad65d5911c53cbc6659e1863e0283e27f7.
|
Thank you @yzhou103 for the thorough review. Everything is addressed in Blocking
Requests
Non-blocking
The two small shapes: 2048x8192x1024 and 4096x4096x8192 are exactly 256 tiles, a single round, so the only gain is the epilogue overlap. That makes them sensitive to the board's power cap and clocks. On our MI350X (1000 W) they measure 0.93 and 0.92 in separate processes, in both orders. Could you share the power cap and clocks of your run ( Also found while testing: the existing ps=0 |
|
889c02b fixes the Atom CI failures caused by this PR. ATOM's dense MXFP4 linear still calls gemm_a4w4(x, weight, x_scale, weight_scale, y) (atom/model_ops/linear.py#L322-L328) from before #1679 removed the out parameter. Its output buffer therefore lands in the bias slot with beta 0, and the bias/beta guard from blocking 2 rejected that. The f4gemm asm kernels ignore bias, so with beta == 0 the result is exact (D = alpha·A·B + beta·C). The guard now rejects only bias with beta != 0, both for an explicit kernel name and in ps_supported(), and there's a test for the ATOM call pattern. The ATOM call site is probably worth updating separately. |
|
It seems there are conflicts, please resolve the conflicts, thanks! |
|
@yzhou103 Thanks! Resolved & merged the latest main |
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 |
|
Hi @tarik-sarac please resolve conflicts |
yzhou103
left a comment
There was a problem hiding this comment.
I validated head 2ca6b9b on gfx950 with 256 CUs, using PyTorch 2.10.0+rocm7.2.4 and HIP 7.2.53211.
- Ran the unmodified python3 op_tests/test_gemm_a4w4.py. All 373 normal-path comparisons passed. The 18 split-K configurations also ran for both kernels. Both implementations still log failed! at the default elementwise tolerance, consistent with the documented BF16 atomic accumulation behavior; no catastrophic failures occurred, and the script exited with code 0.
- Tested all 364 changed CSV rows, comparing the previous and new kernel selections with identical quantized inputs. All these rows use splitK=0. All 728 comparisons passed, with finite outputs and err_ratio=0, against an FP32 dequantized GEMM reference rounded to BF16.
- For performance, used run_perftest with automatic buffer rotation, 5 warmup calls and 51 measured calls per round, taking medians over three alternating old/new rounds. Quantization and preshuffle were outside the timing. The 357 persistent-kernel replacements reduced geometric-mean latency by 7.9%. The single 160x128 replacement reduced latency by 45.8%; the six unchanged-kernel rows were approximately flat.
- Repeated all seven initially slower persistent configurations with reversed execution order, together with the six unchanged-kernel controls. Four became faster; the remaining three were only 0.27–0.91% slower, comparable to the approximately ±1% variation observed in the controls. I did not reproduce a clear, stable performance regression.
Based on this validation, I do not see a remaining correctness or performance blocker for merging on gfx950. The earlier stale-timing issue is addressed, so that discussion can be resolved.
As a follow-up, please make the expected BF16 split-K tolerance explicit and assert the acceptance criterion while retaining catastrophic_check=True, so ordinary accuracy regressions cannot merely print failed! and still pass CI.
Summary
This PR adds a persistent 256x256 MXFP4 GEMM kernel for gfx950,
f4gemm_bf16_per1x32Fp4_BpreShuffle_256x256_ps, next to the existingBpreShuffle_256x256kernel. It follows the mla_v4 convention for persistent kernels: its own_ps.co and symbol, and a documentedpscolumn in the manifest. The tuned a4w4 configs that picked the 256x256 kernel now pick the_psone.Changes
hsa/gfx950/f4gemm/f4gemm_bf16_per1x32Fp4_BpreShuffle_256x256_ps.co_ZN5aiter45f4gemm_bf16_per1x32Fp4_BpreShuffle_256x256_psEhsa/gfx950/f4gemm/f4gemm_bf16_per1x32Fp4.csvpscolumn (0 on every existing row) with a one-line comment, plus a note that ps=1 kernels readnum_cuat kernarg offset 372; new_psrowcsrc/py_itfs_cu/asm_gemm_a4w4.cuPsKernelArgswithnum_cufor ps=1 kernels, static_assert on both layouts,ps_supported()checks, persistent launch, heuristic fallbackcsrc/ck_gemm_a4w4_blockscale/gemm_a4w4_blockscale_tune.py//comments; tunes ps and non-ps kernels as separate candidatesaiter/configs/a4w4_blockscale_tuned_gemm.csv,aiter/configs/model_configs/*a4w4_blockscale_tuned_gemm.csvBpreShuffle_256x256now name the_pskernel (kernel name only)op_tests/test_gemm_a4w4.pyps_supported()holds; new split-K test, including single-tile shapeshsa/stays binary-only: no source or build script in this PR. See "Provenance" below.Kernel arguments
KernelArgsstruct is unchanged (372 bytes,log2_k_splitat offset 368). They get the same 372 bytes as before this PR.PsKernelArgs { KernelArgs args; unsigned int num_cu; }(376 bytes,num_cuat offset 372). This follows the separate persistent structs inasm_mla.cuandasm_pa.cu, and mla_v4'sMlaV4PsKernelArgs.static_assertinasm_gemm_a4w4.cupins both layouts. The CSV comment notes thenum_cuoffset.KernelArgsis now zero-initialized (KernelArgs args{}).Launcher
bias/beta not supported. Other limits give a message listing them.ps_supported()decides per call. A ps kernel that supports the call replaces its ps=0 twin (same tile). One that doesn't is skipped, so the heuristic falls back to the ps=0 kernel instead of throwing. The cache key includes this bit.ps_supported()limits:ps_supported()instead of approximating it.Fixes from review
log2_k_split >= 1) returned wrong results. The magic divisor for tiles == 1 wraps to 0, so every workgroup computed K slice 0. Fixed in the kernel the same way as the existingsize == 1case:slice = umulhi(work_item, tiles_magic) + (split && tiles == 1 ? work_item : 0). The non-split path is unchanged.Accuracy
Relative error (Frobenius norm) against an fp32 reference,
gemm_a4w4_asmby name, ps=0 kernel vs the new ps=1 kernel:max|out - ref| = 1.0(bf16 rounding) for the ps=0 kernel with a bias of scale 10 and beta = 1, i.e. it also drops bias today. The ps kernel raises instead.Performance
Measured on an MI350X VF (gfx950, 256 CUs, 1000 W cap).
run_perftestwith 51 iterations and 5 warmup (rotated buffers), both kernels by name in one process, 3 alternating runs, median. Times in us. Ratio = ps / ps=0.Testing
Default shape list: 138 checks pass, none catastrophic.
Split-K (new
-mnk_splitklist, log2_k_split 1-3, both 256x256 kernels): single-tile 256x256 and 128x128 shapes, multi-tile 256x512, 512x512 and 2048x2048. None catastrophic. The ps kernel's mismatch ratio at 1e-2 equals the ps=0 kernel's on every row (5-16%, from the bf16 atomic adds).Rejection paths: an explicit
_psname with bias raisesbias/beta not supported. The same call through the heuristic (no kernel name) runs on a ps=0 kernel, as before.Unit tests updated (
op_tests/test_gemm_a4w4.py)Performance benchmarks run
Tested on gfx950 (MI350X). gfx950 only; no other architecture's files change.
Provenance of the .co
8b541b78bff2de71f3cb48db89572f1eb4adc403e09a675be9a8a546a60ff938(30480 bytes)91864066e8826818faff395eb15145ad65d5911c53cbc6659e1863e0283e27f7. A rebuild reproduces the.textexactly. Only the build-metadata note differs, so compare.text.build.shthere runs the command above).Dependencies
No new dependencies.
Breaking Changes
None. The ps=0
BpreShuffle_256x256kernel and its .co are unchanged and still selectable by name.