Flux.jl/src/tracker/back.jl

149 lines
3.0 KiB
Julia
Raw Normal View History

2018-02-07 22:20:44 +00:00
init_grad(x) = zero(x)
2018-02-12 12:31:15 +00:00
zero_grad!(x) = zero(x)
zero_grad!(x::AbstractArray) = (x .= 0)
2018-02-07 22:20:44 +00:00
2017-09-07 03:09:32 +00:00
scan(c::Call) = foreach(scan, c.args)
function scan(x::Tracked)
2018-02-12 12:31:15 +00:00
x.isleaf && return
2017-10-18 21:54:58 +00:00
ref = x.ref += 1
2017-09-07 03:09:32 +00:00
if ref == 1
scan(x.f)
2018-02-12 12:31:15 +00:00
isdefined(x, :grad) && (x.grad = zero_grad!(x.grad))
2017-09-07 03:09:32 +00:00
end
return
2017-09-07 01:21:35 +00:00
end
function scan(x)
istracked(x) && scan(tracker(x))
return
end
2018-07-06 10:28:18 +00:00
function back_(c::Call, Δ)
Δs = c.func(Δ)
2018-07-09 12:39:10 +00:00
(Δs isa Tuple && length(Δs) >= length(c.args)) ||
2018-07-06 10:28:18 +00:00
error("Gradient is not a tuple of length $(length(c.args))")
2018-07-09 18:44:14 +00:00
foreach(back, c.args, Δs)
2018-07-06 10:28:18 +00:00
end
back_(::Call{Void}, Δ) = nothing
2017-09-07 03:09:32 +00:00
2018-02-07 22:20:44 +00:00
accum!(x, Δ) = x .+ Δ
accum!(x::AbstractArray, Δ) = (x .+= Δ)
2018-02-07 20:39:36 +00:00
function back(x::Tracked, Δ)
2018-03-21 11:25:47 +00:00
x.isleaf && (x.grad = accum!(x.grad, Δ); return)
2017-10-18 21:54:58 +00:00
ref = x.ref -= 1
2018-07-09 18:44:14 +00:00
if ref > 0 || isdefined(x, :grad)
if isdefined(x, :grad)
x.grad = accum!(x.grad, Δ)
else
x.grad = Δ
end
2018-07-06 10:28:18 +00:00
ref == 0 && back_(x.f, x.grad)
2017-09-07 03:09:32 +00:00
else
2018-07-06 10:28:18 +00:00
ref == 0 && back_(x.f, Δ)
2017-09-07 03:09:32 +00:00
end
return
end
2017-09-07 01:21:35 +00:00
2018-07-09 18:44:14 +00:00
back(::Void, _) = return
2017-09-07 03:09:32 +00:00
# Interface methods
2017-12-15 16:17:45 +00:00
# TODO: if an error occurs in `back` the refcounts will be broken
# and `back` will silently fail to update.
2018-07-09 15:57:44 +00:00
# Refcounts are also probably not safe in some situations (e.g. back called
# from within a backpropagator)
2017-12-15 16:17:45 +00:00
2018-07-09 18:44:14 +00:00
function back!(x, Δ)
istracked(x) || return
2017-09-07 03:09:32 +00:00
scan(x)
2018-07-09 18:44:14 +00:00
back(tracker(x), Δ)
return
2017-09-07 03:09:32 +00:00
end
2018-07-09 15:57:44 +00:00
# Out-of-place gradients
struct Params
params::IdSet
Params(xs) = new(IdSet(xs))
end
@forward Params.params Base.start, Base.next, Base.done
struct Grads
grads::ObjectIdDict
end
Grads() = Grads(ObjectIdDict())
Base.getindex(g::Grads, x::Tracked) = g.grads[x]
function Base.getindex(g::Grads, x)
istracked(x) || error("Object not tracked: $x")
g[tracker(x)]
end
@forward Grads.grads Base.setindex!, Base.haskey
accum!(g::Grads, x, Δ) = g[x] = haskey(g, x) ? g[x] + Δ : Δ
function back_(g::Grads, c::Call, Δ)
Δs = c.func(Δ)
(Δs isa Tuple && length(Δs) >= length(c.args)) ||
error("Gradient is not a tuple of length $(length(c.args))")
2018-07-09 18:44:14 +00:00
foreach((x, Δ) -> back(g, x, Δ), c.args, Δs)
2018-07-09 15:57:44 +00:00
end
back_(g::Grads, ::Call{Void}, Δ) = nothing
function back(g::Grads, x::Tracked, Δ)
x.isleaf && (accum!(g, x, Δ); return)
ref = x.ref -= 1
if ref > 0 || haskey(g, x)
accum!(g, x, Δ)
ref == 0 && back_(g, x.f, g[x])
else
ref == 0 && back_(g, x.f, Δ)
end
return
end
2018-07-09 18:44:14 +00:00
back(::Grads, ::Void, _) = return
2018-07-09 15:57:44 +00:00
function forward(f, ps::Params)
y = f()
y, function (Δ)
g = Grads()
if istracked(y)
scan(y)
2018-07-09 18:44:14 +00:00
back(g, tracker(y), Δ)
2018-07-09 15:57:44 +00:00
end
for p in ps
haskey(g, tracker(p)) ||
(g[tracker(p)] = init_grad(data(p)))
end
return g
end
end
function forward(f, args...)
args = param.(args)
y, back = forward(() -> f(args...), Params(args))
y, Δ -> getindex.(back(Δ), args)
end
function losscheck(x)
x isa Real || error("Function output is not scalar")
isinf(x) && error("Loss is infinite")
isnan(x) && error("Loss is NaN")
end
function gradient(f, args...)
y, back = forward(f, args...)
losscheck(y)
return back(1)
end
derivative(f, x) = gradient(f, x)[1]