Skip to content

Commit 3484936

Browse files
committed
Remove support for autodiff::Symbol, autodiff::Bool, and chunk keyword arguments
1 parent 72b1467 commit 3484936

File tree

10 files changed

+128
-210
lines changed

10 files changed

+128
-210
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
name = "NLSolversBase"
22
uuid = "d41bc354-129a-5804-8e4c-c37616107c6c"
3-
version = "7.10.0"
3+
version = "8.0.0-dev"
44

55
[deps]
66
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
77
DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
88
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
99
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
10-
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1110

1211
[compat]
1312
ADTypes = "1.11.0"
@@ -19,6 +18,7 @@ julia = "1.10"
1918
[extras]
2019
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
2120
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
21+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
2222
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
2323
OptimTestProblems = "cec144fc-5a64-5bc6-99fb-dde8f63e154c"
2424
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
@@ -27,4 +27,4 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2727
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2828

2929
[targets]
30-
test = ["ADTypes", "ComponentArrays", "LinearAlgebra", "OptimTestProblems", "Random", "RecursiveArrayTools", "SparseArrays", "Test"]
30+
test = ["ADTypes", "ComponentArrays", "ForwardDiff", "LinearAlgebra", "OptimTestProblems", "Random", "RecursiveArrayTools", "SparseArrays", "Test"]

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ The words in front of `Differentiable` in the type names (`Non`, `Once`, `Twice`
2727
## Automatic differentiation
2828

2929
Some constructors for `OnceDifferentiable`, `TwiceDifferentiable`, `OnceDifferentiableConstraints` and `TwiceDifferentiableConstraints` accept a positional argument called `autodiff`.
30-
This argument can be either:
31-
32-
- An object subtyping `AbstractADType`, defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl) and supported by [DifferentiationInterface.jl](https://github.com/JuliaDiff/DifferentiationInterface.jl).
33-
- A `Symbol` like `:finite` (and variants thereof) or `:forward`, which fall back on `ADTypes.AutoFiniteDiff` and `ADTypes.AutoForwardDiff` respectively.
34-
- A `Bool`, namely `true`, which falls back on `ADTypes.AutoForwardDiff`.
35-
36-
When the positional argument `chunk` is passed, it is used to configure chunk size in `ADTypes.AutoForwardDiff`, but _only_ if `autodiff in (:forward, true)`.
37-
Indeed, if `autodiff isa ADTypes.AutoForwardDiff`, we assume that the user already selected the appropriate chunk size and so `chunk` is ignored.
30+
This argument an instance of an `AbstractADType`, defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl) and supported by [DifferentiationInterface.jl](https://github.com/JuliaDiff/DifferentiationInterface.jl).
3831

3932
## Examples
4033
#### Optimization

src/NLSolversBase.jl

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ __precompile__(true)
22

33
module NLSolversBase
44

5-
using ADTypes: AbstractADType, AutoForwardDiff, AutoFiniteDiff
5+
using ADTypes: AbstractADType, AutoFiniteDiff
66
import DifferentiationInterface as DI
77
using FiniteDiff: FiniteDiff
8-
using ForwardDiff: ForwardDiff
98
import Distributed: clear!
109
export AbstractObjective,
1110
NonDifferentiable,
@@ -46,35 +45,6 @@ export AbstractObjective,
4645
export AbstractConstraints, OnceDifferentiableConstraints,
4746
TwiceDifferentiableConstraints, ConstraintBounds
4847

49-
function finitediff_fdtype(autodiff)
50-
if autodiff == :finiteforward
51-
fdtype = Val{:forward}
52-
elseif autodiff == :finitecomplex
53-
fdtype = Val{:complex}
54-
elseif any(autodiff .== (:finite, :central, :finitecentral))
55-
fdtype = Val{:central}
56-
end
57-
fdtype
58-
end
59-
60-
forwarddiff_chunksize(::Nothing) = nothing
61-
forwarddiff_chunksize(::ForwardDiff.Chunk{C}) where {C} = C
62-
63-
is_finitediff(autodiff) = autodiff (:central, :finite, :finiteforward, :finitecomplex)
64-
is_forwarddiff(autodiff) = autodiff (:forward, :forwarddiff, true)
65-
66-
get_adtype(autodiff::AbstractADType, chunk=nothing) = autodiff
67-
68-
function get_adtype(autodiff::Union{Symbol,Bool}, chunk=nothing)
69-
if is_finitediff(autodiff)
70-
return AutoFiniteDiff(; fdtype=finitediff_fdtype(autodiff)())
71-
elseif is_forwarddiff(autodiff)
72-
return AutoForwardDiff(; chunksize=forwarddiff_chunksize(chunk))
73-
else
74-
error("The autodiff value $autodiff is not supported. Use :finite or :forward.")
75-
end
76-
end
77-
7848
x_of_nans(x, Tf=eltype(x)) = fill!(Tf.(x), Tf(NaN))
7949

8050
include("objective_types/inplace_factory.jl")

src/objective_types/constraints.jl

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,9 @@ function OnceDifferentiableConstraints(bounds::ConstraintBounds)
119119
OnceDifferentiableConstraints(c!, J!, bounds)
120120
end
121121

122-
function checked_chunk(lx)
123-
if isempty(lx)
124-
throw(ArgumentError("autodiff on constraints require the full lower bound vector `lx`."))
125-
end
126-
ForwardDiff.Chunk(lx)
127-
end
128-
129122
function OnceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVector,
130123
lc::AbstractVector, uc::AbstractVector,
131-
autodiff::Symbol = :central,
132-
chunk::ForwardDiff.Chunk = checked_chunk(lx))
133-
124+
autodiff::AbstractADType = AutoFiniteDiff(; fdtype = Val(:central)))
134125
bounds = ConstraintBounds(lx, ux, lc, uc)
135126
T = eltype(bounds)
136127
sizex = size(lx)
@@ -139,11 +130,12 @@ function OnceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVecto
139130
xcache = zeros(T, sizex)
140131
ccache = zeros(T, sizec)
141132

142-
backend = get_adtype(autodiff, chunk)
143-
jac_prep = DI.prepare_jacobian(c!, ccache, backend, xcache)
144-
function j!(_j, _x)
145-
DI.jacobian!(c!, ccache, _j, jac_prep, backend, _x)
146-
return _j
133+
jac_prep = DI.prepare_jacobian(c!, ccache, autodiff, xcache)
134+
j! = let c! = c!, ccache = ccache, jac_prep = jac_prep, autodiff = autodiff
135+
function (_j, _x)
136+
DI.jacobian!(c!, ccache, _j, jac_prep, autodiff, _x)
137+
return _j
138+
end
147139
end
148140
return OnceDifferentiableConstraints(c!, j!, bounds)
149141
end
@@ -163,8 +155,7 @@ end
163155

164156
function TwiceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVector,
165157
lc::AbstractVector, uc::AbstractVector,
166-
autodiff::Symbol = :central,
167-
chunk::ForwardDiff.Chunk = checked_chunk(lx))
158+
autodiff::AbstractADType = AutoFiniteDiff(; fdtype = Val(:central)))
168159
bounds = ConstraintBounds(lx, ux, lc, uc)
169160
T = eltype(bounds)
170161
nc = length(lc)
@@ -180,31 +171,31 @@ function TwiceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVect
180171
return sum(_λ[i] * ccache[i] for i in eachindex(_λ, ccache))
181172
end
182173

183-
backend = get_adtype(autodiff, chunk)
184-
185-
186-
jac_prep = DI.prepare_jacobian(c!, ccache, backend, x_example)
187-
function con_jac!(_j, _x)
188-
DI.jacobian!(c!, ccache, _j, jac_prep, backend, _x)
189-
return _j
174+
jac_prep = DI.prepare_jacobian(c!, ccache, autodiff, x_example)
175+
con_jac! = let c! = c!, ccache = ccache, jac_prep = jac_prep, autodiff = autodiff
176+
function (_j, _x)
177+
DI.jacobian!(c!, ccache, _j, jac_prep, autodiff, _x)
178+
return _j
179+
end
190180
end
191181

192-
hess_prep = DI.prepare_hessian(sum_constraints, backend, x_example, DI.Constant(λ_example))
193-
function con_hess!(_h, _x, _λ)
194-
DI.hessian!(sum_constraints, _h, hess_prep, backend, _x, DI.Constant(_λ))
195-
return _h
182+
hess_prep = DI.prepare_hessian(sum_constraints, autodiff, x_example, DI.Constant(λ_example))
183+
con_hess! = let sum_constraints = sum_constraints, hess_prep = hess_prep, autodiff = autodiff
184+
function (_h, _x, _λ)
185+
DI.hessian!(sum_constraints, _h, hess_prep, autodiff, _x, DI.Constant(_λ))
186+
return _h
187+
end
196188
end
197189

198190
return TwiceDifferentiableConstraints(c!, con_jac!, con_hess!, bounds)
199191
end
200192

201193
function TwiceDifferentiableConstraints(c!, con_jac!,lx::AbstractVector, ux::AbstractVector,
202194
lc::AbstractVector, uc::AbstractVector,
203-
autodiff::Symbol = :central,
204-
chunk::ForwardDiff.Chunk = checked_chunk(lx))
195+
autodiff::AbstractADType = AutoFiniteDiff(; fdtype = Val(:central)),)
205196
# TODO: is con_jac! still useful? we ignore it here
206197

207-
return TwiceDifferentiableConstraints(c!, lx, ux, lc, uc, autodiff, chunk)
198+
return TwiceDifferentiableConstraints(c!, lx, ux, lc, uc, autodiff)
208199
end
209200

210201

src/objective_types/oncedifferentiable.jl

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ end
1313

1414
### Only the objective
1515
# Ambiguity
16-
OnceDifferentiable(f, x::AbstractArray,
16+
function OnceDifferentiable(f, x::AbstractArray,
1717
F::Real = real(zero(eltype(x))),
18-
DF::AbstractArray = alloc_DF(x, F); inplace = true, autodiff = :finite,
19-
chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x)) =
20-
OnceDifferentiable(f, x, F, DF, autodiff, chunk)
21-
#OnceDifferentiable(f, x::AbstractArray, F::AbstractArray; autodiff = :finite) =
22-
# OnceDifferentiable(f, x::AbstractArray, F::AbstractArray, alloc_DF(x, F))
18+
DF::AbstractArray = alloc_DF(x, F);
19+
inplace::Bool = true,
20+
autodiff::AbstractADType = AutoFiniteDiff(; fdtype = Val(:central)))
21+
OnceDifferentiable(f, x, F, DF, autodiff)
22+
end
2323
function OnceDifferentiable(f, x::AbstractArray,
2424
F::AbstractArray, DF::AbstractArray = alloc_DF(x, F);
25-
inplace = true, autodiff = :finite)
25+
inplace::Bool = true,
26+
autodiff::AbstractADType = AutoFiniteDiff(; fdtype = Val(:central)))
2627
f! = f!_from_f(f, F, inplace)
27-
28-
OnceDifferentiable(f!, x::AbstractArray, F::AbstractArray, DF, autodiff)
28+
OnceDifferentiable(f!, x, F, DF, autodiff)
2929
end
3030

3131

3232
function OnceDifferentiable(f, x_seed::AbstractArray{T},
3333
F::Real,
3434
DF::AbstractArray,
35-
autodiff, chunk) where T
35+
autodiff::AbstractADType) where {T}
3636
# When here, at the constructor with positional autodiff, it should already
3737
# be the case, that f is inplace.
3838
if typeof(f) <: Union{InplaceObjective, NotInplaceObjective}
@@ -43,62 +43,29 @@ function OnceDifferentiable(f, x_seed::AbstractArray{T},
4343

4444
return OnceDifferentiable(fF, dfF, fdfF, x_seed, F, DF)
4545
else
46-
backend = get_adtype(autodiff, chunk)
47-
grad_prep = DI.prepare_gradient(f, backend, x_seed)
48-
function g!(_g, _x)
49-
DI.gradient!(f, _g, grad_prep, backend, _x)
50-
return nothing
46+
grad_prep = DI.prepare_gradient(f, autodiff, x_seed)
47+
g! = let f = f, grad_prep = grad_prep, autodiff = autodiff
48+
function (_g, _x)
49+
DI.gradient!(f, _g, grad_prep, autodiff, _x)
50+
return nothing
51+
end
5152
end
52-
function fg!(_g, _x)
53-
y, _ = DI.value_and_gradient!(f, _g, grad_prep, backend, _x)
54-
return y
53+
fg! = let f = f, grad_prep = grad_prep, autodiff = autodiff
54+
function (_g, _x)
55+
y, _ = DI.value_and_gradient!(f, _g, grad_prep, autodiff, _x)
56+
return y
57+
end
5558
end
5659
return OnceDifferentiable(f, g!, fg!, x_seed, F, DF)
5760
end
5861
end
5962

60-
has_not_dep_symbol_in_ad = Ref{Bool}(true)
61-
OnceDifferentiable(f, x::AbstractArray, F::AbstractArray, autodiff::Symbol, chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x)) =
62-
OnceDifferentiable(f, x, F, alloc_DF(x, F), autodiff, chunk)
63-
function OnceDifferentiable(f, x::AbstractArray, F::AbstractArray,
64-
autodiff::Bool, chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x))
65-
if autodiff == false
66-
throw(ErrorException("It is not possible to set the `autodiff` keyword to `false` when constructing a OnceDifferentiable instance from only one function. Pass in the (partial) derivative or specify a valid `autodiff` symbol."))
67-
elseif has_not_dep_symbol_in_ad[]
68-
@warn("Setting the `autodiff` keyword to `true` is deprecated. Please use a valid symbol instead.")
69-
has_not_dep_symbol_in_ad[] = false
70-
end
71-
OnceDifferentiable(f, x, F, alloc_DF(x, F), :forward, chunk)
72-
end
73-
function OnceDifferentiable(f, x_seed::AbstractArray, F::AbstractArray, DF::AbstractArray,
74-
autodiff::Symbol , chunk::ForwardDiff.Chunk = ForwardDiff.Chunk(x_seed))
75-
if typeof(f) <: Union{InplaceObjective, NotInplaceObjective}
76-
fF = make_f(f, x_seed, F)
77-
dfF = make_df(f, x_seed, F)
78-
fdfF = make_fdf(f, x_seed, F)
79-
return OnceDifferentiable(fF, dfF, fdfF, x_seed, F, DF)
80-
else
81-
F2 = similar(F)
82-
backend = get_adtype(autodiff, chunk)
83-
jac_prep = DI.prepare_jacobian(f, F2, backend, x_seed)
84-
function j!(_j, _x)
85-
DI.jacobian!(f, F2, _j, jac_prep, backend, _x)
86-
return _j
87-
end
88-
function fj!(_y, _j, _x)
89-
y, _ = DI.value_and_jacobian!(f, _y, _j, jac_prep, backend, _x)
90-
return y
91-
end
92-
return OnceDifferentiable(f, j!, fj!, x_seed, F, DF)
93-
end
94-
end
95-
9663
### Objective and derivative
9764
function OnceDifferentiable(f, df,
9865
x::AbstractArray,
9966
F::Real = real(zero(eltype(x))),
10067
DF::AbstractArray = alloc_DF(x, F);
101-
inplace = true)
68+
inplace::Bool = true)
10269

10370

10471
df! = df!_from_df(df, F, inplace)
@@ -112,7 +79,7 @@ function OnceDifferentiable(f, j,
11279
x::AbstractArray,
11380
F::AbstractArray,
11481
J::AbstractArray = alloc_DF(x, F);
115-
inplace = true)
82+
inplace::Bool = true)
11683

11784
f! = f!_from_f(f, F, inplace)
11885
j! = df!_from_df(j, F, inplace)
@@ -127,7 +94,7 @@ function OnceDifferentiable(f, df, fdf,
12794
x::AbstractArray,
12895
F::Real = real(zero(eltype(x))),
12996
DF::AbstractArray = alloc_DF(x, F);
130-
inplace = true)
97+
inplace ::Bool = true)
13198

13299
# f is never "inplace" since F is scalar
133100
df! = df!_from_df(df, F, inplace)
@@ -145,7 +112,7 @@ function OnceDifferentiable(f, df, fdf,
145112
x::AbstractArray,
146113
F::AbstractArray,
147114
DF::AbstractArray = alloc_DF(x, F);
148-
inplace = true)
115+
inplace::Bool = true)
149116

150117
f = f!_from_f(f, F, inplace)
151118
df! = df!_from_df(df, F, inplace)

0 commit comments

Comments
 (0)