Skip to content

[Perf] Overlap NSA-CP key all-gather with query computation for DeepSeek-V3.2#20438

Merged
Fridge003 merged 6 commits intosgl-project:mainfrom
Baidu-AIAK:cp_keyallgather_overlap
Mar 24, 2026
Merged

[Perf] Overlap NSA-CP key all-gather with query computation for DeepSeek-V3.2#20438
Fridge003 merged 6 commits intosgl-project:mainfrom
Baidu-AIAK:cp_keyallgather_overlap

Conversation

@Baidu-AIAK
Copy link
Copy Markdown
Contributor

Motivation

In the current Native Sparse Attention (NSA) implementation, specifically when Context Parallelism (CP) is enabled via --enable-nsa-prefill-context-parallel, the key_all_gather operation is executed synchronously.
For large-scale models like DeepSeek-V3.2, this synchronous communication creates a significant bottleneck during the Prefill stage. This PR introduces a dual-stream execution mechanism to overlap the key_all_gather (communication) with the rotate_activation of the query (computation), effectively masking communication latency and boosting overall prefill throughput.

Performance Profiling

I used NVIDIA Nsight Systems to profile the prefill stage and verify the overlap effectiveness:
Before: The cp_all_gather (NCCL kernels) and rotate_activation (Compute kernels) were executed serially on the default stream, leading to idle compute time during communication.
Snipaste_2026-03-12_16-58-17

After (This PR): As shown in the attached screenshot, the communication is now successfully hidden. The cp_all_gather_rerange_output runs on the alt_stream and completely overlaps with the computation on the default stream.

Snipaste_2026-03-12_16-59-03

@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 significantly enhances the performance of the prefill stage in Native Sparse Attention (NSA) for large models like DeepSeek-V3.2, particularly when Context Parallelism (CP) is active. By introducing a dual-stream execution strategy, it cleverly overlaps communication-intensive operations with computation, thereby eliminating a critical bottleneck and boosting overall throughput. This change ensures more efficient utilization of GPU resources during model inference.

Highlights

  • Performance Optimization: Implemented a dual-stream execution mechanism to overlap the key_all_gather communication with the rotate_activation computation during the prefill stage for Native Sparse Attention (NSA) with Context Parallelism (CP).
  • Bottleneck Resolution: Addressed a significant bottleneck in DeepSeek-V3.2's NSA implementation where synchronous key_all_gather operations caused idle compute time.
  • Throughput Improvement: The new approach effectively masks communication latency, leading to improved overall prefill throughput.
  • Profiling Verification: NVIDIA Nsight Systems profiling confirmed successful overlap, showing cp_all_gather_rerange_output running on an alternate stream concurrently with computation on the default stream.

🧠 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/layers/attention/nsa/nsa_indexer.py
    • Added a new conditional block to enable dual-stream execution for key_all_gather and rotate_activation when NSA prefill context parallelism is active, allowing communication and computation to run concurrently.
Activity
  • No human activity has been recorded on this pull request yet.
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 performance optimization for DeepSeek-V3.2's prefill stage when using Native Sparse Attention (NSA) with Context Parallelism (CP). By leveraging a dual-stream execution mechanism, it successfully overlaps the key_all_gather communication operation with the rotate_activation computation for the query. This change effectively masks communication latency, which should improve prefill throughput as demonstrated by the provided profiling results. The implementation correctly uses CUDA streams to achieve this overlap. I have one minor suggestion to remove a potentially redundant .contiguous() call for a small additional performance gain.

Comment thread python/sglang/srt/layers/attention/nsa/nsa_indexer.py
Comment thread python/sglang/srt/layers/attention/nsa/nsa_indexer.py
@Fridge003
Copy link
Copy Markdown
Collaborator

Please fix lint

@Baidu-AIAK
Copy link
Copy Markdown
Contributor Author

Please fix lint

Thanks for the review. I have fixed the lint errors (trailing whitespaces) and synced with the latest main branch. Please take a look again.

JSR-2000

This comment was marked as outdated.

@Baidu-AIAK
Copy link
Copy Markdown
Contributor Author

/tag-and-rerun-ci

@Baidu-AIAK Baidu-AIAK requested a review from Fridge003 March 19, 2026 03:33
@Baidu-AIAK
Copy link
Copy Markdown
Contributor Author

Please fix lint
I have fixed the lint errors @Fridge003 Please take a look again.

@Fridge003
Copy link
Copy Markdown
Collaborator

/rerun-ut registered/8-gpu-models/test_deepseek_v32_cp_single_node.py

@github-actions
Copy link
Copy Markdown
Contributor

❌ File not found: test/registered/8-gpu-models/test_deepseek_v32_cp_single_node.py

@Fridge003
Copy link
Copy Markdown
Collaborator

/rerun-ut registered/cp/test_deepseek_v32_cp_single_node.py

@github-actions
Copy link
Copy Markdown
Contributor

✅ Triggered /rerun-ut on 8-gpu-h200 runner:

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

@github-actions
Copy link
Copy Markdown
Contributor

🔗 View workflow run

@Fridge003 Fridge003 merged commit 6491728 into sgl-project:main Mar 24, 2026
52 of 60 checks passed
adityavaid pushed a commit to adityavaid/sglang that referenced this pull request Mar 24, 2026
…eek-V3.2 (sgl-project#20438)

Co-authored-by: Shurui Jia <18817781975@163.com>
Co-authored-by: Baidu-AIAK <baiduaiak~123>
adityavaid pushed a commit to adityavaid/sglang that referenced this pull request Mar 24, 2026
…eek-V3.2 (sgl-project#20438)

Co-authored-by: Shurui Jia <18817781975@163.com>
Co-authored-by: Baidu-AIAK <baiduaiak~123>
0-693 pushed a commit to 0-693/sglang that referenced this pull request Mar 25, 2026
…eek-V3.2 (sgl-project#20438)

Co-authored-by: Shurui Jia <18817781975@163.com>
Co-authored-by: Baidu-AIAK <baiduaiak~123>
johnnycxm pushed a commit to johnnycxm/sglang that referenced this pull request Mar 25, 2026
…eek-V3.2 (sgl-project#20438)

Co-authored-by: Shurui Jia <18817781975@163.com>
Co-authored-by: Baidu-AIAK <baiduaiak~123>
johnnycxm pushed a commit to johnnycxm/sglang that referenced this pull request Mar 25, 2026
…eek-V3.2 (sgl-project#20438)

Co-authored-by: Shurui Jia <18817781975@163.com>
Co-authored-by: Baidu-AIAK <baiduaiak~123>
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
…eek-V3.2 (sgl-project#20438)

Co-authored-by: Shurui Jia <18817781975@163.com>
Co-authored-by: Baidu-AIAK <baiduaiak~123>
yhyang201 pushed a commit to yhyang201/sglang that referenced this pull request Apr 22, 2026
…eek-V3.2 (sgl-project#20438)

Co-authored-by: Shurui Jia <18817781975@163.com>
Co-authored-by: Baidu-AIAK <baiduaiak~123>
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.

3 participants