Skip to content

[kernel] NVLink collectives, and move the K3 path onto them - #36720

Closed
DarkSharpness wants to merge 7 commits into
sgl-project:mainfrom
DarkSharpness:nvlink-collectives
Closed

DarkSharpness wants to merge 7 commits into
sgl-project:mainfrom
DarkSharpness:nvlink-collectives

Conversation

@DarkSharpness

@DarkSharpness DarkSharpness commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

Generated by Claude.

Motivation

NVLink multicast gives a Blackwell node a set of collectives that the ring in NCCL cannot match at the sizes inference actually runs at, and K3 had already grown its own copy of them. This adds the shared implementation, folds a residual into it, and retires the K3 copy.

Modifications

Seven commits, each standalone:

commit what
Name the state space on the bulk multimem ops .global on four PTX ops; 12 ST.E.128 become STG.E.128, no measured change
Add the NVLink collectives on the communicator planes all-reduce / all-gather / reduce-scatter, push and pull, on the CustomAllReduceV2 planes
Add a unicast copy-engine all-gather driven by stream memory ops an all-gather that launches no kernel: copy engine for the payload, batched stream memory ops for the barriers
Fold a residual into the NVLink collectives a residual rides the reduction on all six entry points
Fold the residual into the pull collectives' tail bug fix, see below
Round-robin the v2 all-reduce push index over the blocks fills the grid in the size band where the block-size ladder leaves it part idle
Move the K3 collectives onto the shared NVLink kernels retires K3's reduce-scatter, all-gather and residual all-reduce; -1531 lines

Three things worth calling out.

A residual was being dropped. The pull kernels added it on the unrolled path and not on the remainder loop, so any element past the last whole warp-chunk came out without it. It hid because the vector count per token was a multiple of the chunk at every size in play -- 7168 and 3584 both are. test_ar_fusion_pull_2shot[1-True] now covers it: at one token per rank the count is 112 and nothing reaches the unrolled path. Reverting the fix fails that case.

The tuning tables shrank. Round-robining the push kernel's warps over its blocks flattened the block-size curve enough that one block size serves every size (at world 8, 128 tokens: the curve was 5.46us to 7.25us across block sizes, now 5.53us to 5.65us). What is left is four numbers per world size -- where the gather and the scatter switch from push to pull, and where each hands back to NCCL -- keyed by SM arch rather than device name. The per-size num_blocks / block_size / unroll tables are gone; only the side-stream all-reduce keeps a block cap, which is what it was for.

The three K3 communicator registries became one, in kimi_k3/comm.py, which is also how gemm_ag stops importing another module's private name.

Accuracy Tests

test_ar_fusion.py 55 passed and test_collectives.py 6 passed, at TP=4 and TP=8 on 8xB200. Against the K3 kernels the outputs were compared element for element across the sweep below and matched everywhere.

New coverage: a ragged token count (the plain pair routes on a prefix sum, so the shards differ in length by one), and a hidden size that leaves a non-empty remainder loop.

Speed Tests and Profiling

8xB200, H=7168, bf16, global tokens. Ours on defaults, K3 on the best of a block sweep, so the comparison leans its way.

tokens reduce-scatter +residual all-gather
128 -13.5% -11.8% -9.3%
512 -6.7% -6.1% -12.6%
1024 -12.8% -12.7% -12.9%
2048 -16.6% -16.6% -13.1%

Residual all-reduce, in place, against K3's push_res / pull_res:

tokens push pull
1 -1% -8%
16 -1% -16%
256 -16% -5%
1024 -4% -1%

Against NCCL at TP=8, the reduce-scatter leads over the whole measured range (329.5us against 367.9us at 16384 global tokens); the all-gather leads until 16384, where NCCL takes it by 2.7%.

Folding the residual is close to free -- 0.0 to 0.3% at 16384 tokens, against 2.7% for a separate pass on the scatter and 20.2% on the all-reduce.

Known gaps

  • The K3 SP collective only enables on SM103, so its glue path has not run on the B200 this was measured on. The strategy boundaries were measured on a single-node B200 and GB300 borrows them by arch alias -- and GB300 is a different fabric, so they want confirming there. The GB300 tables this replaced were measured across two nodes and were off by 2-5x on one node, which is why they were not carried over.
  • The push/pull switch in k3_ar_fusion still sits at _PUSH_MAX_BYTES; the shared push is faster than the K3 one in that band, so the crossover has likely moved.

Checklist


CI States

Latest PR Test (Base): ❌ Run #33105131825
Latest PR Test (Extra): ❌ Run #33105320367
Latest PR Test (AMD ROCm 7.2): ❌ Run #33105131594

`multimem.ld_reduce` and `multimem.st` were written without `.global`, so ptxas
lowered them as generic-address accesses: 12 of the 16-byte stores in the
distributed module came out as `ST.E.128` rather than `STG.E.128`. The
semaphore helpers a few lines below already spell the state space out.

No measured effect on the all-gather or the scatter at any size -- both sit on
the fabric ceiling either way -- so this is for the codegen, not the clock.
all_reduce, all_gather and reduce_scatter in two forms. The push stages through
the plane's own workspace and polls Lamport pos-zero markers, so the caller
brings plain tensors; the pull runs directly on the caller's symmetric buffers
through their multicast aliases, and only the full-length side needs one. Also
adds an all-gather that hands the payload to the copy engine, and the multicast
barrier it and the pull path share.

The split across ranks is ragged -- rank r owns `avg + (r < rem)` tokens at
`r * avg + min(r, rem)` -- so neither a scatter nor a gather needs the token
count to divide by the world size, and an RS/AG pair round-trips exactly.

The barrier takes its stream from the caller. tvm-ffi only publishes the
framework stream when a call carries a DLPack tensor, and that op carries none;
resolved from the environment instead, its launch landed on a stale stream and
a graph capture ran it once at capture time and skipped it on every replay.

The benchmark under benchmark/bench_nvlink_comm is a scratch harness -- not
registered, not wired into CI.
… ops

The multicast copy-engine gather injects at a flat ~100 GB/s no matter how
wide the fan-out, so it only pays once the world is large enough to amortise
that; at TP=4 it lands at half of NCCL. This takes the other route -- one
peer-to-peer copy per rank over the unicast links, walked from this rank so
the senders do not converge on one destination -- which is what NCCL itself
switches to at large sizes.

Both barriers are stream memory ops rather than kernels, so the collective
occupies no SM at all. A sequence number would be baked into a graph at
capture time and every replay would wait on a stale value, so the flag is a
constant and the same batch clears it again; two flag arrays alternate, which
keeps one round's reset from erasing the next round's arrivals.

At TP=4/16384 tokens this is 1047 us against NCCL symmetric memory's 1041,
where the multicast path takes 2166. It carries a ~50 us floor from the
per-rank copies and the two barriers, so it is a large-size path only.
The reduce is already reading and writing the shard, so a residual add costs
nothing extra folded in and a whole pass over HBM kept separate. All six
entry points take one: it may be shaped like this rank's shard, or like the
whole tensor, in which case the slice comes from `get_routing` rather than a
uniform stride, so ragged splits need no view on the caller's side.

The push kernel now round-robins its warps over the blocks instead of giving
each a contiguous run. Its poll domain is the shard for the reduce-scatter,
world_size times smaller than what the push loop walks, so a block-major index
parked all of it on the first `num_poll_vecs / blockDim` CTAs and idled the
rest -- with the grid pinned to the SM count, most of them. At world size 8
that is worth 22% at 128 tokens and 11% at 512, and it flattens the block-size
curve enough that 1024 threads is now the right default everywhere rather than
a parameter to tune per size.

Against the K3 SP collective at TP=8 the two now sit 7-16% apart in our
favour across 128..2048 tokens, on our defaults against its best of a block
sweep, with the outputs verified equal element for element.
The unrolled path added it and the remainder loop did not, so any element
past the last whole warp-chunk came out without its residual. All three pull
primitives were affected.

It stayed hidden because the vector count per token was always a multiple of
the chunk at the sizes in play -- 7168 and 3584 both are, and so was the 1024
the test happened to use -- which left the remainder loop empty. A reduce
over a hidden size that is not, or a change to the unroll width, would have
exposed it. `test_ar_fusion_pull_2shot[1-True]` now covers it: at one token
per rank the vector count is 112, and nothing reaches the unrolled path.
The grid is pinned to the counter array, so once the vector count stops
filling `gridDim * blockDim` a block-major index leaves the tail CTAs idle.
`choose_block_size` steps 128 / 256 / 512 and then jumps to 1024, which
leaves a 2x band of sizes -- 84 to 169 tokens at hidden 7168 -- where the
grid is only part occupied.

Worth 1.5 to 2.2% below 128 tokens and 3.2 to 3.9% at 192 and 256, at both
world 4 and world 8, against 0.2 to 1.2% lost above 512 where the grid was
already full and the round-robin only costs locality. This path exists for
small messages, so that is the right side of the trade.
K3 carried its own reduce-scatter, all-gather and residual all-reduce. The
shared NVLink collectives now cover all of them -- the residual folds into
the same reduction, and they run in place -- so the K3 copies go, taking
1531 lines with them.

Head to head at TP=8 the shared kernels win every size, on their own
defaults against the best of a block sweep of the K3 ones: 7-16% on the
scatter and gather from 128 to 2048 global tokens, and 1-16% on the residual
all-reduce, with the outputs verified equal element for element. Two things
close the gap the old tuning table was there to paper over: round-robining
the push kernel's warps over its blocks, which flattened the block-size
curve enough that one block size serves every size, and letting the block
count come from the size rather than a checked-in number.

So the per-size tables go. What replaces them is four numbers per world size
-- where the gather and the scatter switch from push to pull, and where each
hands back to NCCL -- keyed by SM arch rather than device name, since they
track the fabric more than the GPU. The GB300 tables they replace were
measured across two nodes and were off by 2-5x on a single node. The block
count and unroll knobs are gone with them; only the side-stream all-reduce
keeps a block cap, which is what it was for.

Three smaller things fall out. The three per-module communicator registries
become one in `kimi_k3/comm.py`, which is also how `gemm_ag` stops importing
another module's private name. `sp_collective.py` keeps only what a custom
op needs: the op declarations and the strategy table. And the plain scatter
gains ragged support, since the kernels route on a prefix sum; the fused
attention-residual path still needs a uniform stride, so that check moves to
its two callers rather than gating everything.

What stays: the three RMSNorm epilogues, which have no equivalent here, and
the attention-residual fusion beside them. `ar_fusion.cuh` keeps those and
loses 148 lines.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant