[PyTorch] Reduce CUDA graph memory retention - #3427
Conversation
Signed-off-by: Robin Zhang <robinz@nvidia.com>
Signed-off-by: Robin Zhang <robinz@nvidia.com>
Greptile SummaryThe PR shortens CUDA graph tensor and closure lifetimes during warmup, capture, and teardown.
Confidence Score: 4/5The PR is not yet safe to merge because delayed-weight-gradient callers can crash by invoking backward_dw after resetting the graphed callable. Reset clears bwd_dw_graph while the backward_dw closure retains a true need_bwd_dw flag, leaving a reachable None.replay() failure on the exposed post-reset lifecycle path. Files Needing Attention: transformer_engine/pytorch/graph.py Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
C[Captured callable] --> B[backward_dw closure]
C --> R[reset closure]
B -->|need_bwd_dw is true| G[bwd_dw_graph.replay]
R --> X[Reset graphs and release static state]
X --> N[bwd_dw_graph becomes None]
N -->|later backward_dw call| E[AttributeError]
Reviews (2): Last reviewed commit: "[PyTorch] Release per-callable state on ..." | Re-trigger Greptile |
Signed-off-by: Robin Zhang <robinz@nvidia.com>
38f14e4 to
6103b27
Compare
| def backward_dw(): | ||
| if need_bwd_dw_graph.get(graph_idx, False): | ||
| bwd_dw_graphs[graph_idx].replay() | ||
| if need_bwd_dw: | ||
| bwd_dw_graph.replay() |
There was a problem hiding this comment.
When a callable captured for delayed weight-gradient computation invokes backward_dw() after reset(), the unchanged need_bwd_dw flag causes the closure to call replay() on the cleared bwd_dw_graph, raising AttributeError during teardown.
| def backward_dw(): | |
| if need_bwd_dw_graph.get(graph_idx, False): | |
| bwd_dw_graphs[graph_idx].replay() | |
| if need_bwd_dw: | |
| bwd_dw_graph.replay() | |
| def backward_dw(): | |
| if need_bwd_dw and bwd_dw_graph is not None: | |
| bwd_dw_graph.replay() |
Description
Reduce avoidable GPU-memory retention across CUDA graph construction and teardown.
make_graphed_attribute_functionswhile snapshotting only per-callable graph state, and clear replay closure state whenreset()is called.The changes preserve warmup/capture order and public APIs.
Testing
python -m pytest -q tests/pytorch/test_cuda_graphs.py -k "warmup_releases_consumed_outputs or inference_warmup_does_not_retain_outputs or reused_capture_buffers_release_outputs_after_backward or reset_releases_only_the_selected_callable or capture_time_hooks or interleaved_pipeline_parallelism"Signed-off-by: Robin Zhang robinz@nvidia.com