Skip to content

Commit b4e88ab

Browse files
committed
Fix tests
1 parent d62b308 commit b4e88ab

File tree

6 files changed

+277
-258
lines changed

6 files changed

+277
-258
lines changed

test/enzyme.jl

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,35 @@ end
7575

7676
test_autodiff_backend_error()
7777

78+
include("sparse_jacobian.jl")
79+
include("sparse_jacobian_nls.jl")
80+
include("sparse_hessian.jl")
81+
include("sparse_hessian_nls.jl")
82+
83+
list_sparse_jac_backend = ((ADNLPModels.SparseEnzymeADJacobian, Dict()),)
84+
7885
@testset "Sparse Jacobian" begin
79-
list_sparse_jac_backend = ((ADNLPModels.SparseEnzymeADJacobian, Dict()),)
80-
include("sparse_jacobian.jl")
81-
include("sparse_jacobian_nls.jl")
86+
for (backend, kw) in list_sparse_jac_backend
87+
sparse_jacobian(backend, kw)
88+
sparse_jacobian_nls(backend, kw)
89+
end
8290
end
8391

92+
list_sparse_hess_backend = (
93+
( ADNLPModels.SparseEnzymeADHessian,
94+
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
95+
),
96+
(
97+
ADNLPModels.SparseEnzymeADHessian,
98+
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
99+
),
100+
)
101+
84102
@testset "Sparse Hessian" begin
85-
list_sparse_hess_backend = (
86-
( ADNLPModels.SparseEnzymeADHessian,
87-
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
88-
),
89-
(
90-
ADNLPModels.SparseEnzymeADHessian,
91-
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
92-
),
93-
)
94-
include("sparse_hessian.jl")
95-
include("sparse_hessian_nls.jl")
103+
for (backend, kw) in list_sparse_hess_backend
104+
sparse_hessian(backend, kw)
105+
sparse_hessian_nls(backend, kw)
106+
end
96107
end
97108

98109
for problem in NLPModelsTest.nlp_problems ["GENROSE"]

test/runtests.jl

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,43 @@ end
2323
include("manual.jl")
2424
end
2525

26+
include("sparse_jacobian.jl")
27+
include("sparse_jacobian_nls.jl")
28+
include("sparse_hessian.jl")
29+
include("sparse_hessian_nls.jl")
30+
31+
list_sparse_jac_backend = ((ADNLPModels.SparseADJacobian, Dict()),
32+
(ADNLPModels.ForwardDiffADJacobian, Dict()))
33+
2634
@testset "Sparse Jacobian" begin
27-
list_sparse_jac_backend = ((ADNLPModels.SparseADJacobian, Dict()),
28-
(ADNLPModels.ForwardDiffADJacobian, Dict()))
29-
include("sparse_jacobian.jl")
30-
include("sparse_jacobian_nls.jl")
35+
for (backend, kw) in list_sparse_jac_backend
36+
sparse_jacobian(backend, kw)
37+
sparse_jacobian_nls(backend, kw)
38+
end
3139
end
3240

41+
list_sparse_hess_backend = (
42+
(ADNLPModels.SparseADHessian, Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}())),
43+
(
44+
ADNLPModels.SparseADHessian,
45+
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
46+
),
47+
(
48+
ADNLPModels.SparseReverseADHessian,
49+
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
50+
),
51+
(
52+
ADNLPModels.SparseReverseADHessian,
53+
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
54+
),
55+
(ADNLPModels.ForwardDiffADHessian, Dict()),
56+
)
57+
3358
@testset "Sparse Hessian" begin
34-
list_sparse_hess_backend = (
35-
(ADNLPModels.SparseADHessian, Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}())),
36-
(
37-
ADNLPModels.SparseADHessian,
38-
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
39-
),
40-
(
41-
ADNLPModels.SparseReverseADHessian,
42-
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
43-
),
44-
(
45-
ADNLPModels.SparseReverseADHessian,
46-
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
47-
),
48-
(ADNLPModels.ForwardDiffADHessian, Dict()),
49-
)
50-
include("sparse_hessian.jl")
51-
include("sparse_hessian_nls.jl")
59+
for (backend, kw) in list_sparse_hess_backend
60+
sparse_hessian(backend, kw)
61+
sparse_hessian_nls(backend, kw)
62+
end
5263
end
5364

5465
for problem in NLPModelsTest.nlp_problems ["GENROSE"]

test/sparse_hessian.jl

Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
1-
@testset "Basic Hessian derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
2-
(backend, kw) in list_sparse_hess_backend
1+
function sparse_hessian(backend, kw)
2+
@testset "Basic Hessian derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
3+
c!(cx, x) = begin
4+
cx[1] = x[1] - 1
5+
cx[2] = 10 * (x[2] - x[1]^2)
6+
cx[3] = x[2] + 1
7+
cx
8+
end
9+
x0 = T[-1.2; 1.0]
10+
nvar = 2
11+
ncon = 3
12+
nlp = ADNLPModel!(
13+
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
14+
x0,
15+
c!,
16+
zeros(T, ncon),
17+
zeros(T, ncon),
18+
hessian_backend = backend;
19+
kw...,
20+
)
321

4-
c!(cx, x) = begin
5-
cx[1] = x[1] - 1
6-
cx[2] = 10 * (x[2] - x[1]^2)
7-
cx[3] = x[2] + 1
8-
cx
9-
end
10-
x0 = T[-1.2; 1.0]
11-
nvar = 2
12-
ncon = 3
13-
nlp = ADNLPModel!(
14-
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
15-
x0,
16-
c!,
17-
zeros(T, ncon),
18-
zeros(T, ncon),
19-
hessian_backend = backend;
20-
kw...,
21-
)
22-
23-
x = rand(T, 2)
24-
y = rand(T, 3)
25-
rows, cols = zeros(Int, nlp.meta.nnzh), zeros(Int, nlp.meta.nnzh)
26-
vals = zeros(T, nlp.meta.nnzh)
27-
hess_structure!(nlp, rows, cols)
28-
hess_coord!(nlp, x, vals)
29-
@test eltype(vals) == T
30-
H = sparse(rows, cols, vals, nvar, nvar)
31-
@test H == [2*x[2] 0; 2*(x[1] + x[2]) 2*x[1]]
22+
x = rand(T, 2)
23+
y = rand(T, 3)
24+
rows, cols = zeros(Int, nlp.meta.nnzh), zeros(Int, nlp.meta.nnzh)
25+
vals = zeros(T, nlp.meta.nnzh)
26+
hess_structure!(nlp, rows, cols)
27+
hess_coord!(nlp, x, vals)
28+
@test eltype(vals) == T
29+
H = sparse(rows, cols, vals, nvar, nvar)
30+
@test H == [2*x[2] 0; 2*(x[1] + x[2]) 2*x[1]]
3231

33-
# Test also the implementation of the backends
34-
b = nlp.adbackend.hessian_backend
35-
obj_weight = 0.5
36-
@test nlp.meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar)
37-
ADNLPModels.hess_structure!(b, nlp, rows, cols)
38-
ADNLPModels.hess_coord!(b, nlp, x, obj_weight, vals)
39-
@test eltype(vals) == T
40-
H = sparse(rows, cols, vals, nvar, nvar)
41-
@test H == [x[2] 0; x[1]+x[2] x[1]]
42-
ADNLPModels.hess_coord!(b, nlp, x, y, obj_weight, vals)
43-
@test eltype(vals) == T
44-
H = sparse(rows, cols, vals, nvar, nvar)
45-
@test H == [x[2] 0; x[1]+x[2] x[1]] + y[2] * [-20 0; 0 0]
32+
# Test also the implementation of the backends
33+
b = nlp.adbackend.hessian_backend
34+
obj_weight = 0.5
35+
@test nlp.meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar)
36+
ADNLPModels.hess_structure!(b, nlp, rows, cols)
37+
ADNLPModels.hess_coord!(b, nlp, x, obj_weight, vals)
38+
@test eltype(vals) == T
39+
H = sparse(rows, cols, vals, nvar, nvar)
40+
@test H == [x[2] 0; x[1]+x[2] x[1]]
41+
ADNLPModels.hess_coord!(b, nlp, x, y, obj_weight, vals)
42+
@test eltype(vals) == T
43+
H = sparse(rows, cols, vals, nvar, nvar)
44+
@test H == [x[2] 0; x[1]+x[2] x[1]] + y[2] * [-20 0; 0 0]
4645

47-
if backend != ADNLPModels.ForwardDiffADHessian
48-
H_sp = get_sparsity_pattern(nlp, :hessian)
49-
@test H_sp == SparseMatrixCSC{Bool, Int}([
50-
1 0
51-
1 1
52-
])
53-
end
46+
if backend != ADNLPModels.ForwardDiffADHessian
47+
H_sp = get_sparsity_pattern(nlp, :hessian)
48+
@test H_sp == SparseMatrixCSC{Bool, Int}([
49+
1 0
50+
1 1
51+
])
52+
end
5453

55-
nlp = ADNLPModel!(
56-
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
57-
x0,
58-
c!,
59-
zeros(T, ncon),
60-
zeros(T, ncon),
61-
matrix_free = true;
62-
kw...,
63-
)
64-
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
54+
nlp = ADNLPModel!(
55+
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
56+
x0,
57+
c!,
58+
zeros(T, ncon),
59+
zeros(T, ncon),
60+
matrix_free = true;
61+
kw...,
62+
)
63+
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
6564

66-
n = 4
67-
x = ones(T, 4)
68-
nlp = ADNLPModel(
69-
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
70-
x,
71-
hessian_backend = backend,
72-
name = "Extended Rosenbrock",
73-
)
74-
@test hess(nlp, x) == T[802 -400 0 0; -400 1002 -400 0; 0 -400 1002 -400; 0 0 -400 200]
65+
n = 4
66+
x = ones(T, 4)
67+
nlp = ADNLPModel(
68+
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
69+
x,
70+
hessian_backend = backend,
71+
name = "Extended Rosenbrock",
72+
)
73+
@test hess(nlp, x) == T[802 -400 0 0; -400 1002 -400 0; 0 -400 1002 -400; 0 0 -400 200]
7574

76-
x = ones(T, 2)
77-
nlp = ADNLPModel(x -> x[1]^2 + x[1] * x[2], x, hessian_backend = backend)
78-
@test hess(nlp, x) == T[2 1; 1 0]
75+
x = ones(T, 2)
76+
nlp = ADNLPModel(x -> x[1]^2 + x[1] * x[2], x, hessian_backend = backend)
77+
@test hess(nlp, x) == T[2 1; 1 0]
7978

80-
nlp = ADNLPModel(
81-
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
82-
x,
83-
name = "Extended Rosenbrock",
84-
matrix_free = true,
85-
)
86-
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
79+
nlp = ADNLPModel(
80+
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
81+
x,
82+
name = "Extended Rosenbrock",
83+
matrix_free = true,
84+
)
85+
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
86+
end
8787
end

test/sparse_hessian_nls.jl

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
1-
if test_enzyme
2-
list_sparse_hess_backend = (
3-
( ADNLPModels.SparseEnzymeADHessian,
4-
@testset "Basic Hessian of residual derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
5-
(backend, kw) in list_sparse_hess_backend
1+
function sparse_hessian_nls(backend, kw)
2+
@testset "Basic Hessian of residual derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
3+
F!(Fx, x) = begin
4+
Fx[1] = x[1] - 1
5+
Fx[2] = 10 * (x[2] - x[1]^2)
6+
Fx[3] = x[2] + 1
7+
Fx
8+
end
9+
x0 = T[-1.2; 1.0]
10+
nvar = 2
11+
nequ = 3
12+
nls = ADNLPModels.ADNLSModel!(F!, x0, 3, hessian_residual_backend = backend; kw...)
613

7-
F!(Fx, x) = begin
8-
Fx[1] = x[1] - 1
9-
Fx[2] = 10 * (x[2] - x[1]^2)
10-
Fx[3] = x[2] + 1
11-
Fx
12-
end
13-
x0 = T[-1.2; 1.0]
14-
nvar = 2
15-
nequ = 3
16-
nls = ADNLPModels.ADNLSModel!(F!, x0, 3, hessian_residual_backend = backend; kw...)
14+
x = rand(T, nvar)
15+
v = rand(T, nequ)
16+
rows, cols = zeros(Int, nls.nls_meta.nnzh), zeros(Int, nls.nls_meta.nnzh)
17+
vals = zeros(T, nls.nls_meta.nnzh)
18+
hess_structure_residual!(nls, rows, cols)
19+
hess_coord_residual!(nls, x, v, vals)
20+
@test eltype(vals) == T
21+
H = Symmetric(sparse(rows, cols, vals, nvar, nvar), :L)
22+
@test H == [-20*v[2] 0; 0 0]
1723

18-
x = rand(T, nvar)
19-
v = rand(T, nequ)
20-
rows, cols = zeros(Int, nls.nls_meta.nnzh), zeros(Int, nls.nls_meta.nnzh)
21-
vals = zeros(T, nls.nls_meta.nnzh)
22-
hess_structure_residual!(nls, rows, cols)
23-
hess_coord_residual!(nls, x, v, vals)
24-
@test eltype(vals) == T
25-
H = Symmetric(sparse(rows, cols, vals, nvar, nvar), :L)
26-
@test H == [-20*v[2] 0; 0 0]
24+
# Test also the implementation of the backends
25+
b = nls.adbackend.hessian_residual_backend
26+
@test nls.nls_meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar)
27+
ADNLPModels.hess_structure_residual!(b, nls, rows, cols)
28+
ADNLPModels.hess_coord_residual!(b, nls, x, v, vals)
29+
@test eltype(vals) == T
30+
H = Symmetric(sparse(rows, cols, vals, nvar, nvar), :L)
31+
@test H == [-20*v[2] 0; 0 0]
2732

28-
# Test also the implementation of the backends
29-
b = nls.adbackend.hessian_residual_backend
30-
@test nls.nls_meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar)
31-
ADNLPModels.hess_structure_residual!(b, nls, rows, cols)
32-
ADNLPModels.hess_coord_residual!(b, nls, x, v, vals)
33-
@test eltype(vals) == T
34-
H = Symmetric(sparse(rows, cols, vals, nvar, nvar), :L)
35-
@test H == [-20*v[2] 0; 0 0]
33+
if backend != ADNLPModels.ForwardDiffADHessian
34+
H_sp = get_sparsity_pattern(nls, :hessian_residual)
35+
@test H_sp == SparseMatrixCSC{Bool, Int}([
36+
1 0
37+
0 0
38+
])
39+
end
3640

37-
if backend != ADNLPModels.ForwardDiffADHessian
38-
H_sp = get_sparsity_pattern(nls, :hessian_residual)
39-
@test H_sp == SparseMatrixCSC{Bool, Int}([
40-
1 0
41-
0 0
42-
])
41+
nls = ADNLPModels.ADNLSModel!(F!, x0, 3, matrix_free = true; kw...)
42+
@test nls.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
43+
@test nls.adbackend.hessian_residual_backend isa ADNLPModels.EmptyADbackend
4344
end
44-
45-
nls = ADNLPModels.ADNLSModel!(F!, x0, 3, matrix_free = true; kw...)
46-
@test nls.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
47-
@test nls.adbackend.hessian_residual_backend isa ADNLPModels.EmptyADbackend
4845
end

0 commit comments

Comments
 (0)