Repository navigation
Fix hidden checkpoint parameter handling in local CUDA graphs - #6373
Merged
Merged
Conversation
svcnvidia-nemo-ci
marked this pull request as draft
August 9, 2026 04:23
Contributor
|
This PR has been automatically converted to draft because all PRs must start as drafts. When you are ready for review, click Ready for Review to begin the review process. This will:
See the contribution guide for more details. |
Contributor
Author
|
@claude strict-review |
Contributor
Author
|
/ok to test 0616f63 |
Open
Contributor
|
@jiemingz can you please take a look at this MR? |
JF-D
force-pushed
the
jiangfeid/checkpoint_cg_fix
branch
from
August 10, 2026 16:46
0616f63 to
72f111f
Compare
Contributor
Author
|
@claude strict-review |
Contributor
Author
|
/ok to test cb7e656 |
jiemingz
approved these changes
Aug 14, 2026
yaoyu-33
approved these changes
Aug 14, 2026
JF-D
enabled auto-merge
August 14, 2026 18:26
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/31839975216 |
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/31842104782 |
ksivaman
pushed a commit
to ksivaman/Megatron-LM
that referenced
this pull request
Sep 1, 2026
…#6373) Signed-off-by: Jiangfei Duan <jiangfeid@nvidia.com>
This branch was previously deployed
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.
What does this PR do?
Fixes local CUDA-graph training when
CheckpointWithoutOutputrecomputation uses parameters from nested modules that are not visible in the outer autograd graph.Problem
CheckpointWithoutOutputruns its recompute function during backward. Parametersused only by that function do not have autograd edges from the outer forward
outputs.
CUDA-graph capture previously discovered
params_to_backproponly by traversingthe autograd graph from the captured forward outputs. Consequently, a parameter
used exclusively by checkpoint recomputation could be omitted from the captured
backward inputs.
This caused two related problems:
The nested recompute backward accumulated the parameter gradient into
param.grad, buttorch.autograd.gradreturnedNonefor the correspondingouter parameter input. The gradient was therefore not transferred to
param.main_grad.Because the parameter was not passed as a synthetic input to the CUDA-graph
replay node, replay had no autograd edge to its
AccumulateGradnode. Thenormal DDP hook registered on that node did not fire, leaving the DDP bucket
with an incomplete ready count.
This can result in missing gradients, incorrect gradient accumulation, or DDP
waiting for a bucket that never becomes ready.
Example:
self.projection.weightis used by the outer forward autograd graph, so it can bediscovered by traversing backward from output. In contrast,
self.checkpointed.weightis closed over by the checkpoint recompute function. Its computation is deferred until
the nested recompute backward and therefore has no AccumulateGrad edge in the
outer graph. Traversing only the captured forward outputs consequently discovers
projection.weightbut missescheckpointed.weight, even though backward eventuallyproduces a gradient for it.
Solution
Automatically discover hidden checkpoint dependencies
During CUDA-graph warmup,
CheckpointWithoutOutputbypasses checkpointing andexecutes its recompute function with autograd enabled. Parameters used by nested
recompute modules are therefore visible through their
AccumulateGradnodes.This change:
captured forward outputs.
base_module.parameters()ordering so capture and replay use a stablepositional input layout.
cuda_graph_warmup_steps=0, because one discovery pass is required for correctcheckpoint handling.
This discovery is automatic and does not require checkpoint call sites or
individual modules to declare additional dependencies.
Capture gradients produced by nested recomputation
For a parameter hidden behind
CheckpointWithoutOutput,torch.autograd.grad(..., allow_unused=True)preserves its requested output slotbut returns
Nonefor that slot. Its nested backward instead writes the realgradient to
param.grad.Backward capture now transfers that contribution into
param.main_grad.The returned
wgradandparam.gradare handled independently. This isimportant when the same parameter is used both outside and inside checkpoint
recomputation:
wgradrepresents uses visible to the outer autograd graph.param.gradrepresents uses performed by the nested recompute backward.Both are legitimate contributions and must be accumulated exactly once.
Preserve normal DDP bucket readiness during replay
All discovered parameters are passed as synthetic inputs to
_CudagraphReplayNode.apply. For ordinary DDP parameters, this creates replayautograd edges to the existing
AccumulateGradnodes where DDP registered itshooks.
For checkpoint parameters whose gradients were already transferred from
param.gradtoparam.main_grad, replay setsgrad_added_to_main_grad=Truebefore the normal DDP hook runs. The hook then:main_grada second time.param.grad.register_grad_ready(), allowing the DDP bucket to be dispatched.GTP parameters retain their existing explicit hook path in
_CudagraphReplayNode.backward.Contribution process
Pre-checks