Skip to content

Commit b3380d0

Browse files
committed
Remove support for autodiff::Symbol, autodiff::Bool, and chunk keyword arguments
1 parent 950a12c commit b3380d0

File tree

10 files changed

+151
-195
lines changed

10 files changed

+151
-195
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.11.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
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1211

1312
[compat]
@@ -21,11 +20,12 @@ julia = "1.10"
2120
[extras]
2221
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
2322
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
23+
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
2424
OptimTestProblems = "cec144fc-5a64-5bc6-99fb-dde8f63e154c"
2525
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2626
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
2727
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2828
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2929

3030
[targets]
31-
test = ["ADTypes", "ComponentArrays", "OptimTestProblems", "Random", "RecursiveArrayTools", "SparseArrays", "Test"]
31+
test = ["ADTypes", "ComponentArrays", "ForwardDiff", "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
@@ -1,9 +1,8 @@
11
module NLSolversBase
22

3-
using ADTypes: AbstractADType, AutoForwardDiff, AutoFiniteDiff
3+
using ADTypes: AbstractADType, AutoFiniteDiff
44
import DifferentiationInterface as DI
55
using FiniteDiff: FiniteDiff
6-
using ForwardDiff: ForwardDiff
76
using LinearAlgebra: LinearAlgebra
87
import Distributed: clear!
98
export AbstractObjective,
@@ -45,35 +44,6 @@ export AbstractObjective,
4544
export AbstractConstraints, OnceDifferentiableConstraints,
4645
TwiceDifferentiableConstraints, ConstraintBounds
4746

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

7949
include("objective_types/inplace_factory.jl")

src/objective_types/constraints.jl

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

122-
function checked_chunk(lx)
122+
function check_autodiff(lx::AbstractVector)
123123
if isempty(lx)
124124
throw(ArgumentError("autodiff on constraints require the full lower bound vector `lx`."))
125125
end
126-
ForwardDiff.Chunk(lx)
127126
end
128127

129128
function OnceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVector,
130129
lc::AbstractVector, uc::AbstractVector,
131-
autodiff::Symbol = :central,
132-
chunk::ForwardDiff.Chunk = checked_chunk(lx))
133-
130+
autodiff::AbstractADType = AutoFiniteDiff(; fdtype = Val(:central)))
131+
132+
check_autodiff(lx)
134133
bounds = ConstraintBounds(lx, ux, lc, uc)
135134
T = eltype(bounds)
136135
sizex = size(lx)
@@ -139,11 +138,12 @@ function OnceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVecto
139138
xcache = zeros(T, sizex)
140139
ccache = zeros(T, sizec)
141140

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
141+
jac_prep = DI.prepare_jacobian(c!, ccache, autodiff, xcache)
142+
j! = let c! = c!, ccache = ccache, jac_prep = jac_prep, autodiff = autodiff
143+
function (_j, _x)
144+
DI.jacobian!(c!, ccache, _j, jac_prep, autodiff, _x)
145+
return _j
146+
end
147147
end
148148
return OnceDifferentiableConstraints(c!, j!, bounds)
149149
end
@@ -163,8 +163,8 @@ end
163163

164164
function TwiceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVector,
165165
lc::AbstractVector, uc::AbstractVector,
166-
autodiff::Symbol = :central,
167-
chunk::ForwardDiff.Chunk = checked_chunk(lx))
166+
autodiff::AbstractADType = AutoFiniteDiff(; fdtype = Val(:central)))
167+
check_autodiff(lx)
168168
bounds = ConstraintBounds(lx, ux, lc, uc)
169169
T = eltype(bounds)
170170
nc = length(lc)
@@ -180,31 +180,31 @@ function TwiceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVect
180180
return LinearAlgebra.dot(_λ, ccache_righttype)
181181
end
182182

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
183+
jac_prep = DI.prepare_jacobian(c!, ccache, autodiff, x_example)
184+
con_jac! = let c! = c!, ccache = ccache, jac_prep = jac_prep, autodiff = autodiff
185+
function (_j, _x)
186+
DI.jacobian!(c!, ccache, _j, jac_prep, autodiff, _x)
187+
return _j
188+
end
190189
end
191190

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
191+
hess_prep = DI.prepare_hessian(sum_constraints, autodiff, x_example, DI.Constant(λ_example))
192+
con_hess! = let sum_constraints = sum_constraints, hess_prep = hess_prep, autodiff = autodiff
193+
function (_h, _x, _λ)
194+
DI.hessian!(sum_constraints, _h, hess_prep, autodiff, _x, DI.Constant(_λ))
195+
return _h
196+
end
196197
end
197198

198199
return TwiceDifferentiableConstraints(c!, con_jac!, con_hess!, bounds)
199200
end
200201

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

207-
return TwiceDifferentiableConstraints(c!, lx, ux, lc, uc, autodiff, chunk)
207+
return TwiceDifferentiableConstraints(c!, lx, ux, lc, uc, autodiff)
208208
end
209209

210210

src/objective_types/oncedifferentiable.jl

Lines changed: 44 additions & 47 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

32-
function OnceDifferentiable(f, x_seed::AbstractArray{T},
32+
function OnceDifferentiable(f, x_seed::AbstractArray,
3333
F::Real,
3434
DF::AbstractArray,
35-
autodiff, chunk) where T
35+
autodiff::AbstractADType)
3636
# When here, at the constructor with positional autodiff, it should already
3737
# be the case, that f is inplace.
3838
if f isa Union{InplaceObjective, NotInplaceObjective}
@@ -43,51 +43,48 @@ 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 f isa Union{InplaceObjective, NotInplaceObjective}
63+
OnceDifferentiable(f, x::AbstractArray, F::AbstractArray, autodiff::AbstractADType) =
64+
OnceDifferentiable(f, x, F, alloc_DF(x, F), autodiff)
65+
function OnceDifferentiable(f, x_seed::AbstractArray,
66+
F::AbstractArray,
67+
DF::AbstractArray,
68+
autodiff::AbstractADType)
69+
if f isa Union{InplaceObjective, NotInplaceObjective}
7670
fF = make_f(f, x_seed, F)
7771
dfF = make_df(f, x_seed, F)
7872
fdfF = make_fdf(f, x_seed, F)
7973
return OnceDifferentiable(fF, dfF, fdfF, x_seed, F, DF)
8074
else
8175
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
76+
jac_prep = DI.prepare_jacobian(f, F2, autodiff, x_seed)
77+
j! = let f = f, F2 = F2, jac_prep = jac_prep, autodiff = autodiff
78+
function (_j, _x)
79+
DI.jacobian!(f, F2, _j, jac_prep, autodiff, _x)
80+
return _j
81+
end
8782
end
88-
function fj!(_y, _j, _x)
89-
y, _ = DI.value_and_jacobian!(f, _y, _j, jac_prep, backend, _x)
90-
return y
83+
fj! = let f = f, jac_prep = jac_prep, autodiff = autodiff
84+
function (_y, _j, _x)
85+
y, _ = DI.value_and_jacobian!(f, _y, _j, jac_prep, autodiff, _x)
86+
return y
87+
end
9188
end
9289
return OnceDifferentiable(f, j!, fj!, x_seed, F, DF)
9390
end
@@ -98,7 +95,7 @@ function OnceDifferentiable(f, df,
9895
x::AbstractArray,
9996
F::Real = real(zero(eltype(x))),
10097
DF::AbstractArray = alloc_DF(x, F);
101-
inplace = true)
98+
inplace::Bool = true)
10299

103100

104101
df! = df!_from_df(df, F, inplace)
@@ -112,7 +109,7 @@ function OnceDifferentiable(f, j,
112109
x::AbstractArray,
113110
F::AbstractArray,
114111
J::AbstractArray = alloc_DF(x, F);
115-
inplace = true)
112+
inplace::Bool = true)
116113

117114
f! = f!_from_f(f, F, inplace)
118115
j! = df!_from_df(j, F, inplace)
@@ -127,7 +124,7 @@ function OnceDifferentiable(f, df, fdf,
127124
x::AbstractArray,
128125
F::Real = real(zero(eltype(x))),
129126
DF::AbstractArray = alloc_DF(x, F);
130-
inplace = true)
127+
inplace ::Bool = true)
131128

132129
# f is never "inplace" since F is scalar
133130
df! = df!_from_df(df, F, inplace)
@@ -145,7 +142,7 @@ function OnceDifferentiable(f, df, fdf,
145142
x::AbstractArray,
146143
F::AbstractArray,
147144
DF::AbstractArray = alloc_DF(x, F);
148-
inplace = true)
145+
inplace::Bool = true)
149146

150147
f = f!_from_f(f, F, inplace)
151148
df! = df!_from_df(df, F, inplace)

0 commit comments

Comments
 (0)