fix: reclaim GPU memory after checkpoint load with CPU optimizer offloading#2154
Draft
fix: reclaim GPU memory after checkpoint load with CPU optimizer offloading#2154
Conversation
…oading
When resuming from a checkpoint with optim_cpu_offload, set_state_dict()
materializes optimizer states on GPU. After _move_states("cpu") moves them
to CPU, the state_dict (owned by dcp_load) still holds references to the
stale GPU tensors, and the CUDA caching allocator retains the memory.
This causes OOM on the first training step.
Fix: clear the state_dict references, run gc.collect(), and call
torch.cuda.empty_cache() to fully reclaim GPU memory.
Measured on Qwen3-0.6B (single GPU):
- Before: 11.4GB wasted GPU memory after checkpoint load
- After: 58MB wasted (essentially zero)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
S1ro1
approved these changes
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
optim_cpu_offloadenabledset_state_dict()+_move_states("cpu"), thestate_dict(owned bydcp_load) still holds references to stale GPU optimizer tensors, and the CUDA caching allocator retains the memory — causing OOM on the first training stepgc.collect(), andtorch.cuda.empty_cache()to fully reclaim GPU memoryBefore / After
Measured on Qwen3-0.6B with
optim_cpu_offload=true, single GPU,expandable_segments:True:🤖 Generated with Claude Code