Skip to content

Adapt to pending Enzyme breaking change #539

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

Merged
2 commits merged into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ ChainRulesCore = "1.22"
ConcreteStructs = "0.2.3"
DocStringExtensions = "0.9.3"
EnumX = "1.0.4"
Enzyme = "0.11.15, 0.12, 0.13"
EnzymeCore = "0.6.5, 0.7, 0.8"
Enzyme = "0.13"
EnzymeCore = "0.8"
FastAlmostBandedMatrices = "0.1"
FastLapackInterface = "2"
FiniteDiff = "2.22"
Expand Down
40 changes: 30 additions & 10 deletions ext/LinearSolveEnzymeExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ using Enzyme

using EnzymeCore

function EnzymeCore.EnzymeRules.forward(
function EnzymeCore.EnzymeRules.forward(config::ConfigWidth{1},
func::Const{typeof(LinearSolve.init)}, ::Type{RT}, prob::EnzymeCore.Annotation{LP},
alg::Const; kwargs...) where {RT, LP <: LinearSolve.LinearProblem}
@assert !(prob isa Const)
res = func.val(prob.val, alg.val; kwargs...)
if RT <: Const
return res
if EnzymeRules.needs_primal(config)
return res
else
return nothing
end
end
dres = func.val(prob.dval, alg.val; kwargs...)
dres.b .= res.b == dres.b ? zero(dres.b) : dres.b
Expand All @@ -25,17 +29,31 @@ function EnzymeCore.EnzymeRules.forward(
return Duplicated(res, dres)
end
error("Unsupported return type $RT")

if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
Duplicated(res, dres)
elseif EnzymeRules.needs_shadow(config)
dres
elseif EnzymeRules.needs_primal(config)
res
else
nothing
end
end

function EnzymeCore.EnzymeRules.forward(func::Const{typeof(LinearSolve.solve!)},
function EnzymeCore.EnzymeRules.forward(config::ConfigWidth{1}, func::Const{typeof(LinearSolve.solve!)},
::Type{RT}, linsolve::EnzymeCore.Annotation{LP};
kwargs...) where {RT, LP <: LinearSolve.LinearCache}
@assert !(linsolve isa Const)

res = func.val(linsolve.val; kwargs...)

if RT <: Const
return res
if EnzymeRules.needs_primal(config)
return res
else
return nothing
end
end
if linsolve.val.alg isa LinearSolve.AbstractKrylovSubspaceMethod
error("Algorithm $(_linsolve.alg) is currently not supported by Enzyme rules on LinearSolve.jl. Please open an issue on LinearSolve.jl detailing which algorithm is missing the adjoint handling")
Expand All @@ -50,13 +68,15 @@ function EnzymeCore.EnzymeRules.forward(func::Const{typeof(LinearSolve.solve!)},

linsolve.val.b = b

if RT <: DuplicatedNoNeed
return dres
elseif RT <: Duplicated
return Duplicated(res, dres)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
Duplicated(res, dres)
elseif EnzymeRules.needs_shadow(config)
dres
elseif EnzymeRules.needs_primal(config)
res
else
nothing
end

return Duplicated(res, dres)
end

function EnzymeCore.EnzymeRules.augmented_primal(
Expand Down
Loading