forked from rtqichen/torchdiffeq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorm_tests.py
304 lines (252 loc) · 12.8 KB
/
norm_tests.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import contextlib
import unittest
import torch
import torchdiffeq
from problems import (DTYPES, DEVICES, ADAPTIVE_METHODS)
@contextlib.contextmanager
def random_seed_torch(seed):
cpu_rng_state = torch.get_rng_state()
torch.manual_seed(seed)
try:
yield
finally:
torch.set_rng_state(cpu_rng_state)
class _NeuralF(torch.nn.Module):
def __init__(self, width, oscillate):
super(_NeuralF, self).__init__()
# Use the same set of random weights for every instance.
with random_seed_torch(0):
self.linears = torch.nn.Sequential(torch.nn.Linear(2, width),
torch.nn.Tanh(),
torch.nn.Linear(width, 2),
torch.nn.Tanh())
self.nfe = 0
self.oscillate = oscillate
def forward(self, t, x):
self.nfe += 1
out = self.linears(x)
if self.oscillate:
out = out * t.mul(2).sin()
return out
class TestNorms(unittest.TestCase):
def test_norm(self):
def f(t, x):
return x
t = torch.tensor([0., 1.])
# First test that tensor input appears in the norm.
is_called = False
def norm(state):
nonlocal is_called
is_called = True
self.assertIsInstance(state, torch.Tensor)
self.assertEqual(state.shape, ())
return state.pow(2).mean().sqrt()
x0 = torch.tensor(1.)
torchdiffeq.odeint(f, x0, t, options=dict(norm=norm))
self.assertTrue(is_called)
# Now test that tupled input appears in the norm
is_called = False
def norm(state):
nonlocal is_called
is_called = True
self.assertIsInstance(state, tuple)
self.assertEqual(len(state), 1)
state, = state
self.assertEqual(state.shape, ())
return state.pow(2).mean().sqrt()
x0 = (torch.tensor(1.),)
torchdiffeq.odeint(f, x0, t, options=dict(norm=norm))
self.assertTrue(is_called)
is_called = False
def norm(state):
nonlocal is_called
is_called = True
self.assertIsInstance(state, tuple)
self.assertEqual(len(state), 2)
state1, state2 = state
self.assertEqual(state1.shape, ())
self.assertEqual(state2.shape, (2, 2))
return state1.pow(2).mean().sqrt()
x0 = (torch.tensor(1.), torch.tensor([[0.5, 0.5], [0.1, 0.1]]))
torchdiffeq.odeint(f, x0, t, options=dict(norm=norm))
self.assertTrue(is_called)
def test_adjoint_norm(self):
def f(t, x):
return x
t = torch.tensor([0., 1.])
adjoint_params = (torch.rand(7, requires_grad=True), torch.rand((), requires_grad=True))
def make_spy_on_adjoint_norm(adjoint_norm, actual_norm):
is_spy_called = [False]
def spy_on_adjoint_norm(tensor):
nonlocal is_spy_called
is_spy_called[0] = True
norm_result = adjoint_norm(tensor)
true_norm_result = actual_norm(tensor)
self.assertIsInstance(norm_result, torch.Tensor)
self.assertEqual(norm_result.shape, true_norm_result.shape)
self.assertLess((norm_result - true_norm_result).abs().max(), 1e-6)
return norm_result
return spy_on_adjoint_norm, is_spy_called
# Test the various auto-constructed adjoint norms with tensor (not tuple) state
for shape in ((), (1,), (2, 2)):
for use_adjoint_options, seminorm in ((False, False), (True, False), (True, True)):
with self.subTest(shape=shape, use_adjoint_options=use_adjoint_options, seminorm=seminorm):
x0 = torch.full(shape, 1.)
if use_adjoint_options:
if seminorm:
# Test passing adjoint_options and wanting the seminorm
kwargs = dict(adjoint_options=dict(norm='seminorm'))
else:
# Test passing adjoint_options but not specify the adjoint norm
kwargs = dict(adjoint_options={})
else:
# Test not passing adjoint_options at all.
kwargs = {}
xs = torchdiffeq.odeint_adjoint(f, x0, t, adjoint_params=adjoint_params, **kwargs)
_adjoint_norm = xs.grad_fn.adjoint_options['norm']
is_called = False
def actual_norm(tensor_tuple):
nonlocal is_called
is_called = True
self.assertIsInstance(tensor_tuple, tuple)
t, y, adj_y, adj_param1, adj_param2 = tensor_tuple
self.assertEqual(t.shape, ())
self.assertEqual(y.shape, shape)
self.assertEqual(adj_y.shape, shape)
self.assertEqual(adj_param1.shape, (7,))
self.assertEqual(adj_param2.shape, (()))
out = max(t.abs(), y.pow(2).mean().sqrt(), adj_y.pow(2).mean().sqrt())
if not seminorm:
out = max(out, adj_param1.pow(2).mean().sqrt(), adj_param2.abs())
return out
xs.grad_fn.adjoint_options['norm'], is_spy_called = make_spy_on_adjoint_norm(_adjoint_norm,
actual_norm)
xs.sum().backward()
self.assertTrue(is_called)
self.assertTrue(is_spy_called[0])
# Test the various auto-constructed adjoint norms with tuple (not tensor) state
for use_adjoint_options, seminorm in ((False, False), (True, False), (True, True)):
with self.subTest(shape=shape, use_adjoint_options=use_adjoint_options, seminorm=seminorm):
x0 = torch.tensor(1.), torch.tensor([[0.5, 0.5], [0.1, 0.1]])
if use_adjoint_options:
if seminorm:
# Test passing adjoint_options and wanting the seminorm
kwargs = dict(adjoint_options=dict(norm='seminorm'))
else:
# Test passing adjoint_options but not specify the adjoint norm
kwargs = dict(adjoint_options={})
else:
# Test not passing adjoint_options at all.
kwargs = {}
xs = torchdiffeq.odeint_adjoint(f, x0, t, adjoint_params=adjoint_params, **kwargs)
adjoint_options_dict = xs[0].grad_fn.next_functions[0][0].next_functions[0][0].adjoint_options
_adjoint_norm = adjoint_options_dict['norm']
is_called = False
def actual_norm(tensor_tuple):
nonlocal is_called
is_called = True
self.assertIsInstance(tensor_tuple, tuple)
t, y, adj_y, adj_param1, adj_param2 = tensor_tuple
self.assertEqual(t.shape, ())
self.assertEqual(y.shape, (5,))
self.assertEqual(adj_y.shape, (5,))
self.assertEqual(adj_param1.shape, (7,))
self.assertEqual(adj_param2.shape, ())
ya = y[0]
yb = y[1:]
adj_ya = adj_y[0]
adj_yb = adj_y[1:4]
out = max(t.abs(), ya.abs(), yb.pow(2).mean().sqrt(), adj_ya.abs(), adj_yb.pow(2).mean().sqrt())
if not seminorm:
out = max(out, adj_param1.pow(2).mean().sqrt(), adj_param2.abs())
return out
spy_on_adjoint_norm, is_spy_called = make_spy_on_adjoint_norm(_adjoint_norm, actual_norm)
adjoint_options_dict['norm'] = spy_on_adjoint_norm
xs[0].sum().backward()
self.assertTrue(is_called)
self.assertTrue(is_spy_called[0])
# Test user-passed adjoint norms with tensor (not tuple) state
is_called = False
def adjoint_norm(tensor_tuple):
nonlocal is_called
is_called = True
self.assertIsInstance(tensor_tuple, tuple)
t, y, adj_y, adj_param1, adj_param2 = tensor_tuple
self.assertEqual(t.shape, ())
self.assertEqual(y.shape, ())
self.assertEqual(adj_y.shape, ())
self.assertEqual(adj_param1.shape, (7,))
self.assertEqual(adj_param2.shape, ())
return max(t.abs(), y.pow(2).mean().sqrt(), adj_y.pow(2).mean().sqrt(), adj_param1.pow(2).mean().sqrt(),
adj_param2.abs())
x0 = torch.tensor(1.)
xs = torchdiffeq.odeint_adjoint(f, x0, t, adjoint_params=adjoint_params,
adjoint_options=dict(norm=adjoint_norm))
xs.sum().backward()
self.assertTrue(is_called)
# Test user-passed adjoint norms with tuple (not tensor) state
is_called = False
def adjoint_norm(tensor_tuple):
nonlocal is_called
is_called = True
self.assertIsInstance(tensor_tuple, tuple)
t, ya, yb, adj_ya, adj_yb, adj_param1, adj_param2 = tensor_tuple
self.assertEqual(t.shape, ())
self.assertEqual(ya.shape, ())
self.assertEqual(yb.shape, (2, 2))
self.assertEqual(adj_ya.shape, ())
self.assertEqual(adj_yb.shape, (2, 2))
self.assertEqual(adj_param1.shape, (7,))
self.assertEqual(adj_param2.shape, ())
return max(t.abs(), ya.abs(), yb.pow(2).mean().sqrt(), adj_ya.abs(), adj_yb.pow(2).mean().sqrt(),
adj_param1.pow(2).mean().sqrt(), adj_param2.abs())
x0 = torch.tensor(1.), torch.tensor([[0.5, 0.5], [0.1, 0.1]])
xs = torchdiffeq.odeint_adjoint(f, x0, t, adjoint_params=adjoint_params,
adjoint_options=dict(norm=adjoint_norm))
xs[0].sum().backward()
self.assertTrue(is_called)
def test_large_norm(self):
def norm(tensor):
return tensor.abs().max()
def large_norm(tensor):
return 10 * tensor.abs().max()
for dtype in DTYPES:
for device in DEVICES:
for method in ADAPTIVE_METHODS:
if dtype == torch.float32 and method == 'dopri8':
continue
with self.subTest(dtype=dtype, device=device, method=method):
x0 = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
t = torch.tensor([0., 1.0], device=device, dtype=torch.float64)
norm_f = _NeuralF(width=10, oscillate=True).to(device, dtype)
torchdiffeq.odeint(norm_f, x0, t, method=method, options=dict(norm=norm))
large_norm_f = _NeuralF(width=10, oscillate=True).to(device, dtype)
with torch.no_grad():
for norm_param, large_norm_param in zip(norm_f.parameters(), large_norm_f.parameters()):
large_norm_param.copy_(norm_param)
torchdiffeq.odeint(large_norm_f, x0, t, method=method, options=dict(norm=large_norm))
self.assertLessEqual(norm_f.nfe, large_norm_f.nfe)
def test_seminorm(self):
for dtype in DTYPES:
for device in DEVICES:
for method in ADAPTIVE_METHODS:
with self.subTest(dtype=dtype, device=device, method=method):
if dtype == torch.float64:
tol = 1e-8
else:
tol = 1e-6
x0 = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
t = torch.tensor([0., 1.0], device=device, dtype=torch.float64)
ode_f = _NeuralF(width=1024, oscillate=True).to(device, dtype)
out = torchdiffeq.odeint_adjoint(ode_f, x0, t, atol=tol, rtol=tol, method=method)
ode_f.nfe = 0
out.sum().backward()
default_nfe = ode_f.nfe
out = torchdiffeq.odeint_adjoint(ode_f, x0, t, atol=tol, rtol=tol, method=method,
adjoint_options=dict(norm='seminorm'))
ode_f.nfe = 0
out.sum().backward()
seminorm_nfe = ode_f.nfe
self.assertLessEqual(seminorm_nfe, default_nfe)
if __name__ == '__main__':
unittest.main()