forked from SciML/Optimization.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimizationDISparseExt.jl
More file actions
602 lines (560 loc) · 19.6 KB
/
Copy pathOptimizationDISparseExt.jl
File metadata and controls
602 lines (560 loc) · 19.6 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
using OptimizationBase
import OptimizationBase.ArrayInterface
import SciMLBase: OptimizationFunction
import OptimizationBase.LinearAlgebra: I
import DifferentiationInterface
import DifferentiationInterface: prepare_gradient, prepare_hessian, prepare_hvp,
prepare_jacobian, value_and_gradient!,
value_derivative_and_second_derivative!,
value_and_gradient, value_derivative_and_second_derivative,
gradient!, hessian!, hvp!, jacobian!, gradient, hessian,
hvp, jacobian
using ADTypes
using SparseConnectivityTracer, SparseMatrixColorings
function instantiate_function(
f::OptimizationFunction{true}, x, adtype::ADTypes.AutoSparse{<:AbstractADType},
p = SciMLBase.NullParameters(), num_cons = 0;
g = false, h = false, hv = false, fg = false, fgh = false,
cons_j = false, cons_vjp = false, cons_jvp = false, cons_h = false,
lag_h = false
)
adtype, soadtype = generate_sparse_adtype(adtype)
if g == true && f.grad === nothing
prep_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p))
function grad(res, θ)
return gradient!(f.f, res, prep_grad, adtype.dense_ad, θ, Constant(p))
end
if p !== SciMLBase.NullParameters()
function grad(res, θ, p)
return gradient!(f.f, res, prep_grad, adtype.dense_ad, θ, Constant(p))
end
end
elseif g == true
grad = (G, θ, p = p) -> f.grad(G, θ, p)
else
grad = nothing
end
if fg == true && f.fg === nothing
if g == false
prep_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p))
end
function fg!(res, θ)
(
y,
_,
) = value_and_gradient!(
f.f, res, prep_grad, adtype.dense_ad, θ, Constant(p)
)
return y
end
if p !== SciMLBase.NullParameters()
prep_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p))
function fg!(res, θ, p)
(
y,
_,
) = value_and_gradient!(
f.f, res, prep_grad, adtype.dense_ad, θ, Constant(p)
)
return y
end
end
elseif fg == true
fg! = (G, θ, p = p) -> f.fg(G, θ, p)
else
fg! = nothing
end
hess_sparsity = f.hess_prototype
hess_colors = f.hess_colorvec
if f.hess === nothing && h == true
prep_hess = prepare_hessian(f.f, soadtype, x, Constant(p))
function hess(res, θ)
return hessian!(f.f, res, prep_hess, soadtype, θ, Constant(p))
end
hess_sparsity = prep_hess.coloring_result.A
hess_colors = prep_hess.coloring_result.color
if p !== SciMLBase.NullParameters() && p !== nothing
function hess(res, θ, p)
return hessian!(f.f, res, prep_hess, soadtype, θ, Constant(p))
end
end
elseif h == true
hess = (H, θ, p = p) -> f.hess(H, θ, p)
else
hess = nothing
end
if fgh == true && f.fgh === nothing
function fgh!(G, H, θ)
(
y,
_,
_,
) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype.dense_ad, θ, Constant(p)
)
return y
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(G, H, θ, p)
(
y,
_,
_,
) = value_derivative_and_second_derivative!(
f.f, G, H, prep_hess, soadtype.dense_ad, θ, Constant(p)
)
return y
end
end
elseif fgh == true
fgh! = (G, H, θ, p = p) -> f.fgh(G, H, θ, p)
else
fgh! = nothing
end
if hv == true && f.hv === nothing
prep_hvp = prepare_hvp(
f.f, soadtype.dense_ad, x, (zeros(eltype(x), size(x)),), Constant(p)
)
function hv!(H, θ, v)
return only(hvp!(f.f, (H,), prep_hvp, soadtype.dense_ad, θ, (v,), Constant(p)))
end
if p !== SciMLBase.NullParameters() && p !== nothing
function hv!(H, θ, v, p)
return only(hvp!(f.f, (H,), prep_hvp, soadtype.dense_ad, θ, (v,), Constant(p)))
end
end
elseif hv == true
hv! = (H, θ, v, p = p) -> f.hv(H, θ, v, p)
else
hv! = nothing
end
if f.cons === nothing
cons = nothing
else
cons = let f = f, p = p
(res, θ, p_call = p) -> f.cons(res, θ, p_call)
end
end
function cons_oop(x)
_res = zeros(eltype(x), num_cons)
f.cons(_res, x, p)
return _res
end
function cons_oop(x, i)
_res = zeros(eltype(x), num_cons)
f.cons(_res, x, p)
return _res[i]
end
function lagrangian(θ, σ, λ, p)
if eltype(θ) <: SparseConnectivityTracer.AbstractTracer || !iszero(θ)
return σ * f.f(θ, p) + dot(λ, cons_oop(θ))
else
return dot(λ, cons_oop(θ))
end
end
cons_jac_prototype = f.cons_jac_prototype
cons_jac_colorvec = f.cons_jac_colorvec
if f.cons !== nothing && cons_j == true && f.cons_j === nothing
prep_jac = prepare_jacobian(cons_oop, adtype, x)
function cons_j!(J, θ)
jacobian!(cons_oop, J, prep_jac, adtype, θ)
return if size(J, 1) == 1
J = vec(J)
end
end
cons_jac_prototype = prep_jac.coloring_result.A
cons_jac_colorvec = prep_jac.coloring_result.color
elseif cons_j === true && f.cons !== nothing
cons_j! = (J, θ) -> f.cons_j(J, θ, p)
else
cons_j! = nothing
end
if f.cons_vjp === nothing && cons_vjp == true && f.cons !== nothing
prep_pullback = prepare_pullback(
cons_oop, adtype.dense_ad, x, (ones(eltype(x), num_cons),)
)
function cons_vjp!(J, θ, v)
return only(pullback!(cons_oop, (J,), prep_pullback, adtype.dense_ad, θ, (v,)))
end
elseif cons_vjp === true && f.cons !== nothing
cons_vjp! = (J, θ, v) -> f.cons_vjp(J, θ, v, p)
else
cons_vjp! = nothing
end
if f.cons_jvp === nothing && cons_jvp == true && f.cons !== nothing
prep_pushforward = prepare_pushforward(
cons_oop, adtype.dense_ad, x, (ones(eltype(x), length(x)),)
)
function cons_jvp!(J, θ, v)
return only(pushforward!(cons_oop, (J,), prep_pushforward, adtype.dense_ad, θ, (v,)))
end
elseif cons_jvp === true && f.cons !== nothing
cons_jvp! = (J, θ, v) -> f.cons_jvp(J, θ, v, p)
else
cons_jvp! = nothing
end
conshess_sparsity = f.cons_hess_prototype
conshess_colors = f.cons_hess_colorvec
if f.cons !== nothing && f.cons_h === nothing && cons_h == true
prep_cons_hess = [
prepare_hessian(cons_oop, soadtype, x, Constant(i))
for i in 1:num_cons
]
colores = getfield.(prep_cons_hess, :coloring_result)
conshess_sparsity = getfield.(colores, :A)
conshess_colors = getfield.(colores, :color)
function cons_h!(H, θ)
for i in 1:num_cons
hessian!(cons_oop, H[i], prep_cons_hess[i], soadtype, θ, Constant(i))
end
return
end
elseif cons_h == true && f.cons !== nothing
cons_h! = (res, θ) -> f.cons_h(res, θ, p)
else
cons_h! = nothing
end
lag_hess_prototype = f.lag_hess_prototype
lag_hess_colors = f.lag_hess_colorvec
if f.cons !== nothing && lag_h == true && f.lag_h === nothing
lag_prep = prepare_hessian(
lagrangian, soadtype, x, Constant(one(eltype(x))),
Constant(ones(eltype(x), num_cons)), Constant(p)
)
lag_hess_prototype = lag_prep.coloring_result.A
lag_hess_colors = lag_prep.coloring_result.color
function lag_h!(H::AbstractMatrix, θ, σ, λ)
return if σ == zero(eltype(θ))
cons_h!(H, θ)
H *= λ
else
hessian!(
lagrangian, H, lag_prep, soadtype, θ,
Constant(σ), Constant(λ), Constant(p)
)
end
end
function lag_h!(h, θ, σ, λ)
H = hessian(
lagrangian, lag_prep, soadtype, θ, Constant(σ), Constant(λ), Constant(p)
)
k = 0
rows, cols, _ = findnz(H)
for (i, j) in zip(rows, cols)
if i <= j
k += 1
h[k] = H[i, j]
end
end
return
end
if p !== SciMLBase.NullParameters() && p !== nothing
function lag_h!(H::AbstractMatrix, θ, σ, λ, p)
return if σ == zero(eltype(θ))
cons_h(H, θ)
H *= λ
else
hessian!(
lagrangian, H, lag_prep, soadtype, θ,
Constant(σ), Constant(λ), Constant(p)
)
end
end
function lag_h!(h, θ, σ, λ, p)
H = hessian(
lagrangian, lag_prep, soadtype, θ,
Constant(σ), Constant(λ), Constant(p)
)
k = 0
rows, cols, _ = findnz(H)
for (i, j) in zip(rows, cols)
if i <= j
k += 1
h[k] = H[i, j]
end
end
return
end
end
elseif lag_h == true
lag_h! = (H, θ, σ, λ, p = p) -> f.lag_h(H, θ, σ, λ, p)
else
lag_h! = nothing
end
return OptimizationFunction{true}(
f.f, adtype;
grad = grad, fg = fg!, hess = hess, hv = hv!, fgh = fgh!,
cons = cons, cons_j = cons_j!, cons_h = cons_h!,
cons_vjp = cons_vjp!, cons_jvp = cons_jvp!,
hess_prototype = hess_sparsity,
hess_colorvec = hess_colors,
cons_jac_prototype = cons_jac_prototype,
cons_jac_colorvec = cons_jac_colorvec,
cons_hess_prototype = conshess_sparsity,
cons_hess_colorvec = conshess_colors,
lag_h = lag_h!,
lag_hess_prototype = lag_hess_prototype,
lag_hess_colorvec = lag_hess_colors,
sys = f.sys,
expr = f.expr,
cons_expr = f.cons_expr
)
end
function instantiate_function(
f::OptimizationFunction{true}, cache::OptimizationBase.ReInitCache,
adtype::ADTypes.AutoSparse{<:AbstractADType}, num_cons = 0; kwargs...
)
x = cache.u0
p = cache.p
return instantiate_function(f, x, adtype, p, num_cons; kwargs...)
end
function instantiate_function(
f::OptimizationFunction{false}, x, adtype::ADTypes.AutoSparse{<:AbstractADType},
p = SciMLBase.NullParameters(), num_cons = 0;
g = false, h = false, hv = false, fg = false, fgh = false,
cons_j = false, cons_vjp = false, cons_jvp = false, cons_h = false,
lag_h = false
)
adtype, soadtype = generate_sparse_adtype(adtype)
if g == true && f.grad === nothing
prep_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p))
function grad(θ)
return gradient(f.f, prep_grad, adtype.dense_ad, θ, Constant(p))
end
if p !== SciMLBase.NullParameters() && p !== nothing
function grad(θ, p)
return gradient(f.f, prep_grad, adtype.dense_ad, θ, Constant(p))
end
end
elseif g == true
grad = (θ, p = p) -> f.grad(θ, p)
else
grad = nothing
end
if fg == true && f.fg === nothing
if g == false
prep_grad = prepare_gradient(f.f, adtype.dense_ad, x, Constant(p))
end
function fg!(θ)
(y, G) = value_and_gradient(f.f, prep_grad, adtype.dense_ad, θ, Constant(p))
return y, G
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fg!(θ, p)
(y, G) = value_and_gradient(f.f, prep_grad, adtype.dense_ad, θ, Constant(p))
return y, G
end
end
elseif fg == true
fg! = (θ, p = p) -> f.fg(θ, p)
else
fg! = nothing
end
if fgh == true && f.fgh === nothing
function fgh!(θ)
(
y,
G,
H,
) = value_derivative_and_second_derivative(
f.f, prep_hess, soadtype, θ, Constant(p)
)
return y, G, H
end
if p !== SciMLBase.NullParameters() && p !== nothing
function fgh!(θ, p)
(
y,
G,
H,
) = value_derivative_and_second_derivative(
f.f, prep_hess, soadtype, θ, Constant(p)
)
return y, G, H
end
end
elseif fgh == true
fgh! = (θ, p = p) -> f.fgh(θ, p)
else
fgh! = nothing
end
hess_sparsity = f.hess_prototype
hess_colors = f.hess_colorvec
if h == true && f.hess === nothing
prep_hess = prepare_hessian(f.f, soadtype, x, Constant(p))
function hess(θ)
return hessian(f.f, prep_hess, soadtype, θ, Constant(p))
end
hess_sparsity = prep_hess.coloring_result.A
hess_colors = prep_hess.coloring_result.color
if p !== SciMLBase.NullParameters() && p !== nothing
function hess(θ, p)
return hessian(f.f, prep_hess, soadtype, θ, Constant(p))
end
end
elseif h == true
hess = (θ, p = p) -> f.hess(θ, p)
else
hess = nothing
end
if hv == true && f.hv === nothing
prep_hvp = prepare_hvp(
f.f, soadtype.dense_ad, x, (zeros(eltype(x), size(x)),), Constant(p)
)
function hv!(θ, v)
return only(hvp(f.f, prep_hvp, soadtype.dense_ad, θ, (v,), Constant(p)))
end
if p !== SciMLBase.NullParameters() && p !== nothing
function hv!(θ, v, p)
return only(hvp(f.f, prep_hvp, soadtype.dense_ad, θ, (v,), Constant(p)))
end
end
elseif hv == true
hv! = (θ, v, p = p) -> f.hv(θ, v, p)
else
hv! = nothing
end
if f.cons === nothing
cons = nothing
else
cons = let f = f, p = p
(x, p_call = p) -> f.cons(x, p_call)
end
end
function lagrangian(θ, σ, λ, p)
return σ * f.f(θ, p) + dot(λ, f.cons(θ, p))
end
cons_jac_prototype = f.cons_jac_prototype
cons_jac_colorvec = f.cons_jac_colorvec
if f.cons !== nothing && cons_j == true && f.cons_j === nothing
prep_jac = prepare_jacobian(f.cons, adtype, x, Constant(p))
function cons_j!(θ)
J = jacobian(f.cons, prep_jac, adtype, θ, Constant(p))
if size(J, 1) == 1
J = vec(J)
end
return J
end
cons_jac_prototype = prep_jac.coloring_result.A
cons_jac_colorvec = prep_jac.coloring_result.color
elseif cons_j === true && f.cons !== nothing
cons_j! = (θ) -> f.cons_j(θ, p)
else
cons_j! = nothing
end
if f.cons_vjp === nothing && cons_vjp == true && f.cons !== nothing
prep_pullback = prepare_pullback(
f.cons, adtype.dense_ad, x, (ones(eltype(x), num_cons),), Constant(p)
)
function cons_vjp!(θ, v)
return only(pullback(f.cons, prep_pullback, adtype.dense_ad, θ, (v,), Constant(p)))
end
elseif cons_vjp === true && f.cons !== nothing
cons_vjp! = (θ, v) -> f.cons_vjp(θ, v, p)
else
cons_vjp! = nothing
end
if f.cons_jvp === nothing && cons_jvp == true && f.cons !== nothing
prep_pushforward = prepare_pushforward(
f.cons, adtype.dense_ad, x, (ones(eltype(x), length(x)),), Constant(p)
)
function cons_jvp!(θ, v)
return only(
pushforward(
f.cons, prep_pushforward, adtype.dense_ad, θ, (v,), Constant(p)
)
)
end
elseif cons_jvp === true && f.cons !== nothing
cons_jvp! = (θ, v) -> f.cons_jvp(θ, v, p)
else
cons_jvp! = nothing
end
conshess_sparsity = f.cons_hess_prototype
conshess_colors = f.cons_hess_colorvec
if f.cons !== nothing && cons_h == true && f.cons_h === nothing
function cons_i(x, i)
return f.cons(x, p)[i]
end
prep_cons_hess = [
prepare_hessian(cons_i, soadtype, x, Constant(i))
for i in 1:num_cons
]
function cons_h!(θ)
H = map(1:num_cons) do i
hessian(cons_i, prep_cons_hess[i], soadtype, θ, Constant(i))
end
return H
end
colores = getfield.(prep_cons_hess, :coloring_result)
conshess_sparsity = getfield.(colores, :A)
conshess_colors = getfield.(colores, :color)
elseif cons_h == true && f.cons !== nothing
cons_h! = (res, θ) -> f.cons_h(res, θ, p)
else
cons_h! = nothing
end
lag_hess_prototype = f.lag_hess_prototype
lag_hess_colors = f.lag_hess_colorvec
if f.cons !== nothing && lag_h == true && f.lag_h === nothing
lag_prep = prepare_hessian(
lagrangian, soadtype, x, Constant(one(eltype(x))),
Constant(ones(eltype(x), num_cons)), Constant(p)
)
function lag_h!(θ, σ, λ)
if σ == zero(eltype(θ))
return λ .* cons_h!(θ)
else
hess = hessian(
lagrangian, lag_prep, soadtype, θ,
Constant(σ), Constant(λ), Constant(p)
)
return hess
end
end
lag_hess_prototype = lag_prep.coloring_result.A
lag_hess_colors = lag_prep.coloring_result.color
if p !== SciMLBase.NullParameters() && p !== nothing
function lag_h!(θ, σ, λ, p)
if σ == zero(eltype(θ))
return λ .* cons_h!(θ)
else
hess = hessian(
lagrangian, lag_prep, θ, Constant(σ), Constant(λ), Constant(p)
)
return hess
end
end
end
elseif lag_h == true && f.cons !== nothing
lag_h! = (θ, σ, μ, p = p) -> f.lag_h(θ, σ, μ, p)
else
lag_h! = nothing
end
return OptimizationFunction{false}(
f.f, adtype;
grad = grad, fg = fg!, hess = hess, hv = hv!, fgh = fgh!,
cons = cons, cons_j = cons_j!, cons_h = cons_h!,
cons_vjp = cons_vjp!, cons_jvp = cons_jvp!,
hess_prototype = hess_sparsity,
hess_colorvec = hess_colors,
cons_jac_prototype = cons_jac_prototype,
cons_jac_colorvec = cons_jac_colorvec,
cons_hess_prototype = conshess_sparsity,
cons_hess_colorvec = conshess_colors,
lag_h = lag_h!,
lag_hess_prototype = lag_hess_prototype,
lag_hess_colorvec = lag_hess_colors,
sys = f.sys,
expr = f.expr,
cons_expr = f.cons_expr
)
end
function instantiate_function(
f::OptimizationFunction{false}, cache::OptimizationBase.ReInitCache,
adtype::ADTypes.AutoSparse{<:AbstractADType}, num_cons = 0; kwargs...
)
x = cache.u0
p = cache.p
return instantiate_function(f, x, adtype, p, num_cons; kwargs...)
end