Skip to content

[2/n] DP Enhancement: Reduce the memory usage of gathered buffer in DP attention#8277

Closed
ch-wan wants to merge 3 commits intogh/ch-wam/2/basefrom
gh/ch-wam/2/head
Closed

[2/n] DP Enhancement: Reduce the memory usage of gathered buffer in DP attention#8277
ch-wan wants to merge 3 commits intogh/ch-wam/2/basefrom
gh/ch-wam/2/head

Conversation

@ch-wan
Copy link
Collaborator

@ch-wan ch-wan commented Jul 23, 2025

Stack from ghstack (oldest at bottom):

Two optimizations:

  • batch.gathered_buffer is not needed when cuda graph is enabled. This PR defers the allocation of gathered_buffer after checking if cuda graph is runnable.
  • Correcting global_num_tokens_for_logprob during draft extend to reduce the buffer size in logits processor.

This PR also pads input tokens to num_tokens * (speculative_num_steps + 1) during draft decode to avoid potential illegal memory access when copying hidden states to gathered buffer..

[ghstack-poisoned]
Copy link
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.

Summary of Changes

Hello @ch-wan, 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 optimizes memory usage within the distributed attention mechanism by implementing a more dynamic and precise allocation strategy for the gathered_buffer and related tensors. These changes are particularly impactful for speculative decoding scenarios. Additionally, the PR includes general code clean-up, type hinting improvements, and necessary adjustments to existing test cases to reflect the updated behavior and performance characteristics.

Highlights

  • Memory Optimization for DP Attention: The primary goal of this PR is to reduce memory usage of the gathered_buffer in distributed attention (DP attention). This is achieved by removing redundant padding logic in _dp_gather and dp_scatter and by dynamically allocating the buffer.
  • Dynamic Buffer Preparation: The gathered_buffer is now initialized and prepared dynamically via a new prepare_mlp_sync_batch method within ForwardBatch. This ensures memory is allocated more precisely when needed, especially during speculative decoding's draft_extend mode, and also handles padding of related tensors like input_ids and hidden_states.
  • Refined Token Count Calculation: The calculation of global token counts (global_num_tokens, global_num_tokens_for_logprob) has been updated to leverage detailed speculative decoding information (from EagleDraftInput and EagleVerifyInput), leading to more accurate sizing and preventing over-allocation.
  • Code Clean-up and Type Hinting: Several files received minor refactorings, including updating type hints from torch.tensor to torch.Tensor, moving imports into TYPE_CHECKING blocks for better performance and clarity, and removing a deprecated spec_num_draft_tokens field from ModelWorkerBatch.
  • Test Adjustments: Existing hybrid DP/EP/TP/MTP tests were updated. This includes renaming numerous test classes, removing the test_mgsm_en methods, and adjusting the assertion thresholds for mmlu evaluation metrics to 0.48.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 or fill out our survey to provide feedback.

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
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 effectively reduces memory usage by deferring the allocation of the gathered_buffer in DP attention until it's actually needed for MLP sync. The refactoring to use spec_info for speculative decoding parameters instead of spec_num_draft_tokens also improves code clarity and maintainability.

I've left a few comments, mainly concerning the significant changes in the test file test/srt/test_hybrid_dp_ep_tp_mtp.py, where a test case was removed and classes were renamed. I've also noted some good practices like adding and correcting type hints.

Overall, the changes look solid and well-aligned with the goal of memory optimization.

@ch-wan ch-wan changed the title Reduce the memory usage of gathered buffer in DP attention [2/n] DP Enhancement: Reduce the memory usage of gathered buffer in DP attention Jul 23, 2025
@ch-wan ch-wan mentioned this pull request Jul 23, 2025
6 tasks
ch-wan added 2 commits July 23, 2025 10:23
[ghstack-poisoned]
[ghstack-poisoned]
@ch-wan ch-wan closed this Jul 25, 2025
@ch-wan ch-wan deleted the gh/ch-wam/2/head branch July 25, 2025 04:50
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