Skip to content

Commit 1970207

Browse files
committed
Add allocation tests for ReverseAD
1 parent 2bd236f commit 1970207

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

test/Nonlinear/ReverseAD.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,28 @@ 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)]
149+
MOI.hessian_objective_structure(evaluator) == [(1, 1), (2, 2), (2, 1)]
146150
H = [NaN, NaN, NaN]
147-
MOI.eval_hessian_objective(evaluator, H, [1.2, 2.3])
151+
MOI.eval_hessian_objective(evaluator, H, val)
148152
@test H == [2.0, 2.0, 1.0]
153+
@test evaluator.backend.max_chunk == 2
154+
# The call of `_eval_hessian_inner` from `_eval_hessian` needs dynamic dispatch for `Val(chunk)` so it allocates.
155+
# We call directly `_eval_hessian_inner` to check that the rest does not allocates.
156+
@test @allocated MOI.Nonlinear.ReverseAD._eval_hessian_inner(evaluator.backend, evaluator.backend.objective, H, 1.0, 0, Val(2))
149157
@test MOI.hessian_lagrangian_structure(evaluator) ==
150158
[(1, 1), (2, 2), (2, 1)]
151159
H = [NaN, NaN, NaN]
152-
MOI.eval_hessian_lagrangian(evaluator, H, [1.2, 2.3], 1.5, Float64[])
160+
MOI.eval_hessian_lagrangian(evaluator, H, val, 1.5, Float64[])
153161
@test H == 1.5 .* [2.0, 2.0, 1.0]
162+
MOI.eval_hessian_lagrangian(evaluator, H, val, 1.5, Float64[])
154163
v = [0.3, 0.4]
155164
hv = [NaN, NaN]
156165
MOI.eval_hessian_lagrangian_product(

0 commit comments

Comments
 (0)