Fix unused gradient tracking to respect create_graph#168295
Fix unused gradient tracking to respect create_graph#168295dsashidh wants to merge 2 commits intopytorch:mainfrom
Conversation
|
@pytorchbot label "topic: not user facing" |
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/168295
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 1 Unrelated FailureAs of commit 145c885 with merge base 9760a63 ( UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 1 mandatory check(s) failed. The first few are: Dig deeper by viewing the failures on hud |
|
@pytorchbot merge -i |
Merge startedYour change will be merged while ignoring the following 2 checks: pull / cuda12.8-py3.10-gcc11-sm75 / build, trunk / linux-jammy-py3-clang12-executorch / test (executorch, 1, 1, linux.2xlarge, unstable) Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Fixes #168059 PyTorch was incorrectly setting requires_grad=True on unused gradients even when create_graph=False. This caused unnecessary autograd tracking and extra memory usage. This PR updates the logic to set requires_grad based on the value of create_graph, ensuring that unused gradients are tracked only when explicitly requested. This aligns torch.autograd.grad( ) behavior with expectations. Includes test coverage in test_unused_grad_requires_grad_with_materialize Pull Request resolved: #168295 Approved by: https://github.com/soulitzer
Fixes #168059
PyTorch was incorrectly setting requires_grad=True on unused gradients even when create_graph=False. This caused unnecessary autograd tracking and extra memory usage.
This PR updates the logic to set requires_grad based on the value of create_graph, ensuring that unused gradients are tracked only when explicitly requested. This aligns torch.autograd.grad( ) behavior with expectations.
Includes test coverage in test_unused_grad_requires_grad_with_materialize