-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Fix prod double backward when there are 2+ zeros #113969
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
Fix prod double backward when there are 2+ zeros #113969
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/113969
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit fe78741 with merge base 0d6d97d ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
[ghstack-poisoned]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any details?
Also this is most likely going to be a challenging change just for perf reasons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ho ok!
You can update the condition to only be used when double backward is not used then (see condition below)
You can also add a small comment to that effect: when setting up for double backward, we must do the hard work of properly computing the result even though we know it is going to all 0s to ensure the autograd graph is properly created.
| Tensor zero_idx = (input == 0).nonzero(); | ||
| if (zero_idx.sym_numel() == 0) { | ||
| return grad * (result / input).conj(); | ||
| } else if (zero_idx.size(0) > 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(!at::GradMode::is_enabled() && zero_idx.size(0) > 1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tip, @albanD. I've updated the code.
[ghstack-poisoned]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I would expect that there is some test that used to fail that is now passing?
|
@albanD, yes! The following fails on PyTorch main but works on this branch: import torch
x = torch.tensor([2., 3, 0, 0], requires_grad=True)
y = torch.cumprod(x, dim=0)
gx, = torch.autograd.grad(y.sum(), x, create_graph=True)
gy = torch.autograd.grad(gx.sum(), x)
print(gy) |
|
Ho I meant there should be a test in CI running this. |
|
The extra input I added on common_method_invocarions should do it, no? Edit: I will send in a bit the test that would fail without the changes for this input. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extra input I added on common_method_invocarions should do it, no?
That was my question :p So we already do have gradgradcheck check for this input and so adding this input without the update in c++ would fail?
If so, then SGTM!
Yep! Thanks for the review, @albanD. I'll merge this one. |
|
@pytorchbot merge |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
|
@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 |
Stack from ghstack (oldest at bottom):
cc @ezyang @albanD @zou3519 @gqchen @pearu @nikitaved @soulitzer @lezcano @Varal7