Skip to content

Commit be6eaba

Browse files
authored
[Nonlinear.ReverseAD] add allocation tests (#2737)
1 parent cd7248f commit be6eaba

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

test/Nonlinear/ReverseAD.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,30 +138,46 @@ function test_objective_quadratic_multivariate_subexpressions()
138138
evaluator =
139139
Nonlinear.Evaluator(model, Nonlinear.SparseReverseMode(), [x, y])
140140
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
141-
@test MOI.eval_objective(evaluator, [1.2, 2.3]) == 1.2^2 + 1.2 * 2.3 + 2.3^2
141+
val = [1.2, 2.3]
142+
@test MOI.eval_objective(evaluator, val) == 1.2^2 + 1.2 * 2.3 + 2.3^2
143+
@test 0 == @allocated MOI.eval_objective(evaluator, val)
142144
g = [NaN, NaN]
143-
MOI.eval_objective_gradient(evaluator, g, [1.2, 2.3])
145+
MOI.eval_objective_gradient(evaluator, g, val)
144146
@test g == [2 * 1.2 + 2.3, 1.2 + 2 * 2.3]
147+
@test 0 == @allocated MOI.eval_objective_gradient(evaluator, g, val)
145148
@test MOI.hessian_objective_structure(evaluator) == [(1, 1), (2, 2), (2, 1)]
146149
H = [NaN, NaN, NaN]
147-
MOI.eval_hessian_objective(evaluator, H, [1.2, 2.3])
150+
MOI.eval_hessian_objective(evaluator, H, val)
148151
@test H == [2.0, 2.0, 1.0]
152+
@test evaluator.backend.max_chunk == 2
153+
# The call of `_eval_hessian_inner` from `_eval_hessian` needs dynamic dispatch for `Val(chunk)` so it allocates.
154+
# We call directly `_eval_hessian_inner` to check that the rest does not allocates.
155+
@test 0 == @allocated MOI.Nonlinear.ReverseAD._eval_hessian_inner(
156+
evaluator.backend,
157+
evaluator.backend.objective,
158+
H,
159+
1.0,
160+
0,
161+
Val(2),
162+
)
149163
@test MOI.hessian_lagrangian_structure(evaluator) ==
150164
[(1, 1), (2, 2), (2, 1)]
151165
H = [NaN, NaN, NaN]
152-
MOI.eval_hessian_lagrangian(evaluator, H, [1.2, 2.3], 1.5, Float64[])
166+
μ = Float64[]
167+
MOI.eval_hessian_lagrangian(evaluator, H, val, 1.5, μ)
153168
@test H == 1.5 .* [2.0, 2.0, 1.0]
154169
v = [0.3, 0.4]
155170
hv = [NaN, NaN]
156-
MOI.eval_hessian_lagrangian_product(
171+
MOI.eval_hessian_lagrangian_product(evaluator, hv, val, v, 1.5, μ)
172+
@test hv 1.5 .* [2 1; 1 2] * v
173+
@test 0 == @allocated MOI.eval_hessian_lagrangian_product(
157174
evaluator,
158175
hv,
159-
[1.2, 2.3],
176+
val,
160177
v,
161178
1.5,
162-
Float64[],
179+
μ,
163180
)
164-
@test hv 1.5 .* [2 1; 1 2] * v
165181
return
166182
end
167183

0 commit comments

Comments
 (0)