Skip to content

Conversation

ch-wan
Copy link
Collaborator

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

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 a clone operation after CUDA Graph replay in the event of the output buffer is reused after it is overwritten. This should not be a problem because our original implementation has this issue.
  • we can further extend this optimization to speculative decoding.

Modifications

Accuracy Test

Benchmark & Profiling

python3 -m sglang.launch_server --model-path /dev/shm/GLM-4.5-Air --trust-remote-code --tp 4

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

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 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 the CudaGraphRunner 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 in logits_processor.py to utilize the next_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 the LogitsMetadata and ForwardBatch 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

  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.

@ch-wan ch-wan changed the title [CUDA Graph] save cuda graph memory by using next_token_logits_buffer [CUDA Graph] WIP: save cuda graph memory by using next_token_logits_buffer Jul 30, 2025
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 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.

@ch-wan ch-wan changed the title [CUDA Graph] WIP: save cuda graph memory by using next_token_logits_buffer [CUDA Graph] save cuda graph memory by using next_token_logits_buffer Jul 31, 2025
@ch-wan ch-wan linked an issue Aug 3, 2025 that may be closed by this pull request
5 tasks
@zhyncs zhyncs merged commit cb099d2 into sgl-project:main Aug 3, 2025
118 of 126 checks passed
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.

[Bug] Llama4 Large Cuda Graph Memory
2 participants