-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathfrom_primitive.jl
More file actions
271 lines (240 loc) · 7.58 KB
/
from_primitive.jl
File metadata and controls
271 lines (240 loc) · 7.58 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
abstract type FromPrimitive{inplace} <: AbstractADType end
check_available(backend::FromPrimitive) = check_available(backend.backend)
inplace_support(::FromPrimitive{true}) = InPlaceSupported()
inplace_support(::FromPrimitive{false}) = InPlaceNotSupported()
function pick_batchsize(backend::FromPrimitive, x_or_y::AbstractArray)
return pick_batchsize(backend.backend, x_or_y)
end
function pick_batchsize(backend::FromPrimitive, N::Integer)
return pick_batchsize(backend.backend, N)
end
function inner_preparation_behavior(backend::FromPrimitive)
return inner_preparation_behavior(backend.backend)
end
function overloaded_input(::typeof(pushforward), f, backend::FromPrimitive, x, tx::NTuple)
return overloaded_input(pushforward, f, backend.backend, x, tx)
end
function overloaded_input(
::typeof(pushforward), f!, y, backend::FromPrimitive, x, tx::NTuple
)
return overloaded_input(pushforward, f!, y, backend.backend, x, tx)
end
"""
AutoForwardFromPrimitive(backend::AbstractADType)
Wrapper which forces a given backend to act as a forward-mode backend, using only its native `value_and_pushforward` primitive and re-implementing the rest from scratch.
!!! tip
This can be useful to circumvent high-level operators when they have impractical limitations.
For instance, ForwardDiff.jl's `jacobian` does not support GPU arrays but its `pushforward` does, so `AutoForwardFromPrimitive(AutoForwardDiff())` has a GPU-friendly `jacobian`.
"""
struct AutoForwardFromPrimitive{inplace,B<:AbstractADType} <: FromPrimitive{inplace}
backend::B
end
function AutoForwardFromPrimitive(
backend::AbstractADType; inplace::Bool=Bool(inplace_support(backend))
)
return AutoForwardFromPrimitive{inplace,typeof(backend)}(backend)
end
ADTypes.mode(::AutoForwardFromPrimitive) = ADTypes.ForwardMode()
function threshold_batchsize(
backend::AutoForwardFromPrimitive{inplace}, dimension::Integer
) where {inplace}
return AutoForwardFromPrimitive(
threshold_batchsize(backend.backend, dimension); inplace
)
end
struct FromPrimitivePushforwardPrep{SIG,E<:PushforwardPrep} <: PushforwardPrep{SIG}
_sig::Val{SIG}
pushforward_prep::E
end
function prepare_pushforward_nokwarg(
strict::Val,
f::F,
backend::AutoForwardFromPrimitive,
x,
tx::NTuple,
contexts::Vararg{Context,C};
) where {F,C}
_sig = signature(f, backend, x, tx, contexts...; strict)
primitive_prep = prepare_pushforward_nokwarg(
strict, f, backend.backend, x, tx, contexts...
)
return FromPrimitivePushforwardPrep(_sig, primitive_prep)
end
function prepare_pushforward_nokwarg(
strict::Val,
f!::F,
y,
backend::AutoForwardFromPrimitive,
x,
tx::NTuple,
contexts::Vararg{Context,C};
) where {F,C}
_sig = signature(f!, y, backend, x, tx, contexts...; strict)
primitive_prep = prepare_pushforward_nokwarg(
strict, f!, y, backend.backend, x, tx, contexts...
)
return FromPrimitivePushforwardPrep(_sig, primitive_prep)
end
function value_and_pushforward(
f::F,
prep::FromPrimitivePushforwardPrep,
backend::AutoForwardFromPrimitive,
x,
tx::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f, prep, backend, x, tx, contexts...)
return value_and_pushforward(
f, prep.pushforward_prep, backend.backend, x, tx, contexts...
)
end
function value_and_pushforward(
f!::F,
y,
prep::FromPrimitivePushforwardPrep,
backend::AutoForwardFromPrimitive,
x,
tx::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f!, y, prep, backend, x, tx, contexts...)
return value_and_pushforward(
f!, y, prep.pushforward_prep, backend.backend, x, tx, contexts...
)
end
function value_and_pushforward!(
f::F,
ty::NTuple,
prep::FromPrimitivePushforwardPrep,
backend::AutoForwardFromPrimitive,
x,
tx::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f, prep, backend, x, tx, contexts...)
return value_and_pushforward!(
f, ty, prep.pushforward_prep, backend.backend, x, tx, contexts...
)
end
function value_and_pushforward!(
f!::F,
y,
ty::NTuple,
prep::FromPrimitivePushforwardPrep,
backend::AutoForwardFromPrimitive,
x,
tx::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f!, y, prep, backend, x, tx, contexts...)
return value_and_pushforward!(
f!, y, ty, prep.pushforward_prep, backend.backend, x, tx, contexts...
)
end
"""
AutoReverseFromPrimitive(backend::AbstractADType)
Wrapper which forces a given backend to act as a reverse-mode backend, using only its native `value_and_pullback` implementation and rebuilding the rest from scratch.
"""
struct AutoReverseFromPrimitive{inplace,B<:AbstractADType} <: FromPrimitive{inplace}
backend::B
end
function AutoReverseFromPrimitive(
backend::AbstractADType; inplace::Bool=Bool(inplace_support(backend))
)
return AutoReverseFromPrimitive{inplace,typeof(backend)}(backend)
end
ADTypes.mode(::AutoReverseFromPrimitive) = ADTypes.ReverseMode()
function threshold_batchsize(
backend::AutoReverseFromPrimitive{inplace}, dimension::Integer
) where {inplace}
return AutoReverseFromPrimitive(
threshold_batchsize(backend.backend, dimension); inplace
)
end
struct FromPrimitivePullbackPrep{SIG,E<:PullbackPrep} <: PullbackPrep{SIG}
_sig::Val{SIG}
pullback_prep::E
end
function prepare_pullback_nokwarg(
strict::Val,
f::F,
backend::AutoReverseFromPrimitive,
x,
ty::NTuple,
contexts::Vararg{Context,C};
) where {F,C}
_sig = signature(f, backend, x, ty, contexts...; strict)
primitive_prep = prepare_pullback_nokwarg(
strict, f, backend.backend, x, ty, contexts...
)
return FromPrimitivePullbackPrep(_sig, primitive_prep)
end
function prepare_pullback_nokwarg(
strict::Val,
f!::F,
y,
backend::AutoReverseFromPrimitive,
x,
ty::NTuple,
contexts::Vararg{Context,C};
) where {F,C}
_sig = signature(f!, y, backend, x, ty, contexts...; strict)
primitive_prep = prepare_pullback_nokwarg(
strict, f!, y, backend.backend, x, ty, contexts...
)
return FromPrimitivePullbackPrep(_sig, primitive_prep)
end
function value_and_pullback(
f::F,
prep::FromPrimitivePullbackPrep,
backend::AutoReverseFromPrimitive,
x,
ty::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f, prep, backend, x, ty, contexts...)
return value_and_pullback(f, prep.pullback_prep, backend.backend, x, ty, contexts...)
end
function value_and_pullback(
f!::F,
y,
prep::FromPrimitivePullbackPrep,
backend::AutoReverseFromPrimitive,
x,
ty::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f!, y, prep, backend, x, ty, contexts...)
return value_and_pullback(
f!, y, prep.pullback_prep, backend.backend, x, ty, contexts...
)
end
function value_and_pullback!(
f::F,
tx::NTuple,
prep::FromPrimitivePullbackPrep,
backend::AutoReverseFromPrimitive,
x,
ty::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f, prep, backend, x, ty, contexts...)
return value_and_pullback!(
f, tx, prep.pullback_prep, backend.backend, x, ty, contexts...
)
end
function value_and_pullback!(
f!::F,
y,
tx::NTuple,
prep::FromPrimitivePullbackPrep,
backend::AutoReverseFromPrimitive,
x,
ty::NTuple,
contexts::Vararg{Context,C},
) where {F,C}
check_prep(f!, y, prep, backend, x, ty, contexts...)
return value_and_pullback!(
f!, y, tx, prep.pullback_prep, backend.backend, x, ty, contexts...
)
end