Skip to content

[Feat] kimi-k3.1: Prefill context parallelism (CP-TP group sharing) - #48709

Merged
Fridge003 merged 3 commits into
mainfrom
k31-pcp/1-core
Nov 7, 2026
Merged

Fridge003 merged 3 commits into
mainfrom
k31-pcp/1-core

Conversation

@Fridge003

@Fridge003 Fridge003 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Original authors: @yhyang201

Port of #48414

Prefill context parallelism for Kimi-K3.1 (radixark/sglang-k31#22), the first PR of the split of #48504: --enable-prefill-cp --cp-strategy interleave --attn-cp-size N on the layer-boundary prefill CP framework (#42044), sharing the CP and TP groups.

Against the raw port: main split the KDA attention into forward_pre_o_proj + forward (#45202), so the reorder back to interleave rows now ends forward_pre_o_proj, keeping its output on the input's rows; forward is unchanged. Left for the next PRs of the stack: MegaMoE, breakable prefill graphs, integrated (non-PD) DFlash and PP × PCP graphs.

Split of #48504. Stack: #48709 (core) → #48710 (MegaMoE / BCG / DFlash) → #48711 (fused kernels) → #48712 (PP × PCP BCG). Independent: #48713 (shared-expert TP across PCP), #48714 (trtllm DSA), #48715 (Linker PCP L3).

Modifications

  • Config (cp_tp_group_sharing_hook.py, model_overrides/kimi_k3.py): add KimiK31ForConditionalGeneration to CP-TP group sharing with the interleave strategy. Prefill only: a decode node and Kimi-K3 prefill CP are rejected; DFLASH is allowed on a PD prefill node.
  • KDA (kimi_k3.py, configs/kimi_linear.py, runtime_context.py): partition heads over linear_attn_parallel_group() (the TP group under CP-TP group sharing) via the new linear_attn_tp_rank(); reorder rows from interleave to sequence order before the recurrence and back before o_proj (one strided copy, cp/interleave.py).
  • Latent MoE: under attention CP it stays off the residual stream; with the new stage fact output_on_cp_shards (layer_boundary/), it reduce-scatters its gathered TP-partial sums onto this rank's CP rows (KimiK3MoE._forward_cp_shard_rows).
  • Model: embed every token, run the layers on this rank's CP shard and gather the hidden states (and DFlash aux hidden states) before the logits.

Accuracy Tests

Measured on sglang-k31 dev with the original PR, not re-run on this branch:

  • gsm8k: PP2 × PCP4 0.954, PP2 × PCP8 (2 nodes) 0.958 (TP2 × PP4 0.949).
  • PD (PP4 × PCP2 prefill + DFLASH → TP8 DCP8 decode): passkey 32K/128K 5/5; gsm8k 0.954 / 0.952 before / after a 10-min c160 load.

Speed Tests and Profiling

PD TTFT through the router, bs=1, same decode (sglang-k31 dev):

input PP4 × PCP2 PP4 × TP2 PP8
8K 0.95 s 1.15 s 1.65 s
32K 2.25 s 2.50 s 3.65 s
128K 5.22 s 5.45 s 6.12 s
256K 9.67 s 9.85 s 9.99 s

Tests

test_kimi_k3_stage_kernels.py and test_producer_completion.py plan K3 stages with the parallel layout the declaration now reads (k3_planning).

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #42291603844
Latest PR Test (Extra): ❌ Run #42291603756
Latest PR Test (AMD ROCm 10): ❌ Run #42291603549

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@Fridge003

Copy link
Copy Markdown
Collaborator Author

_forward_cp almost copied eager_runner._execute_extend_cp. Please use eager_runner._execute_extend_cp instead.

@Fridge003

Copy link
Copy Markdown
Collaborator Author

Please remove the changes in interleave.py, since it will be replaced by interleave_row kernel in #48711

Fridge003 and others added 2 commits November 6, 2026 00:39
Port of radixark/sglang-k31#22, split out of #48504.

Enable `--enable-prefill-cp --cp-strategy interleave --attn-cp-size N` for
Kimi-K3.1 on the prefill CP layer-boundary framework:

- Share the CP and TP groups for KimiK31ForConditionalGeneration.
- Partition KDA heads over linear_attn_parallel_group() (the TP group under
  CP-TP group sharing) and reorder KDA rows between interleave and sequence
  order around the recurrence.
- Keep the latent MoE off the residual stream under attention CP and
  reduce-scatter its sum onto this rank's CP rows (output_on_cp_shards).
- Shard the embeddings in the model and gather the rows, plus DFlash aux
  hidden states, before the logits.
- Allow a Kimi-K3.1 PD prefill node with DFLASH; reject decode nodes and
  Kimi-K3 prefill CP.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…main's interleave copies

Kimi-K3 now hands its CP boundary to EagerRunner._execute_extend_cp via
prepare_cp_inputs (the multimodal wrapper injects image embeddings first),
replacing the model-local _forward_cp copy. A middle PP stage returns its
proxy before the aux unpack, and a wrapper that exposes its causal LM as
.model runs that LM's text body. interleave.py reverts to main; the
interleave_rows kernel replaces those copies in the fused-kernels PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Fridge003

Copy link
Copy Markdown
Collaborator Author

Addressed both comments in 1cc046d (also rebased onto main, resolving the conflict with #48721):

Not yet run on GPU: Kimi eager CP and GLM CP need a re-check.

_kimi_k3_overrides now refuses --enable-prefill-cp for Kimi-K3 before its
DCP branch, so the SimpleNamespace standing in for the resolved server
args needs the field; without it every _kimi_k3_mode case raised
AttributeError.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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