Files
9159a6443d feat(k3.1): support KV/context parallelism on gfx950 and serve startup requests (#3082)
## Summary

Kimi-K3.1 on AMD gfx950 with the DSA KV cache sharded over eight
KV-parallel ranks (`--kv-parallel-size 8 --decode-context-parallel-size
8` at TP8). KVP1, NVIDIA and the bitwise numerics envelopes keep main's
paths.

**How a DSA layer runs, per GPU**

| | KVP1 | KVP8 decode | KVP8 prefill |
|---|---|---|---|
| Projections (q, kv, o) | 12 of 96 heads | 12 of 96 heads | 12 of 96
heads |
| KV cache held | every page | one eighth of the pages | one eighth of
the pages |
| Index scoring | full history; every row in decode, one eighth of the
rows in prefill | every row over its own pages | one eighth of the rows
over the full history |
| Top-k it attends | the full top-k | its share of the global top-k (the
keys it holds) | the full top-k |
| Attention core | 12 heads over the top-k | all 96 heads over its share
| 12 heads over the top-k |
| Moved between GPUs | prefill's top-k selections | index scores with
the 96-head query riding along; then each head's partial output and LSE
to the GPU that owns the head | the chunk's history pages it lacks
(index-K, then latent); prefill's top-k selections |

**Optimizations**
- **Prefill over gathered history:** the sharded prefill exchanges each
chunk's 96-head query and partials, and its index scoring grows faster
with history. Instead, each rank gathers the history pages it lacks,
which moves less and keeps KVP1's kernels and numerics. The gather
buffer is reserved from the cache budget, so memory planning counts it.
- **Owned top-k decode:** each rank scores only the pages it holds
(`launch_gluon_dsa_index_candidates_gfx950`, now the default), then
selects only its share of the global top-k from the gathered scores in
one launch (`_select_owned_from_gathered_kernel`), instead of every rank
merging every rank's candidates. The query rides in the same score
gather, saving a separate query gather.
- **Records exchange over Iris:** the decode core writes each head's
partial output and LSE straight into the exchange's layout, splitting
keys by each row's live length
(`launch_gluon_dsa_decode_dcp_records_gfx950`). These exchanges are
small and latency-bound, so the target and the EAGLE drafter send them
as GPU-side pulls instead of NCCL and combine records as they arrive
(`IrisPullExchange`, `try_combine_records`). The Iris heap reserves room
for it, so collectives created later still fit.
- **Startup requests:** model-only warmup runs on a probe cache and
never takes the request path (prefix extends, partial prefill chunks,
draft verify), so first requests still compiled kernels. With the opt-in
`--enable-startup-requests` (on in the Kimi-K3.1 AMD recipe), the Rust
event loop serves a few requests of its own, from one token up to a full
batch of prefill chunks, before it reports ready, then drops their
prefixes. PD disaggregation and L3 KV stores refuse the flag for now.
- **Warmup variants:** kernels that pick a compile variant from the
batch (a row tile, a config bucket, a K split, a warp count) either keep
the row count out of the specialization or compile every variant they
can pick while warming up, so a batch size the startup requests did not
run does not compile while serving.
- **MLA decode under context parallelism:** the MLA backend serves
decode over the sharded cache as well (`_forward_dcp_decode`): the Gluon
MLA decode returns the per-head LSEs the cross-rank combine needs, and
its partials reduce takes the key-split count as a runtime value in
power-of-two buckets (`_mla_softmax_reducev_kernel`), so an eager batch
of any size runs on its bucket's binaries. Block drafts, batch-invariant
numerics, sliding windows and logit caps refuse DCP decode for now.
- **Smaller savings:** KVP write slots resolve once per forward instead
of at every cache write; query gathers move 8-byte words instead of
strided bytes; Kimi-K3 side streams get their own hardware queues
instead of sharing the forward's.

## Performance

Kimi-K3.1 on 8x MI350X, TP8, EAGLE3, the recipe's command with
`--max-num-seqs 48` and `--max-model-len 1048576`. Each row sends its
requests at once. Total tokens per second counts every prompt token,
cached or not, plus output tokens, over the run's duration; latency is
per request, from send to last token, averaged over requests.

| Workload | Mode | Output tokens per second | Total tokens per second |
Mean TTFT | Mean E2E latency | Running at once |
|---|---|---|---|---|---|---|
| 1K input and 750 output tokens, 64 requests | KVP1 | 1,767 | 4,124 |
1.2 s | 14.6 s | 48 |
| | KVP8 | 1,763 | 4,113 | 1.1 s | 15.4 s | 48 |
| 50K input and 500 output tokens, 64 requests | KVP1 | 174 | 17,623 |
75.9 s | 106.7 s | 13 |
| | KVP8 | **184** | **18,566** | **48.9 s** | 126.4 s | 48 |
| 650K input and 800 output tokens, 4 fresh requests | KVP1 | 19 |
15,602 | 100.7 s | 104.2 s | 1 |
| | KVP8 | 19 | 15,695 | 98.9 s | 164.4 s | 4 |
| 650K second turn (+1.8K new tokens), 800 output tokens, 4 requests |
KVP1 | 177 | 144,101 | 7.2 s | 11.3 s | 1 |
| | KVP8 | **348** | **283,787** | **0.77 s** | **7.5 s** | 4 |
| 1M input and 800 output tokens, 8 requests | KVP1 | rejected (at most
748K prompt tokens) | | | | |
| | KVP8 | **11** | **14,226** | 310 s | 471 s | 8 |

KVP8 is for long-context serving: each GPU holds one eighth of the KV
cache instead of all of it, so KVP8 serves reused long contexts (agentic
multi-turn) about 2x faster with a 9x faster first token, runs more long
requests at once, and serves prompts beyond KVP1's 748K limit. At short
context it matches KVP1.

- **Second turn:** the same four 650K conversations come back with 1.8K
new tokens each, with the prefix cache and host KV store on in both
modes (`--enable-prefix-caching --kvstore-ratio 8`); 99.7% of its prompt
tokens are cache hits.
- KVP8's higher latency on fresh long prompts comes from the scheduler
processing all admitted prompts before decoding.

**Startup**, cold compile cache, with and without
`--enable-startup-requests`; steady-state C16 latency and throughput are
unchanged:

| | KVP1 | KVP8 |
|---|---|---|
| First 50K-token request after ready | 6.70 s → 2.6 s | 7.42 s → 2.8 s
|
| Kernel compiles after ready | 37 → 0 | 35 → 0 |
| Time to ready | 233 s → 258 s | 240 s → 268 s |

## Test Plan

- gfx950 kernel tests for the touched kernels on MI350X: 1,755 passed.
The 11 `test_k3_prefill_dispatch` cases fail, as on main.
- Multi-GPU tests at two and eight ranks (Iris pull exchange, column
gather and Lamport all-reduce, owned top-k, DCP exchange, history
gather, Kimi-K3.1 DCP): 145 passed.
- Runtime unit tests for the touched modules: 919 passed.
`test_registered_pool_and_router_bind_distinct_leaf_geometries` (it
expects NVIDIA's TRTLLM MLA backend) and four
`test_qwen4_backend_composition` cases fail the same way on main.
- `ts-control` Rust tests, including the startup requests: 156 unit and
60 serving tests pass.
- Teacher-forced logprobs, KVP8 vs KVP1: mean |delta logprob| 0.056 on
short prompts and 0.153 on 40K-token prompts.

---------

Signed-off-by: Zhewen Yu <zhewenyu@amd.com>
Signed-off-by: Max Dawkins <max.dawkins@gmail.com>
Signed-off-by: Lei Zhang <antiagainst@gmail.com>
Co-authored-by: Max Dawkins <max.dawkins@gmail.com>
Co-authored-by: Lei Zhang <antiagainst@gmail.com>
2026-11-14 17:51:04 +00:00
..