Skip to content

[feat] CPU mem: release and realloc gradient buffers when offload train engine - #719

Merged
yueming-yuan merged 28 commits into
mainfrom
feat/save_grad_mem
Mar 11, 2026
Merged

[feat] CPU mem: release and realloc gradient buffers when offload train engine#719
yueming-yuan merged 28 commits into
mainfrom
feat/save_grad_mem

Conversation

@yueming-yuan

@yueming-yuan yueming-yuan commented Mar 9, 2026

Copy link
Copy Markdown
Collaborator

ci-megatron-pr: #17

core changes

Megatron: radixark/Megatron-LM#17
torch memory saver: fzyzcjy/torch_memory_saver#72

result

Green is the newest
compare_all

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 optimizes memory utilization within the offload training engine by implementing a strategy to release and reallocate gradient buffers. By explicitly freeing gradient buffer GPU memory before offloading the model and then reallocating it upon resuming, the system avoids unnecessary transfer of gradient data to CPU memory, thereby reducing the overall memory footprint during the offload process.

Highlights

  • Gradient Buffer Management: Introduced new methods (_get_grad_buffers, _free_grad_buffers, _realloc_grad_buffers) to explicitly manage the allocation and deallocation of gradient buffers.
  • Memory Optimization during Offload: Integrated the _free_grad_buffers call into the sleep method to release GPU memory associated with gradients before offloading, preventing unnecessary CPU offload.
  • Buffer Reallocation on Wake-up: Incorporated the _realloc_grad_buffers call into the wake_up method to restore gradient buffers after the model resumes, ensuring views remain valid.
  • Reduced CPU Memory Footprint: The changes aim to save approximately 4 bytes per parameter of CPU memory during offload by not preserving gradient data across sleep/wake-up cycles.
Changelog
  • miles/backends/megatron_utils/actor.py
    • Added _free_grad_buffers method to release GPU memory associated with gradient buffers before model offload.
    • Added _realloc_grad_buffers method to reallocate gradient buffers after the model resumes.
    • Added _get_grad_buffers helper method to retrieve all ParamAndGradBuffer objects from the DDP model.
    • Called _free_grad_buffers within the sleep method to optimize memory before torch_memory_saver.pause().
    • Called _realloc_grad_buffers within the wake_up method to restore buffers after torch_memory_saver.resume().
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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. 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

  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.

@yueming-yuan yueming-yuan changed the title [WIP] release and realloc gradient buffers when offload train engine [WIP] CPU mem: release and realloc gradient buffers when offload train engine Mar 9, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 by releasing and reallocating gradient buffers when the training engine is offloaded. The logic is sound and correctly placed within the sleep and wake_up methods. I have one suggestion to make the implementation more robust against potential errors during the buffer freeing process.

Comment thread miles/backends/megatron_utils/actor.py Outdated
Comment on lines +207 to +210
self._grad_buffer_nbytes = []
for buffer in self._get_grad_buffers():
self._grad_buffer_nbytes.append(buffer.grad_data.untyped_storage().nbytes())
buffer.grad_data.untyped_storage().resize_(0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve robustness, it's better to first collect all buffer sizes and then resize them in a separate loop. This ensures that if an error occurs while getting a buffer's size, no buffers are resized, leaving the system in a consistent state. The current implementation could lead to a partially freed state if an error occurs mid-loop.

Suggested change
self._grad_buffer_nbytes = []
for buffer in self._get_grad_buffers():
self._grad_buffer_nbytes.append(buffer.grad_data.untyped_storage().nbytes())
buffer.grad_data.untyped_storage().resize_(0)
buffers = self._get_grad_buffers()
self._grad_buffer_nbytes = [b.grad_data.untyped_storage().nbytes() for b in buffers]
for buffer in buffers:
buffer.grad_data.untyped_storage().resize_(0)

@yueming-yuan
yueming-yuan requested a review from guapisolo as a code owner March 9, 2026 23:03
@yueming-yuan yueming-yuan changed the title [WIP] CPU mem: release and realloc gradient buffers when offload train engine [feat] CPU mem: release and realloc gradient buffers when offload train engine Mar 9, 2026
@yueming-yuan
yueming-yuan changed the base branch from fix/cpu_backup to main March 10, 2026 02:25
Comment thread miles/utils/arguments.py Outdated
"Note: do not set `--ref-load` and `--keep-old-actor` if disable weights backuper."
),
)
parser.add_argument(

@yueming-yuan yueming-yuan Mar 10, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do people think we need to keep this argument? Is there any senario that we need to keep cpu copy of gradient and distributed optimizer's parameter gather buffer? if no i will delete

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally do not have an example to need this flag in my mind

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Comment thread miles/utils/arguments.py Outdated
"Note: do not set `--ref-load` and `--keep-old-actor` if disable weights backuper."
),
)
parser.add_argument(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally do not have an example to need this flag in my mind

@yueming-yuan
yueming-yuan merged commit 4a70256 into main Mar 11, 2026
41 of 53 checks passed
@yueming-yuan
yueming-yuan deleted the feat/save_grad_mem branch March 11, 2026 06:29
JD-ETH pushed a commit to JensenFire/miles that referenced this pull request Apr 11, 2026
GuanxingLu pushed a commit to GuanxingLu/miles that referenced this pull request Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants