Skip to content

Return an empty result for M=0 in a16w8/a4w6/a6w4/a6w6/a8w4 GEMMs - #8992

Merged
yifehuan merged 2 commits into
ROCm:mainfrom
zufayu:fix/zero-m-dense-gemm
Dec 3, 2026
Merged

yifehuan merged 2 commits into
ROCm:mainfrom
zufayu:fix/zero-m-dense-gemm

Conversation

@zufayu

@zufayu zufayu commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Context

What changed

  • gemm_a4w6 / gemm_a6w4: previously raised ValueError on M<=0. M=0 is now a legal no-op, answered before _select_gemm_a4w6_kernel/_select_gemm_a6w4_kernel (and therefore before any tuned-config lookup).
  • gemm_a6w6: had no guard at all for M=0. Verified on MI300X that without this fix it reaches the ASM launcher and crashes with M must be a positive multiple of 256! from csrc/py_itfs_cu/asm_gemm_a6w6.cu.
  • gemm_a8w4_mxfp8: had no M guard; M=0 reached _resolve_mxfp8_gemm_config and the kernel launch with an untested shape. The new guard sits after the N/K validation (still enforced) but before the a_preshuffle M%2 check, which is moot for M=0.
  • gemm_a16w8_asm: allocates out unconditionally but always called the kernel. This file's own test (test_gemm_a16w8_asm.py::_run_guard_checks, added with the original gfx950 decode kernel in [ASM] [HIP] [JIT] Add a gfx950 asm GEMM with BF16 activations and FP8 weights for decode #6101) already asserts gemm_a16w8_asm(M=0, ...).shape == (0, 4608) — that assertion was not actually true until this change.

Testing

  • Verified on an MI300X (gfx942) machine with a standalone script calling each function directly with M=0 placeholder tensors:
    • On origin/main (before this change): gemm_a4w6/gemm_a6w4 raise ValueError, gemm_a6w6 crashes with the ASM M must be a positive multiple of 256! error, gemm_a8w4_mxfp8/gemm_a16w8_asm fail reaching an unvalidated kernel/dtype path.
    • With this change: all five return a correctly-shaped (0, N) bf16 tensor without reaching a backend.
  • Added test_*_returns_empty_result_for_m_zero pytest cases to op_tests/test_gemm_a4w6.py, test_gemm_a6w4.py, test_gemm_a6w6.py, and a new minimal op_tests/test_gemm_a8w4_zero_m.py, following the existing placeholder = torch.empty(0, dtype=torch.uint8) pattern already used in this repo for pre-kernel validation tests (e.g. test_a4w6_rejects_unsupported_launch_contracts_before_allocation). All four pass locally.
  • gemm_a16w8_asm already had the right assertion in its existing guard checks; no new test added there, this PR just makes it pass.
  • Caveat: gemm_a4w6/gemm_a6w4/gemm_a6w6 require gfx950 and gemm_a8w4_mxfp8 requires gfx1250 for their actual (M>0) compute paths, which I could not exercise on this MI300X machine. Only the M=0 short-circuit itself (and the new tests, which never reach a kernel) were verified here; please let CI/gfx950+gfx1250 runners confirm the M>0 paths are unaffected.

Related: #8984, #8958, #8960

PR Self-Review

Reviewed revision

52b4451

Behavior and intent

Before this PR, gemm_a4w6/gemm_a6w4 raise ValueError on M<=0, gemm_a6w6/gemm_a8w4_mxfp8 have no M guard at all and reach the tuned-config lookup and kernel launch with M=0 (an untested shape), and gemm_a16w8_asm unconditionally calls its kernel regardless of M. This is insufficient per #8984: an idle data-parallel rank's dummy step (#8958) produces M=0 in production, and the existing behavior either errors where a no-op is expected, or reaches a backend that may reject or mishandle a zero-size launch (verified below: gemm_a6w6 actually crashes). The PR adds an if M == 0: return <empty tensor> short-circuit to each of the five entries, placed before any tuned lookup or kernel dispatch, matching the convention gemm_a16w16 already uses (#8960, not yet merged).

Validation and unverified scope

Verified on an MI300X (gfx942) machine by calling each of the five functions directly with M=0 placeholder tensors, both on origin/main (f7c6089) and on this PR's head:

  • origin/main: gemm_a4w6/gemm_a6w4 raise ValueError("... requires positive dimensions ..."); gemm_a6w6 crashes with the ASM runtime error M must be a positive multiple of 256!; gemm_a8w4_mxfp8/gemm_a16w8_asm fail with an AssertionError reaching an unvalidated ctypes/dtype path (this specific failure mode is an artifact of this MI300X not being this kernel's target arch, not itself evidence of the M=0 bug for those two -- the gemm_a6w6 crash and the two ValueErrors are the clean evidence).
  • This PR's head: all five return a correctly-shaped (0, N) bf16 tensor, no backend reached.
    Unverified: the actual M>0 compute path for gemm_a4w6/gemm_a6w4/gemm_a6w6 (needs gfx950) and gemm_a8w4_mxfp8 (needs gfx1250) was not exercised on this MI300X hardware, since this PR does not touch that path's logic (the new if M == 0 check is strictly before it) I believe it is unaffected, but CI on the relevant architectures is the authority here, not my local run.

PR-specific question

Why does gemm_a16w8_asm get no new test file, while the other four do? Because op_tests/test_gemm_a16w8_asm.py already has an _run_guard_checks() assertion (assert gemm_a16w8_asm(a, b, s).shape == (0, 4608), "M 0 should be a no-op") dating to the function's original PR (#6101) that was not actually true until this change -- adding a second test for the same behavior would be redundant with a check the repo already owns.

Changed op tests

  • op_tests/test_gemm_a4w6.py (added test_a4w6_returns_empty_result_for_m_zero): covers the new M=0 behavior for gemm_a4w6. Not redundant with the file's existing test_gemm_a4w6_matches_quantized_reference (which only exercises M>0 shapes) or test_a4w6_rejects_unsupported_launch_contracts_before_allocation (which covers alpha/K-range errors, not M=0).
  • op_tests/test_gemm_a6w4.py (added test_a6w4_returns_empty_result_for_m_zero): same reasoning, mirrored for gemm_a6w4.
  • op_tests/test_gemm_a6w6.py (added test_a6w6_returns_empty_result_for_m_zero): same reasoning for gemm_a6w6, which previously had no M=0 guard or test at all (not even an error-path one).
  • op_tests/test_gemm_a8w4_zero_m.py (new file): no prior test file existed for gemm_a8w4_mxfp8's M=0 behavior to extend; this is a minimal, narrowly-scoped regression test rather than a full correctness suite for the op, which is out of this PR's scope.

M=0 happens in production when an idle data-parallel rank runs a dummy
step to stay in its group's collectives (ROCm#8958). gemm_a16w16 already
treats this as a no-op (ROCm#8960); this extends the same convention to the
other dense GEMM entry points that were missing it (ROCm#8984):

- gemm_a4w6 / gemm_a6w4 raised ValueError on M<=0; M=0 is now a legal
  no-op instead of an error, answered before the tuned-config lookup.
- gemm_a6w6 had no M guard at all and reaches the ASM launcher with
  M=0, which aborts with "M must be a positive multiple of 256".
- gemm_a8w4_mxfp8 had no M guard; M=0 reaches the config lookup and
  kernel launch with an untested shape.
- gemm_a16w8_asm allocates `out` but calls the kernel unconditionally;
  M=0 is already asserted as a no-op in this file's own guard checks
  (test_gemm_a16w8_asm.py::_run_guard_checks), which this makes true.

Verified on MI300X (gfx942): on origin/main, gemm_a6w6(M=0) crashes
with the ASM "M must be a positive multiple of 256" error, and the
other four either raise or reach an unvalidated kernel path; with this
change all five return the correctly-shaped empty/bf16 result without
reaching a backend. a4w6/a6w4/a6w6 require gfx950 end-to-end and a8w4
requires gfx1250 for their non-zero-M paths, so only the M=0 guard
itself (and the new regression tests, which use placeholder tensors
and never reach a kernel) could be exercised on this hardware.

Related: ROCm#8984, ROCm#8958, ROCm#8960
@zufayu
zufayu requested a review from a team December 1, 2026 08:38
@github-actions

github-actions Bot commented Dec 1, 2026

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 8992 --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.

@aiter-bot

aiter-bot commented Dec 1, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 52b4451a26d1 · 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

Before this PR, `gemm_a4w6`/`gemm_a6w4` raise `ValueError` on `M<=0`, `gemm_a6w6`/`gemm_a8w4_mxfp8` have no M guard at all and reach the tuned-config lookup and kernel launch with M=0 (an untested shape), and `gemm_a16w8_asm` unconditionally calls its kernel regardless of M. This is insufficient per #8984: an idle data-parallel rank's dummy step (#8958) produces M=0 in production, and the existing behavior either errors where a no-op is expected, or reaches a backend that may reject or mishandle a zero-size launch (verified below: `gemm_a6w6` actually crashes). The PR adds an `if M == 0: return <empty tensor>` short-circuit to each of the five entries, placed before any tuned lookup or kernel dispatch, matching the convention `gemm_a16w16` already uses (#8960, not yet merged).

Validation and unverified scope

Verified on an MI300X (gfx942) machine by calling each of the five functions directly with M=0 placeholder tensors, both on `origin/main` (f7c60896) and on this PR's head:
- `origin/main`: `gemm_a4w6`/`gemm_a6w4` raise `ValueError("... requires positive dimensions ...")`; `gemm_a6w6` crashes with the ASM runtime error `M must be a positive multiple of 256!`; `gemm_a8w4_mxfp8`/`gemm_a16w8_asm` fail with an `AssertionError` reaching an unvalidated ctypes/dtype path (this specific failure mode is an artifact of this MI300X not being this kernel's target arch, not itself evidence of the M=0 bug for those two -- the `gemm_a6w6` crash and the two `ValueError`s are the clean evidence).
- This PR's head: all five return a correctly-shaped `(0, N)` bf16 tensor, no backend reached.
Unverified: the actual M>0 compute path for `gemm_a4w6`/`gemm_a6w4`/`gemm_a6w6` (needs gfx950) and `gemm_a8w4_mxfp8` (needs gfx1250) was not exercised on this MI300X hardware, since this PR does not touch that path's logic (the new `if M == 0` check is strictly before it) I believe it is unaffected, but CI on the relevant architectures is the authority here, not my local run.

PR-specific question

Why does `gemm_a16w8_asm` get no new test file, while the other four do? Because `op_tests/test_gemm_a16w8_asm.py` already has an `_run_guard_checks()` assertion (`assert gemm_a16w8_asm(a, b, s).shape == (0, 4608), "M 0 should be a no-op"`) dating to the function's original PR (#6101) that was not actually true until this change -- adding a second test for the same behavior would be redundant with a check the repo already owns.

Changed op tests

- `op_tests/test_gemm_a4w6.py` (added `test_a4w6_returns_empty_result_for_m_zero`): covers the new M=0 behavior for `gemm_a4w6`. Not redundant with the file's existing `test_gemm_a4w6_matches_quantized_reference` (which only exercises M>0 shapes) or `test_a4w6_rejects_unsupported_launch_contracts_before_allocation` (which covers alpha/K-range errors, not M=0).
- `op_tests/test_gemm_a6w4.py` (added `test_a6w4_returns_empty_result_for_m_zero`): same reasoning, mirrored for `gemm_a6w4`.
- `op_tests/test_gemm_a6w6.py` (added `test_a6w6_returns_empty_result_for_m_zero`): same reasoning for `gemm_a6w6`, which previously had no M=0 guard or test at all (not even an error-path one).
- `op_tests/test_gemm_a8w4_zero_m.py` (new file): no prior test file existed for `gemm_a8w4_mxfp8`'s M=0 behavior to extend; this is a minimal, narrowly-scoped regression test rather than a full correctness suite for the op, which is out of this PR's scope.

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.

@zufayu

zufayu commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 31 review(s); expect to start in about 0 min. The box reviews one PR at a time.

@aiter-bot aiter-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ROCm/aiter PR #8992 — Return an empty result for M=0 in a16w8/a4w6/a6w4/a6w6/a8w4 GEMMs

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — auto-validation disabled (REVIEW_AUTO_VALIDATE=0); the PR ships the single test target op_tests/test_gemm_a8w4_zero_m.py, so a deterministic report could have run — environment gap, not a PR defect
Perf (advisory): NOT RUN — the PR's test target exposes no benchmark entry point (no --scenario bench, no perftest/@benchmark harness); the M>0 path gains one integer comparison per call and no kernel code changes
Size: 90 lines / 9 files
Design (advisory): per-entry inline M=0 guards at each of the five wrappers, ahead of the tuned lookup and backend dispatch, rather than the shared empty_gemm_result() helper #8984 proposes
Human: recommend Kristian Sikiric review whether the per-entry inline M=0 guard in this PR (vs the shared helper #8984 proposes) is the convention to carry across the remaining entries — the _out/asm variants and the pack/quant path

📝 [inferred] The new guards look unreachable end-to-end for the packed ops: a 0-row activation cannot be packed, because mxfp4_gemm_pack_size (aiter/ops/gemm_op_a6w4.py:276) and the mxfp6 pack layout (aiter/ops/gemm_op_a6w6.py:855) still raise "requires positive dimensions" on rows==0, so the idle-rank dummy step this PR cites (#8958) would die at the quant step before reaching the fixed gemm_a4w6/gemm_a6w4/gemm_a6w6 entries — unless serving frameworks skip activation quant for a dummy step, which I could not confirm. Reviewer should ask whether the pack/quant entries also need the M=0 no-op convention for the scenario to complete end-to-end.

if rows <= 0 or K <= 0:

if rows <= 0 or K <= 0:

2 finding(s) posted inline, on the lines they are about:

  • aiter/ops/gemm_op_a16w8.py:50 — The description and self-review claim the pre-existing test_gemm_a16w8_asm.py::_run_guard_checks M=0…
  • aiter/ops/gemm_op_a6w6.py:1166 — The M=0 convention stops at the wrapper layer: the exported asm-level entries still dispatch M=0 to a backend

Comment thread aiter/ops/gemm_op_a16w8.py Resolved
Comment thread aiter/ops/gemm_op_a6w6.py Resolved
@zufayu
zufayu requested a review from yifehuan December 2, 2026 01:27
zufayu added a commit to zufayu/aiter that referenced this pull request Dec 3, 2026
Zero tokens happens in production when an idle data-parallel rank runs
a dummy step to stay in its group's collectives (ROCm#8958). This extends
the no-op convention already applied to the GEMM entry points (ROCm#8960,

- fused_moe_dp_share_expert (aiter/fused_moe_dp_shared_expert.py):
  already had an `if M == 0: return moe_buf` guard, but it sat after
  the get_2stage_cfgs tuned lookup, so a zero-token call was still
  doing a config lookup it never used. Moved the guard (and the
  moe_buf allocation it depends on) before that lookup; nothing else
  on this path reads `metadata` before the lookup, so the reorder is
  safe.
- fused_moe_2stages (aiter/fused_moe.py): had no guard at all. Added
  one right after `token_num` and the `moe_out` buffer are resolved
  (the existing `moe_out.numel() == 0` check right above it is a
  different thing -- a reduce-mode workspace placeholder, not a
  token-count check), before get_2stage_cfgs.
- fused_moe_1stage (aiter/fused_moe.py): had no guard. Its first branch
  (pure bf16) dispatches straight into aiter.fmoe before `token_num` is
  even read, so the guard has to sit above all three branches, right
  after the kernelName validation block.

Verified on MI300X (gfx942): on origin/main, fused_moe_2stages and
fused_moe_1stage both reach their real kernel entry point for
token_num=0 (confirmed by a TypeError demanding non-None sorting
metadata tensors that a token_num=0 caller has no reason to build).
fused_moe_dp_share_expert already worked correctly on origin/main, just
with the wasted lookup this change removes. With this change all three
return a correctly-shaped empty bf16 result without reaching a kernel,
and without requiring real sorted_ids/sorted_weights/etc. for the
token_num=0 case.

fused_moe / fused_moe_ / _fused_moe_impl (the main public MoE entry,
layered above fused_moe_1stage/2stages) are left out of this PR's
scope: the real first-dispatch point in _fused_moe_impl was not
pinned down, and guessing at it risks a guard that looks right but
never executes. Flagging as a follow-up.

Added op_tests/test_fused_moe_zero_tokens.py covering all three.

Related: ROCm#8984, ROCm#8958, ROCm#8960, ROCm#8992, ROCm#8995, ROCm#8996

@yifehuan yifehuan 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.

LGTM

@yifehuan
yifehuan merged commit a5f34c4 into ROCm:main Dec 3, 2026
64 checks passed
zufayu added a commit to zufayu/aiter that referenced this pull request Dec 4, 2026
Zero tokens happens in production when an idle data-parallel rank runs
a dummy step to stay in its group's collectives (ROCm#8958). This extends
the no-op convention already applied to the GEMM entry points (ROCm#8960,

- fused_moe_dp_share_expert (aiter/fused_moe_dp_shared_expert.py):
  already had an `if M == 0: return moe_buf` guard, but it sat after
  the get_2stage_cfgs tuned lookup, so a zero-token call was still
  doing a config lookup it never used. Moved the guard (and the
  moe_buf allocation it depends on) before that lookup; nothing else
  on this path reads `metadata` before the lookup, so the reorder is
  safe.
- fused_moe_2stages (aiter/fused_moe.py): had no guard at all. Added
  one right after `token_num` and the `moe_out` buffer are resolved
  (the existing `moe_out.numel() == 0` check right above it is a
  different thing -- a reduce-mode workspace placeholder, not a
  token-count check), before get_2stage_cfgs.
- fused_moe_1stage (aiter/fused_moe.py): had no guard. Its first branch
  (pure bf16) dispatches straight into aiter.fmoe before `token_num` is
  even read, so the guard has to sit above all three branches, right
  after the kernelName validation block.

Verified on MI300X (gfx942): on origin/main, fused_moe_2stages and
fused_moe_1stage both reach their real kernel entry point for
token_num=0 (confirmed by a TypeError demanding non-None sorting
metadata tensors that a token_num=0 caller has no reason to build).
fused_moe_dp_share_expert already worked correctly on origin/main, just
with the wasted lookup this change removes. With this change all three
return a correctly-shaped empty bf16 result without reaching a kernel,
and without requiring real sorted_ids/sorted_weights/etc. for the
token_num=0 case.

fused_moe / fused_moe_ / _fused_moe_impl (the main public MoE entry,
layered above fused_moe_1stage/2stages) are left out of this PR's
scope: the real first-dispatch point in _fused_moe_impl was not
pinned down, and guessing at it risks a guard that looks right but
never executes. Flagging as a follow-up.

Added op_tests/test_fused_moe_zero_tokens.py covering all three.

Related: ROCm#8984, ROCm#8958, ROCm#8960, ROCm#8992, ROCm#8995, ROCm#8996
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.

3 participants