Repository navigation
[kernel] NVLink collectives, and move the K3 path onto them - #36720
Closed
DarkSharpness wants to merge 7 commits into
Closed
DarkSharpness wants to merge 7 commits into
DarkSharpness wants to merge 7 commits into
Conversation
`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.
DarkSharpness
requested review from
BBuf,
Edwardf0t1,
Fridge003,
HaiShaw,
HydraQYH,
Ying1123,
celve,
ch-wan,
ispobock,
merrymercy and
yuan-luo
as code owners
August 27, 2026 18:46
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Name the state space on the bulk multimem ops.globalon four PTX ops; 12ST.E.128becomeSTG.E.128, no measured changeAdd the NVLink collectives on the communicator planesAdd a unicast copy-engine all-gather driven by stream memory opsFold a residual into the NVLink collectivesFold the residual into the pull collectives' tailRound-robin the v2 all-reduce push index over the blocksMove the K3 collectives onto the shared NVLink kernelsThree 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/unrolltables 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 howgemm_agstops importing another module's private name.Accuracy Tests
test_ar_fusion.py55 passed andtest_collectives.py6 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.
Residual all-reduce, in place, against K3's
push_res/pull_res: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
k3_ar_fusionstill 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