forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_autograd_funs.py
212 lines (172 loc) · 6.57 KB
/
test_autograd_funs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Owner(s): ["module: onnx"]
import pytorch_test_common
import torch
from onnx_test_common import run_model_test
from torch.onnx import OperatorExportTypes
from torch.onnx._globals import GLOBALS
from torch.onnx.utils import _model_to_graph
from torch.testing._internal import common_utils
class TestAutogradFuns(pytorch_test_common.ExportTestCase):
opset_version = GLOBALS.export_onnx_opset_version
keep_initializers_as_inputs = False
onnx_shape_inference = True
def test_single_output(self):
class SingleOut(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i.exp()
result = result.log()
ctx.save_for_backward(result)
return result
@staticmethod
def backward(ctx, grad_output):
(result,) = ctx.saved_tensors
return grad_output * result
class Caller(torch.nn.Module):
def forward(self, input):
result = input + 5
return SingleOut.apply(result) + 3
model = Caller()
input = torch.ones(1)
run_model_test(self, model, input_args=(input,))
def test_multi_output(self):
class MultiOut(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result_exp = i.exp()
result_log = result_exp.log()
ctx.save_for_backward(result_exp, result_log)
return result_exp, result_log
@staticmethod
def backward(ctx, grad_output):
(result,) = ctx.saved_tensors
return grad_output * result
class Caller(torch.nn.Module):
def forward(self, input):
return MultiOut.apply(input)
model = Caller()
input = torch.ones(1, 5)
run_model_test(self, model, input_args=(input,))
def test_partial_output(self):
class PartialOut(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
ctx.save_for_backward(input)
values, indices = torch.topk(input, 3)
return values
class Caller(torch.nn.Module):
def forward(self, input):
return PartialOut.apply(input)
model = Caller()
input = torch.ones(1, 5)
run_model_test(self, model, input_args=(input,))
def test_nested_autograd(self):
class Child(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i.log()
result_log = result.log()
ctx.save_for_backward(result_log)
return result_log
@staticmethod
def backward(ctx, grad_output):
(result,) = ctx.saved_tensors
return grad_output * result
class Parent(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result_exp = i.exp()
result_log = Child.apply(result_exp)
ctx.save_for_backward(result_exp, result_log)
return result_exp, result_log
@staticmethod
def backward(ctx, grad_output):
(result,) = ctx.saved_tensors
return grad_output * result
class Caller(torch.nn.Module):
def forward(self, input):
return Parent.apply(input)
model = Caller()
input = torch.ones(1, 5)
run_model_test(self, model, input_args=(input,))
# Run export in ONNX_FALLTHROUGH mode as torch.erf() is not supported
def test_aten_unsupported(self):
class Erf(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
erf_out = torch.special.erf(x)
ctx.save_for_backward(erf_out)
return erf_out
@staticmethod
def backward(ctx, grad_output):
result = ctx.saved_tensors
return torch.special.erfinv(result), None
class Caller(torch.nn.Module):
def forward(self, input):
return Erf.apply(input)
model = Caller()
input = torch.ones(1, 5)
# Test ONNX_FALLTHROUGH_MODE
graph, _, _ = _model_to_graph(
model,
(input,),
operator_export_type=OperatorExportTypes.ONNX_FALLTHROUGH,
)
iter = graph.nodes()
self.assertEqual(next(iter).kind(), "prim::PythonOp")
# Test ATEN_FALLBACK_MODE
graph, _, _ = _model_to_graph(
model,
(input,),
operator_export_type=OperatorExportTypes.ONNX_ATEN_FALLBACK,
)
iter = graph.nodes()
self.assertEqual(next(iter).kind(), "aten::ATen")
def test_inline_and_symbolic(self):
class Exp(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
ctx.save_for_backward(input)
return i.exp()
@staticmethod
def symbolic(g, input):
return g.op("Exp", input)
class LogLog(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
ctx.save_for_backward(input)
return i.log().log()
class Caller(torch.nn.Module):
def forward(self, input):
exp_result = Exp.apply(input)
return LogLog.apply(exp_result)
model = Caller()
input = torch.ones(1)
run_model_test(self, model, input_args=(input,))
def test_inline_with_scoped_tracing(self):
class Exp(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
ctx.save_for_backward(input)
return i.exp()
@staticmethod
def symbolic(g, input):
return g.op("Exp", input)
class LogLog(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
ctx.save_for_backward(input)
return i.log().log()
class Caller(torch.nn.Module):
def forward(self, input):
exp_result = Exp.apply(input)
return LogLog.apply(exp_result)
model = Caller()
input = torch.ones(1)
torch.jit._trace._trace_module_map = {
_m: torch.typename(type(_m)) for _m in model.modules()
}
run_model_test(self, model, input_args=(input,))
torch.jit._trace._trace_module_map = None
if __name__ == "__main__":
common_utils.run_tests()