-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Full support of sparse AD for NLS models (#239)
* Full support of sparse AD for NLS models * up test * Update dense hessian residual * Test SparseReverseADHessian * Add get_residual_nnzh * Update predefined_backend.jl * Update sparse_hessian.jl for NLS * Fix the dispatch * Fix jth_hess_residual --------- Co-authored-by: tmigot <[email protected]>
- Loading branch information
1 parent
a80023e
commit 4f55b76
Showing
9 changed files
with
167 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
list_sparse_hess_backend = ( | ||
(ADNLPModels.SparseADHessian, Dict()), | ||
(ADNLPModels.SparseReverseADHessian, Dict()), | ||
(ADNLPModels.ForwardDiffADHessian, Dict()), | ||
) | ||
|
||
dt = (Float32, Float64) | ||
|
||
@testset "Basic Hessian of residual derivative with backend=$(backend) and T=$(T)" for T in dt, | ||
(backend, kw) in list_sparse_hess_backend | ||
|
||
F!(Fx, x) = begin | ||
Fx[1] = x[1] - 1 | ||
Fx[2] = 10 * (x[2] - x[1]^2) | ||
Fx[3] = x[2] + 1 | ||
Fx | ||
end | ||
x0 = T[-1.2; 1.0] | ||
nvar = 2 | ||
nequ = 3 | ||
nls = ADNLPModels.ADNLSModel!(F!, x0, 3, hessian_residual_backend = backend; kw...) | ||
|
||
x = rand(T, nvar) | ||
v = rand(T, nequ) | ||
rows, cols = zeros(Int, nls.nls_meta.nnzh), zeros(Int, nls.nls_meta.nnzh) | ||
vals = zeros(T, nls.nls_meta.nnzh) | ||
hess_structure_residual!(nls, rows, cols) | ||
hess_coord_residual!(nls, x, v, vals) | ||
@test eltype(vals) == T | ||
H = Symmetric(sparse(rows, cols, vals, nvar, nvar), :L) | ||
@test H == [-20 * v[2] 0; 0 0] | ||
|
||
# Test also the implementation of the backends | ||
b = nls.adbackend.hessian_residual_backend | ||
@test nls.nls_meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar) | ||
ADNLPModels.hess_structure_residual!(b, nls, rows, cols) | ||
ADNLPModels.hess_coord_residual!(b, nls, x, v, vals) | ||
@test eltype(vals) == T | ||
H = Symmetric(sparse(rows, cols, vals, nvar, nvar), :L) | ||
@test H == [-20 * v[2] 0; 0 0] | ||
|
||
nls = ADNLPModels.ADNLSModel!(F!, x0, 3, matrix_free = true; kw...) | ||
@test nls.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend | ||
@test nls.adbackend.hessian_residual_backend isa ADNLPModels.EmptyADbackend | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters