Skip to content
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

fix autodiff=AutoFiniteDiff() for TrustRegion #361

Merged
merged 2 commits into from
Feb 1, 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
11 changes: 9 additions & 2 deletions src/algorithms/trust_region.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@
initial_trust_radius::Real = 0 // 1, step_threshold::Real = 1 // 10000,
shrink_threshold::Real = 1 // 4, expand_threshold::Real = 3 // 4,
shrink_factor::Real = 1 // 4, expand_factor::Real = 2 // 1,
max_shrink_times::Int = 32, vjp_autodiff = nothing, autodiff = nothing)
max_shrink_times::Int = 32, autodiff = nothing, vjp_autodiff = nothing)
descent = Dogleg(; linsolve, precs)
forward_ad = autodiff isa ADTypes.AbstractForwardMode ? autodiff : nothing
if autodiff isa Union{ADTypes.AbstractForwardMode, ADTypes.AbstractFiniteDifferencesMode}
forward_ad = autodiff

Check warning on line 30 in src/algorithms/trust_region.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/trust_region.jl#L30

Added line #L30 was not covered by tests
else
forward_ad = nothing
end
if isnothing(vjp_autodiff) && autodiff isa ADTypes.AbstractFiniteDifferencesMode
vjp_autodiff = autodiff

Check warning on line 35 in src/algorithms/trust_region.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/trust_region.jl#L35

Added line #L35 was not covered by tests
end
trustregion = GenericTrustRegionScheme(; method = radius_update_scheme, step_threshold,
shrink_threshold, expand_threshold, shrink_factor, expand_factor,
reverse_ad = vjp_autodiff, forward_ad)
Expand Down
11 changes: 7 additions & 4 deletions test/misc/polyalgs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ end
maxiters = 10)
end

no_ad_fast = FastShortcutNonlinearPolyalg(autodiff=AutoFiniteDiff())
no_ad_robust = RobustMultiNewton(autodiff=AutoFiniteDiff())
no_ad_algs = Set([no_ad_fast, no_ad_robust, no_ad_fast.algs..., no_ad_robust.algs...])
@testset "[IIP] no AD" begin
f_iip = Base.Experimental.@opaque (du, u, p) -> du .= u .* u .- p
u0 = [0.0]
u0 = [0.5]
prob = NonlinearProblem(f_iip, u0, 1.0)
for alg in [RobustMultiNewton(autodiff = AutoFiniteDiff())]
for alg in no_ad_algs
sol = solve(prob, alg)
@test isapprox(only(sol.u), 1.0)
@test SciMLBase.successful_retcode(sol.retcode)
Expand All @@ -106,9 +109,9 @@ end

@testset "[OOP] no AD" begin
f_oop = Base.Experimental.@opaque (u, p) -> u .* u .- p
u0 = [0.0]
u0 = [0.5]
prob = NonlinearProblem{false}(f_oop, u0, 1.0)
for alg in [RobustMultiNewton(autodiff = AutoFiniteDiff())]
for alg in no_ad_algs
sol = solve(prob, alg)
@test isapprox(only(sol.u), 1.0)
@test SciMLBase.successful_retcode(sol.retcode)
Expand Down
Loading