[Feature] Add DCP support for DeepSeek v3.2#18167
[Feature] Add DCP support for DeepSeek v3.2#18167FENP wants to merge 5 commits intosgl-project:mainfrom
Conversation
Summary of ChangesHello @FENP, 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 integrates Decode Context Parallel (DCP) support into the SGLang framework, primarily targeting the DeepSeek v3.2 model. The core motivation is to mitigate KV cache redundancy, a critical concern for models like DeepSeek v3.2 that use a single KV head in their MLA architecture. By distributing the KV cache across multiple devices, DCP effectively multiplies the available cache capacity, allowing for larger context windows and more efficient memory utilization. The changes span across distributed state management, attention mechanisms, memory allocation, and scheduling, ensuring a cohesive and performant implementation of this new parallelism strategy. 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 Decode Context Parallel (DCP) support for DeepSeek v3.2, which is a significant feature for improving KV cache capacity. The changes are extensive, touching many parts of the distributed state management, memory allocation, and attention kernels. The implementation correctly follows standard patterns for context parallelism, including sharding the KV cache, gathering query tensors, and using log-sum-exp correction to combine partial attention results. The reuse of vllm's context parallelism utilities is a good practice.
I've identified a critical issue related to page size configuration in the memory allocator when DCP is enabled, which could break the sharding logic. I've also included a couple of suggestions to refactor duplicated code in the NSA backend to improve maintainability. Overall, this is a solid contribution, and addressing the identified issues will make it even better.
| self.max_total_num_tokens * self.dcp_size, | ||
| page_size=self.page_size * self.dcp_size, |
There was a problem hiding this comment.
The page_size passed to PagedTokenToKVPoolAllocator appears to be incorrect when DCP is enabled. It should be the physical page size (self.page_size), not scaled by dcp_size. The total number of tokens is already correctly scaled. By also scaling page_size, the number of pages managed by the allocator becomes (max_total_num_tokens * dcp_size) / (page_size * dcp_size) = max_total_num_tokens / page_size, which is the per-rank page count. This breaks the global page pool assumption required for DCP sharding logic (e.g., page_index % dcp_size). The allocator should manage the global pool of physical pages.
| self.max_total_num_tokens * self.dcp_size, | |
| page_size=self.page_size * self.dcp_size, | |
| self.max_total_num_tokens * self.dcp_size, | |
| page_size=self.page_size, |
| # Prefill policy | ||
| adder = PrefillAdder( | ||
| self.page_size, | ||
| self.page_size * self.dcp_size, |
There was a problem hiding this comment.
Consistent with the proposed change for PagedTokenToKVPoolAllocator, the page_size passed to PrefillAdder should also be the physical page size, not scaled by dcp_size. PrefillAdder uses this to calculate the number of pages required for a request, and this calculation should be based on the physical page size to correctly interact with the global page pool.
| self.page_size * self.dcp_size, | |
| self.page_size, |
| if self.dcp_size > 1: | ||
| q_nope = get_dcp_group().all_gather(q_nope.contiguous(), dim=1) | ||
| q_rope = get_dcp_group().all_gather(q_rope.contiguous(), dim=1) | ||
| else: | ||
| q_all = q.contiguous().view(-1, layer.tp_q_head_num, layer.head_dim) | ||
| if self.dcp_size > 1: | ||
| q_all = get_dcp_group().all_gather(q_all, dim=1) | ||
| q_nope = q_all[:, :, : layer.v_head_dim] | ||
| q_rope = q_all[:, :, layer.v_head_dim :] |
There was a problem hiding this comment.
This logic for preparing and gathering query tensors (q_nope, q_rope, q_all) is duplicated in forward_decode (lines 1430-1438). To improve code maintainability and reduce redundancy, consider refactoring this logic into a helper method. For example:
def _gather_q(self, q, q_rope, layer):
if q_rope is not None:
q_nope = q.view(-1, layer.tp_q_head_num, layer.v_head_dim)
q_rope = q_rope.view(
-1, layer.tp_q_head_num, layer.head_dim - layer.v_head_dim
)
if self.dcp_size > 1:
q_nope = get_dcp_group().all_gather(q_nope.contiguous(), dim=1)
q_rope = get_dcp_group().all_gather(q_rope.contiguous(), dim=1)
return q_nope, q_rope, None
else:
q_all = q.contiguous().view(-1, layer.tp_q_head_num, layer.head_dim)
if self.dcp_size > 1:
q_all = get_dcp_group().all_gather(q_all, dim=1)
q_nope = q_all[:, :, : layer.v_head_dim]
q_rope = q_all[:, :, layer.v_head_dim :]
return q_nope, q_rope, q_all| if self.dcp_size > 1: | ||
| return cp_lse_ag_out_rs(o, s, get_dcp_group()) | ||
| return o |
There was a problem hiding this comment.
The pattern of checking self.dcp_size > 1 and calling cp_lse_ag_out_rs is repeated multiple times (6 times in total) in both forward_extend and forward_decode for different attention backends. This could be extracted into a helper method to reduce code duplication and improve clarity. For example:
def _process_context_parallel_output(self, o, s):
if self.dcp_size > 1:
return cp_lse_ag_out_rs(o, s, get_dcp_group())
return oThen you could replace this block with a single call: return self._process_context_parallel_output(o, s).
Signed-off-by: FENP <yuanyongjie.yyj@antgroup.com>
Signed-off-by: FENP <yuanyongjie.yyj@antgroup.com>
Signed-off-by: FENP <yuanyongjie.yyj@antgroup.com>
Signed-off-by: FENP <yuanyongjie.yyj@antgroup.com>
Signed-off-by: FENP <yuanyongjie.yyj@antgroup.com>
|
Great work! Do you have any plans to support Prefill-Decode Disaggregation and MTP? |
Thank you for your attention. These two features will be supported in future work. Contributions from the community are also welcome. |
|
🌹 I've observed that for the sparse_attn kernel, even when topk_indices is locally set to -1, there is still no performance gain. Therefore, partitioning the KV is much slower than partitioning the Q. Do you have any best practices or experience regarding this issue? |
|
Hello @FENP, Great work! May I ask, after enabling DCP, in the pressure testing scenario above, what is the highest proportion of |
Do you mean pure kernel performance testing or DCP performance testing? |
Good! Based on my previous experience (#14982), |
Motivation
Following PR #14982, add decode context parallel (DCP) support for DeepSeek v3.2. The KV cache redundancy issue in DeepSeek v3.2 is even more severe because the MLA architecture uses only 1 KV head. DCP can completely eliminate this redundancy: compared to TP8, TP8+DCP8 can expand the KV cache capacity by 8×.
Modifications
The changes are largely the same as those in PR #14982. For DSA, this PR includes the following additional design considerations:
Usage
Accuracy Tests
few_shot_gsm8k
Benchmarking and Profiling
4K/1.5K
DCP results in 8% to 13% performance degradation compared to TP. Further testing will be conducted after optimizing communication (e.g., symmetric memory, replicated linear ).
It is worth noting that, under the TP8DCP4 configuration, the number of attention heads in Q becomes exactly 64 after the DCP all-gather, enabling flashmla_sparse to run on SM90 without padding.
TODOs
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci