Skip to content

Return an empty result for M=0 in the a8w8 GEMM family - #8995

Open
zufayu wants to merge 2 commits into
ROCm:mainfrom
zufayu:fix/zero-m-a8w8
Open

zufayu wants to merge 2 commits into
ROCm:mainfrom
zufayu:fix/zero-m-a8w8

Conversation

@zufayu

@zufayu zufayu commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Context

What changed (aiter/ops/gemm_op_a8w8.py)

  • gemm_a8w8_ASM, gemm_a8w8_CK: guard added directly in each function, right after m/n/k are read, before any tuned-config lookup.
  • gemm_a8w8_bpreshuffle: guard added in the shared _gemm_a8w8_bpreshuffle_into helper (which it delegates to) rather than in the wrapper itself, since gemm_a8w8_bpreshuffle_out calls the same helper — one guard now covers both the already-fixed _out variant's remaining path and the previously-unguarded allocating variant.
  • gemm_a8w8_blockscale: guard added right after m/n/k, before the isBpreshuffled branch (which can itself dispatch into the blockscale_bpreshuffle native body).
  • gemm_a8w8_blockscale_bpreshuffle: guard added in the shared _gemm_a8w8_blockscale_bpreshuffle_native body, which both the routed (out=None) and buffered (out=) public spellings call into, and which gemm_a8w8_blockscale's group32 branch also calls directly.
  • gemm_a8w8_blockscale_abpreshuffle: guard added after Y is resolved (from out or freshly allocated) and before the gfx1250 arch check — M=0 is a no-op regardless of backend, so it should not need to satisfy the "needs gfx1250" requirement either.
  • gemm_a8w8_mxfp8: same pattern as gemm_a8w4_mxfp8 in Return an empty result for M=0 in a16w8/a4w6/a6w4/a6w6/a8w4 GEMMs #8992 — guard added after the N/K validation, before the a_preshuffle M%2 check (which is moot for M=0) and the tuned-config resolution.

Testing

  • Verified on an MI300X (gfx942) machine, calling all seven entry points directly with M=0 placeholder tensors:
    • On origin/main (before this change): every one of the seven either raises, fails to resolve a kernel, or reaches a kernel build/launch with an untested shape. Notably gemm_a8w8_mxfp8 fails with an actual HIP invalid configuration argument error (a bad kernel launch, not a clean Python exception), and gemm_a8w8_CK/gemm_a8w8_ASM/gemm_a8w8_bpreshuffle/gemm_a8w8_blockscale all trigger a JIT kernel build for the M=0 shape (one of which happens to fail here on an unrelated CK/toolchain issue — itself evidence this path was never meant to reach a kernel build).
    • With this change: all seven return a correctly-shaped (0, N) bf16 tensor without reaching a backend.
  • Added op_tests/test_gemm_a8w8_zero_m.py with one pytest case per entry point, using placeholder tensors that never reach a kernel for M=0 (so no particular GPU architecture is required beyond a CUDA/HIP device). All 7 pass locally.

Related: #8984, #8958, #8960

PR Self-Review

Reviewed revision

4de4836

Behavior and intent

Before this PR, seven gemm_a8w8* entry points in aiter/ops/gemm_op_a8w8.py reach a tuned-config lookup and/or a kernel launch for M=0, unlike the two entries in the same file (gemm_a8w8_bpreshuffle_out, gemm_a8w8_mxfp8_bpreshuffle) that already short-circuit. This is insufficient per #8984: M=0 happens in production from an idle data-parallel rank's dummy step (#8958), and reaching a backend with it is backend-dependent and undefined (verified below: one of the seven produces an actual HIP launch failure, not a clean exception). The PR adds the same if m == 0: return <empty tensor/Y> short-circuit, placed before the first tuned lookup or kernel dispatch in each path, reusing the shared _gemm_a8w8_bpreshuffle_into / _gemm_a8w8_blockscale_bpreshuffle_native bodies where two public entry points funnel into one implementation.

Validation and unverified scope

Verified on an MI300X (gfx942) machine by calling all seven entries directly with M=0 placeholder tensors, both on origin/main (f7c6089) and this PR's head:

  • origin/main: all seven fail. Notably gemm_a8w8_mxfp8 fails with RuntimeError(... HIP runtime error: invalid configuration argument ...) -- a real bad kernel launch. gemm_a8w8_blockscale_abpreshuffle raises its own gfx1250 with e8m0 scales RuntimeError (unrelated to M=0, since my FP32-scale test tensors don't satisfy that function's arch/dtype precondition regardless of M -- this result does not by itself prove the M=0 bug for that one entry, though the if m == 0 guard in this PR is placed before that check too, which is itself the behavior change requested: M=0 should not need to satisfy an arch precondition). The remaining five reach a JIT kernel build or a "no kernel registered"/"no usable config" path for the untested M=0 shape.
  • This PR's head: all seven return a correctly-shaped (0, N) bf16 tensor, no backend reached.
    Unverified: the M>0 compute path for gemm_a8w8_blockscale_abpreshuffle and gemm_a8w8_mxfp8 (both gfx1250-only) was not exercised on this MI300X hardware. Since this PR's guard is strictly before the existing logic for M>0 (no reordering of M>0 behavior), I believe those paths are unaffected, but I have not run gfx1250 CI myself.

PR-specific question

Why is the guard placed in the shared _gemm_a8w8_bpreshuffle_into / _gemm_a8w8_blockscale_bpreshuffle_native bodies rather than in the public gemm_a8w8_bpreshuffle / gemm_a8w8_blockscale_bpreshuffle wrappers? Because each shared body is also called directly by another already-fixed or in-scope path (gemm_a8w8_bpreshuffle_out's own M=0 check short-circuits before reaching _gemm_a8w8_bpreshuffle_into at all, so this PR's guard there is a no-op for that path and only activates for the previously-unguarded gemm_a8w8_bpreshuffle; gemm_a8w8_blockscale's group32 branch calls _gemm_a8w8_blockscale_bpreshuffle_native directly, and my separate top-level guard in gemm_a8w8_blockscale already covers that call site, so the body-level guard is a second, deliberately redundant layer rather than the only one). Putting the check at the body level means one line protects every caller instead of duplicating it per wrapper.

Changed op tests

  • op_tests/test_gemm_a8w8_zero_m.py (new file): one pytest case per fixed entry point (gemm_a8w8_ASM, gemm_a8w8_CK, gemm_a8w8_bpreshuffle, gemm_a8w8_blockscale, gemm_a8w8_blockscale_bpreshuffle, gemm_a8w8_blockscale_abpreshuffle, gemm_a8w8_mxfp8), using placeholder tensors that never reach a kernel for M=0. Not redundant with the existing op_tests/test_gemm_a8w8.py, which is a correctness/benchmark script (test_gemm, test_skinny_gemm, etc.) exercising M>0 shapes against a numeric reference -- it has no M=0 case, and M=0 needs only a shape/dtype assertion, not a numeric comparison, so extending it would have meant bolting an unrelated check onto an unrelated test's parametrization rather than adding a focused new one.

@zufayu
zufayu requested a review from a team December 1, 2026 08:59
@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 8995 --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

WAITING ON AUTHOR

PR Self-Review

Revision 15909f1a54b1 · mode shadow

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

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

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 #8995 — Return an empty result for M=0 in the a8w8 GEMM family

Review (advisory): 🔴 HIGH RISK
Validation (deterministic): NOT RUN — REVIEW_AUTO_VALIDATE=0 (auto-validation disabled; the target op_tests/test_gemm_a8w8_zero_m.py existed but the run could not happen — an environment gap, not a PR defect)
Perf (advisory): NOT RUN — the target ships no benchmark entry point (no --scenario bench, no perftest/@benchmark harness); the change adds no kernel code and leaves the M>0 path byte-identical, so there is no measured perf surface
Size: 125 lines / 2 files
Design (advisory): the M=0 short-circuit lives in the shared implementation bodies (_gemm_a8w8_bpreshuffle_into, _gemm_a8w8_blockscale_bpreshuffle_native) rather than in each public wrapper, so one guard covers every caller including gemm_a8w8_blockscale's group32 branch, and it answers before the arch gates
Human: recommend BingYuan.Zhou review whether the M=0 short-circuit in PR #8995 belongs in the shared bodies of gemm_op_a8w8.py ahead of the arch gates, as this PR places it, or at each public wrapper

🔴 [verified] Every downstream job is skipped on this PR — Sglang, vLLM Benchmark, ATOM Accuracy and the 8-GPU Multi-GPU Tests all sit at "skipping" with no ci: label — yet the diff changes the M=0 behavior of the default-dispatch a8w8 GEMM path in aiter/ops/gemm_op_a8w8.py that those frameworks consume, and the 8-GPU Multi-GPU job is the only configuration that could exercise the production trigger, an idle data-parallel rank's M=0 dummy step (#8958). Author must add ci:all (the M=0 trigger is model-agnostic, so no smaller label set can be named) and require it green before merge.

🔴 [verified] aiter/ops/gemm_op_a8w8.py is a Tier-2 downstream contract (public via aiter/init.py:123) and this PR changes the M=0 behavior of seven of its public entry points from raise-or-launch to returning an empty tensor, yet the PR carries zero reviews and no owner ping — a contract change merged on CI alone is exactly what the sign-off rule exists to stop. Reviewer must @-mention the de-facto owner BingYuan.Zhou (top committer of gemm_op_a8w8.py, 13 commits) in a PR comment requesting explicit sign-off before merge.

📝 [inferred] The public gemm_a8w8 dispatcher's RDNA route (aiter/ops/gemm_op_a8w8.py:741-747) still forwards M=0 to the Triton kernel (aiter/ops/triton/gemm/basic/gemm_a8w8.py:29), which has no M=0 guard — whether M=0 returns cleanly there depends on Triton treating a zero grid as a launch no-op, which this PR neither tests nor mentions. Reviewer should ask whether #8984's sweep covers that route.

if not _ck_a8w8_supported():
# RDNA (gfx11/gfx12): the CK/asm a8w8 kernel is unavailable; route to the
# portable Triton kernel. Registered/faked via @torch_compile_guard above,
# so callers stay torch.compile- and graph-capture-safe.
from ..ops.triton.gemm.basic.gemm_a8w8 import gemm_a8w8 as gemm_a8w8_triton
return gemm_a8w8_triton(XQ, WQ, x_scale, w_scale, bias, dtype=dtype)

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

  • aiter/ops/gemm_op_a8w8.py:2916 — gemm_a8w8_mxfp8's M=0 guard (aiter/ops/gemm_op_a8w8.py:2916) sits after require_gfx1250_asm, which raises on…

Comment thread aiter/ops/gemm_op_a8w8.py
raise NotImplementedError(
f"gfx1250 a8w8 MXFP8 GEMM requires N%16==0, got N={N}"
)
if M == 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 [verified] gemm_a8w8_mxfp8's M=0 guard (aiter/ops/gemm_op_a8w8.py:2916) sits after require_gfx1250_asm, which raises on gfx1250-strict/A0 stepping (aiter/jit/utils/asm_guard.py:62), while gemm_a8w8_blockscale_abpreshuffle's guard deliberately precedes its arch check — so on A0 hardware an M=0 mxfp8 call would still raise, the one spot where the PR's "M=0 is a no-op regardless of backend" principle does not hold. Reviewer should ask whether M=0 should also bypass the A0 gate for consistency.

@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 pushed a commit that referenced this pull request Dec 3, 2026
…8996)

M=0 (and, for a batched GEMM, batch=0) happens in production when an
idle data-parallel rank runs a dummy step to stay in its group's
collectives (#8958). This extends the no-op convention already applied
to gemm_a16w16 (#8960) and the a8w8 family (#8995) to the batched GEMM
entry points flagged in #8984:

- batched_gemm_bf16_CK / batched_gemm_a8w8_CK: guard added right after
  the output buffer Y is allocated (b, m, n are already known at that
  point), before the tuned-config lookup (get_CKBatchedGEMM_config)
  and the kernel launch.
- batched_gemm_a8w8_mxscale (_batched_gemm_a8w8_mxscale_impl) and
  batched_gemm_a8w8_mxscale_bpreshuffle
  (_batched_gemm_a8w8_mxscale_bpreshuffle_impl): guard added right
  after m, g, n, k are read, before the tuned lookup and the
  opus/flydsl libtype dispatch.

batched_gemm_a8w8_mxscale_bpreshuffle_fused_quant (a distinct
(fp8, e8m0-scale) tuple return contract) is left out of this PR's scope;
flagging it as a follow-up.

Verified on MI300X (gfx942): with this change all four entries return
a correctly-shaped empty result for batch=0 or M=0 without reaching a
backend. On origin/main (before this change), batched_gemm_bf16_CK(B=0)
reaches get_CKBatchedGEMM_config and a JIT kernel build/launch for an
untested shape.

Added op_tests/test_batched_gemm_zero_m.py covering all four entries.

Related: #8984, #8958, #8960, #8995

Co-authored-by: zufayu <[email protected]>
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
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), and gemm_a8w8_bpreshuffle_out /
gemm_a8w8_mxfp8_bpreshuffle already do within gemm_op_a8w8.py. This
extends the same convention to the remaining a8w8 entry points flagged
in ROCm#8984: gemm_a8w8_ASM, gemm_a8w8_CK, gemm_a8w8_bpreshuffle (via the
shared _gemm_a8w8_bpreshuffle_into, which also protects
gemm_a8w8_blockscale, gemm_a8w8_blockscale_bpreshuffle (via the shared
_gemm_a8w8_blockscale_bpreshuffle_native), gemm_a8w8_blockscale_abpreshuffle,
and gemm_a8w8_mxfp8.

Each guard sits right after the output buffer's shape is known and
before the first tuned-config lookup or kernel dispatch, matching the
pattern of the two existing guards in this file.

Verified on MI300X (gfx942): on origin/main, calling each of these
seven entries with M=0 either raises (gemm_a8w8_blockscale_abpreshuffle's
gfx1250 check), fails to resolve a kernel (gemm_a8w8_blockscale_bpreshuffle,
gemm_a8w8_mxfp8 -- the latter's failure is a HIP "invalid configuration
argument" error, i.e. an actual bad kernel launch, not a clean Python
exception), or triggers a JIT build of an untested shape (gemm_a8w8_CK /
gemm_a8w8_ASM / gemm_a8w8_bpreshuffle / gemm_a8w8_blockscale -- the
latter's build happens to fail here on an unrelated CK/toolchain issue,
which is itself evidence the M=0 path was never meant to reach a kernel
build at all). With this change all seven return a correctly-shaped
empty bf16 result without reaching a backend.

Added op_tests/test_gemm_a8w8_zero_m.py covering all seven entries with
placeholder tensors that never reach a kernel for M=0.

Related: ROCm#8984, ROCm#8958, ROCm#8960
@zufayu

zufayu commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@BingYuan.Zhou (via GitHub handle, please correct if wrong) — could you take a look at whether the M=0 short-circuit placement in gemm_op_a8w8.py (shared bodies _gemm_a8w8_bpreshuffle_into / _gemm_a8w8_blockscale_bpreshuffle_native, ahead of the arch gates) is the right spot? This changes the M=0 behavior of 7 public entry points you're the top committer on. Added ci:all to unskip the downstream Sglang/vLLM/ATOM/Multi-GPU jobs that exercise the actual trigger (#8958's idle data-parallel M=0 dummy step).

🤖 Generated with Claude Code

@zufayu

zufayu commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Correction: my previous mention didn't actually ping anyone (wrong format). @solinzby1 — could you take a look at the M=0 guard placement question above when you get a chance?

@zufayu zufayu added the ci:all label Dec 4, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants