-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add simplenonlinearsolve AD specific dispatches
- Loading branch information
Showing
7 changed files
with
168 additions
and
2 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
23 changes: 23 additions & 0 deletions
23
lib/SimpleNonlinearSolve/ext/SimpleNonlinearSolveChainRulesCoreExt.jl
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,23 @@ | ||
module SimpleNonlinearSolveChainRulesCoreExt | ||
|
||
using ChainRulesCore: ChainRulesCore, NoTangent | ||
using NonlinearSolveBase: ImmutableNonlinearProblem | ||
using SciMLBase: ChainRulesOriginator, NonlinearLeastSquaresProblem | ||
|
||
using SimpleNonlinearSolve: SimpleNonlinearSolve, simplenonlinearsolve_solve_up, | ||
solve_adjoint | ||
|
||
function ChainRulesCore.rrule(::typeof(simplenonlinearsolve_solve_up), | ||
prob::Union{InternalNonlinearProblem, NonlinearLeastSquaresProblem}, | ||
sensealg, u0, u0_changed, p, p_changed, alg, args...; kwargs...) | ||
out, ∇internal = solve_adjoint( | ||
prob, sensealg, u0, p, ChainRulesOriginator(), alg, args...; kwargs...) | ||
function ∇simplenonlinearsolve_solve_up(Δ) | ||
∂f, ∂prob, ∂sensealg, ∂u0, ∂p, _, ∂args... = ∇internal(Δ) | ||
return ( | ||
∂f, ∂prob, ∂sensealg, ∂u0, NoTangent(), ∂p, NoTangent(), NoTangent(), ∂args...) | ||
end | ||
return out, ∇simplenonlinearsolve_solve_up | ||
end | ||
|
||
end |
11 changes: 11 additions & 0 deletions
11
lib/SimpleNonlinearSolve/ext/SimpleNonlinearSolveDiffEqBaseExt.jl
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,11 @@ | ||
module SimpleNonlinearSolveDiffEqBaseExt | ||
|
||
using DiffEqBase: DiffEqBase | ||
|
||
using SimpleNonlinearSolve: SimpleNonlinearSolve | ||
|
||
function SimpleNonlinearSolve.solve_adjoint_internal(args...; kwargs...) | ||
return DiffEqBase._solve_adjoint(args...; kwargs...) | ||
end | ||
|
||
end |
37 changes: 37 additions & 0 deletions
37
lib/SimpleNonlinearSolve/ext/SimpleNonlinearSolveReverseDiffExt.jl
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,37 @@ | ||
module SimpleNonlinearSolveReverseDiffExt | ||
|
||
using ArrayInterface: ArrayInterface | ||
using NonlinearSolveBase: ImmutableNonlinearProblem | ||
using ReverseDiff: ReverseDiff, TrackedArray, TrackedReal | ||
using SciMLBase: ReverseDiffOriginator, NonlinearLeastSquaresProblem, remake | ||
|
||
using SimpleNonlinearSolve: SimpleNonlinearSolve, solve_adjoint | ||
|
||
for pType in (InternalNonlinearProblem, NonlinearLeastSquaresProblem) | ||
aTypes = (TrackedArray, AbstractArray{<:TrackedReal}, Any) | ||
for (uT, pT) in collect(Iterators.product(aTypes, aTypes))[1:(end - 1)] | ||
@eval function SimpleNonlinearSolve.simplenonlinearsolve_solve_up( | ||
prob::$(pType), sensealg, u0::$(uT), u0_changed, | ||
p::$(pT), p_changed, alg, args...; kwargs...) | ||
return ReverseDiff.track(SimpleNonlinearSolve.simplenonlinearsolve_solve_up, | ||
prob, sensealg, ArrayInterface.aos_to_soa(u0), true, | ||
ArrayInterface.aos_to_soa(p), true, alg, args...; kwargs...) | ||
end | ||
end | ||
|
||
@eval ReverseDiff.@grad function SimpleNonlinearSolve.simplenonlinearsolve_solve_up( | ||
tprob::$(pType), sensealg, tu0, u0_changed, | ||
tp, p_changed, alg, args...; kwargs...) | ||
u0, p = ReverseDiff.value(tu0), ReverseDiff.value(tp) | ||
prob = remake(tprob; u0, p) | ||
out, ∇internal = solve_adjoint( | ||
prob, sensealg, u0, p, ReverseDiffOriginator(), alg, args...; kwargs...) | ||
|
||
function ∇simplenonlinearsolve_solve_up(Δ...) | ||
∂prob, ∂sensealg, ∂u0, ∂p, _, ∂args... = ∇internal(Δ...) | ||
return (∂prob, ∂sensealg, ∂u0, nothing, ∂p, nothing, nothing, ∂args...) | ||
end | ||
end | ||
end | ||
|
||
end |
37 changes: 37 additions & 0 deletions
37
lib/SimpleNonlinearSolve/ext/SimpleNonlinearSolveTrackerExt.jl
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,37 @@ | ||
module SimpleNonlinearSolveTrackerExt | ||
|
||
using ArrayInterface: ArrayInterface | ||
using NonlinearSolveBase: ImmutableNonlinearProblem | ||
using SciMLBase: TrackerOriginator, NonlinearLeastSquaresProblem, remake | ||
using Tracker: Tracker, TrackedArray, TrackedReal | ||
|
||
using SimpleNonlinearSolve: SimpleNonlinearSolve, solve_adjoint | ||
|
||
for pType in (InternalNonlinearProblem, NonlinearLeastSquaresProblem) | ||
aTypes = (TrackedArray, AbstractArray{<:TrackedReal}, Any) | ||
for (uT, pT) in collect(Iterators.product(aTypes, aTypes))[1:(end - 1)] | ||
@eval function SimpleNonlinearSolve.simplenonlinearsolve_solve_up( | ||
prob::$(pType), sensealg, u0::$(uT), u0_changed, | ||
p::$(pT), p_changed, alg, args...; kwargs...) | ||
return Tracker.track(SimpleNonlinearSolve.simplenonlinearsolve_solve_up, prob, | ||
sensealg, ArrayInterface.aos_to_soa(u0), true, | ||
ArrayInterface.aos_to_soa(p), true, alg, args...; kwargs...) | ||
end | ||
end | ||
|
||
@eval Tracker.@grad function SimpleNonlinearSolve.simplenonlinearsolve_solve_up( | ||
tprob::$(pType), sensealg, tu0, u0_changed, | ||
tp, p_changed, alg, args...; kwargs...) | ||
u0, p = Tracker.data(tu0), Tracker.data(tp) | ||
prob = remake(tprob; u0, p) | ||
out, ∇internal = solve_adjoint( | ||
prob, sensealg, u0, p, TrackerOriginator(), alg, args...; kwargs...) | ||
|
||
function ∇simplenonlinearsolve_solve_up(Δ) | ||
∂prob, ∂sensealg, ∂u0, ∂p, _, ∂args... = ∇internal(Tracker.data(Δ)) | ||
return (∂prob, ∂sensealg, ∂u0, nothing, ∂p, nothing, nothing, ∂args...) | ||
end | ||
end | ||
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