Skip to content

[Nonlinear.ReverseAD] Fix use of reinterpret instead _reinterpret_unsafe #2744

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 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Nonlinear/ReverseAD/forward_over_reverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function _forward_eval_ϵ(
partials_storage_ϵ::AbstractVector{P},
) where {N,T,P<:ForwardDiff.Partials{N,T}}
storage_ϵ = _reinterpret_unsafe(P, d.storage_ϵ)
x_values_ϵ = reinterpret(P, d.input_ϵ)
x_values_ϵ = _reinterpret_unsafe(P, d.input_ϵ)
subexpression_values_ϵ =
_reinterpret_unsafe(P, d.subexpression_forward_values_ϵ)
@assert length(storage_ϵ) >= length(ex.nodes)
Expand Down
22 changes: 22 additions & 0 deletions test/Nonlinear/ReverseAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,28 @@ function test_generate_hessian_slice_inner()
return
end

function test_hessian_reinterpret_unsafe()
model = MOI.Nonlinear.Model()
x = MOI.VariableIndex.(1:5)
MOI.Nonlinear.add_constraint(
model,
:(($(x[1]) + $(x[2])) * $(x[3])),
MOI.EqualTo(0.0),
)
MOI.Nonlinear.add_constraint(model, :($(x[4]) * $(x[5])), MOI.EqualTo(1.0))
evaluator =
MOI.Nonlinear.Evaluator(model, MOI.Nonlinear.SparseReverseMode(), x)
MOI.initialize(evaluator, [:Hess])
H_s = MOI.hessian_lagrangian_structure(evaluator)
H = zeros(length(H_s))
x_v = ones(5)
MOI.eval_hessian_lagrangian(evaluator, H, x_v, 0.0, [1.0, 1.0])
@test count(isapprox.(H, 1.0; atol = 1e-8)) == 3
@test count(isapprox.(H, 0.0; atol = 1e-8)) == 6
@test sort(H_s[round.(Bool, H)]) == [(3, 1), (3, 2), (5, 4)]
return
end

end # module

TestReverseAD.runtests()
Loading