forked from JuliaDiff/DifferentiationInterface.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferentiate_with.jl
More file actions
268 lines (248 loc) · 9.35 KB
/
differentiate_with.jl
File metadata and controls
268 lines (248 loc) · 9.35 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
@is_primitive MinimalCtx Tuple{DI.DifferentiateWith,<:Union{Number,AbstractArray,Tuple}}
# nested vectors (eg. [[1.0]]), Tuples (eg. ((1.0,),)) or similar (eg. [(1.0,)]) primal types are not supported by DI yet !
# This is because basis construction (DI.basis) does not have overloads for these types.
# For details, refer commented out test cases to see where the pullback creation fails.
function Mooncake.rrule!!(
dw::CoDual{<:DI.DifferentiateWith}, x::Union{CoDual{<:Number},CoDual{<:Tuple}}
)
primal_func = primal(dw)
primal_x = primal(x)
(; f, backend) = primal_func
y = zero_fcodual(f(primal_x))
# output is a vector, so we need to use the vector pullback
function pullback_array!!(dy::NoRData)
tx = DI.pullback(f, backend, primal_x, (y.dx,))
@assert rdata(only(tx)) isa rdata_type(tangent_type(typeof(primal_x)))
return NoRData(), rdata(only(tx))
end
# output is a scalar, so we can use the scalar pullback
function pullback_scalar!!(dy::Number)
tx = DI.pullback(f, backend, primal_x, (dy,))
@assert rdata(only(tx)) isa rdata_type(tangent_type(typeof(primal_x)))
return NoRData(), rdata(only(tx))
end
# output is a Tuple, NTuple
function pullback_tuple!!(dy::Tuple)
tx = DI.pullback(f, backend, primal_x, (dy,))
@assert rdata(only(tx)) isa rdata_type(tangent_type(typeof(primal_x)))
return NoRData(), rdata(only(tx))
end
# inputs are non Differentiable
function pullback_nodiff!!(dy::NoRData)
@assert tangent_type(typeof(primal(x))) <: NoTangent
return NoRData(), dy
end
pullback = if tangent_type(typeof(primal(x))) <: NoTangent
pullback_nodiff!!
elseif typeof(primal(y)) <: Number
pullback_scalar!!
elseif typeof(primal(y)) <: Array
pullback_array!!
elseif typeof(primal(y)) <: Tuple
pullback_tuple!!
else
error(
"For the function type $(typeof(primal_func)) and input type $(typeof(primal_x)), the primal type $(typeof(primal(y))) is currently not supported.",
)
end
return y, pullback
end
function Mooncake.rrule!!(dw::CoDual{<:DI.DifferentiateWith}, x::CoDual{<:AbstractArray})
primal_func = primal(dw)
primal_x = primal(x)
fdata_arg = x.dx
(; f, backend) = primal_func
y = zero_fcodual(f(primal_x))
# output is a vector, so we need to use the vector pullback
function pullback_array!!(dy::NoRData)
tx = DI.pullback(f, backend, primal_x, (y.dx,))
@assert rdata(first(only(tx))) isa rdata_type(tangent_type(typeof(first(primal_x))))
fdata_arg .+= only(tx)
return NoRData(), dy
end
# output is a scalar, so we can use the scalar pullback
function pullback_scalar!!(dy::Number)
tx = DI.pullback(f, backend, primal_x, (dy,))
@assert rdata(first(only(tx))) isa rdata_type(tangent_type(typeof(first(primal_x))))
fdata_arg .+= only(tx)
return NoRData(), NoRData()
end
# output is a Tuple, NTuple
function pullback_tuple!!(dy::Tuple)
tx = DI.pullback(f, backend, primal_x, (dy,))
@assert rdata(first(only(tx))) isa rdata_type(tangent_type(typeof(first(primal_x))))
fdata_arg .+= only(tx)
return NoRData(), NoRData()
end
# inputs are non Differentiable
function pullback_nodiff!!(dy::NoRData)
@assert tangent_type(typeof(primal(x))) <: Vector{NoTangent}
return NoRData(), dy
end
pullback = if tangent_type(typeof(primal(x))) <: Vector{NoTangent}
pullback_nodiff!!
elseif typeof(primal(y)) <: Number
pullback_scalar!!
elseif typeof(primal(y)) <: AbstractArray
pullback_array!!
elseif typeof(primal(y)) <: Tuple
pullback_tuple!!
else
error(
"For the function type $(typeof(primal_func)) and input type $(typeof(primal_x)), the primal type $(typeof(primal(y))) is currently not supported.",
)
end
return y, pullback
end
function Mooncake.generate_derived_rrule!!_test_cases(rng_ctor, ::Val{:diffwith})
return Any[], Any[]
end
function Mooncake.generate_hand_written_rrule!!_test_cases(rng_ctor, ::Val{:diffwith})
test_cases = reduce(
vcat,
map([(x) -> DI.DifferentiateWith(x, DI.AutoFiniteDiff())]) do F
map([Float64, Float32]) do P
return Any[
# (false, :none, nothing, F(identity), ((1.0,),)), # (DI.basis fails for this, correct it!)
# (false, :none, nothing, F(identity), [[1.0]]), # (DI.basis fails for this, correct it!)
(false, :stability_and_allocs, nothing, F(cosh), P(0.3)),
(false, :stability_and_allocs, nothing, F(sinh), P(0.3)),
(
false,
:stability_and_allocs,
nothing,
F(Base.FastMath.exp10_fast),
P(0.5),
),
(
false,
:stability_and_allocs,
nothing,
F(Base.FastMath.exp2_fast),
P(0.5),
),
(
false,
:stability_and_allocs,
nothing,
F(Base.FastMath.exp_fast),
P(5.0),
),
(false, :stability, nothing, F(copy), rand(Int32, 5)),
]
end
end...,
)
map([(x) -> DI.DifferentiateWith(x, DI.AutoFiniteDiff())]) do F
push!(
test_cases,
Any[
(false, :stability, nothing, copy, randn(5, 4)),
(
# Check that Core._apply_iterate gets lifted to _apply_iterate_equivalent.
false,
:stability,
nothing,
F(x -> +(x...)),
randn(33),
),
(
false,
:stability,
nothing,
(F(
function (x)
rx = Ref(x)
return Base.pointerref(
Base.bitcast(Ptr{Float64}, pointer_from_objref(rx)), 1, 1
)
end,
)),
5.0,
),
# (false, :none, nothing, F(Mooncake.__vec_to_tuple), Any[(1.0,)]), # (DI.basis fails for this, correct it!)
(
false,
:stability_and_allocs,
nothing,
F(Mooncake.IntrinsicsWrappers.ctlz_int),
5,
),
(
false,
:stability_and_allocs,
nothing,
F(Mooncake.IntrinsicsWrappers.ctpop_int),
5,
),
(
false,
:stability_and_allocs,
nothing,
F(Mooncake.IntrinsicsWrappers.cttz_int),
5,
),
(
false,
:stability_and_allocs,
nothing,
F(Mooncake.IntrinsicsWrappers.abs_float),
5.0f0,
),
(false, :stability_and_allocs, nothing, F(deepcopy), 5.0),
(false, :stability, nothing, F(deepcopy), randn(5)),
(false, :stability_and_allocs, nothing, F(sin), 1.1),
(false, :stability_and_allocs, nothing, F(sin), 1.0f1),
(false, :stability_and_allocs, nothing, F(cos), 1.1),
(false, :stability_and_allocs, nothing, F(cos), 1.0f1),
(false, :stability_and_allocs, nothing, F(exp), 1.1),
(false, :stability_and_allocs, nothing, F(exp), 1.0f1),
]...,
)
end
map([(x) -> DI.DifferentiateWith(x, DI.AutoForwardDiff())]) do F
map([Float64, Float32]) do P
push!(
test_cases,
Any[
(
false,
:stability_and_allocs,
nothing,
F(Base.FastMath.sincos),
P(3.0),
),
(false, :none, nothing, F(Mooncake.__vec_to_tuple), [P(1.0)]),
]...,
)
end
push!(
test_cases,
Any[
(
false,
:stability_and_allocs,
nothing,
F(Mooncake.IntrinsicsWrappers.ctlz_int),
5,
),
(
false,
:stability_and_allocs,
nothing,
F(Mooncake.IntrinsicsWrappers.ctpop_int),
5,
),
(
false,
:stability_and_allocs,
nothing,
F(Mooncake.IntrinsicsWrappers.cttz_int),
5,
),
]...,
)
end
memory = Any[]
return test_cases, memory
end