-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from SciML/ap/reversediff
Automatic Differentiation
- Loading branch information
Showing
8 changed files
with
132 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module SimpleNonlinearSolveReverseDiffExt | ||
|
||
using ArrayInterface, DiffEqBase, ReverseDiff, SciMLBase, SimpleNonlinearSolve | ||
import ReverseDiff: TrackedArray, TrackedReal | ||
import SimpleNonlinearSolve: __internal_solve_up | ||
|
||
function __internal_solve_up( | ||
prob::NonlinearProblem, sensealg, u0::TrackedArray, u0_changed, | ||
p::TrackedArray, p_changed, alg, args...; kwargs...) | ||
return ReverseDiff.track(__internal_solve_up, prob, sensealg, u0, | ||
u0_changed, p, p_changed, alg, args...; kwargs...) | ||
end | ||
|
||
function __internal_solve_up( | ||
prob::NonlinearProblem, sensealg, u0, u0_changed, | ||
p::TrackedArray, p_changed, alg, args...; kwargs...) | ||
return ReverseDiff.track(__internal_solve_up, prob, sensealg, u0, | ||
u0_changed, p, p_changed, alg, args...; kwargs...) | ||
end | ||
|
||
function __internal_solve_up( | ||
prob::NonlinearProblem, sensealg, u0::TrackedArray, u0_changed, | ||
p, p_changed, alg, args...; kwargs...) | ||
return ReverseDiff.track(__internal_solve_up, prob, sensealg, u0, | ||
u0_changed, p, p_changed, alg, args...; kwargs...) | ||
end | ||
|
||
function __internal_solve_up(prob::NonlinearProblem, sensealg, | ||
u0::AbstractArray{<:TrackedReal}, u0_changed, p::AbstractArray{<:TrackedReal}, | ||
p_changed, alg, args...; kwargs...) | ||
return __internal_solve_up( | ||
prob, sensealg, ArrayInterface.aos_to_soa(u0), true, | ||
ArrayInterface.aos_to_soa(p), true, alg, args...; kwargs...) | ||
end | ||
|
||
function __internal_solve_up(prob::NonlinearProblem, sensealg, u0, u0_changed, | ||
p::AbstractArray{<:TrackedReal}, p_changed, alg, args...; kwargs...) | ||
return __internal_solve_up( | ||
prob, sensealg, u0, true, ArrayInterface.aos_to_soa(p), true, alg, args...; kwargs...) | ||
end | ||
|
||
function __internal_solve_up(prob::NonlinearProblem, sensealg, | ||
u0::AbstractArray{<:TrackedReal}, u0_changed, p, p_changed, alg, args...; kwargs...) | ||
return __internal_solve_up( | ||
prob, sensealg, u0, true, ArrayInterface.aos_to_soa(p), true, alg, args...; kwargs...) | ||
end | ||
|
||
ReverseDiff.@grad function __internal_solve_up( | ||
prob::NonlinearProblem, sensealg, u0, u0_changed, p, p_changed, alg, args...; kwargs...) | ||
out, ∇internal = DiffEqBase._solve_adjoint( | ||
prob, sensealg, ReverseDiff.value(u0), ReverseDiff.value(p), | ||
SciMLBase.ReverseDiffOriginator(), alg, args...; kwargs...) | ||
function ∇__internal_solve_up(_args...) | ||
∂prob, ∂sensealg, ∂u0, ∂p, ∂originator, ∂args... = ∇internal(_args...) | ||
return (∂prob, ∂sensealg, ∂u0, nothing, ∂p, nothing, nothing, ∂args...) | ||
end | ||
return Array(out), ∇__internal_solve_up | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module SimpleNonlinearSolveTrackerExt | ||
|
||
using DiffEqBase, SciMLBase, SimpleNonlinearSolve, Tracker | ||
|
||
function SimpleNonlinearSolve.__internal_solve_up(prob::NonlinearProblem, | ||
sensealg, u0::TrackedArray, u0_changed, p, p_changed, alg, args...; kwargs...) | ||
return Tracker.track( | ||
SimpleNonlinearSolve.__internal_solve_up, prob, sensealg, u0, u0_changed, | ||
p, p_changed, alg, args...; kwargs...) | ||
end | ||
|
||
function SimpleNonlinearSolve.__internal_solve_up( | ||
prob::NonlinearProblem, sensealg, u0::TrackedArray, u0_changed, | ||
p::TrackedArray, p_changed, alg, args...; kwargs...) | ||
return Tracker.track( | ||
SimpleNonlinearSolve.__internal_solve_up, prob, sensealg, u0, u0_changed, | ||
p, p_changed, alg, args...; kwargs...) | ||
end | ||
|
||
function SimpleNonlinearSolve.__internal_solve_up(prob::NonlinearProblem, | ||
sensealg, u0, u0_changed, p::TrackedArray, p_changed, alg, args...; kwargs...) | ||
return Tracker.track( | ||
SimpleNonlinearSolve.__internal_solve_up, prob, sensealg, u0, u0_changed, | ||
p, p_changed, alg, args...; kwargs...) | ||
end | ||
|
||
Tracker.@grad function SimpleNonlinearSolve.__internal_solve_up(_prob::NonlinearProblem, | ||
sensealg, u0_, u0_changed, p_, p_changed, alg, args...; kwargs...) | ||
u0, p = Tracker.data(u0_), Tracker.data(p_) | ||
prob = remake(_prob; u0, p) | ||
out, ∇internal = DiffEqBase._solve_adjoint(prob, sensealg, u0, p, | ||
SciMLBase.TrackerOriginator(), alg, args...; kwargs...) | ||
|
||
function ∇__internal_solve_up(Δ) | ||
∂prob, ∂sensealg, ∂u0, ∂p, ∂originator, ∂args... = ∇internal(Δ) | ||
return (∂prob, ∂sensealg, ∂u0, nothing, ∂p, nothing, nothing, ∂args...) | ||
end | ||
|
||
return out, ∇__internal_solve_up | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
@testitem "Simple Adjoint Test" begin | ||
using ForwardDiff, SciMLSensitivity, Zygote | ||
using ForwardDiff, ReverseDiff, SciMLSensitivity, Tracker, Zygote | ||
|
||
ff(u, p) = u .^ 2 .- p | ||
|
||
function solve_nlprob(p) | ||
prob = NonlinearProblem{false}(ff, [1.0, 2.0], p) | ||
return sum(abs2, solve(prob, SimpleNewtonRaphson()).u) | ||
sol = solve(prob, SimpleNewtonRaphson()) | ||
res = sol isa AbstractArray ? sol : sol.u | ||
return sum(abs2, res) | ||
end | ||
|
||
p = [3.0, 2.0] | ||
|
||
@test only(Zygote.gradient(solve_nlprob, p)) ≈ ForwardDiff.gradient(solve_nlprob, p) | ||
∂p_zygote = only(Zygote.gradient(solve_nlprob, p)) | ||
∂p_forwarddiff = ForwardDiff.gradient(solve_nlprob, p) | ||
∂p_tracker = Tracker.data(only(Tracker.gradient(solve_nlprob, p))) | ||
∂p_reversediff = ReverseDiff.gradient(solve_nlprob, p) | ||
|
||
@test ∂p_zygote ≈ ∂p_forwarddiff ≈ ∂p_tracker ≈ ∂p_reversediff | ||
end |
5e0f188
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.
@JuliaRegistrator register
5e0f188
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.
Registration pull request created: JuliaRegistries/General/102673
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: