-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathreverse_twoarg.jl
More file actions
157 lines (149 loc) · 4.73 KB
/
reverse_twoarg.jl
File metadata and controls
157 lines (149 loc) · 4.73 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
## Pullback
struct EnzymeReverseTwoArgPullbackPrep{SIG,TY} <: DI.PullbackPrep{SIG}
_sig::Val{SIG}
ty_copy::TY
end
function DI.prepare_pullback_nokwarg(
strict::Val,
f!::F,
y,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple,
contexts::Vararg{DI.Context,C};
) where {F,C}
_sig = DI.signature(f!, y, backend, x, ty, contexts...; strict)
ty_copy = map(copy, ty)
return EnzymeReverseTwoArgPullbackPrep(_sig, ty_copy)
end
function DI.value_and_pullback(
f!::F,
y,
prep::EnzymeReverseTwoArgPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x::Number,
ty::NTuple{1},
contexts::Vararg{DI.Context,C},
) where {F,C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
copyto!(only(prep.ty_copy), only(ty))
mode = reverse_noprimal(backend)
f!_and_df! = get_f_and_df(f!, backend, mode)
dy = only(prep.ty_copy)
y_and_dy = Duplicated(y, dy)
annotated_contexts = translate(backend, mode, Val(1), contexts...)
dinputs = only(
autodiff(mode, f!_and_df!, Const, y_and_dy, Active(x), annotated_contexts...)
)
dx = dinputs[2]
return y, (dx,)
end
function DI.value_and_pullback(
f!::F,
y,
prep::EnzymeReverseTwoArgPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x::Number,
ty::NTuple{B},
contexts::Vararg{DI.Context,C},
) where {F,B,C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
foreach(copyto!, prep.ty_copy, ty)
mode = reverse_noprimal(backend)
f!_and_df! = get_f_and_df(f!, backend, mode, Val(B))
ty = prep.ty_copy
y_and_ty = BatchDuplicated(y, ty)
annotated_contexts = translate(backend, mode, Val(B), contexts...)
dinputs = only(
autodiff(mode, f!_and_df!, Const, y_and_ty, Active(x), annotated_contexts...)
)
tx = values(dinputs[2])
return y, tx
end
function DI.value_and_pullback(
f!::F,
y,
prep::EnzymeReverseTwoArgPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{1},
contexts::Vararg{DI.Context,C},
) where {F,C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
copyto!(only(prep.ty_copy), only(ty))
mode = reverse_noprimal(backend)
f!_and_df! = get_f_and_df(f!, backend, mode)
dx = make_zero(x) # allocates
dy = only(prep.ty_copy)
x_and_dx = Duplicated(x, dx)
y_and_dy = Duplicated(y, dy)
annotated_contexts = translate(backend, mode, Val(1), contexts...)
autodiff(mode, f!_and_df!, Const, y_and_dy, x_and_dx, annotated_contexts...)
return y, (dx,)
end
function DI.value_and_pullback(
f!::F,
y,
prep::EnzymeReverseTwoArgPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{B},
contexts::Vararg{DI.Context,C},
) where {F,B,C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
foreach(copyto!, prep.ty_copy, ty)
mode = reverse_noprimal(backend)
f!_and_df! = get_f_and_df(f!, backend, mode, Val(B))
tx = ntuple(_ -> make_zero(x), Val(B)) # allocates
ty = prep.ty_copy
x_and_tx = BatchDuplicated(x, tx)
y_and_ty = BatchDuplicated(y, ty)
annotated_contexts = translate(backend, mode, Val(B), contexts...)
autodiff(mode, f!_and_df!, Const, y_and_ty, x_and_tx, annotated_contexts...)
return y, tx
end
function DI.value_and_pullback!(
f!::F,
y,
tx::NTuple{1},
prep::EnzymeReverseTwoArgPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{1},
contexts::Vararg{DI.Context,C},
) where {F,C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
copyto!(only(prep.ty_copy), only(ty))
mode = reverse_noprimal(backend)
f!_and_df! = get_f_and_df(f!, backend, mode)
dx = only(tx)
make_zero!(dx)
dy = only(prep.ty_copy)
x_and_dx = Duplicated(x, dx)
y_and_dy = Duplicated(y, dy)
annotated_contexts = translate(backend, mode, Val(1), contexts...)
autodiff(mode, f!_and_df!, Const, y_and_dy, x_and_dx, annotated_contexts...)
return y, (dx,)
end
function DI.value_and_pullback!(
f!::F,
y,
tx::NTuple{B},
prep::EnzymeReverseTwoArgPullbackPrep,
backend::AutoEnzyme{<:Union{ReverseMode,Nothing}},
x,
ty::NTuple{B},
contexts::Vararg{DI.Context,C},
) where {F,B,C}
DI.check_prep(f!, y, prep, backend, x, ty, contexts...)
foreach(copyto!, prep.ty_copy, ty)
mode = reverse_noprimal(backend)
f!_and_df! = get_f_and_df(f!, backend, mode, Val(B))
make_zero!(tx)
ty = prep.ty_copy
x_and_tx = BatchDuplicated(x, tx)
y_and_ty = BatchDuplicated(y, ty)
annotated_contexts = translate(backend, mode, Val(B), contexts...)
autodiff(mode, f!_and_df!, Const, y_and_ty, x_and_tx, annotated_contexts...)
return y, tx
end