-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_grad.py
More file actions
282 lines (203 loc) · 8.14 KB
/
test_grad.py
File metadata and controls
282 lines (203 loc) · 8.14 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import torch
from pytest import raises
from torchjd.autojac._transform import Grad, Gradients
from ._dict_assertions import assert_tensor_dicts_are_close
def test_single_input():
"""
Tests that the Grad transform works correctly for a very simple example of differentiation.
Here, the function considered is: `y = a * x`. We want to compute the derivative of `y` with
respect to the parameter `a`. This derivative should be equal to `x`.
"""
x = torch.tensor(5.0)
a = torch.tensor(2.0, requires_grad=True)
y = a * x
input = Gradients({y: torch.ones_like(y)})
grad = Grad(outputs=[y], inputs=[a])
gradients = grad(input)
expected_gradients = {a: x}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_empty_inputs_1():
"""
Tests that the Grad transform works correctly when the `inputs` parameter is an empty
`Iterable`.
"""
y = torch.tensor(1.0, requires_grad=True)
input = Gradients({y: torch.ones_like(y)})
grad = Grad(outputs=[y], inputs=[])
gradients = grad(input)
expected_gradients = {}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_empty_inputs_2():
"""
Tests that the Grad transform works correctly when the `inputs` parameter is an empty
`Iterable`.
"""
x = torch.tensor(5.0)
a = torch.tensor(1.0, requires_grad=True)
y = a * x
input = Gradients({y: torch.ones_like(y)})
grad = Grad(outputs=[y], inputs=[])
gradients = grad(input)
expected_gradients = {}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_empty_outputs():
"""
Tests that the Grad transform works correctly when the `outputs` parameter is an empty
`Iterable`.
"""
a = torch.tensor(1.0, requires_grad=True)
input = Gradients({})
grad = Grad(outputs=[], inputs=[a])
gradients = grad(input)
expected_gradients = {a: torch.zeros_like(a)}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_retain_graph():
"""Tests that the `Grad` transform behaves as expected with the `retain_graph` flag."""
x = torch.tensor(5.0)
a = torch.tensor(2.0, requires_grad=True)
y = a * x
input = Gradients({y: torch.ones_like(y)})
grad_retain_graph = Grad(outputs=[y], inputs=[a], retain_graph=True)
grad_discard_graph = Grad(outputs=[y], inputs=[a], retain_graph=False)
grad_retain_graph(input)
grad_retain_graph(input)
grad_discard_graph(input)
with raises(RuntimeError):
grad_retain_graph(input)
with raises(RuntimeError):
grad_discard_graph(input)
def test_single_input_two_levels():
"""
Tests that the Grad transform works correctly when composed with another Grad transform.
Here, the function considered is: `z = a * x1 * x2`, which is computed in 2 parts: `y = a * x1`
and `z = y * x2`. We want to compute the derivative of `z` with respect to the parameter `a`, by
using chain rule. This derivative should be equal to `x1 * x2`.
"""
x1 = torch.tensor(5.0)
x2 = torch.tensor(6.0)
a = torch.tensor(2.0, requires_grad=True)
y = a * x1
z = y * x2
input = Gradients({z: torch.ones_like(z)})
outer_grad = Grad(outputs=[y], inputs=[a])
inner_grad = Grad(outputs=[z], inputs=[y])
grad = outer_grad << inner_grad
gradients = grad(input)
expected_gradients = {a: x1 * x2}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_empty_inputs_two_levels():
"""
Tests that the Grad transform works correctly when the `inputs` parameter is an empty
`Iterable`, with 2 composed Grad transforms.
"""
x1 = torch.tensor(5.0)
x2 = torch.tensor(6.0)
a = torch.tensor(2.0, requires_grad=True)
y = a * x1
z = y * x2
input = Gradients({z: torch.ones_like(z)})
outer_grad = Grad(outputs=[y], inputs=[])
inner_grad = Grad(outputs=[z], inputs=[y])
composed_grad = outer_grad << inner_grad
gradients = composed_grad(input)
expected_gradients = {}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_vector_output():
"""
Tests that the Grad transform works correctly when the `outputs` contains a single vector.
The input (grad_outputs) is not the same for both values of the output, so that this test also
checks that the scaling is performed correctly.
"""
x = torch.tensor(5.0)
a = torch.tensor(2.0, requires_grad=True)
y = torch.stack([a * x, a**2])
input = Gradients({y: torch.tensor([3.0, 1.0])})
grad = Grad(outputs=[y], inputs=[a])
gradients = grad(input)
expected_gradients = {a: x * 3 + 2 * a}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_multiple_outputs():
"""
Tests that the Grad transform works correctly when the `outputs` contains 2 scalars.
The input (grad_outputs) is not the same for both outputs, so that this test also checks that
the scaling is performed correctly.
"""
x = torch.tensor(5.0)
a = torch.tensor(2.0, requires_grad=True)
y1 = a * x
y2 = a**2
input = Gradients({y1: torch.ones_like(y1) * 3, y2: torch.ones_like(y2)})
grad = Grad(outputs=[y1, y2], inputs=[a])
gradients = grad(input)
expected_gradients = {a: x * 3 + 2 * a}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_multiple_tensor_outputs():
"""
Tests that the Grad transform works correctly when the `outputs` contains several tensors of
different shapes. The input (grad_outputs) is not the same for all values of the outputs, so
that this test also checks that the scaling is performed correctly.
"""
x = torch.tensor(5.0)
a = torch.tensor(2.0, requires_grad=True)
y1 = a * x
y2 = torch.stack([a**2, 2 * a**2])
y3 = torch.stack([a**3, 2 * a**3]).unsqueeze(0)
input = Gradients(
{
y1: torch.tensor(3.0),
y2: torch.tensor([6.0, 7.0]),
y3: torch.tensor([[9.0, 10.0]]),
}
)
grad = Grad(outputs=[y1, y2, y3], inputs=[a])
gradients = grad(input)
g = x * 3 + 2 * a * 6 + 2 * a * 2 * 7 + 3 * a**2 * 9 + 3 * a**2 * 2 * 10.0
expected_gradients = {a: g}
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_composition_of_grads_is_grad():
"""
Tests that the composition of 2 Grad transforms is equivalent to computing the Grad directly in
a single transform.
"""
x1 = torch.tensor(5.0)
x2 = torch.tensor(6.0)
a = torch.tensor(2.0, requires_grad=True)
b = torch.tensor(1.0, requires_grad=True)
y1 = a * x1
y2 = a * x2
z1 = y1 + x2
z2 = y2 + x1
input = Gradients({z1: torch.ones_like(z1), z2: torch.ones_like(z2)})
outer_grad = Grad(outputs=[y1, y2], inputs=[a, b], retain_graph=True)
inner_grad = Grad(outputs=[z1, z2], inputs=[y1, y2], retain_graph=True)
composed_grad = outer_grad << inner_grad
grad = Grad(outputs=[z1, z2], inputs=[a, b])
gradients = composed_grad(input)
expected_gradients = grad(input)
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_conjunction_of_grads_is_grad():
"""
Tests that the conjunction of 2 Grad transforms is equivalent to computing the Grad directly in
a single transform.
"""
x1 = torch.tensor(5.0)
x2 = torch.tensor(6.0)
a1 = torch.tensor(2.0, requires_grad=True)
a2 = torch.tensor(3.0, requires_grad=True)
y = torch.stack([a1 * x1, a2 * x2])
input = Gradients({y: torch.ones_like(y)})
grad1 = Grad(outputs=[y], inputs=[a1], retain_graph=True)
grad2 = Grad(outputs=[y], inputs=[a2], retain_graph=True)
conjunction = grad1 | grad2
grad = Grad(outputs=[y], inputs=[a1, a2])
gradients = conjunction(input)
expected_gradients = grad(input)
assert_tensor_dicts_are_close(gradients, expected_gradients)
def test_create_graph():
"""Tests that the Grad transform behaves correctly when `create_graph` is set to `True`."""
a = torch.tensor(2.0, requires_grad=True)
y = a * a
input = Gradients({y: torch.ones_like(y)})
grad = Grad(outputs=[y], inputs=[a], create_graph=True)
gradients = grad(input)
assert gradients[a].requires_grad