Remove sync points in mamba cache + prefill cudagraph plumbing for DP#19639
Remove sync points in mamba cache + prefill cudagraph plumbing for DP#19639ispobock merged 1 commit intosgl-project:mainfrom
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 focuses on significant performance enhancements and synchronization reductions within the Mamba cache management system. By vectorizing key operations and optimizing memory allocation strategies, it aims to improve sampling and end-to-end MFU. Additionally, it introduces necessary plumbing for prefill cudagraphs with data parallel attention, streamlining the batch processing logic. The changes also include minor code cleanups to prevent unnecessary overhead. 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 several performance optimizations, primarily for Mamba models, by removing synchronization points, vectorizing operations, and improving memory management. The changes, such as using torch.gather for vectorized indexing and avoiding GPU-CPU roundtrips, are well-implemented and should lead to noticeable performance gains. The plumbing for all_extend_in_batch is also correctly integrated. Overall, this is a solid set of improvements.
| for i in range(len(self.mamba_cache.conv)): | ||
| self.mamba_cache.conv[i][:, select_index] = 0 | ||
| self.mamba_cache.temporal[:, select_index] = 0 | ||
| t = self.mamba_cache.conv[i] | ||
| z = torch.zeros(1, dtype=t.dtype, device=t.device).expand( | ||
| t.shape[0], need_size, *t.shape[2:] | ||
| ) | ||
| t[:, select_index] = z | ||
| t = self.mamba_cache.temporal | ||
| z = torch.zeros(1, dtype=t.dtype, device=t.device).expand( | ||
| t.shape[0], need_size, *t.shape[2:] | ||
| ) | ||
| t[:, select_index] = z |
There was a problem hiding this comment.
The logic for creating a zero tensor and assigning it to a slice of the cache is duplicated for self.mamba_cache.conv and self.mamba_cache.temporal. You can refactor this into a single loop to improve code clarity and reduce duplication.
| for i in range(len(self.mamba_cache.conv)): | |
| self.mamba_cache.conv[i][:, select_index] = 0 | |
| self.mamba_cache.temporal[:, select_index] = 0 | |
| t = self.mamba_cache.conv[i] | |
| z = torch.zeros(1, dtype=t.dtype, device=t.device).expand( | |
| t.shape[0], need_size, *t.shape[2:] | |
| ) | |
| t[:, select_index] = z | |
| t = self.mamba_cache.temporal | |
| z = torch.zeros(1, dtype=t.dtype, device=t.device).expand( | |
| t.shape[0], need_size, *t.shape[2:] | |
| ) | |
| t[:, select_index] = z | |
| for t in self.mamba_cache.conv + [self.mamba_cache.temporal]: | |
| z = torch.zeros(1, dtype=t.dtype, device=t.device).expand( | |
| t.shape[0], need_size, *t.shape[2:] | |
| ) | |
| t[:, select_index] = z |
- Vectorize mamba_track_indices via torch.gather instead of per-req scalar extraction - Vectorize mamba_track_mask via tensor arithmetic instead of Python list comprehension - Replace Python-list advanced indexing in free_mamba_cache with integer slicing - Use GPU zero-expand pattern in MambaPool.alloc to avoid implicit CPU-GPU sync - Keep tensor references in HybridReqToTokenPool.alloc instead of .tolist() roundtrip - Add all_extend_in_batch field for prefill cudagraph with DP attention - Default device=None in create_remote_connector - Avoid unnecessary cache clearing in weight update logging Split from sgl-project#19190 (reverted in sgl-project#19581): excludes logits_processor.py changes that caused SWA KL test regression. Mamba decode vectorization from internal PR.
|
/tag-and-rerun-ci |
1 similar comment
|
/tag-and-rerun-ci |
|
/rerun-failed-ci |
|
The kl test passed: https://github.com/sgl-project/sglang/actions/runs/22559145546/job/65354366094?pr=19639#step:5:3291 |
Summary
mamba_track_indicesconstruction viatorch.gatherinstead of per-request scalar extraction inprepare_for_decodemamba_track_maskvia tensor arithmetic instead of Python list comprehensionfree_mamba_cachewith integer slicingMambaPool.allocto avoid implicit CPU-GPU sync on cache clearHybridReqToTokenPool.allocinstead of.tolist()roundtripall_extend_in_batchfield plumbing for prefill cudagraph with DP attentiondevice=Noneincreate_remote_connectortorch.cuda.empty_cache()in weight update diagnostic loggingSplit from #19190 (reverted in #19581): this PR excludes the
logits_processor.pychanges that caused the SWA KL test regression.Test plan
test_swa_radix_cache_kl) passes (this PR does not touch logits_processor.py)