Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[autograd] Do not detach when unpacking tensors that do not require grad #127959

Closed
wants to merge 23 commits into from

Conversation

soulitzer
Copy link
Contributor

@soulitzer soulitzer commented Jun 4, 2024

Stack from ghstack (oldest at bottom):

In this PR:

  • Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
  • Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:

  • For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops.

Copy link

pytorch-bot bot commented Jun 4, 2024

🔗 Helpful Links

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

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:

✅ You can merge normally! (1 Unrelated Failure)

As of commit 994a888 with merge base e6d4451 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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

soulitzer added a commit that referenced this pull request Jun 4, 2024
ghstack-source-id: fd728b4f37088faa03d2c44e3c416558b42045be
Pull Request resolved: #127959
soulitzer added a commit that referenced this pull request Jun 6, 2024
ghstack-source-id: e86d6b9cdee39a442829c51db19f1a444fb95067
Pull Request resolved: #127959
soulitzer added a commit that referenced this pull request Jun 10, 2024
ghstack-source-id: a290a3d1320918b2a2bc9e502db653398bac4c93
Pull Request resolved: #127959
@soulitzer soulitzer requested a review from albanD as a code owner June 11, 2024 13:16
soulitzer added a commit that referenced this pull request Jun 11, 2024
ghstack-source-id: fb514383b45a7bb292683818fca9581d60e7aa24
Pull Request resolved: #127959
@soulitzer soulitzer changed the title Support nested tensor with activation checkpoint [checkpoint] Activation checkpoint preserves object identity when tensor does not require grad Jun 11, 2024
]
)

def fn(values, offsets):
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean def fn(values, lengths) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, good catch

…ty when tensor does not require grad"


This PR ensures that if a tensor does not require grad, and it is saved for backward, (1) unpacking in general preserve the object, (2) non-reentrant checkpoint preserves the object.


[ghstack-poisoned]
…ty when tensor does not require grad"


This PR ensures that if a tensor does not require grad, and it is saved for backward, (1) unpacking in general preserve the object, (2) non-reentrant checkpoint preserves the object.


[ghstack-poisoned]
@soulitzer soulitzer marked this pull request as draft June 12, 2024 15:45
…ty when tensor does not require grad"


This PR ensures that if a tensor does not require grad, and it is saved for backward, (1) unpacking in general preserve the object, (2) non-reentrant checkpoint preserves the object.

For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
@soulitzer soulitzer changed the title [checkpoint] Activation checkpoint preserves object identity when tensor does not require grad [autograd] Do not detach when unpacking tensors that do not require grad Jun 12, 2024
…t require grad "


In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
…t require grad "


In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Jun 18, 2024
ghstack-source-id: bb06e4f94231b2af19ccb88e1b706e2c76c7161f
Pull Request resolved: #127959
…t require grad "


In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
…t require grad "


In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
…t require grad "


In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
…t require grad "


In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Jun 21, 2024
ghstack-source-id: 229a838975d0f4fc597745c4f867aa89b96a0d09
Pull Request resolved: #127959
…t require grad "


In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops. 


[ghstack-poisoned]
soulitzer added a commit that referenced this pull request Jun 25, 2024
ghstack-source-id: 7fca85d18ff52c390579e906378a9ccfd695fa1c
Pull Request resolved: #127959
@soulitzer
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jun 27, 2024
@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: This PR needs a release notes: label
If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Details for Dev Infra team Raised by workflow job

@soulitzer soulitzer added the release notes: autograd release notes category label Jun 27, 2024
@YuqingJ
Copy link
Contributor

YuqingJ commented Jul 1, 2024

Ready to merge again?

@soulitzer
Copy link
Contributor Author

@pytorchbot merge -i

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: pull / linux-focal-cuda12.1-py3.10-gcc9-sm86 / test (default, 5, 5, linux.g5.4xlarge.nvidia.gpu)

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 pushed a commit to khushi-411/pytorch that referenced this pull request Jul 2, 2024
…rad (pytorch#127959)

In this PR:
- Ensure that if a tensor not requiring grad is saved for backward unpacking does not trigger a detach (unless the user installs a saved tensor pack hook that returns a tensor requiring grad).
- Update non-reentrant checkpoint to also no longer detach for this case.

Alternatives:
- For custom autograd Function, you could directly save on ctx to work around this, but that would not work for when we switch to using custom ops.

Pull Request resolved: pytorch#127959
Approved by: https://github.com/YuqingJ
ghstack dependencies: pytorch#125795, pytorch#128545, pytorch#129262
@github-actions github-actions bot deleted the gh/soulitzer/306/head branch August 1, 2024 02:01
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 release notes: autograd release notes category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants