-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
lib.jl
311 lines (252 loc) · 8.21 KB
/
lib.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
using Base: RefValue
# Interfaces
accum() = nothing
accum(x) = x
accum(x, y) =
x === nothing ? y :
y === nothing ? x :
x + y
accum(x, y, zs...) = accum(accum(x, y), zs...)
accum(x::Tuple, y::Tuple) = accum.(x, y)
accum(x::AbstractArray, y::AbstractArray) = accum.(x, y)
@generated function accum(x::NamedTuple, y::NamedTuple)
grad(x) = x in fieldnames(y) ? :(y.$x) : :nothing
Expr(:tuple, [:($f=accum(x.$f, $(grad(f)))) for f in fieldnames(x)]...)
end
function accum(x::RefValue, y::RefValue)
@assert x === y
return x
end
# Core functions
@nograd eps, Base.eval, Core.TypeVar, Core.UnionAll
@adjoint deepcopy(x) = deepcopy(x), ȳ -> (ȳ,)
@adjoint (::Type{V})(x...) where V<:Val = V(x...), _ -> nothing
@adjoint ifelse(cond::Bool, t, f) =
ifelse(cond, t, f),
Δ -> cond ? (nothing, Δ, zero(Δ)) : (nothing, zero(Δ), Δ)
@adjoint Base.typeassert(x, T) = Base.typeassert(x, T), Δ -> (Δ, nothing)
@generated function accum_param(cx::Context, x, Δ)
isbitstype(x) && return :(Δ)
quote
if haskey(cache(cx), x)
cache(cx)[x] = accum(cache(cx)[x],Δ)
return
else
return Δ
end
end
end
function accum_global(cx::Context, ref, x̄)
(x̄ === nothing || isconst(ref.mod, ref.name)) && return
gs = cache(cx)
gs[ref] = accum(get(gs, ref, nothing), x̄)
return
end
unwrap(x) = x
@adjoint unwrap(x) = unwrap(x), x̄ -> (accum_param(__context__, x, x̄),)
unwrap(ref, x) = x
@adjoint unwrap(ref, x) = unwrap(x), function (x̄)
accum_global(__context__, ref, x̄)
(accum_param(__context__, x, x̄),)
end
function global_set(ref, val)
ccall(:jl_set_global, Cvoid, (Any, Any, Any),
ref.mod, ref.name, val)
end
@adjoint! function global_set(ref, x)
global_set(ref, x), function (x̄)
gs = cache(__context__)
x̄ = accum(get(gs, ref, nothing), x̄)
gs[ref] = nothing
return (nothing, x̄)
end
end
# Tuples
using Base: tail
@adjoint tuple(xs...) = xs, identity
@adjoint function literal_getindex(xs::NTuple{N,Any}, ::Val{i}) where {N,i}
val = xs[i]
function back(Δ)
accum_param(__context__, val, Δ) === nothing && return
return ntuple(j -> i == j ? Δ : nothing, Val(N)), nothing
end
val, back
end
@adjoint function getindex(xs::NTuple{N,Any}, i::Integer) where N
val = xs[i]
function back(Δ)
accum_param(__context__, val, Δ) === nothing && return
return ntuple(j -> i == j ? Δ : nothing, Val(N)), nothing
end
return val, back
end
@adjoint getindex(xs::NTuple{N,Any}, r::AbstractUnitRange) where N =
(xs[r], Δ -> (ntuple(j -> j in r ? Δ[findfirst(isequal(j), r)] : nothing, Val(N)), nothing))
@adjoint function getindex(xs::NTuple{N,Any}, r::AbstractVector) where N
val = xs[r]
function back(Δ)
dxs = ntuple(Val(length(xs))) do x
total = nothing
for r_i in eachindex(r)
r[r_i] === x || continue
total = accum(total, Δ[r_i])
end
return total
end
return (dxs, nothing)
end
val, back
end
function _pullback(cx::AContext, ::typeof(literal_indexed_iterate), xs::Tuple, ::Val{i}) where i
y, b = _pullback(cx, literal_getindex, xs, Val(i))
back(::Nothing) = nothing
back(ȳ) = b(ȳ[1])
(y, i+1), back
end
function _pullback(cx::AContext, ::typeof(literal_indexed_iterate), xs::Tuple, ::Val{i}, st) where i
y, b = _pullback(cx, literal_getindex, xs, Val(i))
back(::Nothing) = nothing
back(ȳ) = (b(ȳ[1])..., nothing)
(y, i+1), back
end
# Needed for iteration lowering
@adjoint Core.getfield(xs::NTuple{N,Any}, i::Int) where N =
(xs[i], Δ -> (ntuple(j -> i == j ? Δ : nothing, Val(N)), nothing))
@adjoint Core.getfield(xs::NamedTuple{K,<:NTuple{N,Any}}, i::Int) where {K,N} =
(xs[i], Δ -> (NamedTuple{K}(ntuple(j -> i == j ? Δ : nothing, Val(N))), nothing))
@adjoint function Base.first(xs::Tuple)
drest = map(_->nothing, tail(xs))
first(xs), Δ -> ((Δ, drest...),)
end
@adjoint Base.tail(xs::Tuple) = tail(xs), x̄s -> ((nothing, x̄s...),)
_empty(x) = length(x)
_empty(x::Union{Tuple,NamedTuple}) = map(_->nothing, x)
_unapply(t::Integer, xs) = xs[1:t], xs[t+1:end]
_unapply(t, xs) = first(xs), tail(xs)
_unapply(t::Tuple{}, xs) = (), xs
function _unapply(t::Tuple, xs)
t1, xs1 = _unapply(first(t), xs)
t2, xs2 = _unapply(tail(t), xs1)
(t1, t2...), xs2
end
function _unapply(t::NamedTuple{K}, xs) where K
t, rst = _unapply(Tuple(t), xs)
NamedTuple{K}(t), rst
end
unapply(t, xs) = _unapply(t, xs)[1]
@adjoint! function Core._apply(f, args...)
y, back = Core._apply(_pullback, (__context__, f), args...)
st = map(_empty, args)
y, function (Δ)
Δ = back(Δ)
Δ === nothing ? nothing :
(first(Δ), unapply(st, Base.tail(Δ))...)
end
end
if VERSION >= v"1.4.0-DEV.304"
@adjoint! function Core._apply_iterate(::typeof(iterate), f, args...)
y, back = Core._apply(_pullback, (__context__, f), args...)
st = map(_empty, args)
y, function (Δ)
Δ = back(Δ)
Δ === nothing ? nothing :
(nothing, first(Δ), unapply(st, Base.tail(Δ))...)
end
end
end
# Structs
deref!(x) = x
function deref!(x::Ref)
d = x[]
x[] = nothing
return d
end
@generated nt_nothing(x) = Expr(:tuple, [:($f=nothing) for f in fieldnames(x)]...)
@generated pair(::Val{k}, v, _=nothing) where k = :($k = v,)
@generated pair(::Val{k}, v, ::NamedTuple{keys}) where {k,keys} = k isa Int ? :($(getfield(keys, k)) = v,) : :($k = v,)
@adjoint function literal_getfield(x, ::Val{f}) where f
val = getfield(x, f)
function back(Δ)
accum_param(__context__, val, Δ) === nothing && return
if isimmutable(x)
((; nt_nothing(x)..., pair(Val(f), Δ, x)...), nothing)
else
dx = grad_mut(__context__, x)
dx[] = (; dx[]..., pair(Val(f), accum(getfield(dx[], f), Δ))...)
return (dx,nothing)
end
end
unwrap(val), back
end
_pullback(cx::AContext, ::typeof(getfield), x, field_name::Symbol) =
_pullback(cx, literal_getfield, x, Val(field_name))
function _pullback(cx::AContext, ::typeof(literal_getproperty), x::NamedTuple,
::Val{property_name}) where {property_name}
return _pullback(cx, literal_getfield, x, Val(property_name))
end
function _pullback(cx::AContext, ::typeof(literal_getindex), x::NamedTuple,
::Val{key}) where {key}
return _pullback(cx, literal_getfield, x, Val(key))
end
function _pullback(cx::AContext, ::typeof(literal_getproperty), x::Tuple,
::Val{index}) where {index}
return _pullback(cx, literal_getindex, x, Val(index))
end
function _pullback(cx::AContext, ::typeof(literal_getfield), x::Tuple,
::Val{index}) where {index}
return _pullback(cx, literal_getindex, x, Val(index))
end
grad_mut(x) = Ref{Any}(nt_nothing(x))
function grad_mut(cx::Context, x)
ch = cache(cx)
if haskey(ch, x)
ch[x]
else
ch[x] = grad_mut(x)
end
end
@adjoint! function setfield!(x, f, val)
y = setfield!(x, f, val)
g = grad_mut(__context__, x)
y, function (_)
Δ = getfield(g[], f)
g[] = (;g[]...,pair(Val(f),nothing)...)
(nothing, nothing, Δ)
end
end
struct Jnew{T,G,splat}
g::G
end
Jnew{T}(g) where T = Jnew{T,typeof(g)}(g)
@adjoint! function __new__(T, args...)
x = __new__(T, args...)
g = !T.mutable || fieldcount(T) == 0 ? nothing : grad_mut(__context__, x)
x, Jnew{T,typeof(g),false}(g)
end
@adjoint! function __splatnew__(T, args)
x = __splatnew__(T, args)
g = !T.mutable || fieldcount(T) == 0 ? nothing : grad_mut(__context__, x)
x, Jnew{T,typeof(g),true}(g)
end
# TODO captured mutables + multiple calls to `back`
@generated function (back::Jnew{T,G,false})(Δ::Union{NamedTuple,Nothing,RefValue}) where {T,G}
!T.mutable && Δ == Nothing && return :nothing
Δ = G == Nothing ? :Δ :
Δ <: RefValue ? :(back.g[]) :
:(accum(back.g[], Δ))
quote
x̄ = $Δ
$(G == Nothing || :(back.g[] = nt_nothing($Δ)))
(nothing, $(map(f -> :(x̄.$f), fieldnames(T))...))
end
end
@generated function (back::Jnew{T,G,true})(Δ::Union{NamedTuple,Nothing,RefValue}) where {T,G}
!T.mutable && Δ == Nothing && return :nothing
Δ = G == Nothing ? :Δ : :(back.g)
quote
x̄ = $Δ
$(G == Nothing || :($Δ = nt_nothing($Δ)))
(nothing, ($(map(f -> :(x̄.$f), fieldnames(T))...),))
end
end
(back::Jnew{T})(Δ) where T = error("Need an adjoint for constructor $T. Gradient is of type $(typeof(Δ))")