Repository navigation
fix: release the allocator cache after checkpoint load - #4075
Merged
mikasenghaas merged 3 commits intoOct 13, 2026
Merged
Conversation
A non-offload resume left about 21 GiB of free blocks in the CUDA cache. They are the transient of DCP's zero-grad optimizer-state init. The first step reserved on top of them instead of reusing them, so peak reserved rose by the same amount over a fresh run. Peak allocated did not change. One gc.collect() + empty_cache() after dcp_load returns removes all of it, on every load path. This replaces the unconditional cleanup in AppState.load_state_dict and the extra empty_cache before the SFT startup broadcast: neither is needed once the cache is released here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The cleanup after dcp_load returns already covers the CPU-offload paths. Resumes with offload = "optimizer" and "full" use the same memory with and without the AppState block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
garrett361
approved these changes
Oct 13, 2026
mikasenghaas
merged commit Oct 13, 2026
9193c73
into
fix/sft-resume-ckpt-load-memory
14 of 15 checks passed
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
Stacked on #4053. Ablations on that PR showed that one cleanup point is enough, so this PR replaces both of its changes:
CheckpointManager.load_from_path: rungc.collect()+torch.cuda.empty_cache()right afterdcp_loadreturns. This covers every load path: SFT and RL, with and without CPU offload.AppState.load_state_dict: remove the cleanup. The new cleanup afterdcp_loadmakes it redundant on every path, including the offload-only cleanup thatmainalready had.sft/train.py: remove thesynchronize()+empty_cache()before the startup broadcast. It is not needed, and the sync added no safety.Net diff against
main: the cleanup moves fromAppState.load_state_dict(offload paths only) toload_from_path(all paths).Why
During a non-offload resume, peak allocated memory rises about 21 GiB above the steady state. That is about the size of the sharded fp32 parameters: DCP's
_init_optim_statebuilds the optimizer state with a zero-grad step, which allocateszeros_likegradients for every parameter. When the load ends, those blocks stay in the allocator cache. The first step does not reuse them and reserves on top of them. Peak allocated does not change, so the extra memory is cache fragmentation, not live tensors.Verification
Same setup as the ablation in #4053: Qwen3-30B-A3B SFT on 1 node of H200s, with 6 trainer GPUs and 2 inference GPUs, online evals on, seq len 8192. Each resume loads a step-1 checkpoint and trains step 2. Numbers are rank 0, in GiB. Each configuration was tested with env-var toggles on top of #4053; those debug toggles are not part of this PR.
offload = "none":mainWith this PR, reserved memory after the load equals allocated memory (58.6), and step 2 matches the fresh run. The
ckpt.pychange from #4053 on its own left 1.9 GiB (step-2 peak reserved 95.4).CPU offload, to check that removing the
AppStatecleanup is safe:offload = "optimizer",mainoffload = "optimizer", this PRoffload = "full",mainoffload = "full", both cleanupsoffload = "full", this PRRL was not tested. It uses the same
CheckpointManager.load_from_path.🤖 Generated with Claude Code
Note
Low Risk
Timing-only memory hygiene around checkpoint resume; no change to what is loaded or how training steps run.
Overview
Consolidates post-checkpoint-load CUDA memory cleanup into one place:
CheckpointManager.load_from_pathnow runsgc.collect()andtorch.cuda.empty_cache()immediately afterdcp_load, so SFT/RL resume and all offload modes share the same fix for allocator-cache fragmentation from DCP’s optimizer materialization (zero-grad gradients).Removes redundant cleanup elsewhere:
AppState.load_state_dictno longer clears the loadstate_dictand empties the cache (that path was offload-specific and duplicated the new hook), and SFT startup no longer callssynchronize()+empty_cache()before the policy weight broadcast—resume loads are already cleaned up centrally.Reviewed by Cursor Bugbot for commit a37c28d. Bugbot is set up for automated code reviews on this repo. Configure here.