Skip to content

Commit 9fa53c2

Browse files
committed
Test no allocations in tests
1 parent d4f354d commit 9fa53c2

File tree

3 files changed

+6
-14
lines changed

3 files changed

+6
-14
lines changed

src/Nonlinear/ReverseAD/forward_over_reverse.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
const TAG = :ReverseAD
88

9+
const MAX_CHUNK = 10
10+
911
function _generate_eval_hessian()
10-
exprs = map(1:10) do chunk
12+
exprs = map(1:MAX_CHUNK) do chunk
1113
return :(return _eval_hessian_inner(d, f, H, λ, offset, Val($chunk)))
1214
end
13-
return Nonlinear._create_binary_switch(1:10, exprs)
15+
return Nonlinear._create_binary_switch(1:MAX_CHUNK, exprs)
1416
end
1517

1618
@eval begin

src/Nonlinear/ReverseAD/mathoptinterface_api.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
140140
max_expr_length = max(max_expr_length, length(d.constraints[end].nodes))
141141
max_chunk = max(max_chunk, size(d.constraints[end].seed_matrix, 2))
142142
end
143-
# 10 is hardcoded upper bound to avoid excess memory allocation
144-
max_chunk = min(max_chunk, 10)
143+
max_chunk = min(max_chunk, MAX_CHUNK)
145144
max_expr_with_sub_length = max(max_expr_with_sub_length, max_expr_length)
146145
if d.want_hess || want_hess_storage
147146
d.input_ϵ = zeros(max_chunk * N)

test/Nonlinear/ReverseAD.jl

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,7 @@ function test_objective_quadratic_multivariate_subexpressions()
150150
MOI.eval_hessian_objective(evaluator, H, val)
151151
@test H == [2.0, 2.0, 1.0]
152152
@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-
)
153+
@test 0 == @allocated MOI.eval_hessian_objective(evaluator, H, val)
163154
@test MOI.hessian_lagrangian_structure(evaluator) ==
164155
[(1, 1), (2, 2), (2, 1)]
165156
H = [NaN, NaN, NaN]

0 commit comments

Comments
 (0)