Skip to content

Commit 103fe75

Browse files
authored
[Nonlinear.ReverseAD] Fix use of reinterpret instead _reinterpret_unsafe (#2744)
1 parent e0d6bb3 commit 103fe75

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Nonlinear/ReverseAD/forward_over_reverse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function _forward_eval_ϵ(
190190
partials_storage_ϵ::AbstractVector{P},
191191
) where {N,T,P<:ForwardDiff.Partials{N,T}}
192192
storage_ϵ = _reinterpret_unsafe(P, d.storage_ϵ)
193-
x_values_ϵ = reinterpret(P, d.input_ϵ)
193+
x_values_ϵ = _reinterpret_unsafe(P, d.input_ϵ)
194194
subexpression_values_ϵ =
195195
_reinterpret_unsafe(P, d.subexpression_forward_values_ϵ)
196196
@assert length(storage_ϵ) >= length(ex.nodes)

test/Nonlinear/ReverseAD.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,28 @@ function test_generate_hessian_slice_inner()
13991399
return
14001400
end
14011401

1402+
function test_hessian_reinterpret_unsafe()
1403+
model = MOI.Nonlinear.Model()
1404+
x = MOI.VariableIndex.(1:5)
1405+
MOI.Nonlinear.add_constraint(
1406+
model,
1407+
:(($(x[1]) + $(x[2])) * $(x[3])),
1408+
MOI.EqualTo(0.0),
1409+
)
1410+
MOI.Nonlinear.add_constraint(model, :($(x[4]) * $(x[5])), MOI.EqualTo(1.0))
1411+
evaluator =
1412+
MOI.Nonlinear.Evaluator(model, MOI.Nonlinear.SparseReverseMode(), x)
1413+
MOI.initialize(evaluator, [:Hess])
1414+
H_s = MOI.hessian_lagrangian_structure(evaluator)
1415+
H = zeros(length(H_s))
1416+
x_v = ones(5)
1417+
MOI.eval_hessian_lagrangian(evaluator, H, x_v, 0.0, [1.0, 1.0])
1418+
@test count(isapprox.(H, 1.0; atol = 1e-8)) == 3
1419+
@test count(isapprox.(H, 0.0; atol = 1e-8)) == 6
1420+
@test sort(H_s[round.(Bool, H)]) == [(3, 1), (3, 2), (5, 4)]
1421+
return
1422+
end
1423+
14021424
end # module
14031425

14041426
TestReverseAD.runtests()

0 commit comments

Comments
 (0)