-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Jacobian-Free Krylov Versions for TR/LM/GN #282
Changes from all commits
59a8713
8f739fb
5052a6e
77164c7
8f70a4e
b79228d
6c52956
bcfcc16
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
style = "sciml" | ||
format_markdown = true | ||
annotate_untyped_fields_with_any = false | ||
format_docstrings = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module NonlinearSolveZygoteExt | ||
|
||
import NonlinearSolve, Zygote | ||
|
||
NonlinearSolve.is_extension_loaded(::Val{:Zygote}) = true | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
@concrete struct KrylovJᵀJ | ||
JᵀJ | ||
Jᵀ | ||
end | ||
|
||
SciMLBase.isinplace(JᵀJ::KrylovJᵀJ) = isinplace(JᵀJ.Jᵀ) | ||
|
||
sparsity_detection_alg(_, _) = NoSparsityDetection() | ||
function sparsity_detection_alg(f, ad::AbstractSparseADType) | ||
if f.sparsity === nothing | ||
|
@@ -54,7 +61,7 @@ | |
# NOTE: The deepcopy is needed here since we are using the resid_prototype elsewhere | ||
fu = f.resid_prototype === nothing ? (iip ? _mutable_zero(u) : _mutable(f(u, p))) : | ||
(iip ? deepcopy(f.resid_prototype) : f.resid_prototype) | ||
if !has_analytic_jac && (linsolve_needs_jac || alg_wants_jac || needsJᵀJ) | ||
if !has_analytic_jac && (linsolve_needs_jac || alg_wants_jac) | ||
sd = sparsity_detection_alg(f, alg.ad) | ||
ad = alg.ad | ||
jac_cache = iip ? sparse_jacobian_cache(ad, sd, uf, fu, _maybe_mutable(u, ad)) : | ||
|
@@ -63,12 +70,10 @@ | |
jac_cache = nothing | ||
end | ||
|
||
# FIXME: To properly support needsJᵀJ without Jacobian, we need to implement | ||
# a reverse diff operation with the seed being `Jx`, this is not yet implemented | ||
J = if !(linsolve_needs_jac || alg_wants_jac || needsJᵀJ) | ||
J = if !(linsolve_needs_jac || alg_wants_jac) | ||
if f.jvp === nothing | ||
# We don't need to construct the Jacobian | ||
JacVec(uf, u; autodiff = __get_nonsparse_ad(alg.ad)) | ||
JacVec(uf, u; fu, autodiff = __get_nonsparse_ad(alg.ad)) | ||
else | ||
if iip | ||
jvp = (_, u, v) -> (du = similar(fu); f.jvp(du, v, u, p); du) | ||
|
@@ -92,9 +97,9 @@ | |
du = _mutable_zero(u) | ||
|
||
if needsJᵀJ | ||
JᵀJ = __init_JᵀJ(J) | ||
# FIXME: This needs to be handled better for JacVec Operator | ||
Jᵀfu = J' * _vec(fu) | ||
JᵀJ, Jᵀfu = __init_JᵀJ(J, _vec(fu), uf, u; f, | ||
vjp_autodiff = __get_nonsparse_ad(_getproperty(alg, Val(:vjp_autodiff))), | ||
jvp_autodiff = __get_nonsparse_ad(alg.ad)) | ||
end | ||
|
||
if linsolve_init | ||
|
@@ -120,21 +125,68 @@ | |
nothing)..., weight) | ||
return init(linprob, alg.linsolve; alias_A = true, alias_b = true, Pl, Pr) | ||
end | ||
__setup_linsolve(A::KrylovJᵀJ, b, u, p, alg) = __setup_linsolve(A.JᵀJ, b, u, p, alg) | ||
|
||
__get_nonsparse_ad(::AutoSparseForwardDiff) = AutoForwardDiff() | ||
__get_nonsparse_ad(::AutoSparseFiniteDiff) = AutoFiniteDiff() | ||
__get_nonsparse_ad(::AutoSparseZygote) = AutoZygote() | ||
__get_nonsparse_ad(ad) = ad | ||
|
||
__init_JᵀJ(J::Number) = zero(J) | ||
__init_JᵀJ(J::AbstractArray) = J' * J | ||
__init_JᵀJ(J::StaticArray) = MArray{Tuple{size(J, 2), size(J, 2)}, eltype(J)}(undef) | ||
__init_JᵀJ(J::Number, args...; kwargs...) = zero(J), zero(J) | ||
function __init_JᵀJ(J::AbstractArray, fu, args...; kwargs...) | ||
JᵀJ = J' * J | ||
Jᵀfu = J' * fu | ||
return JᵀJ, Jᵀfu | ||
end | ||
function __init_JᵀJ(J::StaticArray, fu, args...; kwargs...) | ||
JᵀJ = MArray{Tuple{size(J, 2), size(J, 2)}, eltype(J)}(undef) | ||
return JᵀJ, J' * fu | ||
end | ||
function __init_JᵀJ(J::FunctionOperator, fu, uf, u, args...; f = nothing, | ||
vjp_autodiff = nothing, jvp_autodiff = nothing, kwargs...) | ||
# FIXME: Proper fix to this requires the FunctionOperator patch | ||
if f !== nothing && f.vjp !== nothing | ||
@warn "Currently we don't make use of user provided `jvp`. This is planned to be \ | ||
fixed in the near future." | ||
end | ||
autodiff = __concrete_vjp_autodiff(vjp_autodiff, jvp_autodiff, uf) | ||
Jᵀ = VecJac(uf, u; fu, autodiff) | ||
JᵀJ_op = SciMLOperators.cache_operator(Jᵀ * J, u) | ||
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 operator shouldn't need to be constructed. 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. without doing the cache thing, it complained that for in place operations we need to run set the cache (something along those lines) |
||
JᵀJ = KrylovJᵀJ(JᵀJ_op, Jᵀ) | ||
Jᵀfu = Jᵀ * fu | ||
return JᵀJ, Jᵀfu | ||
end | ||
|
||
function __concrete_vjp_autodiff(vjp_autodiff, jvp_autodiff, uf) | ||
if vjp_autodiff === nothing | ||
if isinplace(uf) | ||
# VecJac can be only FiniteDiff | ||
return AutoFiniteDiff() | ||
else | ||
# Short circuit if we see that FiniteDiff was used for J computation | ||
jvp_autodiff isa AutoFiniteDiff && return jvp_autodiff | ||
# Check if Zygote is loaded then use Zygote else use FiniteDiff | ||
is_extension_loaded(Val{:Zygote}()) && return AutoZygote() | ||
return AutoFiniteDiff() | ||
end | ||
else | ||
ad = __get_nonsparse_ad(vjp_autodiff) | ||
if isinplace(uf) && ad isa AutoZygote | ||
@warn "Attempting to use Zygote.jl for linesearch on an in-place problem. \ | ||
Falling back to finite differencing." | ||
return AutoFiniteDiff() | ||
end | ||
return ad | ||
end | ||
end | ||
|
||
__maybe_symmetric(x) = Symmetric(x) | ||
__maybe_symmetric(x::Number) = x | ||
# LinearSolve with `nothing` doesn't dispatch correctly here | ||
__maybe_symmetric(x::StaticArray) = x | ||
__maybe_symmetric(x::SparseArrays.AbstractSparseMatrix) = x | ||
__maybe_symmetric(x::SciMLOperators.AbstractSciMLOperator) = x | ||
__maybe_symmetric(x::KrylovJᵀJ) = x.JᵀJ | ||
|
||
## Special Handling for Scalars | ||
function jacobian_caches(alg::AbstractNonlinearSolveAlgorithm, f::F, u::Number, p, | ||
|
@@ -145,3 +197,37 @@ | |
needsJᵀJ && return uf, nothing, u, nothing, nothing, u, u, u | ||
return uf, nothing, u, nothing, nothing, u | ||
end | ||
|
||
function __update_JᵀJ!(iip::Val, cache, sym::Symbol, J) | ||
return __update_JᵀJ!(iip, cache, sym, getproperty(cache, sym), J) | ||
end | ||
__update_JᵀJ!(::Val{false}, cache, sym::Symbol, _, J) = setproperty!(cache, sym, J' * J) | ||
__update_JᵀJ!(::Val{true}, cache, sym::Symbol, _, J) = mul!(getproperty(cache, sym), J', J) | ||
__update_JᵀJ!(::Val{false}, cache, sym::Symbol, H::KrylovJᵀJ, J) = H | ||
__update_JᵀJ!(::Val{true}, cache, sym::Symbol, H::KrylovJᵀJ, J) = H | ||
|
||
function __update_Jᵀf!(iip::Val, cache, sym1::Symbol, sym2::Symbol, J, fu) | ||
return __update_Jᵀf!(iip, cache, sym1, sym2, getproperty(cache, sym2), J, fu) | ||
end | ||
function __update_Jᵀf!(::Val{false}, cache, sym1::Symbol, sym2::Symbol, _, J, fu) | ||
return setproperty!(cache, sym1, _restructure(getproperty(cache, sym1), J' * fu)) | ||
end | ||
function __update_Jᵀf!(::Val{true}, cache, sym1::Symbol, sym2::Symbol, _, J, fu) | ||
return mul!(_vec(getproperty(cache, sym1)), J', fu) | ||
end | ||
function __update_Jᵀf!(::Val{false}, cache, sym1::Symbol, sym2::Symbol, H::KrylovJᵀJ, J, fu) | ||
return setproperty!(cache, sym1, _restructure(getproperty(cache, sym1), H.Jᵀ * fu)) | ||
end | ||
function __update_Jᵀf!(::Val{true}, cache, sym1::Symbol, sym2::Symbol, H::KrylovJᵀJ, J, fu) | ||
return mul!(_vec(getproperty(cache, sym1)), H.Jᵀ, fu) | ||
end | ||
|
||
# Left-Right Multiplication | ||
__lr_mul(::Val, H, g) = dot(g, H, g) | ||
## TODO: Use a cache here to avoid allocations | ||
__lr_mul(::Val{false}, H::KrylovJᵀJ, g) = dot(g, H.JᵀJ, g) | ||
function __lr_mul(::Val{true}, H::KrylovJᵀJ, g) | ||
c = similar(g) | ||
mul!(c, H.JᵀJ, g) | ||
return dot(g, c) | ||
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.
what is actually using this?
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.
None of the algorithms by default. But if LM/GN/TR is forced to use a Linear Solve which only works with square matrices then this needs to be triggered.