Repository navigation
[HIP] [gfx1250] topk_gating: E=384 single-wave kernel at every token count - #8056
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
NOT 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 |
On gfx1250 the E=384 single-wave kernel (WPT=2) stopped at 1024 tokens and 1025..2048 rows fell to prefill_n (TPW=1), which is slower there. That range is the DSv4.1 decode width with speculative tokens at high concurrency (several hundred requests times the verify width) and the small prefill chunks. Extend the single-wave dispatch to T <= 2048; other wave32 targets keep the multiwave kernel up to 1024 as before, and T > 2048 keeps prefill_n. Results are the same set of experts and weights; as with T <= 1024, only the expert picked among exactly equal (logit, bias) scores follows the single-wave lane order instead of prefill_n's. The op test sweeps T = 2048 by default. Depends on ROCm#7922 (the single-wave kernel); this branch carries its two commits until it merges. Co-Authored-By: Claude Opus 5.5 <[email protected]>
b8c0e78 to
96c174f
Compare
|
#7922 is merged, so this branch is now current main gfx1250 run at Description is updated with the PR Self-Review section (Reviewed revision |
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: Reviews: |
yzhou103
left a comment
There was a problem hiding this comment.
Reviewed 96c174f. Dispatch logic is correct: on gfx1250 E=384 topk<=8 now takes the single-wave kernel to T=2048, other wave32 targets keep multiwave to 1024 and then prefill_n, other arches are untouched. Measured on gfx1250 it is 24-42% faster than main over T=1025..2048; inline notes suggest dropping the bound.
Two description points:
- "the expert sets are identical on every row" holds on rows without an exact tie at the top-k cutoff; where the tie sits at the cutoff the set can change, and where the bias creates the tie the stored weight can too (as in #7922).
- sigmoid takes the same path (only softmax is excluded) and isn't measured; worth a line under unverified scope.
| // and WPT=1 are 5-15% slower), and the single-wave kernel still beats | ||
| // prefill_n (TPW=1) up to T=2048 on gfx1250; above that prefill_n | ||
| // takes over. Other wave32 targets keep the multiwave kernel to 1024. | ||
| if(num_tokens <= 2048) |
There was a problem hiding this comment.
The change is right, but the 2048 bound looks arbitrary: the single-wave kernel keeps beating prefill_n (TPW=1) well past it. One wave per token and no cross-token work, so nothing in it degrades with T; the old T <= 1024 gate came from the multiwave kernel, which only pays off while it fills the CUs.
gfx1250, E=384, bf16 logits, fp32 bias, sqrtsoftplus + renorm, CUDA graph (run_perftest(testGraph=True)), us, min of 2 rounds. "cap raised" is this PR with the bound set to 16384:
| T | topk | main | this PR | cap raised | Triton #7846 |
|---|---|---|---|---|---|
| 1025 | 8 | 4.67 | 2.78 | 3.49 | |
| 1536 | 8 | 5.22 | 3.49 | 3.67 | |
| 2048 | 8 | 5.44 | 3.48 | 2.96 | 4.54 |
| 2304 | 8 | 5.77 | 5.75 | 3.27 | 4.63 |
| 4096 | 8 | 6.28 | 6.23 | 3.81 | 5.78 |
| 8192 | 8 | 9.40 | 5.58 | 9.67 | |
| 16384 | 8 | 14.80 | 9.69 | 17.44 |
topk=6 is the same shape (-31% to -43% against main at every T above 1024). With the bound at 2048 there is a step from ~3.5 us at T=2048 to ~5.2 us at T=2049, and the tie order flips there too. Could the E=384 single-wave dispatch drop the token bound on gfx1250 (keeping T <= 1024 for the multiwave kernel on other wave32 targets)? That also gives E=384 one tie order at every T on gfx1250, as E=64/128/256 already have.
There was a problem hiding this comment.
Agreed, the bound is dropped in e88dee039. On gfx1250 the E=384, topk <= 8 single-wave dispatch (_DISPATCH_WAVE32_VWAVE_KERNEL(384, 2)) now runs at every T; other wave32 targets keep the multiwave kernel to T <= 1024 and prefill_n above, and other arches are untouched. The comment above the dispatch now gives your reason (one wave per token, no cross-token work) instead of a token limit.
Your measurement is what this relies on (gfx1250, E=384, bf16 logits, fp32 bias, sqrtsoftplus + renorm, CUDA graph, top-8, us): with the bound raised, T = 2048 / 2304 / 4096 / 8192 / 16384 go from 5.44 / 5.77 / 6.28 / 9.40 / 14.80 on main to 2.96 / 3.27 / 3.81 / 5.58 / 9.69, ahead of the Triton #7846 path at every T, and topk = 6 is 31-43% below main at every T above 1024. Dropping the bound also removes the step at T = 2049 and the tie-order flip there, so E=384 now has one tie order at every T on gfx1250, as E=64/128/256 do. gfx1250 validation of the widened bound pending on our side; relying on your measurement.
Large-T limits, from reading the code at this head:
- Grid:
grid(num_tokens)blocks of one 32-lane wave.prefill_nwith TPW=1, which these calls ran before, launches the samenum_tokensblocks of one wave, so the launch size limit is unchanged.num_tokensis aninttaken fromsize(0)on the host side, same as before. - Integer overflow: the input row is
gating_output + token_idx * gating_stridewith twoints. That product overflows once T times the row stride reaches 2^31 (about 5.6M tokens at stride 384, a 4 GiB bf16 logits tensor).prefill_n((valid ? token_idx : 0) * gating_stride) and the other kernels in this file use the same expression, so dropping the bound adds no new limit; it is an existing limit of the op on every path. The output offsets usesize_t stride_tk, and expert ids stay below 384. - No shared memory or per-T workspace in the kernel, and
T = 0already took this kernel before (it was inside the old bound). - Device code is unchanged:
hipcc --cuda-device-only -Sof the threetopk_gating/*.cuunits for gfx950 and gfx1250 gives the same assembly as the previous head, except for the per-file__hip_cuidsymbol. Only the host-side dispatch changed.
The 64-bit row offset is a small separate change across all the kernels in this file; I can send it as its own PR if you want it.
| type=str2tuple, | ||
| default=[16384, 4096, 1024, 256, 64, 1], | ||
| help="Comma-separated list of number of tokens (default: 16384,4096,1024,256,64,1)", | ||
| default=[16384, 4096, 2048, 1024, 256, 64, 1], |
There was a problem hiding this comment.
With the bound widened, the unaligned section (line 1336, t <= 1024) no longer reaches every T that takes the single-wave kernel. The kernel doesn't depend on T, so the risk is low, but raising that filter to the new bound (or 2048 if the bound stays) keeps its "every T that reaches the kernel" coverage.
There was a problem hiding this comment.
Done in e88dee039: the unaligned section no longer filters T <= 1024 and runs the whole --num-tokens list (default 1 to 16384). The single-wave kernel now takes every T on gfx1250, so this keeps the "every T that reaches the kernel" coverage. The SKIP branch for an empty filtered list is gone with the filter.
gfx950 at this head: python op_tests/test_moe_topk_gating.py --section unaligned passes on all 1260 configurations (4 expert counts x 7 token counts x 5 topk x 3 score functions x 3 dtypes). gfx950 does not reach the single-wave kernel, so this only checks the widened section itself. gfx1250 validation pending on our side.
Drop the T <= 2048 bound on the gfx1250 E=384 single-wave dispatch. The kernel runs one wave per token with no cross-token work, so nothing in it degrades with T, and it stays ahead of prefill_n (TPW=1) at large T as well. This also gives E=384 one tie order at every T on gfx1250, as E=64/128/256 already have. Other wave32 targets keep the multiwave kernel up to 1024 tokens and prefill_n above it; other arches are unchanged. The op test's unaligned section now runs the whole --num-tokens list instead of T <= 1024, so it reaches every T the single-wave kernel takes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@yzhou103 thanks for the review. New head
gfx1250 validation of the widened bound pending on our side; relying on your measurement. |
Motivation
On gfx1250 the E=384
topk_gatingsingle-wave kernel from #7922 (WPT=2) was dispatched only up to 1024 tokens; above that, E=384 fell toprefill_n(TPW=1), which is slower there. The 1025+ range is the DSv4.1 decode width once speculative-decoding verify rows are counted at high concurrency (several hundred requests times the verify width), plus prefill chunks.#7922 (the single-wave kernel) is merged; this branch is current main plus the commits of this PR.
Technical Details
topk_gating_launch: on gfx1250 (topk_gating_vwave_arch()), the E=384,topk <= 8single-wave dispatch (_DISPATCH_WAVE32_VWAVE_KERNEL(384, 2)) no longer has a token bound. The kernel runs one wave per token with no cross-token work, so nothing in it depends on T. This also gives E=384 one tie order at every T on gfx1250, as E=64/128/256 already have.prefill_nabove it; wave64 arches keep their kernels. Nothing changes off gfx1250.prefill_npath, as forT <= 1024under [HIP] topk_gating: single-wave register kernel for wave32 (gfx1250) #7922: rows without an exact score tie at the top-k cutoff get the same expert set and the same weights. Where equal(logit, bias)scores sit at the cutoff, the single-wave lane order picks the expert instead ofprefill_n's order, so the set can differ; where the bias creates that tie, the stored weight of the picked expert can differ too.op_tests/test_moe_topk_gating.py: the default token list gains T = 2048, and theunalignedsection runs the whole--num-tokenslist instead ofT <= 1024.No new environment variables.
Test Plan
op_tests/test_moe_topk_gating.py(all sections).op_tests/test_moe_topk_gating.py.Test Result
96c174f39(bound at 2048), gfx1250: all sections pass. Versus main on 33,800 random E=384 rows at T = 1025..2048: the expert sets are identical on every row and the weights are bit-identical. gfx950: the full test passes on main and on that head.__hip_cuidsymbol. A reviewer measured the single-wave kernel against main and againstprefill_non gfx1250 with the bound raised well past 2048 (see the review thread). gfx1250 validation of the widened bound is pending on our side.unalignedsection (now over the whole default token list) passes over all 1260 configurations (4 expert counts x 7 token counts x 5 topk x 3 score functions x 3 dtypes).PR Self-Review
Reviewed revision
e88dee0
Behavior and intent
topk_gatingcalls withtopk <= 8ran the single-wave kernel of [HIP] topk_gating: single-wave register kernel for wave32 (gfx1250) #7922 up to 1024 tokens (2048 at the previous head of this PR) andprefill_n(TPW=1) above that.topk_gating_kernel_opt_vwave<.., 384, 2, ..>) at every token count. Other arches, other wave32 targets, other expert counts andtopk > 8are unchanged.prefill_n's; if the bias creates that tie, the stored weight can differ too, as in [HIP] topk_gating: single-wave register kernel for wave32 (gfx1250) #7922.prefill_n(TPW=1): both launchnum_tokensblocks of one wave, and both index the logits with the sameintproducttoken_idx * gating_stride, so dropping the bound adds no new limit.Validation and unverified scope
96c174f39, gfx1250:python op_tests/test_moe_topk_gating.py(all default sections, with T = 2048) passed; E=384 sqrtsoftplus (renorm, bias, top-6) at T = 1025..2048 against main: identical expert sets and bit-identical weights on all compared rows (random logits).hipcc --cuda-device-onlyfor gfx950 and gfx1250 of the threecsrc/kernels/topk_gating/*.cuunits (-S) gives assembly identical to the previous head's apart from the per-file__hip_cuidsymbol (the change is host-side dispatch only).python op_tests/test_moe_topk_gating.py --section unalignedpassed over the full default token list; black and ruff clean on the changed Python file.PR-specific question
Which architecture, dtype and shape boundaries select the changed kernel path, and what invariant prevents unsupported inputs from reaching it?
topk_gating_vwave_arch(), i.e.get_gpu_arch() == "gfx1250"for the current device; gfx1250-strict and every other arch keep their previous path.num_experts == 384andtopk <= 8on that arch, any token count (the changed range is above 1024). Other wave32 targets keep the multiwave kernel up to 1024 andprefill_nabove.Changed op tests
op_tests/test_moe_topk_gating.py: the default--num-tokenslist gains 2048, so a default run checks the newly routed range against the torch reference between 1024 and 4096, where the list had no point before.unalignedsection: it used to keep onlyT <= 1024, the last T that reached the E=384 single-wave kernel. That kernel now takes every T on gfx1250, so the section runs the whole--num-tokenslist (default up to 16384) and keeps checking that the scalar-load path routes bit for bit as the vector-load path at every T that reaches the kernel. Not redundant: no other section feeds unaligned logits or bias.🤖 Generated with Claude Code