Skip to content

[Nonlinear.ReverseAD] improve test coverage #2646

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
Feb 20, 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
24 changes: 1 addition & 23 deletions src/Nonlinear/ReverseAD/graph_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,7 @@
end
for k in 2:length(nodes)
nod = nodes[k]
if nod.type == Nonlinear.NODE_MOI_VARIABLE
error(
"Internal error: Invalid to compute sparsity if Nonlinear.NODE_MOI_VARIABLE " *
"nodes are present.",
)
end
@assert nod.type != Nonlinear.NODE_MOI_VARIABLE

Check warning on line 231 in src/Nonlinear/ReverseAD/graph_tools.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/graph_tools.jl#L231

Added line #L231 was not covered by tests
if nonlinear_wrt_output[k]
continue # already seen this node one way or another
elseif input_linearity[k] == CONSTANT
Expand Down Expand Up @@ -473,20 +468,3 @@
)
return full_sort, individual_sorts
end

"""
_order_subexpressions(
main_expression::Vector{Nonlinear.Node},
subexpressions::Vector{Vector{Nonlinear.Node}},
)

Return a `Vector{Int}` containing the topologically ordered list of
subexpression-indices that need to be evaluated to compute `main_expression`.
"""
function _order_subexpressions(
main_expression::Vector{Nonlinear.Node},
subexpressions::Vector{Vector{Nonlinear.Node}},
)
s = _list_subexpressions(main_expression)
return _topological_sort(s, subexpressions)
end
71 changes: 71 additions & 0 deletions test/Nonlinear/ReverseAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,77 @@ function test_unsafe_vector_view()
return
end

function test_classify_linearity_ifelse()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
model = MOI.Nonlinear.Model()
MOI.Nonlinear.set_objective(model, :(ifelse($y, $x, 1)))
evaluator = MOI.Nonlinear.Evaluator(
model,
MOI.Nonlinear.SparseReverseMode(),
[x, y],
)
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
@test MOI.eval_objective(evaluator, [1.2, 1.0]) == 1.2
@test MOI.eval_objective(evaluator, [1.2, 0.0]) == 1.0
@test isempty(MOI.hessian_lagrangian_structure(evaluator))
return
end

function test_classify_linearity_logic()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
model = MOI.Nonlinear.Model()
MOI.Nonlinear.set_objective(model, :($x && $y))
evaluator = MOI.Nonlinear.Evaluator(
model,
MOI.Nonlinear.SparseReverseMode(),
[x, y],
)
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
@test MOI.eval_objective(evaluator, [1.0, 1.0]) == 1.0
@test MOI.eval_objective(evaluator, [0.0, 1.0]) == 0.0
@test MOI.eval_objective(evaluator, [1.0, 0.0]) == 0.0
@test MOI.eval_objective(evaluator, [0.0, 0.0]) == 0.0
@test isempty(MOI.hessian_lagrangian_structure(evaluator))
return
end

function test_hessian_sparsity_with_subexpressions()
x = MOI.VariableIndex(1)
y = MOI.VariableIndex(2)
model = MOI.Nonlinear.Model()
expr = MOI.Nonlinear.add_expression(model, :($x * $y))
expr2 = MOI.Nonlinear.add_expression(model, :($expr))
MOI.Nonlinear.set_objective(model, :(sin($expr2)))
evaluator = MOI.Nonlinear.Evaluator(
model,
MOI.Nonlinear.SparseReverseMode(),
[x, y],
)
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
MOI.hessian_lagrangian_structure(evaluator)
return
end

function test_toposort_subexpressions()
x = MOI.VariableIndex(1)
model = MOI.Nonlinear.Model()
a = MOI.Nonlinear.add_expression(model, :($x))
b = MOI.Nonlinear.add_expression(model, :($x))
c = MOI.Nonlinear.add_expression(model, :($a + $b))
d = MOI.Nonlinear.add_expression(model, :($c + $b))
MOI.Nonlinear.add_constraint(model, :($d), MOI.LessThan(1.0))
MOI.Nonlinear.add_constraint(model, :($c), MOI.LessThan(1.0))
evaluator =
MOI.Nonlinear.Evaluator(model, MOI.Nonlinear.SparseReverseMode(), [x])
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
g = [NaN, NaN]
MOI.eval_constraint(evaluator, g, [2.0])
@test g == [6.0, 4.0]
return
end

end # module

TestReverseAD.runtests()
Loading