Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amontoison committed Dec 21, 2024
1 parent d62b308 commit b4e88ab
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 258 deletions.
39 changes: 25 additions & 14 deletions test/enzyme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,35 @@ end

test_autodiff_backend_error()

include("sparse_jacobian.jl")
include("sparse_jacobian_nls.jl")
include("sparse_hessian.jl")
include("sparse_hessian_nls.jl")

list_sparse_jac_backend = ((ADNLPModels.SparseEnzymeADJacobian, Dict()),)

@testset "Sparse Jacobian" begin
list_sparse_jac_backend = ((ADNLPModels.SparseEnzymeADJacobian, Dict()),)
include("sparse_jacobian.jl")
include("sparse_jacobian_nls.jl")
for (backend, kw) in list_sparse_jac_backend
sparse_jacobian(backend, kw)
sparse_jacobian_nls(backend, kw)
end
end

list_sparse_hess_backend = (
( ADNLPModels.SparseEnzymeADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
),
(
ADNLPModels.SparseEnzymeADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
),
)

@testset "Sparse Hessian" begin
list_sparse_hess_backend = (
( ADNLPModels.SparseEnzymeADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
),
(
ADNLPModels.SparseEnzymeADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
),
)
include("sparse_hessian.jl")
include("sparse_hessian_nls.jl")
for (backend, kw) in list_sparse_hess_backend
sparse_hessian(backend, kw)
sparse_hessian_nls(backend, kw)
end
end

for problem in NLPModelsTest.nlp_problems ["GENROSE"]
Expand Down
55 changes: 33 additions & 22 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,43 @@ end
include("manual.jl")
end

include("sparse_jacobian.jl")
include("sparse_jacobian_nls.jl")
include("sparse_hessian.jl")
include("sparse_hessian_nls.jl")

list_sparse_jac_backend = ((ADNLPModels.SparseADJacobian, Dict()),
(ADNLPModels.ForwardDiffADJacobian, Dict()))

@testset "Sparse Jacobian" begin
list_sparse_jac_backend = ((ADNLPModels.SparseADJacobian, Dict()),
(ADNLPModels.ForwardDiffADJacobian, Dict()))
include("sparse_jacobian.jl")
include("sparse_jacobian_nls.jl")
for (backend, kw) in list_sparse_jac_backend
sparse_jacobian(backend, kw)
sparse_jacobian_nls(backend, kw)
end
end

list_sparse_hess_backend = (
(ADNLPModels.SparseADHessian, Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}())),
(
ADNLPModels.SparseADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
),
(
ADNLPModels.SparseReverseADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
),
(
ADNLPModels.SparseReverseADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
),
(ADNLPModels.ForwardDiffADHessian, Dict()),
)

@testset "Sparse Hessian" begin
list_sparse_hess_backend = (
(ADNLPModels.SparseADHessian, Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}())),
(
ADNLPModels.SparseADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
),
(
ADNLPModels.SparseReverseADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:direct}()),
),
(
ADNLPModels.SparseReverseADHessian,
Dict(:coloring_algorithm => GreedyColoringAlgorithm{:substitution}()),
),
(ADNLPModels.ForwardDiffADHessian, Dict()),
)
include("sparse_hessian.jl")
include("sparse_hessian_nls.jl")
for (backend, kw) in list_sparse_hess_backend
sparse_hessian(backend, kw)
sparse_hessian_nls(backend, kw)
end
end

for problem in NLPModelsTest.nlp_problems ["GENROSE"]
Expand Down
158 changes: 79 additions & 79 deletions test/sparse_hessian.jl
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
@testset "Basic Hessian derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
(backend, kw) in list_sparse_hess_backend
function sparse_hessian(backend, kw)
@testset "Basic Hessian derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
c!(cx, x) = begin
cx[1] = x[1] - 1
cx[2] = 10 * (x[2] - x[1]^2)
cx[3] = x[2] + 1
cx
end
x0 = T[-1.2; 1.0]
nvar = 2
ncon = 3
nlp = ADNLPModel!(
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
x0,
c!,
zeros(T, ncon),
zeros(T, ncon),
hessian_backend = backend;
kw...,
)

c!(cx, x) = begin
cx[1] = x[1] - 1
cx[2] = 10 * (x[2] - x[1]^2)
cx[3] = x[2] + 1
cx
end
x0 = T[-1.2; 1.0]
nvar = 2
ncon = 3
nlp = ADNLPModel!(
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
x0,
c!,
zeros(T, ncon),
zeros(T, ncon),
hessian_backend = backend;
kw...,
)

x = rand(T, 2)
y = rand(T, 3)
rows, cols = zeros(Int, nlp.meta.nnzh), zeros(Int, nlp.meta.nnzh)
vals = zeros(T, nlp.meta.nnzh)
hess_structure!(nlp, rows, cols)
hess_coord!(nlp, x, vals)
@test eltype(vals) == T
H = sparse(rows, cols, vals, nvar, nvar)
@test H == [2*x[2] 0; 2*(x[1] + x[2]) 2*x[1]]
x = rand(T, 2)
y = rand(T, 3)
rows, cols = zeros(Int, nlp.meta.nnzh), zeros(Int, nlp.meta.nnzh)
vals = zeros(T, nlp.meta.nnzh)
hess_structure!(nlp, rows, cols)
hess_coord!(nlp, x, vals)
@test eltype(vals) == T
H = sparse(rows, cols, vals, nvar, nvar)
@test H == [2*x[2] 0; 2*(x[1] + x[2]) 2*x[1]]

# Test also the implementation of the backends
b = nlp.adbackend.hessian_backend
obj_weight = 0.5
@test nlp.meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar)
ADNLPModels.hess_structure!(b, nlp, rows, cols)
ADNLPModels.hess_coord!(b, nlp, x, obj_weight, vals)
@test eltype(vals) == T
H = sparse(rows, cols, vals, nvar, nvar)
@test H == [x[2] 0; x[1]+x[2] x[1]]
ADNLPModels.hess_coord!(b, nlp, x, y, obj_weight, vals)
@test eltype(vals) == T
H = sparse(rows, cols, vals, nvar, nvar)
@test H == [x[2] 0; x[1]+x[2] x[1]] + y[2] * [-20 0; 0 0]
# Test also the implementation of the backends
b = nlp.adbackend.hessian_backend
obj_weight = 0.5
@test nlp.meta.nnzh == ADNLPModels.get_nln_nnzh(b, nvar)
ADNLPModels.hess_structure!(b, nlp, rows, cols)
ADNLPModels.hess_coord!(b, nlp, x, obj_weight, vals)
@test eltype(vals) == T
H = sparse(rows, cols, vals, nvar, nvar)
@test H == [x[2] 0; x[1]+x[2] x[1]]
ADNLPModels.hess_coord!(b, nlp, x, y, obj_weight, vals)
@test eltype(vals) == T
H = sparse(rows, cols, vals, nvar, nvar)
@test H == [x[2] 0; x[1]+x[2] x[1]] + y[2] * [-20 0; 0 0]

if backend != ADNLPModels.ForwardDiffADHessian
H_sp = get_sparsity_pattern(nlp, :hessian)
@test H_sp == SparseMatrixCSC{Bool, Int}([
1 0
1 1
])
end
if backend != ADNLPModels.ForwardDiffADHessian
H_sp = get_sparsity_pattern(nlp, :hessian)
@test H_sp == SparseMatrixCSC{Bool, Int}([
1 0
1 1
])
end

nlp = ADNLPModel!(
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
x0,
c!,
zeros(T, ncon),
zeros(T, ncon),
matrix_free = true;
kw...,
)
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
nlp = ADNLPModel!(
x -> x[1] * x[2]^2 + x[1]^2 * x[2],
x0,
c!,
zeros(T, ncon),
zeros(T, ncon),
matrix_free = true;
kw...,
)
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend

n = 4
x = ones(T, 4)
nlp = ADNLPModel(
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
x,
hessian_backend = backend,
name = "Extended Rosenbrock",
)
@test hess(nlp, x) == T[802 -400 0 0; -400 1002 -400 0; 0 -400 1002 -400; 0 0 -400 200]
n = 4
x = ones(T, 4)
nlp = ADNLPModel(
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
x,
hessian_backend = backend,
name = "Extended Rosenbrock",
)
@test hess(nlp, x) == T[802 -400 0 0; -400 1002 -400 0; 0 -400 1002 -400; 0 0 -400 200]

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

nlp = ADNLPModel(
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
x,
name = "Extended Rosenbrock",
matrix_free = true,
)
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
nlp = ADNLPModel(
x -> sum(100 * (x[i + 1] - x[i]^2)^2 + (x[i] - 1)^2 for i = 1:(n - 1)),
x,
name = "Extended Rosenbrock",
matrix_free = true,
)
@test nlp.adbackend.hessian_backend isa ADNLPModels.EmptyADbackend
end
end
81 changes: 39 additions & 42 deletions test/sparse_hessian_nls.jl
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
if test_enzyme
list_sparse_hess_backend = (
( ADNLPModels.SparseEnzymeADHessian,
@testset "Basic Hessian of residual derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
(backend, kw) in list_sparse_hess_backend
function sparse_hessian_nls(backend, kw)
@testset "Basic Hessian of residual derivative with backend=$(backend) and T=$(T)" for T in (Float32, Float64),
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...)

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]

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]

# 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]
if backend != ADNLPModels.ForwardDiffADHessian
H_sp = get_sparsity_pattern(nls, :hessian_residual)
@test H_sp == SparseMatrixCSC{Bool, Int}([
1 0
0 0
])
end

if backend != ADNLPModels.ForwardDiffADHessian
H_sp = get_sparsity_pattern(nls, :hessian_residual)
@test H_sp == SparseMatrixCSC{Bool, Int}([
1 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

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
Loading

0 comments on commit b4e88ab

Please sign in to comment.