-
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Jacobian-Free Krylov Versions for TR/LM/GN #282
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
59a8713
Krylov Version for Trust Region
avik-pal 8f739fb
Some progress on LM
avik-pal 5052a6e
Fix StaticArray Case
avik-pal 77164c7
Fix matrix resizing
avik-pal 8f70a4e
Use Zygote for LineSearch if loaded
avik-pal b79228d
Add tests
avik-pal 6c52956
Fix aliasing issue
avik-pal bcfcc16
Avoid Runtime Checks for Zygote Being loaded
avik-pal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
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,7 @@ | ||
module NonlinearSolveZygoteExt | ||
|
||
import NonlinearSolve, Zygote | ||
|
||
NonlinearSolve.is_extension_loaded(::Val{:Zygote}) = true | ||
|
||
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
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 |
---|---|---|
@@ -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 @@ function jacobian_caches(alg::AbstractNonlinearSolveAlgorithm, f::F, u, p, ::Val | |
# 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 @@ function jacobian_caches(alg::AbstractNonlinearSolveAlgorithm, f::F, u, p, ::Val | |
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 @@ function jacobian_caches(alg::AbstractNonlinearSolveAlgorithm, f::F, u, p, ::Val | |
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 @@ function __setup_linsolve(A, b, u, p, alg) | |
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 @@ function jacobian_caches(alg::AbstractNonlinearSolveAlgorithm, f::F, u::Number, | |
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 |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.