Skip to content

Fix unused gradient tracking to respect create_graph#168295

Closed
dsashidh wants to merge 2 commits intopytorch:mainfrom
dsashidh:fix-grad-requires-inconsistency
Closed

Fix unused gradient tracking to respect create_graph#168295
dsashidh wants to merge 2 commits intopytorch:mainfrom
dsashidh:fix-grad-requires-inconsistency

Conversation

@dsashidh
Copy link
Contributor

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

@dsashidh
Copy link
Contributor Author

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot bot added the topic: not user facing topic category label Nov 20, 2025
@albanD albanD removed their request for review November 20, 2025 19:50
@pytorch-bot
Copy link

pytorch-bot bot commented Nov 20, 2025

🔗 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 Failure

As of commit 145c885 with merge base 9760a63 (image):

NEW FAILURE - The following job has failed:

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.

Copy link
Contributor

@soulitzer soulitzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Technically changes behavior, if someone relied on gradients being tracked here it would no longer be tracked. Should be fine though as this is a bug fix.

@soulitzer soulitzer added topic: bug fixes topic category release notes: autograd release notes category and removed topic: not user facing topic category labels Nov 20, 2025
@soulitzer
Copy link
Contributor

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 20, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@soulitzer
Copy link
Contributor

@pytorchbot merge -i

@pytorchmergebot
Copy link
Collaborator

Merge started

Your 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

Advanced Debugging
Check the merge workflow status
here

JacobSzwejbka pushed a commit that referenced this pull request Dec 8, 2025
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: autograd release notes category topic: bug fixes topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gradients wrt unused tensors are still marked with requires_grad=True if allow_unused=True

4 participants