Skip to content

[CP] Add CP-v2 support for Kimi-Linear - #31661

Closed
Fridge003 wants to merge 35 commits into
sgl-project:mainfrom
Fridge003:codex/kimi-linear-cp-v2-main
Closed

Fridge003 wants to merge 35 commits into
sgl-project:mainfrom
Fridge003:codex/kimi-linear-cp-v2-main

Conversation

@Fridge003

@Fridge003 Fridge003 commented Jul 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add a Kimi-specific CP-v2 layer communicator under layers/cp
  • gather model-entry shards before KDA, split full TP batches before MLA, and gather MLA outputs/residuals before post-attention norm and MoE/MLP
  • shard KDA projection, recurrent-cache, and state heads over attention CP ranks; all-reduce KDA outputs over the CP group, with global-TP fallback when CP is disabled
  • support zigzag CP-v2 in the GB300-default FlashInfer MLA backend using two paged MLA plans and materialized full latent KV
  • enable the Kimi-Linear model in the CP-v2 eager path and expose its input embeddings

PR #31619 has merged; this PR is rebased onto its CP-v2 implementation on main.

Layout transitions

Boundary Operation
model entry → first KDA gather CP shard into full token order
full TP MLP output → MLA split into the rank-local zigzag shard
MLA attention → post-attention norm/MLP gather hidden state and residual into full token order
final MLP → generic CP-v2 model exit reshard for the existing model-exit gather

Gathering immediately after MLA is necessary because the following TP MoE/MLP all-reduces require identical token dimensions on all ranks.

Verification

Focused CPU tests on current main:

python3 -m pytest -q \
  test/registered/cp/test_kimi_linear_cp_v2.py \
  test/registered/cp/test_cp_strategy_unit.py

27 passed, 16 warnings, 18 subtests passed

All pre-commit hooks pass for the changed files, and the final diff passes git diff --check.

Latest targeted /rerun-test checks on commit fd7edf561e:

  • CP-v2 CPU coverage: 23 tests passed (run)
  • Kimi Linear GSM8K e2e on 2×H100: passed, score 0.915 (run)

The TP4/CP4 GB300 result below predates the KDA head-sharding update. Repeating it on baizhou-dev-2 is currently blocked because the only advertised free GB300 nodes are NodeStatusUnknown.

Four-GB300 launch on baizhou-dev-2:

python3 -m sglang.launch_server \
  --model-path /mirror/models/Kimi-Linear-48B-A3B-Instruct \
  --tp 4 \
  --attn-cp-size 4 \
  --enable-prefill-cp \
  --cp-strategy zigzag \
  --trust-remote-code

GSM8K, 200 examples, 32 concurrent requests, default decode CUDA graphs:

Score: 0.920
Total latency: 21.471 s
Output throughput: 869.357 token/s

The TP4 non-CP accuracy reference on the same model and dataset subset was 0.890.


CI States

Latest PR Test (Base): ❌ Run #29684396607
Latest PR Test (Extra): ❌ Run #29684396500

Fridge003 added 30 commits July 18, 2026 00:03
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jul 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request implements CP-v2 context-parallel prefill support for Kimi-Linear, alternating between Kimi Delta Attention (KDA) on complete token batches and Multi-head Latent Attention (MLA) on zigzag token shards. It introduces KimiLinearCPV2LayerCommunicator to handle layout transitions, updates FlashInfer MLA to support CP-v2 prefill, ensures KDA state parameters and cache shapes use global tensor parallelism, and adds comprehensive unit tests. Feedback suggests removing the unused stream parameter from the postprocess_layer method signature in the communicator.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +96 to +102
def postprocess_layer(
self,
hidden_states: torch.Tensor,
residual: Optional[torch.Tensor],
forward_batch: ForwardBatch,
stream: Optional[Any] = None,
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The stream parameter in postprocess_layer is unused. It is recommended to remove it from the method signature to keep the API clean and maintainable.

    def postprocess_layer(
        self,
        hidden_states: torch.Tensor,
        residual: Optional[torch.Tensor],
        forward_batch: ForwardBatch,
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:

@Fridge003
Fridge003 marked this pull request as draft July 18, 2026 07:41
@Fridge003

Copy link
Copy Markdown
Collaborator Author

Remove the assertion of deepep?

@Fridge003

Copy link
Copy Markdown
Collaborator Author

/rerun-test

@github-actions

Copy link
Copy Markdown
Contributor

⛔ Please specify a test: /rerun-test <file>::<TestClass.test_method>

Examples:

  • /rerun-test test/registered/core/test_srt_endpoint.py::TestSRTEndpoint.test_simple_decode
  • /rerun-test registered/core/test_srt_endpoint.py::TestSRTEndpoint
  • /rerun-test test_srt_endpoint.py
  • /rerun-test test_a.py test_b.py test_c.py (multiple tests)
  • /rerun-test test_*backend*.py (wildcard — reruns every matching file; wrap the pattern in backticks so GitHub keeps the * literal)

@Fridge003

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/models_e2e/test_kimi_linear_models.py::TestKimiLinear.test_gsm8k

@github-actions

github-actions Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/models_e2e/test_kimi_linear_models.py::TestKimiLinear.test_gsm8k:

🚀 2-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/models_e2e/test_kimi_linear_models.py TestKimiLinear.test_gsm8k

@Fridge003

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/cp/test_kimi_linear_cp_v2.py

@github-actions

github-actions Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/cp/test_kimi_linear_cp_v2.py:

🚀 ubuntu-latest (1 test): ❌ View workflow run

cd test/ && python3 registered/cp/test_kimi_linear_cp_v2.py

@Fridge003

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/cp/test_kimi_linear_cp_v2.py test/registered/models_e2e/test_kimi_linear_models.py::TestKimiLinear.test_gsm8k

@github-actions

github-actions Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/cp/test_kimi_linear_cp_v2.py test/registered/models_e2e/test_kimi_linear_models.py::TestKimiLinear.test_gsm8k:

🚀 ubuntu-latest (1 test): ❌ View workflow run

cd test/ && python3 registered/cp/test_kimi_linear_cp_v2.py

🚀 2-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/models_e2e/test_kimi_linear_models.py TestKimiLinear.test_gsm8k

@Fridge003

Copy link
Copy Markdown
Collaborator Author

/rerun-test test/registered/cp/test_kimi_linear_cp_v2.py test/registered/models_e2e/test_kimi_linear_models.py::TestKimiLinear.test_gsm8k

@github-actions

github-actions Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/cp/test_kimi_linear_cp_v2.py test/registered/models_e2e/test_kimi_linear_models.py::TestKimiLinear.test_gsm8k:

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/cp/test_kimi_linear_cp_v2.py

🚀 2-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/models_e2e/test_kimi_linear_models.py TestKimiLinear.test_gsm8k

@Fridge003 Fridge003 closed this Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant