Fix CUDA graph parameter grad lifetime#2937
Conversation
Signed-off-by: Robin Zhang <robinz@nvidia.com>
4cc8b89 to
6e16c63
Compare
441e419 to
beff9c1
Compare
Signed-off-by: Robin Zhang <robinz@nvidia.com>
beff9c1 to
4077b85
Compare
|
I have a problem with this PR. On one hand I agree that there is a bug here and we should ensure that the gradient buffers are not overwritten before being applied. On the other hand for the cases without gradient accumulation (or when the accumulation is done in a different way, like in Megatron) it is a performance loss. Could we make this behavior optional - have it on by default, but also provide an opt-out option with a clear warning that states where this would be applicable? |
Signed-off-by: Robin Zhang <robinz@nvidia.com>
1a3c2c5 to
c8b2ee3
Compare
|
@ptrendx I added a new argument |
|
Hi @ptrendx can we merge this PR? |
| pool: Optional[Tuple[int, ...]] = None, | ||
| retain_graph_in_backward: bool = False, | ||
| _reuse_graph_input_output_buffers: bool = False, | ||
| _clone_param_grads_on_return: bool = True, |
There was a problem hiding this comment.
I think we should expose it to make_graphed_callables(), not just _make_graphed_callables() and also document it in docstring. Also, I think we can make it fully public without _.
There was a problem hiding this comment.
Also, I think we should make sure that MCore will set it to False if they do their wgrad accumulation. Maybe it's a good idea to create PR there also?
There was a problem hiding this comment.
Thanks! Addressed both suggestions in f286621:
clone_param_grads_on_returnis now a public argument onmake_graphed_callables()and is documented in the public docstring.- I opened Disable TE CUDA graph parameter gradient clones Megatron-LM#5893 to set
clone_param_grads_on_return=Falsefrom MCore when the public option is available, while preserving compatibility with older TE versions.
Thanks for the suggestions!
|
/te-ci pytorch |
Signed-off-by: Robin Zhang <robinz@nvidia.com>
|
/te-ci pytorch |
Summary
Fix CUDA graph replay so parameter gradients returned from
Graphed.backwarddo not expose CUDA graph static buffers to downstream autograd users.The fix clones returned parameter gradients before handing them back to autograd, while preserving the existing aliasing behavior for delayed-wgrad parameters marked with
skip_backward_post_hook.Root Cause
When CUDA graph replay returns parameter grad slots directly from static graph buffers, downstream autograd users can retain references to buffers that are overwritten by later graph replays. This can corrupt retained grads or break gradient accumulation semantics.
This is related to PyTorch issue pytorch/pytorch#181723.
Changes
Graphed.backward.