Skip to content

perf(hybrid): don't split a channel into more parts than it has tokens for - #2

Open
whn09 wants to merge 2 commits into
amazon-contributing:mainfrom
whn09:clamp-parts-by-min-tokens
Open

perf(hybrid): don't split a channel into more parts than it has tokens for#2
whn09 wants to merge 2 commits into
amazon-contributing:mainfrom
whn09:clamp-parts-by-min-tokens

Conversation

@whn09

@whn09 whn09 commented Aug 21, 2026

Copy link
Copy Markdown

Re-post of Xuan-1998/DeepEP#43 against this repo, retargeted and re-measured on main (ec623f3). Two things changed versus the old PR:

  • The ec623f3 refactor split the hybrid kernel in two. The old patch touched hybrid_dispatch.cuh, which is now the ordered (upstream) kernel and has no sub-parts at all. This version targets hybrid_dispatch_unordered.cuh, the kernel EP_HYBRID_KERNEL selects by default and the one EFA actually runs.
  • All numbers below were re-measured on this tree. The old PR's numbers are not quoted.

Stacked on #1 — that PR adds EP_MIN_TOKENS_PER_PART to the JIT forwarding list, which is what makes the control run below possible in a single image. Review/merge #1 first; this branch contains it.

The problem

kNumParts — how many flush_part puts a channel's tokens leave in — is chosen today by compute_part_allocation() alone (common/gin_resource_alloc.cuh). That function only ever caps the count from above when the GIN indexed-signal budget is tight. It is never lowered because the geometry asks for it: kNumParts is never compared against kNumMaxTokensPerChannel, and there is no minimum-tokens-per-part threshold.

The budget is loosest exactly when a channel holds the fewest tokens (low --num-sms, small batch), so decode shapes settle on kMaxParts — the worst end of the axis — with no way to opt out. At 128 tokens / 12 SMs a channel holds 3 tokens and is described as 4 parts × 1 token: the last part is always empty, and 3 tokens leave as three separate single-token puts instead of one 3-token put.

Sub-parts already have exactly this guard — a clamp of kNumSubParts to kBatchSize, plus EP_SM100_MIN_SUB_TOKENS refusing to sub-split a part too small to be worth it. Parts have neither.

The change

kMinTokensPerPart, default 15 (copied from the sub-token precedent in the same file), overridable by EP_MIN_TOKENS_PER_PART. kNumParts becomes min(budget_parts, tokens_per_channel / kMinTokensPerPart).

EP_MIN_TOKENS_PER_PART=1 short-circuits to the old value rather than dividing by one — so it is an exact in-image control, not an approximation. (kNumMaxTokensPerChannel / 1 would still clamp whenever a channel holds fewer tokens than the budget allows parts, which is a different geometry from the old code and would not be a valid control.)

kNumMaxTokensPerChannel moves above the part count in the template parameter list; it depends only on already-declared parameters. Note the parentheses around the comparison in the new kNumGeomParts expression are load-bearing — an unparenthesized > inside a template parameter list closes the list instead of comparing, the same reason (kNumNotifyWarps > 0) above it is wrapped.

Measurement

tests/elastic/test_ep.py, 2 × p5en.48xlarge (8×H200 + 16 EFA each), EP8×2 = 16 ranks, --hidden=7168 --num-topk=8 --num-experts=256 --num-sms=12 --allow-hybrid-mode=1 --prefer-overlap-with-compute=0 --test-first-only. decode = --num-tokens=128, prefill = --num-tokens=8192.

Method: one image, four env-selected variants, 3 reps, variants interleaved within each rep (never all-A-then-all-B), each variant on its own EP_JIT_CACHE_DIR so a variant can never serve another's cubin, GPU memory asserted back to idle between rounds. Mean over all 16 ranks then over reps; ± is stdev across reps, not across ranks. All 48 rounds exited 0.

stock = EP_MIN_TOKENS_PER_PART=1, i.e. the exact pre-patch geometry in the same binary.

decode, 128 tokens — latency

op stock this PR (default 15) this PR + EP_NUM_SUB_PARTS=1
dispatch 367.0 ± 12.1 µs 239.8 ± 3.1 µs (−34.7%) 166.1 ± 0.4 µs (−54.7%)
expanded dispatch 366.1 ± 11.1 µs 239.7 ± 1.9 µs (−34.5%) 156.3 ± 0.9 µs (−57.3%)
cached dispatch 359.2 ± 10.1 µs 235.7 ± 1.4 µs (−34.4%) 150.8 ± 1.3 µs (−58.0%)
combine 178.1 ± 5.0 µs 178.3 ± 2.2 µs (+0.1%) 179.8 ± 1.2 µs (+0.9%)
reduced combine 196.9 ± 5.5 µs 196.3 ± 2.8 µs (−0.3%) 197.6 ± 1.2 µs (+0.4%)

Latency is the right metric at this size: 5.9 MB per rank, ~5 GB/s scale-out. This shape is message-rate bound, which is exactly what merging three single-token puts into one addresses. Combine is untouched, as expected — the change is on the dispatch scale-out path only.

prefill, 8192 tokens — unchanged

op stock this PR this PR + EP_NUM_SUB_PARTS=1
dispatch 1665.1 ± 12.4 µs 1689.7 ± 50.8 µs (+1.5%) 1633.3 ± 2.4 µs (−1.9%)
cached dispatch 1662.8 ± 8.7 µs 1657.8 ± 6.6 µs (−0.3%) 1634.0 ± 2.2 µs (−1.7%)
combine 3560.8 ± 9.2 µs 3552.0 ± 6.9 µs (−0.2%) 3544.2 ± 3.5 µs (−0.5%)
reduced combine 4243.9 ± 9.6 µs 4240.0 ± 9.7 µs (−0.1%) 4205.5 ± 1.4 µs (−0.9%)

Everything is within ±2%, and the one cell that looks like a regression (dispatch +1.5%) carries a ±50.8 µs across-rep stdev — a single noisy rep, not a trend. At 8192 tokens a channel holds far more than 15 tokens, so the clamp is inactive and this is the expected no-op.

Per-rank bandwidth at prefill is likewise flat: dispatch 72–75 GB/s scale-out / 233–246 GB/s scale-up in both arms (399.8 MB per rank). Note this bench's scale-out figure includes intra-node traffic unless --ignore-local-traffic is passed, so it is not a wire-rate number; it is quoted only to show the two arms match.

Caveats

  • Measured on H200 / sm_90 over EFA only. The default of 15 is a judgement call inherited from EP_SM100_MIN_SUB_TOKENS in the same file, not something tuned per architecture — happy to make it arch-conditional, or to default it to 1 (opt-in) if you would rather not change behaviour for shapes nobody has measured.
  • Single node count (2). The interaction with the indexed-signal budget is exactly where more nodes would change the picture, since compute_part_allocation()'s cap tightens with rank count.

@whn09 whn09 changed the title perf(hybrid): dont split a channel into more parts than it has tokens for perf(hybrid): don't split a channel into more parts than it has tokens for Aug 21, 2026
@Xuan-1998 Xuan-1998 added the enhancement New feature or request label Aug 21, 2026
whn09 added 2 commits August 24, 2026 06:26
`hybrid_dispatch_unordered.cuh` gates the sub-part geometry behind `#ifndef`
(`EP_NUM_SUB_PARTS` 2, `EP_MIN_SUB_TOKENS` 1, `EP_SM100_MIN_SUB_TOKENS` 15), but
nothing in the tree sets those macros, so the only way to try a different split
is to edit the header and reinstall. Forward the three names as JIT `-D` flags,
following the `EP_NUM_TOPK_IDX_BITS` block immediately above (and its
`EP_JIT_EXTRA_FLAGS` TODO).

All three are device-only -- no host translation unit reads them -- so a
JIT-only define cannot desync host and device sizing. `flags` is part of
`kernel_signature`, so changing the env re-JITs instead of serving a cached
cubin. Unset => no behaviour change.
…s for

`kNumParts` -- how many `flush_part` puts a channel's tokens leave in -- is
chosen today by `compute_part_allocation()` alone, which only ever caps the
count from ABOVE when the GIN indexed-signal budget is tight. It is never
lowered because the geometry asks for it: `kNumParts` is never compared against
`kNumMaxTokensPerChannel`, and there is no minimum-tokens-per-part threshold.

The budget is loosest exactly when a channel holds the fewest tokens (low
`--num-sms`, small batch), so decode shapes land on `kMaxParts` -- the worst end
of the axis -- with no way to opt out. At 128 tokens / 12 SMs a channel holds 3
tokens and is described as 4 parts x 1 token: the last part is always empty, and
3 tokens leave as three separate single-token puts instead of one 3-token put.

Give parts the guard sub-parts already have. Sub-parts have both a clamp of
`kNumSubParts` to `kBatchSize` and `EP_SM100_MIN_SUB_TOKENS` refusing to
sub-split a part too small to be worth it; parts have neither.
`kMinTokensPerPart` defaults to 15 (copied from the sub-token precedent in the
same file) and is overridable by `EP_MIN_TOKENS_PER_PART`.

`EP_MIN_TOKENS_PER_PART=1` short-circuits to the old value, so it is an exact
in-image control rather than an approximation. `kNumMaxTokensPerChannel` moves
above the part count in the template list; it depends only on already-declared
parameters.
@whn09
whn09 force-pushed the clamp-parts-by-min-tokens branch from bde11bd to b097b03 Compare August 24, 2026 06:26
@whn09

whn09 commented Aug 24, 2026

Copy link
Copy Markdown
Author

Rebased onto main @ 02efc268; force-pushed. The change itself is unmodifiedgit patch-id --stable on the rebased commit matches the original.

The old branch showed a 3,000-line diff over 20 files, which was an artefact, not this change. It was cut from main @ ec623f3 (committed 08-21 00:05 UTC); main was then rewritten and force-pushed (cc55cce, committed 08-21 23:51 UTC, same author date). The four base commits exist in both histories under different SHAs, so ec623f3 became unreachable from main and the merge-base fell back six commits to 01dc3aa, attributing the whole unordered-kernel feature to this PR. Both files this PR touches are byte-identical between ec623f3 and main, so the replay was conflict-free.

Measurement caveat: the numbers in the description were taken on ec623f3, not on the rebased tree. ec623f3 → main is +124/−20 across six files, including csrc/kernels/backend/nccl.cu and csrc/kernels/elastic/combine.hpp, so I would not claim they carry over unchanged. The p5en pair used for them is no longer available to me; happy to re-run if you would like the numbers refreshed on main before merging.

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants