Repository navigation
Return an empty result for a zero batch/row count in batched GEMMs - #8996
Conversation
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 (ROCm#8958). This extends the no-op convention already applied to gemm_a16w16 (ROCm#8960) and the a8w8 family (ROCm#8995) to the batched GEMM entry points flagged in ROCm#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: ROCm#8984, ROCm#8958, ROCm#8960, ROCm#8995
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
READY FOR HUMAN REVIEWPR Self-ReviewRevision Author self-review accepted. Readiness report: Author answers
Show the author's self-review answersBehavior and intentValidation and unverified scopePR-specific questionChanged op testsPR state
Copilot findings
Code and op-test hygiene
Required CI
Shadow mode reports what would block but keeps the status successful. Set |
|
@aiter-bot review |
|
aiter-bot — queued behind 31 review(s); expect to start in about 0 min. The box reviews one PR at a time. |
aiter-bot
left a comment
There was a problem hiding this comment.
ROCm/aiter PR #8996 — Return an empty result for a zero batch/row count in batched GEMMs
Review (advisory):
Validation (deterministic): NOT RUN — REVIEW_AUTO_VALIDATE=0 (auto-validation disabled on this box); the target op_tests/test_batched_gemm_zero_m.py exists and CI's Aiter Test Gate ran it green on the head SHA, but no head-matched validation report was produced
Perf (advisory): NOT RUN — the target op_tests/test_batched_gemm_zero_m.py ships no benchmark entry point (no --scenario bench, no perftest/@benchmark harness); the non-degenerate path only moves the Y allocation and adds no work
Size: 93 lines / 3 files
Design (advisory): NONE — the zero-size no-op pattern the dense GEMM family already established (#8960, #8995) under #8984's "zero leading dim" rule: allocate the output, return it empty before the tuned lookup and the launch
Human: N/A — the design is NONE: the diff implements an existing pattern and decides nothing new; the scope it leaves open (zero n/k, the Triton entry, the fused_quant sibling) is reported as findings below
📝 [verified] The guards cover only the leading dims (b==0 or m==0; m==0 or g==0), so a zero n (WQ.shape[1]==0) or zero k still reaches get_CKBatchedGEMM_config and the launch in all four fixed entries in aiter/ops/batched_gemm_op_a8w8.py — against #8984's "never fail at the backend" premise, and the dense family is itself split on the form (gemm_op_a8w8.py guards M==0 at one entry but numel()==0 at gemm_a8w8_bpreshuffle_out). Reviewer should ask whether the sweep should short-circuit on numel()==0 so a zero N or K is covered too.
📝 [verified] batched_gemm_a8w8_mxscale_bpreshuffle_fused_quant (aiter/ops/batched_gemm_op_a8w8.py:462) is excluded with a stated reason (its (fp8, e8m0) tuple return contract) but no follow-up issue is linked, and aiter/aot/flydsl/gemm.py:242 calls it — so that path still reaches the launch for m==0 and the exclusion lives only in this PR's description. Reviewer should ask for the follow-up issue number so the gap is tracked rather than remembered.
aiter/aiter/ops/batched_gemm_op_a8w8.py
Line 462 in 6549940
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
Conflicts in aiter/ops/batched_gemm_op_a8w8.py and aiter/ops/batched_gemm_op_bf16.py: keep main's early return for b == 0 or m == 0 (#8996) before the tuned lookup, and keep this branch's XQ.device.index argument to get_CKBatchedGEMM_config. The merge also brings main's spec_verify_pick and moves the 3rdparty/composable_kernel pin to 39483cc9. Co-authored-by: Claude <[email protected]> Signed-off-by: Andreas Karatzas <[email protected]>
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
Context
gemm_a16w16([GEMM] gemm_a16w16: return the empty result for M=0 before backend dispatch #8960) and the a8w8 dense-GEMM family (Return an empty result for M=0 in the a8w8 GEMM family #8995) already treat M=0 as a no-op. Issue [Tracking] Zero-size GEMM inputs (M=0/N=0) across AITER: empty = no-op, never hang #8984 explicitly calls out "batched A (A.dim()>=3) with a zero leading dim" as its own rule, but the batched GEMM entry points were not actually in the original scope list -- this PR fills that gap.What changed
batched_gemm_bf16_CK(aiter/ops/batched_gemm_op_bf16.py),batched_gemm_a8w8_CK(aiter/ops/batched_gemm_op_a8w8.py): guard added right after the output bufferYis allocated (b,m,nare already known at that point), beforeget_CKBatchedGEMM_configand the kernel launch.batched_gemm_a8w8_mxscale/batched_gemm_a8w8_mxscale_bpreshuffle(via their shared_implbodies): guard added right afterm, g, n, kare read, before the tuned lookup and the opus/flydsl libtype dispatch.batched_gemm_a8w8_mxscale_bpreshuffle_fused_quant, which has a distinct(fp8, e8m0-scale)tuple return contract rather than a single[M, G, N]tensor -- flagging it as a follow-up rather than guessing at its empty-result shape.Testing
batched_gemm_bf16_CK/batched_gemm_a8w8_CKreturn a correctly-shaped empty result for bothbatch=0andM=0, andbatched_gemm_a8w8_mxscale/batched_gemm_a8w8_mxscale_bpreshuffledo forM=0, all without reaching a backend.origin/main(before this change), bothbatched_gemm_bf16_CK(B=0, ...)andbatched_gemm_bf16_CK(M=0, ...)reachget_CKBatchedGEMM_config, JIT-build the kernel, and then fail the actual launch withHIP runtime error: invalid configuration argument-- a real bad kernel launch, not a clean Python exception, matching this issue's "never hang/fail at the backend" premise directly.op_tests/test_batched_gemm_zero_m.pywith one pytest case per fixed entry point (batch=0andM=0where both are meaningful). All 5 pass locally.Related: #8984, #8958, #8960, #8995
PR Self-Review
Reviewed revision
6549940
Behavior and intent
Before this PR,
batched_gemm_bf16_CK/batched_gemm_a8w8_CKcallget_CKBatchedGEMM_configand launch a kernel unconditionally, andbatched_gemm_a8w8_mxscale/batched_gemm_a8w8_mxscale_bpreshuffle(via their_implbodies) do the same with their tuned lookup, forbatch=0orM=0. Issue #8984 states the zero-size no-op rule explicitly extends to "batched A (A.dim()>=3) with a zero leading dim", but the original scope list only named dense GEMM entries, so these batched entries were an unflagged gap. The PR adds anif b == 0 or m == 0: return Y(or them == 0 or g == 0equivalent for the mxscale pair, where the batch dimension is namedg) short-circuit before the tuned lookup and kernel launch in all four.Validation and unverified scope
Verified on an MI300X (gfx942) machine by calling all four entries directly with
batch=0(where applicable) andM=0, onorigin/main(f7c6089) and this PR's head:origin/main:batched_gemm_bf16_CK(B=0, ...)andbatched_gemm_bf16_CK(M=0, ...)both reachget_CKBatchedGEMM_config, JIT-build the kernel for the shape, and then fail the actual launch withRuntimeError('HIP runtime error: invalid configuration argument. ...')-- a real bad kernel launch, matching this issue's premise directly.batched_gemm_a8w8_CK/batched_gemm_a8w8_mxscale/batched_gemm_a8w8_mxscale_bpreshufflewere exercised but their full origin/main failure trace was not captured with the same depth as the bf16 case (they were observed to reach their own tuned-lookup/build path for the untested shape, consistent with the same risk, but I did not let each one run to a terminal error/hang the way I did for bf16 before moving to verifying the fix).(0, M, N)/(B, 0, N)/(0, G, N)as applicable), no backend reached.Unverified:
batched_gemm_a8w8_mxscale_bpreshuffle_fused_quant, a related function with a distinct(fp8, e8m0-scale)tuple return contract, is explicitly left out of this PR (see below) rather than guessed at.PR-specific question
Why does
batched_gemm_a8w8_mxscale_bpreshuffle_fused_quantnot get the same fix in this PR? It shares the same tuned-lookup-before-kernel-launch shape as the four fixed functions, but its return type is a(fp8 tensor, e8m0 scale tensor)pair rather than a single[M, G, N]tensor, and its scale-tensor shape ((m, g * n // 32)) is derived differently than the other four. I did not want to guess at the right empty-result shape for an untested return contract and ship it unverified; flagging it as a follow-up instead.Changed op tests
op_tests/test_batched_gemm_zero_m.py(new file): one pytest case per fixed entry point coveringbatch=0(bf16, a8w8) andM=0(bf16, a8w8 mxscale, a8w8 mxscale_bpreshuffle). Not redundant with the existingtest_batched_gemm_bf16.py,test_batched_gemm_a8w8.py, ortest_batched_gemm_a8w8_mxscale_bpreshuffle.py, which are correctness/benchmark scripts exercising non-zero shapes against a numeric reference and have no zero-size case; the new file only asserts shape/dtype, which is the right check for an empty result and would not fit those files' numeric-comparison structure.