This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags:
- Loading branch information
Showing
4 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
class TestAutogradFunction(torch.autograd.Function): | ||
|
||
@staticmethod | ||
def forward(ctx, tensor): | ||
tensor = tensor + 1.0 | ||
return tensor | ||
|
||
@staticmethod | ||
def backward(ctx, gradY): | ||
# prints a tensor filled with 0.123, as expected | ||
print('gradY', gradY) | ||
gradY = gradY + 1.0 | ||
return gradY | ||
|
||
class M(nn.Module): | ||
def forward(self, x): | ||
return TestAutogradFunction.apply(x) | ||
|
||
m = M() | ||
|
||
def bw_pre_hook(module, go): | ||
new_go = torch.empty_like(go[0]).fill_(0.123) | ||
return (new_go,) | ||
|
||
m.register_full_backward_pre_hook(bw_pre_hook) | ||
|
||
x = torch.randn(2, 2).requires_grad_() | ||
y = m(x) | ||
y.sum().backward() |