-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest_if_stmt_ad.py
332 lines (269 loc) · 9.69 KB
/
test_if_stmt_ad.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import drjit as dr
import pytest
@pytest.mark.parametrize('variant', [1, 2, 3])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float, is_diff, shape=(*)')
@dr.syntax
def test01_backward_inside(t, variant, mode):
# Test that we can run dr.backward from *inside* an 'if' statement, and
# that this propagates correctly to a prior computation graph
i = dr.arange(dr.uint32_array_t(t), 5)
x = dr.arange(t, 5)
dr.enable_grad(x)
y = x * 2
yo = y
if dr.hint(i < 2, mode=mode):
if dr.hint(variant & 1, mode='scalar'):
z = y * 2
dr.backward_from(z)
else:
if dr.hint(variant & 2, mode='scalar'):
z = y * 3
dr.backward_from(z)
b1 = variant & 1
b2 = (variant & 2) >> 1
assert dr.all(x.grad == [4*b1, 4*b1, 6*b2, 6*b2, 6*b2])
assert yo is y
@pytest.mark.parametrize('variant', [1, 2, 3])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float, is_diff, shape=(*)')
@dr.syntax
def test02_forward_inside(t,variant, mode):
# Test that we can run dr.forward from *inside* an 'if' statement, and
# that this propagates correctly from a prior computation graph
i = dr.arange(dr.uint32_array_t(t), 5)
x = dr.arange(t, 5)
dr.enable_grad(x)
y = x * 2
x.grad = 1
dr.forward_from(x, dr.ADFlag.ClearEdges)
z = t(0)
if dr.hint(i < 2, mode=mode):
if dr.hint(variant & 1, mode='scalar'):
z = dr.forward_to(y*2)
else:
if dr.hint(variant & 2, mode='scalar'):
z = dr.forward_to(y*3)
b1 = variant & 1
b2 = (variant & 2) >> 1
assert dr.all(z == [4*b1, 4*b1, 6*b2, 6*b2, 6*b2])
@pytest.mark.parametrize('variant', [1, 2, 3])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float, is_diff, shape=(*)')
@dr.syntax
def test03_backward_outside(t, variant, mode):
# Test that we can run dr.backward from *outside* an 'if'
# statement, and that the resulting derivatives propagate
# correctly through a prior computation graph
i = dr.arange(dr.uint32_array_t(t), 5)
x = dr.arange(t, 5)
dr.enable_grad(x)
y = x * 2
yo = y
z = t(0)
if dr.hint(i < 2, mode=mode):
if dr.hint(variant & 1, mode='scalar'):
z = y * 2
else:
if dr.hint(variant & 2, mode='scalar'):
z = y * 3
b1 = variant & 1
b2 = (variant & 2) >> 1
dr.backward_from(z)
assert dr.all(x.grad == [4*b1, 4*b1, 6*b2, 6*b2, 6*b2])
assert yo is y
@pytest.mark.parametrize('variant', [1, 2, 3])
@pytest.mark.parametrize('same_size', [True, False])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.mark.parametrize('source_evaluated', [True, False])
@pytest.test_arrays('float, is_diff, shape=(*)')
@pytest.skip_on(RuntimeError, "backend does not support the requested type of atomic reduction")
@dr.syntax
def test04_backward_gather_inside(t, variant, mode, same_size, source_evaluated):
# Variant of test01 where the differentiable read is replaced by a
# differentiable gather
i = dr.arange(dr.uint32_array_t(t), 5)
x = dr.arange(t, 5 if same_size else 6)
dr.enable_grad(x)
y = x * 2
if source_evaluated:
dr.eval(y)
z = t(0)
if dr.hint(i < 2, mode=mode):
if dr.hint(variant & 1, mode='scalar'):
z = dr.gather(t, y, i) * 2
dr.backward_from(z)
else:
if dr.hint(variant & 2, mode='scalar'):
z = dr.gather(t, y, i) * 3
dr.backward_from(z)
b1 = variant & 1
b2 = (variant & 2) >> 1
if same_size:
assert dr.all(x.grad == [4*b1, 4*b1, 6*b2, 6*b2, 6*b2])
else:
assert dr.all(x.grad == [4*b1, 4*b1, 6*b2, 6*b2, 6*b2, 0])
@pytest.mark.parametrize('variant', [1, 2, 3])
@pytest.mark.parametrize('same_size', [True, False])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.mark.parametrize('source_evaluated', [True, False])
@pytest.test_arrays('float, is_diff, shape=(*)')
@pytest.skip_on(RuntimeError, "backend does not support the requested type of atomic reduction")
@dr.syntax
def test05_backward_gather_outside(t, variant, mode, same_size, source_evaluated):
# Variant of test02 where the differentiable read is replaced by a
# differentiable gather
i = dr.arange(dr.uint32_array_t(t), 5)
x = dr.arange(t, 5 if same_size else 6)
dr.enable_grad(x)
y = x * 2
if source_evaluated:
dr.eval(y)
z = t(0)
if dr.hint(i < 2, mode=mode):
if dr.hint(variant & 1, mode='scalar'):
z = dr.gather(t, y, i) * 2
else:
if dr.hint(variant & 2, mode='scalar'):
z = dr.gather(t, y, i) * 3
dr.backward_from(z)
b1 = variant & 1
b2 = (variant & 2) >> 1
if same_size:
assert dr.all(x.grad == [4*b1, 4*b1, 6*b2, 6*b2, 6*b2])
else:
assert dr.all(x.grad == [4*b1, 4*b1, 6*b2, 6*b2, 6*b2, 0])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float, is_diff, shape=(*)')
def test06_ad_bwd_nested(t, mode):
# Test that we can backpropagate through a sequence of nested 'if' statements
@dr.syntax
def f(x, mode):
if dr.hint(x < 5, mode=mode):
y = 10*x
else:
if dr.hint(x < 7, mode=mode):
y = 100*x
else:
y = 1000*x
return y
x = dr.arange(t, 10)
dr.enable_grad(x)
y = f(x, mode)
dr.backward_from(y)
assert dr.all(dr.grad(x) == [10, 10, 10, 10, 10, 100, 100, 1000, 1000, 1000])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float,is_diff,shape=(*)')
@pytest.skip_on(RuntimeError, "backend does not support the requested type of atomic reduction")
@dr.syntax
def test07_ad_bwd_implicit_dep(t, mode):
# Identical to the above, but for reverse mode
y = t(1)
dr.enable_grad(y)
dr.set_grad(y, 1)
x = dr.arange(t, 10)
if dr.hint(x < 5, exclude=[y]):
z = x*y
else:
z = x-y
dr.backward_from(z)
assert dr.all(dr.grad(y) == 6)
@pytest.test_arrays('float32,is_diff,shape=(*)')
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.mark.parametrize('variant', [0, 1])
@dr.syntax
def test07_diff_gather_nest_bwd(t, mode, variant):
# Test that we can backpropagate htough gathers in nested 'if' statements
idx = dr.arange(dr.uint32_array_t(t), 10)
y = dr.zeros(t, 11)
z = dr.zeros(t, 10)
dr.enable_grad(y)
if dr.hint(variant == 0, mode='scalar'):
if dr.hint(idx > 3, label='outer', mode=mode, exclude=[y]):
if dr.hint(idx < 8, label='inner', mode=mode, exclude=[y]):
z = dr.gather(t, y, idx)
else:
if dr.hint(idx > 3, label='outer', mode=mode):
if dr.hint(idx < 8, label='inner', mode=mode):
z = dr.gather(t, y, idx)
dr.backward_from(z)
assert dr.sum(y.grad) == 4
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float,is_diff,shape=(*)')
def test08_ad_fwd_nested(t, mode):
# Test that we can forward-propagate through a series of 'if' statements
@dr.syntax
def f(x, mode):
if dr.hint(x < 5, mode=mode):
y = 10*x
else:
if dr.hint(x < 7, mode=mode):
y = 100*x
else:
y = 1000*x
return x, y
x = dr.arange(t, 10)
dr.enable_grad(x)
xi = x
xo, yo = f(x, mode)
assert dr.all(xo == dr.arange(t, 10))
assert dr.all(yo == [0, 10, 20, 30, 40, 500, 600, 7000, 8000, 9000])
dr.forward_from(x, flags=0)
assert dr.all(dr.grad(xo) == dr.full(t, 1, 10))
assert dr.all(dr.grad(yo) == [10, 10, 10, 10, 10, 100, 100, 1000, 1000, 1000])
assert xi is xo
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float,is_diff,shape=(*)')
@dr.syntax
def test09_ad_fwd_implicit_dep(t, mode):
# Ensure that implicit dependencies ('y') are correctly tracked
y = t(1)
dr.enable_grad(y)
dr.set_grad(y, 1)
x = dr.arange(t, 10)
if dr.hint(x < 5, exclude=[y]):
z = x*y
else:
z = x-y
dr.forward_to(z)
assert dr.all(dr.grad(z) == [0, 1, 2, 3, 4, -1, -1, -1, -1, -1])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.mark.parametrize('variant', [1, 2, 3])
@pytest.test_arrays('float, is_diff, shape=(*)')
@pytest.skip_on(RuntimeError, "backend does not support the requested type of atomic reduction")
@dr.syntax
def test10_scatter_add_bwd(t, variant, mode):
"""Test that we can backpropagate through scatters"""
i = dr.arange(dr.uint32_array_t(t), 5)
x = dr.arange(t, 5)
y = dr.zeros(t, 6)
dr.enable_grad(x)
xo = x
if dr.hint(i < 3, mode=mode):
if dr.hint(variant & 1, mode='scalar'):
dr.scatter_add(y, 2*x, i+1)
else:
if dr.hint(variant & 2, mode='scalar'):
dr.scatter_add(y, 3*x, i)
b1 = variant & 1
b2 = (variant & 2) >> 1
assert dr.all(y == [0, 0*b1, 2*b1, 9*b2 + 4 * b1, 12*b2, 0])
assert xo is x
y.grad = [1,2,3,4,5,6]
xg = dr.backward_to(x)
assert dr.all(xg == [2*b1*2, 2*b1*3, 2*b1*4, 3*b2*4, 3*b2*5])
@pytest.mark.parametrize('mode', ['evaluated', 'symbolic'])
@pytest.test_arrays('float32,diff,shape=(*)')
@dr.syntax(print_code=False)
def test11_bwd_in_cond(t, mode):
Float = t
UInt32 = dr.uint32_array_t(t)
buf1 = Float(0, 0, 0, 0)
dr.enable_grad(buf1)
buf2 = dr.gather(Float, buf1, UInt32([0, 1, 2, 3])) # Postponed AD edge
a = UInt32([0, 1, 2, 3])
index = UInt32(0, 0, 1, 1)
if dr.hint(index < 1, mode=mode, exclude=[buf2]):
d = dr.gather(Float, buf2, a)
dr.backward(2 * d)
assert dr.allclose(buf1.grad, [2, 2, 0, 0])