Skip to content

[ASM] [Bugfix] a4w4: skip f4gemm kernels wrong at a partial N tile (full_n_tile table flag) - #8847

Draft
demonsan wants to merge 3 commits into
mainfrom
demonsan/issue-6255-a4w4-tuner-narrow-n
Draft

demonsan wants to merge 3 commits into
mainfrom
demonsan/issue-6255-a4w4-tuner-narrow-n

Conversation

@demonsan

@demonsan demonsan commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Refs #6255 (the 128-CU cu_num=128 tuning coverage stays open, this PR does not add tuned rows).

Part of a stack split by backend (CONTRIBUTE.md: one backend per PR): this PR is the ASM part. The CK part (tuner and run-time shape legality, run-time fallbacks) is a new Draft PR stacked on this branch. The --run_config base-tuner fix is #8857.

Summary

The default ASM selection of gemm_a4w4 (get_heuristic_kernel) can pick an f4gemm kernel that gives wrong results for the shape. This PR marks the two kernels that do (192x256, 224x256) in the f4gemm table and makes the heuristic skip them unless N % tile_N == 0.

Motivation

get_heuristic_kernel only excluded 128x512 at a partial last N tile (a hard-coded rule). A sweep of every bpreshuffle f4gemm kernel shows that 192x256 and 224x256 are the kernels that return wrong results at a partial N tile; the heuristic can still pick them. On main, the default selection is wrong for M=4097, N=2400 at K = 1024, 5120 and 7168 (relative L2 error 0.09-0.23, measured earlier with the main versions of the two files).

Changes

  • hsa/gfx950/f4gemm/f4gemm_bf16_per1x32Fp4.csv: new full_n_tile column. It is 1 only for 192x256 and 224x256: a sweep of all 35 bpreshuffle kernels at N = 32..1536 (K = 1024, 5120) gave a relative L2 error of 0.3-0.9 for them when N % tile_N != 0, and 4e-3 or less (bf16 split-K noise) for every other kernel.
  • hsa/codegen.py: DEFAULT_COLUMNS = {"f4gemm": {"full_n_tile": 0}}. A launcher reads the field, so it exists in the generated struct with value 0 even when no manifest of the module (for example another arch) has the column, and the C++ always compiles.
  • csrc/py_itfs_cu/asm_gemm_a4w4.cu: get_heuristic_kernel skips a full_n_tile kernel unless N % cfg.tile_N == 0. Its earlier hard-coded 128x512 rule (N % 512 == 0) stays as it was on main; the sweep found 128x512 correct at partial N, but this PR does not lift it.
  • op_tests/tuning_tests/test_a4w4_asm_full_n_tile.py (new, CPU only) and its entry in .github/scripts/split_tests.sh.

No Python run-time code, tuner or CK file changes here. Overlap with other work: #8825 and #7979 touch the a4w4 tuner and gemm_a4w4, which this PR does not.

Performance (if applicable)

Not a performance change. The heuristic only drops two kernels at a partial last N tile, where they gave wrong results.

Testing

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

Tested on MI350X (gfx950), see the Self-Review. Not tested on the MI350P of the issue.

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

The full_n_tile meaning is documented in the header comment of the f4gemm CSV and in the comment at DEFAULT_COLUMNS; no user-facing option changes.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None. For shapes with N % tile_N == 0 the selection is unchanged; for a partial last N tile the two flagged kernels are no longer chosen, so the default selection now returns correct results instead of wrong ones.

PR Self-Review

Reviewed revision

df07919

Behavior and intent

The default ASM selection of gemm_a4w4 never picks a kernel that is wrong for a partial last N tile. Which kernels those are is data in the f4gemm table (full_n_tile), read by the C++ heuristic, and hsa/codegen.py guarantees the field exists on every arch. The 128x512 rule keeps its previous behavior.

Validation and unverified scope

Hardware: mi350x8, MI350X (gfx950, 256 CU), image rocm/atom-dev:sglang-latest with this branch (git archive of the reviewed revision) overlaid, the ASM module rebuilt from it.

  • Default ASM selection (gemm_a4w4_asm, no kernel name, log2_k_split=-1), re-run on this revision: 312 shapes (K = 1024, 5120, 7168; M = 1, 7, 33, 100, 257, 1000, 4097, 16384; N = 32..2400, 13 values), error as relative L2 against torch: 0 shapes above 2e-2, worst 6.5e-3. With the main versions of asm_gemm_a4w4.cu and the table (earlier run): 3 shapes wrong (M=4097, N=2400, K = 1024, 5120, 7168: error 0.09-0.23), the selection picking a full_n_tile kernel at a partial N tile.
  • Kernel flags: every bpreshuffle f4gemm kernel at N = 32..1536 with N % 32 == 0, M = 1, 33, 1000, K = 1024, 5120 (36 shapes, split-K kernels at log2_k_split = 2), earlier run on the same table. Only 192x256 (20 of 36 shapes) and 224x256 (19 of 36) are wrong (0.03-0.9); all other kernels, including 128x512, split-K 256x256 and the persistent 256x256 ps kernel, stay below 2e-2 (3-5e-3 for split-K bf16 rounding, 0 for the others). A 1% element-wise check reads that rounding as "wrong" for every split-K kernel, so it cannot tell the two cases apart.
  • Unit tests (CPU): op_tests/tuning_tests/test_a4w4_asm_full_n_tile.py, 4 passed.
  • Lint: ruff 0.16.0 and black 26.10.1 (the version pinned on main), clean.
    Not verified:
  • Not run on MI350P (128 CU).
  • The C++ change was only built and run on gfx950; other arches have no full_n_tile column and get 0 from codegen.py (covered by a codegen unit test, not by a build on another arch).
  • Tuner candidate lists are not changed here; the tuner still tries the flagged kernels at a partial N tile (handled in the stacked CK PR).

PR-specific question

full_n_tile is a new column of the f4gemm manifest. Do the maintainers prefer it there (one table, read by the codegen and the heuristic), or hard-coded in the heuristic next to the existing 128x512 rule?

Changed op tests

op_tests/tuning_tests/test_a4w4_asm_full_n_tile.py (new, CPU only, registered in split_tests.sh, runs as a script through unittest.main()):

  • the full_n_tile column marks exactly 192x256 and 224x256 among the bpreshuffle kernels, and they are not split-K or persistent;
  • hsa/codegen.py generates the full_n_tile field and the flags match the table;
  • the field exists with value 0 when no table has the column.
    No other op test changes.

…ithout untuned CSV (Refs #6255)

The a4w4_blockscale tuner dispatched every CK instance and ASM kernel for every shape.
On N=96, K=5120 the CK instances (NPerBLOCK >= 128, preshuffled B, no N padding) fault
the worker ("Memory access fault by GPU"), and four ASM kernels (192x256, 224x256,
256x256 split-K, 128x512 split-K) return wrong results unless N % tile_N == 0.
The default ASM heuristic could pick one of them (wrong output at N=96, M=48/64).

- Shared predicates a4w4_ck_shape_legal / a4w4_asm_shape_legal /
  a4w4_kernel_shape_legal in aiter/ops/gemm_op_a4w4.py, used by the tuner candidate
  loops and by gemm_a4w4 (an illegal tuned row is ignored with a one-time warning).
- f4gemm kernel table gets a full_n_tile column; the C++ default selection reads it
  instead of the hard-coded 128x512 case.
- gemm_a4w4 falls back to the default ASM selection, with a one-time warning, when a CK
  row names an instance that is not in the compiled registry.
- --run_config <tuned_csv> no longer needs the default untuned CSV unless --compare or
  --all is given.
- CPU tests: op_tests/tuning_tests/test_a4w4_shape_legality.py.
@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 8847 --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.

…t a partial N tile (192x256, 224x256)

A gfx950 sweep of every f4gemm bpreshuffle kernel at N = 32..1536 (K = 1024, 5120)
with a relative-L2 check against torch shows that only 192x256 and 224x256 give
wrong results when N % tile_N != 0. The split-K 256x256 and 128x512 kernels and the
persistent 256x256 kernel stay at the bf16 split-K noise floor, so full_n_tile is
cleared for them (the earlier 1% element check flagged split-K rounding). The C++
default selection keeps its previous 128x512 rule. No committed tuned row falls back
any more. Document the CK partial-tile over-read as a known gap. Store the new test
with LF line endings. Refs #6255
…he heuristic here

Move the CK tuner / run-time shape legality (gemm_op_a4w4.py, gemm_a4w4_blockscale_tune.py,
the tuner README) and the --run_config base-tuner fix out of this PR, which stays the ASM
part (one backend per PR): the full_n_tile column, hsa/codegen.py giving the field a zero
default when a table has no such column, and the get_heuristic_kernel read. The CK part
continues as a PR stacked on this branch and the base-tuner part as its own PR.
Replace the legality test by test_a4w4_asm_full_n_tile.py. Refs #6255
@demonsan demonsan changed the title [CK] [Tune] [Bugfix] a4w4 tuner: skip candidates illegal for narrow N, --run_config without untuned CSV [ASM] [Bugfix] a4w4: skip f4gemm kernels wrong at a partial N tile (full_n_tile table flag) Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant