Skip to content

Commit f99e9e8

Browse files
patrociniosvekarssekyondaMeta
authored
Fix retain_grad() documentation for leaf vs non-leaf tensors (#3657)
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 <!--- Describe your changes in detail --> ## Checklist <!--- Make sure to add `x` to all items in the following 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 Co-authored-by: Svetlana Karslioglu <[email protected]> Co-authored-by: sekyondaMeta <[email protected]>
1 parent bc79723 commit f99e9e8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

beginner_source/understanding_leaf_vs_nonleaf_tutorial.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@
265265
#
266266
# Computational graph after backward pass
267267
#
268-
# If you call ``retain_grad()`` on a non-leaf node, it results in a no-op.
269-
# If we call ``retain_grad()`` on a node that has ``requires_grad=False``,
268+
# If you call ``retain_grad()`` on a leaf tensor, it results in a no-op
269+
# since leaf tensors already retain their gradients by default (when
270+
# ``requires_grad=True``).
271+
# If we call ``retain_grad()`` on a tensor that has ``requires_grad=False``,
270272
# PyTorch actually throws an error, since it can’t store the gradient if
271273
# it is never calculated.
272274
#
@@ -298,13 +300,13 @@
298300
# +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+
299301
# | ``is_leaf`` | ``requires_grad`` | ``retains_grad`` | ``require_grad()`` | ``retain_grad()`` |
300302
# +================+========================+========================+===================================================+=====================================+
301-
# | ``True`` | ``False`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op |
303+
# | ``True`` | ``False`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | throws error |
302304
# +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+
303-
# | ``True`` | ``True`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op |
305+
# | ``True`` | ``True`` | ``False`` | sets ``requires_grad`` to ``True`` or ``False`` | no-op (already retains) |
304306
# +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+
305307
# | ``False`` | ``True`` | ``False`` | no-op | sets ``retains_grad`` to ``True`` |
306308
# +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+
307-
# | ``False`` | ``True`` | ``True`` | no-op | no-op |
309+
# | ``False`` | ``True`` | ``True`` | no-op | no-op (already retains) |
308310
# +----------------+------------------------+------------------------+---------------------------------------------------+-------------------------------------+
309311
#
310312

0 commit comments

Comments
 (0)