-
Notifications
You must be signed in to change notification settings - Fork 16
/
reverse_onearg.jl
348 lines (322 loc) · 9.34 KB
/
reverse_onearg.jl
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
function seeded_autodiff_thunk(
rmode::ReverseModeSplit{ReturnPrimal},
dresult,
f::FA,
::Type{RA},
args::Vararg{Annotation,N},
) where {ReturnPrimal,FA<:Annotation,RA<:Annotation,N}
forward, reverse = autodiff_thunk(rmode, FA, RA, typeof.(args)...)
tape, result, shadow_result = forward(f, args...)
if RA <: Active
dresult_righttype = convert(typeof(result), dresult)
dinputs = only(reverse(f, args..., dresult_righttype, tape))
else
shadow_result .+= dresult # TODO: generalize beyond arrays
dinputs = only(reverse(f, args..., tape))
end
if ReturnPrimal
return (dinputs, result)
else
return (dinputs,)
end
end
function batch_seeded_autodiff_thunk(
rmode::ReverseModeSplit{ReturnPrimal},
dresults::NTuple{B},
f::FA,
::Type{RA},
args::Vararg{Annotation,N},
) where {ReturnPrimal,B,FA<:Annotation,RA<:Annotation,N}
rmode_rightwidth = set_width(rmode, Val(B))
forward, reverse = autodiff_thunk(rmode_rightwidth, FA, RA, typeof.(args)...)
tape, result, shadow_results = forward(f, args...)
if RA <: Active
dresults_righttype = map(Fix1(convert, typeof(result)), dresults)
dinputs = only(reverse(f, args..., dresults_righttype, tape))
else
foreach(shadow_results, dresults) do d0, d
d0 .+= d # use recursive_add here?
end
dinputs = only(reverse(f, args..., tape))
end
if ReturnPrimal
return (dinputs, result)
else
return (dinputs,)
end
end
## Pullback
function DI.prepare_pullback(
f::F,
::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
return NoPullbackPrep()
end
### Out-of-place
function DI.value_and_pullback(
f::F,
::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x::Number,
ty::NTuple{1},
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = force_annotation(get_f_and_df(f, backend))
mode = reverse_mode_split_withprimal(backend)
RA = eltype(ty) <: Number ? Active : Duplicated
dinputs, result = seeded_autodiff_thunk(
mode, only(ty), f_and_df, RA, Active(x), map(translate, contexts)...
)
return result, (first(dinputs),)
end
function DI.value_and_pullback(
f::F,
::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x::Number,
ty::NTuple{B},
contexts::Vararg{Context,C},
) where {F,B,C}
f_and_df = force_annotation(get_f_and_df(f, backend, Val(B)))
mode = reverse_mode_split_withprimal(backend)
RA = eltype(ty) <: Number ? Active : BatchDuplicated
dinputs, result = batch_seeded_autodiff_thunk(
mode, ty, f_and_df, RA, Active(x), map(translate, contexts)...
)
return result, values(first(dinputs))
end
function DI.value_and_pullback(
f::F,
::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{1},
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = force_annotation(get_f_and_df(f, backend))
mode = reverse_mode_split_withprimal(backend)
RA = eltype(ty) <: Number ? Active : Duplicated
dx = make_zero(x)
_, result = seeded_autodiff_thunk(
mode, only(ty), f_and_df, RA, Duplicated(x, dx), map(translate, contexts)...
)
return result, (dx,)
end
function DI.value_and_pullback(
f::F,
::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{B},
contexts::Vararg{Context,C},
) where {F,B,C}
f_and_df = force_annotation(get_f_and_df(f, backend, Val(B)))
mode = reverse_mode_split_withprimal(backend)
RA = eltype(ty) <: Number ? Active : BatchDuplicated
tx = ntuple(_ -> make_zero(x), Val(B))
_, result = batch_seeded_autodiff_thunk(
mode, ty, f_and_df, RA, BatchDuplicated(x, tx), map(translate, contexts)...
)
return result, tx
end
function DI.pullback(
f::F,
prep::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
return last(DI.value_and_pullback(f, prep, backend, x, ty, contexts...))
end
### In-place
function DI.value_and_pullback!(
f::F,
tx::NTuple{1},
::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{1},
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = force_annotation(get_f_and_df(f, backend))
mode = reverse_mode_split_withprimal(backend)
RA = eltype(ty) <: Number ? Active : Duplicated
dx_righttype = convert(typeof(x), only(tx))
make_zero!(dx_righttype)
_, result = seeded_autodiff_thunk(
mode,
only(ty),
f_and_df,
RA,
Duplicated(x, dx_righttype),
map(translate, contexts)...,
)
only(tx) === dx_righttype || copyto!(only(tx), dx_righttype)
return result, tx
end
function DI.value_and_pullback!(
f::F,
tx::NTuple{B},
::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{B},
contexts::Vararg{Context,C},
) where {F,B,C}
f_and_df = force_annotation(get_f_and_df(f, backend, Val(B)))
mode = reverse_mode_split_withprimal(backend)
RA = eltype(ty) <: Number ? Active : BatchDuplicated
tx_righttype = map(Fix1(convert, typeof(x)), tx)
make_zero!(tx_righttype)
_, result = batch_seeded_autodiff_thunk(
mode,
ty,
f_and_df,
RA,
BatchDuplicated(x, tx_righttype),
map(translate, contexts)...,
)
foreach(copyto!, tx, tx_righttype)
return result, tx
end
function DI.pullback!(
f::F,
tx::NTuple,
prep::NoPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
return last(DI.value_and_pullback!(f, tx, prep, backend, x, ty, contexts...))
end
## Gradient
function DI.prepare_gradient(
f::F,
::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
return NoGradientPrep()
end
function DI.gradient(
f::F,
::NoGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
derivs = gradient(
reverse_mode_noprimal(backend), f_and_df, x, map(translate, contexts)...
)
return first(derivs)
end
function DI.gradient!(
f::F,
grad,
::NoGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
dx_righttype = convert(typeof(x), grad)
make_zero!(dx_righttype)
autodiff(
reverse_mode_noprimal(backend),
f_and_df,
Active,
Duplicated(x, dx_righttype),
map(translate, contexts)...,
)
dx_righttype === grad || copyto!(grad, dx_righttype)
return grad
end
function DI.value_and_gradient(
f::F,
::NoGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
(; derivs, val) = gradient(
reverse_mode_withprimal(backend), f_and_df, x, map(translate, contexts)...
)
return val, first(derivs)
end
function DI.value_and_gradient!(
f::F,
grad,
::NoGradientPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing},<:Union{Nothing,Const}},
x,
contexts::Vararg{Context,C},
) where {F,C}
f_and_df = get_f_and_df(f, backend)
dx_righttype = convert(typeof(x), grad)
make_zero!(dx_righttype)
_, y = autodiff(
reverse_mode_withprimal(backend),
f_and_df,
Active,
Duplicated(x, dx_righttype),
map(translate, contexts)...,
)
dx_righttype === grad || copyto!(grad, dx_righttype)
return y, grad
end
## Jacobian
struct EnzymeReverseOneArgJacobianPrep{Sy,B} <: JacobianPrep end
function DI.prepare_jacobian(f::F, backend::AutoEnzyme{<:ReverseMode,Nothing}, x) where {F}
y = f(x)
Sy = size(y)
B = pick_batchsize(backend, prod(Sy))
return EnzymeReverseOneArgJacobianPrep{Sy,B}()
end
function DI.jacobian(
f::F,
::EnzymeReverseOneArgJacobianPrep{Sy,B},
backend::AutoEnzyme{<:ReverseMode,Nothing},
x,
) where {F,Sy,B}
derivs = jacobian(reverse_mode_noprimal(backend), f, x; n_outs=Val(Sy), chunk=Val(B))
jac_tensor = only(derivs)
return maybe_reshape(jac_tensor, prod(Sy), length(x))
end
function DI.value_and_jacobian(
f::F,
::EnzymeReverseOneArgJacobianPrep{Sy,B},
backend::AutoEnzyme{<:ReverseMode,Nothing},
x,
) where {F,Sy,B}
(; derivs, val) = jacobian(
reverse_mode_withprimal(backend), f, x; n_outs=Val(Sy), chunk=Val(B)
)
jac_tensor = only(derivs)
return val, maybe_reshape(jac_tensor, prod(Sy), length(x))
end
function DI.jacobian!(
f::F,
jac,
prep::EnzymeReverseOneArgJacobianPrep,
backend::AutoEnzyme{<:ReverseMode,Nothing},
x,
) where {F}
return copyto!(jac, DI.jacobian(f, prep, backend, x))
end
function DI.value_and_jacobian!(
f::F,
jac,
prep::EnzymeReverseOneArgJacobianPrep,
backend::AutoEnzyme{<:ReverseMode,Nothing},
x,
) where {F}
y, new_jac = DI.value_and_jacobian(f, prep, backend, x)
return y, copyto!(jac, new_jac)
end