Skip to content

Conversation

@patrocinio
Copy link
Contributor

@patrocinio patrocinio commented Nov 24, 2025

The documentation incorrectly stated that calling retain_grad() on a non-leaf node results in a no-op. This is misleading because:

  • retain_grad() on a non-leaf tensor with requires_grad=True correctly retains gradients (not a no-op)
  • retain_grad() on a leaf tensor is a no-op (already retains by default)
  • retain_grad() on a tensor with requires_grad=False throws an error

Updated the documentation and summary table to accurately reflect this behavior.

Fixes #3652

Description

Checklist

  • The issue that is being fixed is referred in the description (see above "Fixes #ISSUE_NUMBER")
  • Only one issue is addressed in this pull request
  • Labels from the issue that this PR is fixing are added to this pull request
  • No unnecessary issues are included into this pull request.

cc @jbschlosser

The documentation incorrectly stated that calling retain_grad() on a
non-leaf node results in a no-op. This is misleading because:

- retain_grad() on a non-leaf tensor with requires_grad=True correctly
  retains gradients (not a no-op)
- retain_grad() on a leaf tensor is a no-op (already retains by default)
- retain_grad() on a tensor with requires_grad=False throws an error

Updated the documentation and summary table to accurately reflect this
behavior.
@pytorch-bot
Copy link

pytorch-bot bot commented Nov 24, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3657

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 744741c with merge base bc79723 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the cla signed label Nov 24, 2025
@svekars
Copy link
Contributor

svekars commented Nov 24, 2025

cc: @j-silv

@svekars svekars added the core Tutorials of any level of difficulty related to the core pytorch functionality label Nov 24, 2025
@albanD albanD requested a review from soulitzer November 24, 2025 22:58
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!

@sekyondaMeta sekyondaMeta merged commit f99e9e8 into pytorch:main Nov 25, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed core Tutorials of any level of difficulty related to the core pytorch functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

retain_grad() documentation misleading about non-leaf tensors

4 participants