-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[CUDA Graph] save cuda graph memory by using next_token_logits_buffer #8579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CUDA Graph] save cuda graph memory by using next_token_logits_buffer #8579
Conversation
There was a problem hiding this 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 implements a memory optimization for CUDA graphs by introducing a reusable buffer for storing next token logits. By pre-allocating this buffer and copying computed logits into it, the changes aim to reduce dynamic memory allocations during inference, which can lead to improved performance and more efficient memory utilization within the CUDA graph execution environment.
Highlights
- CUDA Graph Memory Optimization: Introduced a pre-allocated
next_token_logits_buffer
within theCudaGraphRunner
to store processed logits. This buffer is designed to be reused across CUDA graph executions, minimizing dynamic memory allocations. - Logits Processing Refinement: Modified the
_get_logits
function inlogits_processor.py
to utilize thenext_token_logits_buffer
. If available, the computed logits are now copied into this pre-existing buffer, avoiding the creation of new tensors for each inference step. - Data Structure Integration: Integrated the
next_token_logits_buffer
into theLogitsMetadata
andForwardBatch
data structures, ensuring the buffer is properly passed and accessible throughout the logits processing pipeline.
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
-
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. ↩
There was a problem hiding this 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 memory optimization for CUDA graphs by using a pre-allocated next_token_logits_buffer
. This avoids repeated memory allocation for logits within the graph, which should improve performance and reduce memory fragmentation. The changes span across forward_batch_info
, logits_processor
, and cuda_graph_runner
to introduce, propagate, and utilize this buffer. The implementation looks solid and aligns with the stated goal. I have a couple of suggestions to improve type safety and robustness.
Motivation
This PR creates a meta buffer for storing
next_token_logits
. The output of cuda graph reuses this buffer to save memory.TODO:
we may need aThis should not be a problem because our original implementation has this issue.clone
operation after CUDA Graph replay in the event of the output buffer is reused after it is overwritten.Modifications
Accuracy Test
Benchmark & Profiling
CUDA graph memory is reduced from 5.16 GB to 2.89 GB. With #8567, cg memory is further decreased to 0.95 GB.
Checklist