-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathjacobian.jl
275 lines (251 loc) · 10.4 KB
/
jacobian.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using Enzyme
"""
JacobianCache(prob, alg, f::F, fu, u, p; autodiff = nothing,
vjp_autodiff = nothing, jvp_autodiff = nothing, linsolve = missing) where {F}
Construct a cache for the Jacobian of `f` w.r.t. `u`.
### Arguments
- `prob`: A [`NonlinearProblem`](@ref) or a [`NonlinearLeastSquaresProblem`](@ref).
- `alg`: A [`AbstractNonlinearSolveAlgorithm`](@ref). Used to check for
[`concrete_jac`](@ref).
- `f`: The function to compute the Jacobian of.
- `fu`: The evaluation of `f(u, p)` or `f(_, u, p)`. Used to determine the size of the
result cache and Jacobian.
- `u`: The current value of the state.
- `p`: The current value of the parameters.
### Keyword Arguments
- `autodiff`: Automatic Differentiation or Finite Differencing backend for computing the
jacobian. By default, selects a backend based on sparsity parameters, type of state,
function properties, etc.
- `vjp_autodiff`: Automatic Differentiation or Finite Differencing backend for computing
the vector-Jacobian product.
- `jvp_autodiff`: Automatic Differentiation or Finite Differencing backend for computing
the Jacobian-vector product.
- `linsolve`: Linear Solver Algorithm used to determine if we need a concrete jacobian
or if possible we can just use a `SciMLJacobianOperators.JacobianOperator` instead.
"""
@concrete mutable struct JacobianCache{iip} <: AbstractNonlinearSolveJacobianCache{iip}
J
f
fu
u
p
stats::NLStats
autodiff
di_extras
end
function reinit_cache!(cache::JacobianCache{iip}, args...; p = cache.p,
u0 = cache.u, kwargs...) where {iip}
cache.u = u0
cache.p = p
end
function JacobianCache(prob, alg, f::F, fu_, u, p; stats, autodiff = nothing,
vjp_autodiff = nothing, jvp_autodiff = nothing, linsolve = missing) where {F}
iip = isinplace(prob)
has_analytic_jac = SciMLBase.has_jac(f)
linsolve_needs_jac = concrete_jac(alg) === nothing && (linsolve === missing ||
(linsolve === nothing || __needs_concrete_A(linsolve)))
alg_wants_jac = concrete_jac(alg) !== nothing && concrete_jac(alg)
needs_jac = linsolve_needs_jac || alg_wants_jac
@bb fu = similar(fu_)
autodiff = get_concrete_forward_ad(
autodiff, prob, Val(false); check_forward_mode = false)
if !has_analytic_jac && needs_jac
autodiff = construct_concrete_adtype(f, autodiff)
di_extras = if !iip && autodiff isa AutoEnzyme
Enzyme.onehot(u)
elseif iip
DI.prepare_jacobian(f, fu, autodiff, u, Constant(prob.p))
else
DI.prepare_jacobian(f, autodiff, u, Constant(prob.p))
end
else
di_extras = nothing
end
J = if !needs_jac
jvp_autodiff = get_concrete_forward_ad(
jvp_autodiff, prob, Val(false); check_forward_mode = true)
vjp_autodiff = get_concrete_reverse_ad(
vjp_autodiff, prob, Val(false); check_reverse_mode = false)
JacobianOperator(prob, fu, u; jvp_autodiff, vjp_autodiff)
else
if f.jac_prototype === nothing
# While this is technically wasteful, it gives out the type of the Jacobian
# which is needed to create the linear solver cache
stats.njacs += 1
if has_analytic_jac
__similar(
fu, promote_type(eltype(fu), eltype(u)), length(fu), length(u))
else
if iip
DI.jacobian(f, fu, di_extras, autodiff, u, Constant(p))
else
if autodiff isa AutoEnzyme
hcat(Enzyme.autodiff(Forward, f, BatchDuplicated(u, di_extras), Const(p))[1]...)
else
DI.jacobian(f, di_extras, autodiff, u, Constant(p))
end
end
end
else
if eltype(f.jac_prototype) <: Bool
similar(f.jac_prototype, promote_type(eltype(fu), eltype(u)))
else
similar(f.jac_prototype)
end
end
end
return JacobianCache{iip}(J, f, fu, u, p, stats, autodiff, di_extras)
end
function JacobianCache(prob, alg, f::F, ::Number, u::Number, p; stats,
autodiff = nothing, kwargs...) where {F}
fu = f(u, p)
if SciMLBase.has_jac(f) || SciMLBase.has_vjp(f) || SciMLBase.has_jvp(f)
return JacobianCache{false}(u, f, fu, u, p, stats, autodiff, nothing)
end
autodiff = get_dense_ad(get_concrete_forward_ad(
autodiff, prob; check_forward_mode = false))
di_extras = DI.prepare_derivative(f, get_dense_ad(autodiff), u, Constant(prob.p))
return JacobianCache{false}(u, f, fu, u, p, stats, autodiff, di_extras)
end
(cache::JacobianCache)(u = cache.u) = cache(cache.J, u, cache.p)
function (cache::JacobianCache)(::Nothing)
cache.J isa JacobianOperator &&
return StatefulJacobianOperator(cache.J, cache.u, cache.p)
return cache.J
end
# Operator
function (cache::JacobianCache)(J::JacobianOperator, u, p = cache.p)
return StatefulJacobianOperator(J, u, p)
end
# Numbers
function (cache::JacobianCache)(::Number, u, p = cache.p) # Scalar
cache.stats.njacs += 1
if SciMLBase.has_jac(cache.f)
return cache.f.jac(u, p)
elseif SciMLBase.has_vjp(cache.f)
return cache.f.vjp(one(u), u, p)
elseif SciMLBase.has_jvp(cache.f)
return cache.f.jvp(one(u), u, p)
end
return DI.derivative(cache.f, cache.di_extras, cache.autodiff, u, Constant(p))
end
# Actually Compute the Jacobian
function (cache::JacobianCache{iip})(
J::Union{AbstractMatrix, Nothing}, u, p = cache.p) where {iip}
cache.stats.njacs += 1
if iip
if SciMLBase.has_jac(cache.f)
cache.f.jac(J, u, p)
else
DI.jacobian!(
cache.f, cache.fu, J, cache.di_extras, cache.autodiff, u, Constant(p))
end
return J
else
if SciMLBase.has_jac(cache.f)
return cache.f.jac(u, p)
elseif cache.autodiff isa AutoEnzyme
hcat(Enzyme.autodiff(Forward, cache.f, BatchDuplicated(u, cache.di_extras), Const(p))[1]...)
else
return DI.jacobian(cache.f, cache.di_extras, cache.autodiff, u, Constant(p))
end
end
end
function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
@assert !(ad isa AutoSparse) "This shouldn't happen. Open an issue."
if f.sparsity === nothing
if f.jac_prototype === nothing
if SciMLBase.has_colorvec(f)
@warn "`colorvec` is provided but `sparsity` and `jac_prototype` is not \
specified. `colorvec` will be ignored."
end
return ad # No sparse AD
else
if !sparse_or_structured_prototype(f.jac_prototype)
if SciMLBase.has_colorvec(f)
@warn "`colorvec` is provided but `jac_prototype` is not a sparse \
or structured matrix. `colorvec` will be ignored."
end
return ad
end
return AutoSparse(
ad;
sparsity_detector = KnownJacobianSparsityDetector(f.jac_prototype),
coloring_algorithm = select_fastest_coloring_algorithm(
f.jac_prototype, f, ad)
)
end
else
if f.sparsity isa AbstractMatrix
if f.jac_prototype !== f.sparsity
if f.jac_prototype !== nothing &&
sparse_or_structured_prototype(f.jac_prototype)
throw(ArgumentError("`sparsity::AbstractMatrix` and a sparse or \
structured `jac_prototype` cannot be both \
provided. Pass only `jac_prototype`."))
end
end
return AutoSparse(
ad;
sparsity_detector = KnownJacobianSparsityDetector(f.sparsity),
coloring_algorithm = select_fastest_coloring_algorithm(
f.sparsity, f, ad)
)
end
@assert f.sparsity isa ADTypes.AbstractSparsityDetector
sparsity_detector = f.sparsity
if f.jac_prototype === nothing
if SciMLBase.has_colorvec(f)
@warn "`colorvec` is provided but `jac_prototype` is not specified. \
`colorvec` will be ignored."
end
return AutoSparse(
ad;
sparsity_detector,
coloring_algorithm = GreedyColoringAlgorithm(LargestFirst())
)
else
if sparse_or_structured_prototype(f.jac_prototype)
if !(sparsity_detector isa NoSparsityDetector)
@warn "`jac_prototype` is a sparse matrix but sparsity = $(f.sparsity) \
has also been specified. Ignoring sparsity field and using \
`jac_prototype` sparsity."
end
sparsity_detector = KnownJacobianSparsityDetector(f.jac_prototype)
end
return AutoSparse(
ad;
sparsity_detector,
coloring_algorithm = select_fastest_coloring_algorithm(
f.jac_prototype, f, ad)
)
end
end
end
function select_fastest_coloring_algorithm(
prototype, f::NonlinearFunction, ad::AbstractADType)
if SciMLBase.has_colorvec(f)
return ConstantColoringAlgorithm{ifelse(
ADTypes.mode(ad) isa ADTypes.ReverseMode, :row, :column)}(
prototype, f.colorvec)
end
return GreedyColoringAlgorithm(LargestFirst())
end
function construct_concrete_adtype(f::NonlinearFunction, ad::AutoSparse)
Base.depwarn(
"Specifying a sparse AD type for Nonlinear Problems is deprecated. \
Instead use the `sparsity`, `jac_prototype`, and `colorvec` to specify \
the right sparsity pattern and coloring algorithm. Ignoring the sparsity \
detection algorithm and coloring algorithm present in $(ad).",
:NonlinearSolve)
if f.sparsity === nothing && f.jac_prototype === nothing
@set! f.sparsity = TracerSparsityDetector()
end
return construct_concrete_adtype(f, get_dense_ad(ad))
end
get_dense_ad(ad) = ad
get_dense_ad(ad::AutoSparse) = ADTypes.dense_ad(ad)
sparse_or_structured_prototype(::AbstractSparseMatrix) = true
function sparse_or_structured_prototype(prototype::AbstractMatrix)
return ArrayInterface.isstructured(prototype)
end