-
-
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 #123 from avik-pal/ap/fix_adjoint
Patch Adjoint Sensitivity for Simple Nonlinear Solve Algorithms
- Loading branch information
Showing
6 changed files
with
58 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module SimpleNonlinearSolveChainRulesCoreExt | ||
|
||
using ChainRulesCore, DiffEqBase, SciMLBase, SimpleNonlinearSolve | ||
|
||
# The expectation here is that no-one is using this directly inside a GPU kernel. We can | ||
# eventually lift this requirement using a custom adjoint | ||
function ChainRulesCore.rrule(::typeof(SimpleNonlinearSolve.__internal_solve_up), | ||
prob::NonlinearProblem, | ||
sensealg::Union{Nothing, DiffEqBase.AbstractSensitivityAlgorithm}, u0, u0_changed, | ||
p, p_changed, alg, args...; kwargs...) | ||
out, ∇internal = DiffEqBase._solve_adjoint(prob, sensealg, u0, p, | ||
SciMLBase.ChainRulesOriginator(), alg, args...; kwargs...) | ||
function ∇__internal_solve_up(Δ) | ||
∂f, ∂prob, ∂sensealg, ∂u0, ∂p, ∂originator, ∂args... = ∇internal(Δ) | ||
return (∂f, ∂prob, ∂sensealg, ∂u0, NoTangent(), ∂p, NoTangent(), ∂originator, | ||
∂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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using ForwardDiff, SciMLSensitivity, SimpleNonlinearSolve, Test, Zygote | ||
|
||
@testset "Simple Adjoint Test" begin | ||
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) | ||
end | ||
|
||
p = [3.0, 2.0] | ||
|
||
@test only(Zygote.gradient(solve_nlprob, p)) ≈ ForwardDiff.gradient(solve_nlprob, p) | ||
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