-
-
Notifications
You must be signed in to change notification settings - Fork 75
Chainrules rrules for Mooncake, LinearSolve integration #801
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -99,3 +99,81 @@ function CRC.rrule(::Type{<:LinearProblem}, A, b, p; kwargs...) | |
| ∇prob(∂prob) = (NoTangent(), ∂prob.A, ∂prob.b, ∂prob.p) | ||
| return prob, ∇prob | ||
| end | ||
|
|
||
| function CRC.rrule(T::typeof(LinearSolve.init), prob::LinearSolve.LinearProblem, alg::Nothing, args...; kwargs...) | ||
| assump = OperatorAssumptions(issquare(prob.A)) | ||
| alg = defaultalg(prob.A, prob.b, assump) | ||
| CRC.rrule(T, prob, alg, args...; kwargs...) | ||
| end | ||
|
|
||
| function CRC.rrule(::typeof(LinearSolve.init), prob::LinearSolve.LinearProblem, alg::Union{LinearSolve.SciMLLinearSolveAlgorithm,Nothing}, args...; kwargs...) | ||
| init_res = LinearSolve.init(prob, alg) | ||
| function init_adjoint(∂init) | ||
| ∂prob = LinearProblem(∂init.A, ∂init.b, NoTangent()) | ||
| return NoTangent(), ∂prob, NoTangent(), ntuple((_ -> NoTangent(), length(args))...) | ||
| end | ||
|
|
||
| return init_res, init_adjoint | ||
| end | ||
|
|
||
| function CRC.rrule(T::typeof(SciMLBase.solve!), cache::LinearSolve.LinearCache, alg::Nothing, args...; kwargs...) | ||
| assump = OperatorAssumptions() | ||
| alg = defaultalg(cache.A, cache.b, assump) | ||
| CRC.rrule(T, cache, alg, args...; kwargs) | ||
| end | ||
|
|
||
| function CRC.rrule(::typeof(SciMLBase.solve!), cache::LinearSolve.LinearCache, alg::LinearSolve.SciMLLinearSolveAlgorithm, args...; alias_A=default_alias_A( | ||
|
Member
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. this is a mutating rule... is it okay to do this with CRC?
Member
Author
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. So the chainrules rrules for Mooncake are actually only used in Mooncake's derived rruleset context. So i dint face any issues with the required tests. But yes it would be safer to write Mooncake rrules here as chainrules in general don't support mutation in reverse mode. I'll try adding the changes (avoided till now due to Mooncake.fdata mutation for structs) |
||
| alg, cache.A, cache.b), kwargs...) | ||
| (; A, sensealg) = cache | ||
| @assert sensealg isa LinearSolveAdjoint "Currently only `LinearSolveAdjoint` is supported for adjoint sensitivity analysis." | ||
|
|
||
| # logic behind caching `A` and `b` for the reverse pass based on rrule above for SciMLBase.solve | ||
| if sensealg.linsolve === missing | ||
| if !(alg isa AbstractFactorization || alg isa AbstractKrylovSubspaceMethod || | ||
| alg isa DefaultLinearSolver) | ||
| A_ = alias_A ? deepcopy(A) : A | ||
| end | ||
| else | ||
| A_ = deepcopy(A) | ||
| end | ||
|
|
||
| sol = solve!(cache) | ||
| function solve!_adjoint(∂sol) | ||
| ∂∅ = NoTangent() | ||
| ∂u = ∂sol.u | ||
|
|
||
| if sensealg.linsolve === missing | ||
| λ = if cache.cacheval isa Factorization | ||
| cache.cacheval' \ ∂u | ||
| elseif cache.cacheval isa Tuple && cache.cacheval[1] isa Factorization | ||
| first(cache.cacheval)' \ ∂u | ||
| elseif alg isa AbstractKrylovSubspaceMethod | ||
| invprob = LinearProblem(adjoint(cache.A), ∂u) | ||
| solve(invprob, alg; cache.abstol, cache.reltol, cache.verbose).u | ||
| elseif alg isa DefaultLinearSolver | ||
| LinearSolve.defaultalg_adjoint_eval(cache, ∂u) | ||
| else | ||
| invprob = LinearProblem(adjoint(A_), ∂u) # We cached `A` | ||
| solve(invprob, alg; cache.abstol, cache.reltol, cache.verbose).u | ||
| end | ||
| else | ||
| invprob = LinearProblem(adjoint(A_), ∂u) # We cached `A` | ||
| λ = solve( | ||
| invprob, sensealg.linsolve; cache.abstol, cache.reltol, cache.verbose).u | ||
| end | ||
|
|
||
| tu = adjoint(sol.u) | ||
| ∂A = BroadcastArray(@~ .-(λ .* tu)) | ||
| ∂b = λ | ||
|
|
||
| if (iszero(∂b) || iszero(∂A)) && !iszero(tu) | ||
| error("Adjoint case currently not handled. Instead of using `solve!(cache); s1 = copy(cache.u) ...`, use `sol = solve!(cache); s1 = copy(sol.u)`.") | ||
| end | ||
|
|
||
| ∂prob = LinearProblem(∂A, ∂b, ∂∅) | ||
| ∂cache = LinearSolve.init(∂prob, u=∂u) | ||
| return (∂∅, ∂cache, ∂∅, ntuple(_ -> ∂∅, length(args))...) | ||
| end | ||
|
|
||
| return sol, solve!_adjoint | ||
| 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.
These are mutating, so there are no chain rules.