-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReverseDiff support #144
base: main
Are you sure you want to change the base?
ReverseDiff support #144
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
if isdefined(ChainRulesCore, :NoTangent) | ||
const NoTangent = ChainRulesCore.NoTangent | ||
else | ||
|
@@ -176,6 +175,60 @@ end | |
_returns_scalar(::AbstractPenalty) = True() | ||
_returns_scalar(sc::SimpleChain) = has_loss_typed(sc) | ||
|
||
function ChainRulesCore.rrule(sc::Chain, arg, params) | ||
_rrule(sc, arg, params, _returns_scalar(sc)) | ||
function ChainRulesCore.rrule(::typeof(call_chain), sc::Chain, arg, params) | ||
v, pb = _rrule(sc, arg, params, _returns_scalar(sc)) | ||
return v, Δ -> (NoTangent(), pb(collect(Δ))...) | ||
end | ||
function call_chain(sc::SimpleChain, arg::AbstractArray, params::ReverseDiff.TrackedVector) | ||
return ReverseDiff.track(call_chain, sc, arg, params) | ||
end | ||
function call_chain(sc::SimpleChain, arg::AbstractArray, params::SubArray{<:ReverseDiff.TrackedReal, 1}) | ||
return ReverseDiff.track(call_chain, sc, arg, params) | ||
end | ||
function call_chain(sc::SimpleChain, arg::ReverseDiff.TrackedArray, params::ReverseDiff.AbstractVector) | ||
return ReverseDiff.track(call_chain, sc, arg, params) | ||
end | ||
function call_chain(sc::SimpleChain, arg::ReverseDiff.TrackedArray, params::ReverseDiff.TrackedVector) | ||
return ReverseDiff.track(call_chain, sc, arg, params) | ||
end | ||
function call_chain(sc::SimpleChain, arg::ReverseDiff.TrackedArray, params::SubArray{<:ReverseDiff.TrackedReal, 1}) | ||
return ReverseDiff.track(call_chain, sc, arg, params) | ||
end | ||
ReverseDiff.@grad function call_chain(sc::Chain, arg::AbstractArray, params::AbstractVector) | ||
argv = ReverseDiff.value(arg) | ||
paramsv = ReverseDiff.value(params) | ||
v, pb = _rrule(sc, argv, paramsv, _returns_scalar(sc)) | ||
return v, Δ -> begin | ||
_Δ = Base.tail(pb(collect(Δ))) | ||
_Δ = Base.tail(pb(collect(Δ))) | ||
(nothing, _Δ...) | ||
end | ||
end | ||
|
||
function params_rrule(Δ) | ||
n = sum(x -> sum(length, x), Δ) | ||
T = eltype(first(first(Δ))) | ||
v = zeros(T, n) | ||
offset = 0 | ||
for x in Δ | ||
for y in x | ||
if y isa Real | ||
v[offset + 1] = y | ||
offset += 1 | ||
else | ||
l = length(y) | ||
v[offset + 1 : offset + l] = vec(y) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that This looks performance sensitive. Could use an But also, why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was called in the log prior calculation. |
||
offset += l | ||
end | ||
end | ||
end | ||
return v | ||
end | ||
function ChainRulesCore.rrule(::typeof(params), sc::SimpleChain, p::AbstractVector) | ||
return params(sc, p), Δ -> (NoTangent(), NoTangent(), params_rrule(Δ)) | ||
end | ||
params(sc::SimpleChain, p::ReverseDiff.TrackedArray) = ReverseDiff.track(params, sc, p) | ||
params(sc::SimpleChain, p::SubArray{<:ReverseDiff.TrackedReal, 1}) = ReverseDiff.track(params, sc, p) | ||
ReverseDiff.@grad function params(sc::SimpleChain, p::AbstractArray) | ||
return params(sc, p), Δ -> (nothing, params_rrule(Δ)) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you comment this line out, you get the correct gradient.
deepcopy
ing everything didn't fix this so I am not sure what's going on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So call it once, it works. Call it twice, it breaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deepcopy
isn't likely to help on pointers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps the
_rrule
function should be a struct which stores the initial pointer address and starts from there every time it's called