Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Jan 17, 2024
1 parent 80c8ced commit 1d7d202
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/algorithms/levenberg_marquardt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
LevenbergMarquardt(; linsolve = nothing,
precs = DEFAULT_PRECS, damping_initial::Real = 1.0, α_geodesic::Real = 0.75,
damping_increase_factor::Real = 2.0, damping_decrease_factor::Real = 3.0,
finite_diff_step_geodesic::Real = 0.1, b_uphill::Real = 1.0, autodiff = nothing,
finite_diff_step_geodesic = 0.1, b_uphill::Real = 1.0, autodiff = nothing,
min_damping_D::Real = 1e-8, disable_geodesic = Val(false))
An advanced Levenberg-Marquardt implementation with the improvements suggested in
Expand Down Expand Up @@ -33,7 +33,7 @@ For the remaining arguments, see [`GeodesicAcceleration`](@ref) and
function LevenbergMarquardt(; concrete_jac = missing, linsolve = nothing,
precs = DEFAULT_PRECS, damping_initial::Real = 1.0, α_geodesic::Real = 0.75,
damping_increase_factor::Real = 2.0, damping_decrease_factor::Real = 3.0,
finite_diff_step_geodesic::Real = 0.1, b_uphill::Real = 1.0, autodiff = nothing,
finite_diff_step_geodesic = 0.1, b_uphill::Real = 1.0, autodiff = nothing,
min_damping_D::Real = 1e-8, disable_geodesic = False)
if concrete_jac !== missing
Base.depwarn("The `concrete_jac` keyword argument is deprecated and will be \

Check warning on line 39 in src/algorithms/levenberg_marquardt.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/levenberg_marquardt.jl#L39

Added line #L39 was not covered by tests
Expand Down
23 changes: 6 additions & 17 deletions src/core/approximate_jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip},
J = initialization_cache(nothing)
inv_workspace, J = INV ? __safe_inv_workspace(J) : (nothing, J)
descent_cache = __internal_init(prob, alg.descent, J, fu, u; abstol, reltol,
internalnorm,
linsolve_kwargs, pre_inverted = Val(INV), timer)
internalnorm, linsolve_kwargs, pre_inverted = Val(INV), timer)
du = get_du(descent_cache)

reinit_rule_cache = __internal_init(alg.reinit_rule, J, fu, u, du)
Expand All @@ -191,8 +190,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip},
supports_trust_region(alg.descent) || error("Trust Region not supported by \

Check warning on line 190 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L190

Added line #L190 was not covered by tests
$(alg.descent).")
trustregion_cache = __internal_init(prob, alg.trustregion, f, fu, u, p;

Check warning on line 192 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L192

Added line #L192 was not covered by tests
internalnorm,
kwargs...)
internalnorm, kwargs...)
GB = :TrustRegion

Check warning on line 194 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L194

Added line #L194 was not covered by tests
end

Expand All @@ -205,12 +203,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem{uType, iip},
GB = :LineSearch
end

update_rule_cache = __internal_init(prob,
alg.update_rule,
J,
fu,
u,
du;
update_rule_cache = __internal_init(prob, alg.update_rule, J, fu, u, du;
internalnorm)

trace = init_nonlinearsolve_trace(alg, u, fu, ApplyArray(__zero, J), du;
Expand Down Expand Up @@ -256,8 +249,7 @@ function __step!(cache::ApproximateJacobianSolveCache{INV, GB, iip};
elseif recompute_jacobian === nothing
# Standard Step
reinit = __internal_solve!(cache.reinit_rule_cache, cache.J, cache.fu,
cache.u,
cache.du)
cache.u, cache.du)
reinit && (countable_reinit = true)
elseif recompute_jacobian
reinit = true # Force ReInitialization: Don't count towards resetting

Check warning on line 255 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L254-L255

Added lines #L254 - L255 were not covered by tests
Expand All @@ -276,9 +268,7 @@ function __step!(cache::ApproximateJacobianSolveCache{INV, GB, iip};
end

if reinit
J_init = __internal_solve!(cache.initialization_cache,
cache.fu,
cache.u,
J_init = __internal_solve!(cache.initialization_cache, cache.fu, cache.u,
Val(true))
cache.J = INV ? __safe_inv!!(cache.inv_workspace, J_init) : J_init
J = cache.J
Expand Down Expand Up @@ -318,8 +308,7 @@ function __step!(cache::ApproximateJacobianSolveCache{INV, GB, iip};
elseif GB === :TrustRegion
@static_timeit cache.timer "trustregion" begin
tr_accepted, u_new, fu_new = __internal_solve!(cache.trustregion_cache, J,

Check warning on line 310 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L308-L310

Added lines #L308 - L310 were not covered by tests
cache.fu,
cache.u, δu, descent_intermediates)
cache.fu, cache.u, δu, descent_intermediates)
if tr_accepted
@bb copyto!(cache.u, u_new)
@bb copyto!(cache.fu, fu_new)

Check warning on line 314 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L312-L314

Added lines #L312 - L314 were not covered by tests
Expand Down
6 changes: 2 additions & 4 deletions src/core/generalized_first_order.jl
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ function __step!(cache::GeneralizedFirstOrderAlgorithmCache{iip, GB};
if GB === :LineSearch
@static_timeit cache.timer "linesearch" begin
linesearch_failed, α = __internal_solve!(cache.linesearch_cache,
cache.u,
δu)
cache.u, δu)
end
if linesearch_failed
cache.retcode = ReturnCode.InternalLineSearchFailed
Expand All @@ -243,8 +242,7 @@ function __step!(cache::GeneralizedFirstOrderAlgorithmCache{iip, GB};
elseif GB === :TrustRegion
@static_timeit cache.timer "trustregion" begin
tr_accepted, u_new, fu_new = __internal_solve!(cache.trustregion_cache, J,
cache.fu,
cache.u, δu, descent_intermediates)
cache.fu, cache.u, δu, descent_intermediates)
if tr_accepted
@bb copyto!(cache.u, u_new)
@bb copyto!(cache.fu, fu_new)
Expand Down
3 changes: 1 addition & 2 deletions src/core/spectral_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ function SciMLBase.__init(prob::AbstractNonlinearProblem, alg::GeneralizedDFSane
@bb fu_cache = copy(fu)

linesearch_cache = __internal_init(prob, alg.linesearch, prob.f, fu, u, prob.p;
maxiters,
internalnorm, kwargs...)
maxiters, internalnorm, kwargs...)

abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fu, u_cache,
termination_condition)
Expand Down
12 changes: 4 additions & 8 deletions src/descent/damped_newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,15 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::DampedNewtonDescen
rhs_damp = fu
end
damping_fn_cache = __internal_init(prob, alg.damping_fn, alg.initial_damping,
jac_damp,
rhs_damp, u, False; kwargs...)
jac_damp, rhs_damp, u, False; kwargs...)
D = damping_fn_cache(nothing)
D isa Number && (D = D * I)
rhs_cache = vcat(_vec(fu), _vec(u))
J_cache = _vcat(J, D)
A, b = J_cache, rhs_cache
elseif mode === :simple
damping_fn_cache = __internal_init(prob, alg.damping_fn, alg.initial_damping, J, fu,
u, False;
kwargs...)
u, False; kwargs...)
J_cache = __maybe_unaliased(J, alias_J)
D = damping_fn_cache(nothing)
J_damped = __dampen_jacobian!!(J_cache, J, D)
Expand Down Expand Up @@ -186,10 +184,8 @@ function __internal_solve!(cache::DampedNewtonDescentCache{INV, mode}, J, fu, u,
INV && (J = inv(J))
@bb cache.JᵀJ_cache = transpose(J) × J
@bb cache.Jᵀfu_cache = transpose(J) × vec(fu)
D = __internal_solve!(cache.damping_fn_cache,
cache.JᵀJ_cache,
cache.Jᵀfu_cache,
True)
D = __internal_solve!(cache.damping_fn_cache, cache.JᵀJ_cache,
cache.Jᵀfu_cache, True)
cache.J = __dampen_jacobian!!(cache.J, cache.JᵀJ_cache, D)
A = __maybe_symmetric(cache.J)
elseif !recompute_A
Expand Down
14 changes: 2 additions & 12 deletions src/descent/dogleg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ function __internal_solve!(cache::DoglegCache{INV, NF}, J, fu, u, idx::Val{N} =
want to use a Trust Region."
δu = get_du(cache, idx)
T = promote_type(eltype(u), eltype(fu))
δu_newton, _, _ = __internal_solve!(cache.newton_cache,
J,
fu,
u,
idx;
skip_solve,
δu_newton, _, _ = __internal_solve!(cache.newton_cache, J, fu, u, idx; skip_solve,
kwargs...)

# Newton's Step within the trust region
Expand All @@ -108,12 +103,7 @@ function __internal_solve!(cache::DoglegCache{INV, NF}, J, fu, u, idx::Val{N} =
@bb cache.δu_cache_mul = JᵀJ × vec(δu_cauchy)
δuJᵀJδu = __dot(δu_cauchy, cache.δu_cache_mul)
else
δu_cauchy, _, _ = __internal_solve!(cache.cauchy_cache,
J,
fu,
u,
idx;
skip_solve,
δu_cauchy, _, _ = __internal_solve!(cache.cauchy_cache, J, fu, u, idx; skip_solve,
kwargs...)
J_ = INV ? inv(J) : J
l_grad = cache.internalnorm(δu_cauchy)
Expand Down
17 changes: 5 additions & 12 deletions src/descent/geodesic_acceleration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ function set_acceleration!(cache::GeodesicAccelerationCache, δa, ::Val{N}) wher
end

function __internal_init(prob::AbstractNonlinearProblem, alg::GeodesicAcceleration, J, fu,
u;
shared::Val{N} = Val(1), pre_inverted::Val{INV} = False, linsolve_kwargs = (;),
u; shared::Val{N} = Val(1), pre_inverted::Val{INV} = False, linsolve_kwargs = (;),
abstol = nothing, reltol = nothing, internalnorm::F = DEFAULT_NORM,
kwargs...) where {INV, N, F}
T = promote_type(eltype(u), eltype(fu))
Expand All @@ -94,8 +93,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::GeodesicAccelerati
@bb δu_ = similar(u)

Check warning on line 93 in src/descent/geodesic_acceleration.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/geodesic_acceleration.jl#L93

Added line #L93 was not covered by tests
end
descent_cache = __internal_init(prob, alg.descent, J, fu, u; shared = Val(N * 2),
pre_inverted,
linsolve_kwargs, abstol, reltol, kwargs...)
pre_inverted, linsolve_kwargs, abstol, reltol, kwargs...)
@bb Jv = similar(fu)
@bb fu_cache = copy(fu)
@bb u_cache = similar(u)
Expand All @@ -107,12 +105,7 @@ function __internal_solve!(cache::GeodesicAccelerationCache, J, fu, u, idx::Val{
skip_solve::Bool = false, kwargs...) where {N}
a, v, δu = get_acceleration(cache, idx), get_velocity(cache, idx), get_du(cache, idx)
skip_solve && return δu, true, (; a, v)
v, _, _ = __internal_solve!(cache.descent_cache,
J,
fu,
u,
Val(2N - 1);
skip_solve,
v, _, _ = __internal_solve!(cache.descent_cache, J, fu, u, Val(2N - 1); skip_solve,
kwargs...)

@bb @. cache.u_cache = u + cache.h * v
Expand All @@ -121,9 +114,9 @@ function __internal_solve!(cache::GeodesicAccelerationCache, J, fu, u, idx::Val{
J !== nothing && @bb(cache.Jv=J × vec(v))
Jv = _restructure(cache.fu_cache, cache.Jv)
@bb @. cache.fu_cache = (2 / cache.h) * ((cache.fu_cache - fu) / cache.h - Jv)

a, _, _ = __internal_solve!(cache.descent_cache, J, cache.fu_cache, u, Val(2N);
skip_solve,
kwargs..., reuse_A_if_factorization = true)
skip_solve, kwargs..., reuse_A_if_factorization = true)

norm_v = cache.internalnorm(v)
norm_a = cache.internalnorm(a)
Expand Down
7 changes: 3 additions & 4 deletions src/descent/steepest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ end
@internal_caches SteepestDescentCache :lincache

@inline function __internal_init(prob::AbstractNonlinearProblem, alg::SteepestDescent, J,
fu,
u; shared::Val{N} = Val(1), pre_inverted::Val{INV} = False, linsolve_kwargs = (;),
abstol = nothing, reltol = nothing, timer = get_timer_output(),
kwargs...) where {INV, N}
fu, u; shared::Val{N} = Val(1), pre_inverted::Val{INV} = False,
linsolve_kwargs = (;), abstol = nothing, reltol = nothing,
timer = get_timer_output(), kwargs...) where {INV, N}
INV && @assert length(fu)==length(u) "Non-Square Jacobian Inverse doesn't make sense."
@bb δu = similar(u)
δus = N 1 ? nothing : map(2:N) do i
Expand Down
6 changes: 1 addition & 5 deletions src/globalization/trust_region.jl
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::GenericTrustRegion
u_cache, fu_cache, false, 0, 0, alg)
end

function __internal_solve!(cache::GenericTrustRegionSchemeCache,
J,
fu,
u,
δu,
function __internal_solve!(cache::GenericTrustRegionSchemeCache, J, fu, u, δu,
descent_stats)
T = promote_type(eltype(u), eltype(fu))
@bb @. cache.u_cache = u + δu
Expand Down
3 changes: 1 addition & 2 deletions src/internal/approximate_initialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ function __internal_init(prob::AbstractNonlinearProblem, alg::IdentityInitializa
internalnorm)
end
function __internal_init(prob::AbstractNonlinearProblem, alg::IdentityInitialization,
solver,
f::F, fu, u, p; internalnorm::IN = DEFAULT_NORM, kwargs...) where {F, IN}
solver, f::F, fu, u, p; internalnorm::IN = DEFAULT_NORM, kwargs...) where {F, IN}
α = __initial_alpha(alg.alpha, u, fu, internalnorm)
if alg.structure isa DiagonalStructure
@assert length(u)==length(fu) "Diagonal Jacobian Structure must be square!"
Expand Down

0 comments on commit 1d7d202

Please sign in to comment.