Skip to content

[PD] Enable all CP ranks for KVCache transfer #19765

Merged
ShangmingCai merged 17 commits intomainfrom
support_cp_transfer
Mar 3, 2026
Merged

[PD] Enable all CP ranks for KVCache transfer #19765
ShangmingCai merged 17 commits intomainfrom
support_cp_transfer

Conversation

@ShangmingCai
Copy link
Copy Markdown
Collaborator

@ShangmingCai ShangmingCai commented Mar 3, 2026

Motivation

Changes:

  • Add an env var to allow all CP ranks for KVCache transfer instead of just cp rank 0. The env var is set to False by default since it has little performance improvement for MLA models.
  • Remove is_mla_backend check, now we can support Qwen3 CP as well (This PR can unblock Support Qwen3 MoE context parallel #18233).

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Signed-off-by: Shangming Cai <csmthu@gmail.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the KVCache disaggregation mechanism by allowing all Control Plane (CP) ranks to actively participate in KVCache data transfer. Previously, this responsibility was limited to CP rank 0. By introducing a new environment variable and modifying the KVCache sender logic across various disaggregation backends, the system can now distribute the KVCache transfer workload more efficiently, potentially improving performance in distributed environments.

Highlights

  • Distributed KVCache Transfer: Introduced an environment variable SGLANG_DISAGGREGATION_CP_ALL_RANKS_TRANSFER to enable all Control Plane (CP) ranks to participate in KVCache transfer, rather than only CP rank 0.
  • KVCache Page Distribution: Implemented logic to distribute KVCache pages among CP ranks, ensuring each rank processes only its assigned subset of pages during transfer.
  • Backend Integration: Applied the new KVCache distribution logic to Mooncake, Mori, and Nixl disaggregation backends.
  • Refined CP Rank Handling: Updated the is_dummy_cp_rank determination and assertions related to decode CP size to align with the new all-ranks transfer capability.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • python/sglang/srt/disaggregation/common/conn.py
    • Added enable_cp_all_ranks_transfer attribute to CommonKVSender based on a new environment variable.
    • Modified the condition for is_dummy_cp_rank to incorporate the enable_cp_all_ranks_transfer flag.
    • Included a debug log for CommonKVSender initialization.
    • Updated assertions and target_cp_ranks logic to support all CP ranks in KVCache transfer.
  • python/sglang/srt/disaggregation/mooncake/conn.py
    • Imported the page_indices_to_cp_rank_page_indices utility function.
    • Replaced local_rank with prefill_unique_rank for consistent identification of prefill senders.
    • Integrated page index filtering within the send method to ensure each CP rank handles its specific portion of KV pages.
  • python/sglang/srt/disaggregation/mori/conn.py
    • Imported the page_indices_to_cp_rank_page_indices utility function.
    • Implemented page index filtering within the send method for distributing KV pages among CP ranks.
  • python/sglang/srt/disaggregation/nixl/conn.py
    • Imported the page_indices_to_cp_rank_page_indices utility function.
    • Applied page index filtering within the send method to manage KV page distribution across CP ranks.
  • python/sglang/srt/disaggregation/utils.py
    • Introduced a new function page_indices_to_cp_rank_page_indices to logically partition global KV page indices among multiple CP ranks.
  • python/sglang/srt/environ.py
    • Defined a new environment variable SGLANG_DISAGGREGATION_CP_ALL_RANKS_TRANSFER with a default value of True.
Activity
  • The pull request was initiated by ShangmingCai to enable all Control Plane (CP) ranks for KVCache transfer, moving beyond the previous limitation of only CP rank 0.
  • The description outlines the motivation and modifications, indicating a focus on improving distributed KVCache handling.
  • Standard contribution checklists are present, suggesting the PR is in an early review stage or awaiting further checks.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

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

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 introduces a feature to enable all Context Parallelism (CP) ranks for KVCache transfer, controlled by a new environment variable. The changes are well-implemented and correctly enable the intended functionality. My main feedback is regarding code duplication across several backend-specific connection files (mooncake/conn.py, mori/conn.py, and nixl/conn.py). I've suggested a refactoring to improve maintainability.

Comment thread python/sglang/srt/disaggregation/mooncake/conn.py Outdated
Signed-off-by: Shangming Cai <csmthu@gmail.com>
@ShangmingCai
Copy link
Copy Markdown
Collaborator Author

/rerun-stage stage-c-test-8-gpu-h20

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

✅ Triggered stage-c-test-8-gpu-h20 to run independently (skipping dependencies).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

🔗 View workflow run

@ShangmingCai
Copy link
Copy Markdown
Collaborator Author

/rerun-stage stage-b-test-large-2-gpu

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

✅ Triggered stage-b-test-large-2-gpu to run independently (skipping dependencies).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

🔗 View workflow run

Signed-off-by: Shangming Cai <csmthu@gmail.com>
@ShangmingCai
Copy link
Copy Markdown
Collaborator Author

/rerun-stage stage-c-test-8-gpu-h20

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

✅ Triggered stage-c-test-8-gpu-h20 to run independently (skipping dependencies).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

🔗 View workflow run

@ShangmingCai
Copy link
Copy Markdown
Collaborator Author

/rerun-stage stage-b-test-large-2-gpu

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

✅ Triggered stage-b-test-large-2-gpu to run independently (skipping dependencies).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

🔗 View workflow run

Signed-off-by: Shangming Cai <csmthu@gmail.com>
@ShangmingCai
Copy link
Copy Markdown
Collaborator Author

/rerun-stage stage-c-test-8-gpu-h20

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

✅ Triggered stage-c-test-8-gpu-h20 to run independently (skipping dependencies).

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 3, 2026

🔗 View workflow run

@ShangmingCai
Copy link
Copy Markdown
Collaborator Author

image

Related CI has passed.

@ShangmingCai ShangmingCai merged commit facde4c into main Mar 3, 2026
82 of 90 checks passed
@ShangmingCai ShangmingCai deleted the support_cp_transfer branch March 3, 2026 11:35
whybeyoung pushed a commit to whybeyoung/sglang that referenced this pull request Mar 3, 2026
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Kangyan-Zhou pushed a commit to Kangyan-Zhou/sglang that referenced this pull request Mar 4, 2026
Signed-off-by: Shangming Cai <csmthu@gmail.com>
magicYang1573 pushed a commit to magicYang1573/sglang that referenced this pull request Mar 9, 2026
Signed-off-by: Shangming Cai <csmthu@gmail.com>
Wangzheee pushed a commit to Wangzheee/sglang that referenced this pull request Mar 21, 2026
Signed-off-by: Shangming Cai <csmthu@gmail.com>
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
Signed-off-by: Shangming Cai <csmthu@gmail.com>
yhyang201 pushed a commit to yhyang201/sglang that referenced this pull request Apr 22, 2026
Signed-off-by: Shangming Cai <csmthu@gmail.com>
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