Skip to content

[Refactor] [GB300] kimi_k31: Admit Q5 by the draft token count and run the DCP query-gather levers by default - #84

Merged
Oasis-Git merged 1 commit into
mainfrom
kimi-k31-q5-by-draft-tokens
Nov 23, 2026
Merged

Oasis-Git merged 1 commit into
mainfrom
kimi-k31-q5-by-draft-tokens

Conversation

@Oasis-Git

Copy link
Copy Markdown
Collaborator

Motivation

After #83 the package read five env variables: K31MK_DFLASH_RUNTIME and the four TP8 levers K31MK_VERIFY_Q5, K31MK_KDA_Q5_R3_C4, K31MK_DSA_EARLY_QAG and K31MK_DSA_DIRECT_QAG. The GB300 TP8 midcurve run (round 5: 1P3D, TP8/DCP8, Q5 DFlash, MK target verify, P90 203.53) set all four to 1. On main a TP8/DCP8 Q5 server needs those four envs, and without them the target refuses Q5.

This PR removes the four flags. Q5 is admitted by the draft token count, and the DCP query-gather levers run wherever they apply. With no env, the round-5 setup gets the same lever state it ran with. The measured TP16 setup (DCP1, Q8, --max-running-requests 1) runs the same path as before.

After this PR the package reads one env variable: K31MK_DFLASH_RUNTIME.

Modifications

Lever Now Kept condition (real limit)
K31MK_VERIFY_Q5 (verify_contract.py, verify_spec.py, spec.py) Q5 is a served query width when --speculative-num-draft-tokens is 5, like Q8: Breq 1..8, rows 5..40. The tp == dcp == 8 admission check and the K31 Q5 requires TP8/DCP8 raise in BuiltK31Verify are gone: the verify path takes the query width as a parameter (KDA verify serves 2..16 queries, MLA/DSA take q, the DCP core takes up to 64 rows). Under DCP, Q5 joins Q8. The Q5 enabled; Breq=..., rows=... log becomes Q%d enabled for every width. DCP needs dcp == tp and DCP 8/16 at Q5/Q8, without attention CP or prefill CP (unchanged; the index-K and latent KV are token-strided over the whole TP group). The sampled accept builds blocks 5 and 8 only (sampled.BLOCKS).
Q5 projection tiles (sm103/k3/gemm/projection.py) The hand-written tp == 8 and batch in (10, 20, 25, 30, 35) / batch == 15 map becomes a rule. A batch wider than 8 rows with no tactic of its own runs the next wider entry for this device and pitch. That entry has a single N tile, and SplitKGemm predicates the tile's extra rows. The rule reproduces the round-5 tiles exactly (10/15 -> 16, 20 -> 24, 25/30 -> 32, 35 -> 40). Exact entries still win, and the TP8 table still comes before the TP16 table and then the GB300 table. The tp argument is gone. Up to 8 rows the N8 fallback stays. A batch wider than every entry still falls back to 8-row tiles and logs once.
K31MK_KDA_Q5_R3_C4 (sm103/k3/kda/verify.py) (3, 5): (0, 8, 2, 4) is a row of SM103_TP8_VERIFY. The comment names the measurement: KDA layer 34.278 -> 31.189 us on GB300 TP8. The table is keyed by shape, as before: SM103 and 12 KDA heads per rank (the sweep's layout). Other Q5 Breq keep the default 8 CTAs per head, because only Breq 3 was measured.
K31MK_DSA_EARLY_QAG (dsa/verify.py) Under DCP the query all-gather runs on main right after the MLA query whenever the one-shot top-k merge serves the rows. No usable merge (a NCCL candidate gather on the index stream): the gather follows the selection, as before, so that only one DCP collective runs at a time.
K31MK_DSA_DIRECT_QAG (dsa/verify.py) It used to raise outside DCP8, 12 heads and 64 rows. Now it dispatches by limit. Under DCP each rank multicasts its FP8 heads straight into the DCP core's query rows wherever the DCP group is one NVLink clique with multicast. Elsewhere the query goes through the group all-gather. The ranks agree on multicast with a MIN all-reduce, which also serves as the barrier after zeroing; otherwise ranks could gather on different paths. The buffer is sized by the DCP core: _CP_MAX_ROWS rows of 128 heads. CpSparseMla already bounds the heads (dcp x heads <= 128) and the rows (<= _CP_MAX_ROWS) to the buffer, so the old dcp == 8, heads == 12 and rows <= 64 checks are not needed. Initializing under graph capture still raises: that is a call-order error, not a setup.

The engaged log lines lose the env names: early query gather engaged (...) and direct query gather engaged (DCP8 ...). The spec name becomes Kimi K3.1 packed-MXFP4 decode / TARGET_VERIFY Q5/Q8/Q16.

#83 (f185d9b) is merged (bf98dd9). This PR is based on main 5aeb11a, which adds only GLM files on top of it.

What changes outside round 5

Setup Change
TP16 DCP1 Q8, --max-running-requests 1 (measured) None on the run path. Only Breq 1 (8 rows) runs: the projection keeps the TP16 entry (MLA (128, 8, 4, 6), KDA (64, 8, 2, 12)), KDA keeps the default schedule, and there is no DCP. The widths built but not run (24/40/48/56 rows) now compile the 32-/64-row tile instead of the 8-row fallback.
TP16 Q8 at --max-running-requests > 1 Breq 3/5/6/7 (24/40/48/56 rows) run one 32- or 64-row N tile instead of 8-row tiles that reread the weights. This is not bitwise against the fallback, because the split K differs. Not measured.
SM100 Q8 TP8 Breq 3/5/6/7 The same rule applies with the GB300 entries (rows -> 32/64). Not measured.
Q5 at TP16 (DCP1 or DCP16) Newly admitted. It runs on the same kernels. Not measured. The DFlash draft chain serves only 8-row blocks, so at Q5 the runtime hands every round to SGLang's stock round.
DCP16 TP16 Q8 The early and direct gathers now engage where they apply (16 x 6 heads fit the 128-head core). Not measured.

GPU validation

Pending. Plan: one TP8/DCP8 Q5 boot on 8 GB300, from SGLang main plus the SGLang fix that lets a draft spec it does not meet decline. Use round 5's D arguments (--tp 8 --dcp-size 8 --dcp-comm-backend fi_a2a, trtllm DSA, DFLASH with 5 draft tokens, block verification, --enable-linear-replayssm-spec --enable-nvidia-megakernel --cuda-graph-bs-decode 1 2 3 4 5 6 7 8) and no K31MK_* env. After startup, run a short greedy probe at concurrency 1 and 8.

The log must show these lines:

  • NVIDIA megakernel: Q5 enabled; Breq=(1, 2, 3, 4, 5, 6, 7, 8), rows=(5, 10, 15, 20, 25, 30, 35, 40);
  • NVIDIA megakernel: active (Kimi K3.1 packed-MXFP4 decode / TARGET_VERIFY Q5/Q8/Q16: TARGET_VERIFY Breq=(1, ..., 8) Q=5; ... widths [5, 10, ..., 40];
  • ... runs the decode graphs of batch sizes [1, 2, 3, 4, 5, 6, 7, 8] (8 of 8);
  • NVIDIA megakernel: early query gather engaged (...) and NVIDIA megakernel: direct query gather engaged (DCP8 FP8 query multicast into the 128-head layout; ...);
  • sparse verify under DCP8 (rank N): CP sparse core + LSE merge Breq=b Q=5, and active mode=TARGET_VERIFY Breq=b Q=5, for b in 1..8;
  • the draft decline from SGLang, kimi_k31_dflash2_draft_chain_nvidia does not serve this setup (tp_size 8 is not one of [16]); the draft keeps its own forward.

These lines must not appear: DFLASH rounds run on the NVIDIA megakernel (no runtime is named), any no %s projection tactic for B.. warning at rows 10-35, and the DCP group ... the query is all-gathered on the group.

Tests

  • Scope equivalence (CPU): each tree's own source was evaluated, via ast and without the GPU imports, at origin/main bf98dd9 and at this head.
    • Round 5 (TP8/DCP8/Q5, PD decode). Base ran with the four envs = 1 and this head with no env. These are equal: the admission reasons (none), Breq 1..8, the verify keys, the MLA/KDA projection tactics of all 8 widths, the KDA schedules (Breq 3 four CTAs), the early gather at every width, and the direct gather.
    • TP16 measured (DCP1, Q8, mrr 1). Both trees ran with no env. These are equal: the admission, the keys, the projection of the 8 rows that run, the KDA schedules, and no early or direct gather. The only differences are the built-but-not-run widths listed above.
    • git grep on kimi_k31 finds only K31MK_DFLASH_RUNTIME among K31MK_* / SGLANG_K31_*.
  • test/unit/test_kimi_k31_split_merge.py adds two classes, which run the real functions from source without the GPU imports:
    • K31VerifyQ5:
      • the round-5 configuration is admitted with no env; TP16/DCP1 and TP16/DCP16 Q5 are admitted; DCP below TP, prefill CP and Q4 are refused; Q5 serves Breq 1..8;
      • the projection rule gives the round-5 tiles for rows 10..40 at the TP8 pitches, the fallback at 5 rows and past the widest entry, and the TP16 8-row entry;
      • KDA Q5 Breq 3 runs 4 CTAs per head with no env, and Breq 2 runs 8.
    • K31DcpQueryGather:
      • the early gather follows the merge's usable;
      • the direct gather returns no buffer off a clique (and allocates nothing) or when any rank lacks multicast; otherwise it returns the zeroed _CP_MAX_ROWS x 128-head buffer.
    • All of these fail on origin/main.
  • Full unit suite, one pytest process per file, against SGLang cf6133a (the CI pin) and main 42abbd7e2c. On both, the failure set equals origin/main's line for line (26 and 177 lines). The pass count rises by 5, from the new tests: on the pin from 1243 to 1248, on main from 1149 to 1154. Rerun after rebasing onto 5aeb11a, against the pin: failure set identical (26 lines), passed 1279 to 1284. On main, the K3.1 DFlash runtime tests fail on origin/main too, because SGLang dropped apply_dflash_verify_logits_adjustments.
  • 12 of 13 mutants killed:
    • Q5 tied to TP8/DCP8 again; Q5 not served; Q5 at Breq 1 only; Q5 behind its env again;
    • no projection round-up; round-up at 8 rows or fewer; round-up to the widest entry;
    • KDA Q5 Breq 3 back to 8 CTAs;
    • early gather off;
    • direct gather on this rank's multicast only; direct gather raising off a clique; a 32-row direct buffer.
    • Survivor: restoring the TP8/DCP8 raise inside BuiltK31Verify.__init__. That constructor needs the GPU weight build, so no CPU test reaches it; the round-5 GPU boot runs at TP8 and does not catch it either.
  • pre-commit run passes on the changed files.

🤖 Generated with Claude Code

…n the DCP query-gather levers by default

Remove the four TP8 env flags. Q5 is admitted when the server runs 5 draft
tokens, at any TP and DCP the verify path serves (no TP8/DCP8 tie). The packed
projection runs a width with no tactic of its own in the next wider entry's
single N tile (the Q5 widths of the GB300 TP8 run), and the KDA verify table
gains the measured Q5 Breq 3 four-CTA row. Under DCP the query is gathered
early wherever the one-shot top-k merge serves the rows, and multicast straight
into the core's query rows wherever the group is one NVLink clique with
multicast on every rank; elsewhere it keeps the group all-gather instead of
raising. The package now reads only K31MK_DFLASH_RUNTIME.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Oasis-Git
Oasis-Git merged commit 76ab864 into main Nov 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant