[Perf] Overlap NSA-CP key all-gather with query computation for DeepSeek-V3.2#20438
Conversation
Summary of ChangesHello, 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
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
|
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. |
|
/tag-and-rerun-ci |
|
|
/rerun-ut registered/8-gpu-models/test_deepseek_v32_cp_single_node.py |
|
❌ File not found: |
|
/rerun-ut registered/cp/test_deepseek_v32_cp_single_node.py |
|
✅ Triggered |
…eek-V3.2 (sgl-project#20438) Co-authored-by: Shurui Jia <18817781975@163.com> Co-authored-by: Baidu-AIAK <baiduaiak~123>
…eek-V3.2 (sgl-project#20438) Co-authored-by: Shurui Jia <18817781975@163.com> Co-authored-by: Baidu-AIAK <baiduaiak~123>
…eek-V3.2 (sgl-project#20438) Co-authored-by: Shurui Jia <18817781975@163.com> Co-authored-by: Baidu-AIAK <baiduaiak~123>
…eek-V3.2 (sgl-project#20438) Co-authored-by: Shurui Jia <18817781975@163.com> Co-authored-by: Baidu-AIAK <baiduaiak~123>
…eek-V3.2 (sgl-project#20438) Co-authored-by: Shurui Jia <18817781975@163.com> Co-authored-by: Baidu-AIAK <baiduaiak~123>
…eek-V3.2 (sgl-project#20438) Co-authored-by: Shurui Jia <18817781975@163.com> Co-authored-by: Baidu-AIAK <baiduaiak~123>
…eek-V3.2 (sgl-project#20438) Co-authored-by: Shurui Jia <18817781975@163.com> Co-authored-by: Baidu-AIAK <baiduaiak~123>
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.
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.