@@ -37,7 +37,6 @@ Construct a cache for the Jacobian of `f` w.r.t. `u`.
37
37
stats:: NLStats
38
38
autodiff
39
39
di_extras
40
- sdifft_extras
41
40
end
42
41
43
42
function reinit_cache! (cache:: JacobianCache{iip} , args... ; p = cache. p,
@@ -63,31 +62,13 @@ function JacobianCache(prob, alg, f::F, fu_, u, p; stats, autodiff = nothing,
63
62
64
63
if ! has_analytic_jac && needs_jac
65
64
autodiff = construct_concrete_adtype (f, autodiff)
66
- using_sparsedifftools = autodiff isa StructuredMatrixAutodiff
67
- # SparseMatrixColorings can't handle structured matrices
68
- if using_sparsedifftools
69
- di_extras = nothing
70
- uf = JacobianWrapper {iip} (f, p)
71
- sdifft_extras = if iip
72
- sparse_jacobian_cache (
73
- autodiff. autodiff, autodiff. sparsity_detection, uf, fu, u)
74
- else
75
- sparse_jacobian_cache (autodiff. autodiff, autodiff. sparsity_detection,
76
- uf, __maybe_mutable (u, autodiff); fx = fu)
77
- end
78
- autodiff = autodiff. autodiff # For saving we unwrap
65
+ di_extras = if iip
66
+ DI. prepare_jacobian (f, fu, autodiff, u, Constant (prob. p))
79
67
else
80
- sdifft_extras = nothing
81
- di_extras = if iip
82
- DI. prepare_jacobian (f, fu, autodiff, u, Constant (prob. p))
83
- else
84
- DI. prepare_jacobian (f, autodiff, u, Constant (prob. p))
85
- end
68
+ DI. prepare_jacobian (f, autodiff, u, Constant (prob. p))
86
69
end
87
70
else
88
- using_sparsedifftools = false
89
71
di_extras = nothing
90
- sdifft_extras = nothing
91
72
end
92
73
93
74
J = if ! needs_jac
@@ -98,22 +79,18 @@ function JacobianCache(prob, alg, f::F, fu_, u, p; stats, autodiff = nothing,
98
79
JacobianOperator (prob, fu, u; jvp_autodiff, vjp_autodiff)
99
80
else
100
81
if f. jac_prototype === nothing
101
- if ! using_sparsedifftools
102
- # While this is technically wasteful, it gives out the type of the Jacobian
103
- # which is needed to create the linear solver cache
104
- stats. njacs += 1
105
- if has_analytic_jac
106
- __similar (
107
- fu, promote_type (eltype (fu), eltype (u)), length (fu), length (u))
82
+ # While this is technically wasteful, it gives out the type of the Jacobian
83
+ # which is needed to create the linear solver cache
84
+ stats. njacs += 1
85
+ if has_analytic_jac
86
+ __similar (
87
+ fu, promote_type (eltype (fu), eltype (u)), length (fu), length (u))
88
+ else
89
+ if iip
90
+ DI. jacobian (f, fu, di_extras, autodiff, u, Constant (p))
108
91
else
109
- if iip
110
- DI. jacobian (f, fu, di_extras, autodiff, u, Constant (p))
111
- else
112
- DI. jacobian (f, autodiff, u, Constant (p))
113
- end
92
+ DI. jacobian (f, autodiff, u, Constant (p))
114
93
end
115
- else
116
- zero (init_jacobian (sdifft_extras; preserve_immutable = Val (true )))
117
94
end
118
95
else
119
96
jac_proto = if eltype (f. jac_prototype) <: Bool
@@ -126,20 +103,19 @@ function JacobianCache(prob, alg, f::F, fu_, u, p; stats, autodiff = nothing,
126
103
end
127
104
end
128
105
129
- return JacobianCache {iip} (
130
- J, f, fu, u, p, stats, autodiff, di_extras, sdifft_extras)
106
+ return JacobianCache {iip} (J, f, fu, u, p, stats, autodiff, di_extras)
131
107
end
132
108
133
109
function JacobianCache (prob, alg, f:: F , :: Number , u:: Number , p; stats,
134
110
autodiff = nothing , kwargs... ) where {F}
135
111
fu = f (u, p)
136
112
if SciMLBase. has_jac (f) || SciMLBase. has_vjp (f) || SciMLBase. has_jvp (f)
137
- return JacobianCache {false} (u, f, fu, u, p, stats, autodiff, nothing , nothing )
113
+ return JacobianCache {false} (u, f, fu, u, p, stats, autodiff, nothing )
138
114
end
139
115
autodiff = get_dense_ad (get_concrete_forward_ad (
140
116
autodiff, prob; check_forward_mode = false ))
141
117
di_extras = DI. prepare_derivative (f, get_dense_ad (autodiff), u, Constant (prob. p))
142
- return JacobianCache {false} (u, f, fu, u, p, stats, autodiff, di_extras, nothing )
118
+ return JacobianCache {false} (u, f, fu, u, p, stats, autodiff, di_extras)
143
119
end
144
120
145
121
(cache:: JacobianCache )(u = cache. u) = cache (cache. J, u, cache. p)
@@ -172,27 +148,16 @@ function (cache::JacobianCache{iip})(
172
148
if iip
173
149
if SciMLBase. has_jac (cache. f)
174
150
cache. f. jac (J, u, p)
175
- elseif cache . di_extras != = nothing
151
+ else
176
152
DI. jacobian! (
177
153
cache. f, cache. fu, J, cache. di_extras, cache. autodiff, u, Constant (p))
178
- else
179
- uf = JacobianWrapper {iip} (cache. f, p)
180
- sparse_jacobian! (J, cache. autodiff, cache. sdifft_extras, uf, cache. fu, u)
181
154
end
182
155
return J
183
156
else
184
157
if SciMLBase. has_jac (cache. f)
185
158
return cache. f. jac (u, p)
186
- elseif cache. di_extras != = nothing
187
- return DI. jacobian (cache. f, cache. di_extras, cache. autodiff, u, Constant (p))
188
159
else
189
- uf = JacobianWrapper {iip} (cache. f, p)
190
- if __can_setindex (typeof (J))
191
- sparse_jacobian! (J, cache. autodiff, cache. sdifft_extras, uf, u)
192
- return J
193
- else
194
- return sparse_jacobian (cache. autodiff, cache. sdifft_extras, uf, u)
195
- end
160
+ return DI. jacobian (cache. f, cache. di_extras, cache. autodiff, u, Constant (p))
196
161
end
197
162
end
198
163
end
@@ -207,10 +172,6 @@ function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
207
172
end
208
173
return ad # No sparse AD
209
174
else
210
- if ArrayInterface. isstructured (f. jac_prototype)
211
- return select_fastest_structured_matrix_autodiff (f. jac_prototype, f, ad)
212
- end
213
-
214
175
return AutoSparse (
215
176
ad;
216
177
sparsity_detector = KnownJacobianSparsityDetector (f. jac_prototype),
@@ -227,10 +188,6 @@ function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
227
188
Base. depwarn (" `sparsity::typeof($(typeof (f. sparsity)) )` is deprecated. \
228
189
Pass it as `jac_prototype` instead." ,
229
190
:NonlinearSolve )
230
- if ArrayInterface. isstructured (f. sparsity)
231
- return select_fastest_structured_matrix_autodiff (f. sparsity, f, ad)
232
- end
233
-
234
191
return AutoSparse (
235
192
ad;
236
193
sparsity_detector = KnownJacobianSparsityDetector (f. sparsity),
@@ -252,11 +209,8 @@ function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
252
209
coloring_algorithm = GreedyColoringAlgorithm (LargestFirst ())
253
210
)
254
211
else
255
- if ArrayInterface. isstructured (f. jac_prototype)
256
- return select_fastest_structured_matrix_autodiff (f. jac_prototype, f, ad)
257
- end
258
-
259
- if f. jac_prototype isa AbstractSparseMatrix
212
+ if f. jac_prototype isa AbstractSparseMatrix ||
213
+ ArrayInterface. isstructured (f. jac_prototype)
260
214
if ! (sparsity_detector isa NoSparsityDetector)
261
215
@warn " `jac_prototype` is a sparse matrix but sparsity = $(f. sparsity) \
262
216
has also been specified. Ignoring sparsity field and using \
@@ -275,38 +229,6 @@ function construct_concrete_adtype(f::NonlinearFunction, ad::AbstractADType)
275
229
end
276
230
end
277
231
278
- @concrete struct StructuredMatrixAutodiff <: AbstractADType
279
- autodiff <: AbstractADType
280
- sparsity_detection
281
- end
282
-
283
- function select_fastest_structured_matrix_autodiff (
284
- prototype:: AbstractMatrix , f:: NonlinearFunction , ad:: AbstractADType )
285
- sparsity_detection = if SciMLBase. has_colorvec (f)
286
- PrecomputedJacobianColorvec (;
287
- jac_prototype = prototype,
288
- f. colorvec,
289
- partition_by_rows = ADTypes. mode (ad) isa ADTypes. ReverseMode
290
- )
291
- else
292
- if ArrayInterface. fast_matrix_colors (prototype)
293
- colorvec = if ADTypes. mode (ad) isa ADTypes. ForwardMode
294
- ArrayInterface. matrix_colors (prototype)
295
- else
296
- ArrayInterface. matrix_colors (prototype' )
297
- end
298
- PrecomputedJacobianColorvec (;
299
- jac_prototype = prototype,
300
- colorvec,
301
- partition_by_rows = ADTypes. mode (ad) isa ADTypes. ReverseMode
302
- )
303
- else
304
- JacPrototypeSparsityDetection (; jac_prototype = prototype)
305
- end
306
- end
307
- return StructuredMatrixAutodiff (AutoSparse (ad), sparsity_detection)
308
- end
309
-
310
232
function select_fastest_coloring_algorithm (
311
233
prototype, f:: NonlinearFunction , ad:: AbstractADType )
312
234
if SciMLBase. has_colorvec (f)
0 commit comments