Skip to content

Add allocation tests for ReverseAD #2737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions test/Nonlinear/ReverseAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,46 @@ function test_objective_quadratic_multivariate_subexpressions()
evaluator =
Nonlinear.Evaluator(model, Nonlinear.SparseReverseMode(), [x, y])
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
@test MOI.eval_objective(evaluator, [1.2, 2.3]) == 1.2^2 + 1.2 * 2.3 + 2.3^2
val = [1.2, 2.3]
@test MOI.eval_objective(evaluator, val) == 1.2^2 + 1.2 * 2.3 + 2.3^2
@test 0 == @allocated MOI.eval_objective(evaluator, val)
g = [NaN, NaN]
MOI.eval_objective_gradient(evaluator, g, [1.2, 2.3])
MOI.eval_objective_gradient(evaluator, g, val)
@test g == [2 * 1.2 + 2.3, 1.2 + 2 * 2.3]
@test 0 == @allocated MOI.eval_objective_gradient(evaluator, g, val)
@test MOI.hessian_objective_structure(evaluator) == [(1, 1), (2, 2), (2, 1)]
H = [NaN, NaN, NaN]
MOI.eval_hessian_objective(evaluator, H, [1.2, 2.3])
MOI.eval_hessian_objective(evaluator, H, val)
@test H == [2.0, 2.0, 1.0]
@test evaluator.backend.max_chunk == 2
# The call of `_eval_hessian_inner` from `_eval_hessian` needs dynamic dispatch for `Val(chunk)` so it allocates.
# We call directly `_eval_hessian_inner` to check that the rest does not allocates.
@test 0 == @allocated MOI.Nonlinear.ReverseAD._eval_hessian_inner(
evaluator.backend,
evaluator.backend.objective,
H,
1.0,
0,
Val(2),
)
@test MOI.hessian_lagrangian_structure(evaluator) ==
[(1, 1), (2, 2), (2, 1)]
H = [NaN, NaN, NaN]
MOI.eval_hessian_lagrangian(evaluator, H, [1.2, 2.3], 1.5, Float64[])
μ = Float64[]
MOI.eval_hessian_lagrangian(evaluator, H, val, 1.5, μ)
@test H == 1.5 .* [2.0, 2.0, 1.0]
v = [0.3, 0.4]
hv = [NaN, NaN]
MOI.eval_hessian_lagrangian_product(
MOI.eval_hessian_lagrangian_product(evaluator, hv, val, v, 1.5, μ)
@test hv ≈ 1.5 .* [2 1; 1 2] * v
@test 0 == @allocated MOI.eval_hessian_lagrangian_product(
evaluator,
hv,
[1.2, 2.3],
val,
v,
1.5,
Float64[],
μ,
)
@test hv ≈ 1.5 .* [2 1; 1 2] * v
return
end

Expand Down
Loading