Skip to content
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

[Nonlinear] allow univariate operators with only gradient information #2542

Merged
merged 2 commits into from
Sep 9, 2024
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
14 changes: 8 additions & 6 deletions src/Nonlinear/ReverseAD/mathoptinterface_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.

_no_hessian(op::MOI.Nonlinear._UnivariateOperator) = op.f′′ === nothing
_no_hessian(op::MOI.Nonlinear._MultivariateOperator) = op.∇²f === nothing

Check warning on line 8 in src/Nonlinear/ReverseAD/mathoptinterface_api.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/mathoptinterface_api.jl#L7-L8

Added lines #L7 - L8 were not covered by tests

function MOI.features_available(d::NLPEvaluator)
# Check if we are missing any hessians for user-defined multivariate
# operators, in which case we need to disable :Hess and :HessVec.
d.disable_2ndorder = any(
op -> op.∇²f === nothing,
d.data.operators.registered_multivariate_operators,
)
# Check if we are missing any hessians for user-defined operators, in which
# case we need to disable :Hess and :HessVec.
d.disable_2ndorder =

Check warning on line 13 in src/Nonlinear/ReverseAD/mathoptinterface_api.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/ReverseAD/mathoptinterface_api.jl#L13

Added line #L13 was not covered by tests
any(_no_hessian, d.data.operators.registered_univariate_operators) ||
any(_no_hessian, d.data.operators.registered_multivariate_operators)
if d.disable_2ndorder
return [:Grad, :Jac, :JacVec]
end
Expand Down
20 changes: 15 additions & 5 deletions src/Nonlinear/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
f::F
f′::F′
f′′::F′′
function _UnivariateOperator(

Check warning on line 57 in src/Nonlinear/operators.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/operators.jl#L57

Added line #L57 was not covered by tests
f::Function,
f′::Function,
f′′::Union{Nothing,Function} = nothing,
)
return new{typeof(f),typeof(f′),typeof(f′′)}(f, f′, f′′)

Check warning on line 62 in src/Nonlinear/operators.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/operators.jl#L62

Added line #L62 was not covered by tests
end
end

struct _MultivariateOperator{F,F′,F′′}
Expand Down Expand Up @@ -339,14 +346,17 @@
function _UnivariateOperator(op::Symbol, f::Function)
_validate_register_assumptions(f, op, 1)
f′ = _checked_derivative(f, op)
f′′ = _checked_derivative(f′, op)
return _UnivariateOperator(f, f′, f′′)
return _UnivariateOperator(op, f, f′)

Check warning on line 349 in src/Nonlinear/operators.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/operators.jl#L349

Added line #L349 was not covered by tests
end

function _UnivariateOperator(op::Symbol, f::Function, f′::Function)
_validate_register_assumptions(f′, op, 1)
f′′ = _checked_derivative(f′, op)
return _UnivariateOperator(f, f′, f′′)
try
_validate_register_assumptions(f′, op, 1)
f′′ = _checked_derivative(f′, op)
return _UnivariateOperator(f, f′, f′′)

Check warning on line 356 in src/Nonlinear/operators.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/operators.jl#L353-L356

Added lines #L353 - L356 were not covered by tests
Copy link
Member Author

@odow odow Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess one open question is this behavior: do we always try to ForwardDiff f′ and fall back to not providing the Hessian if it fails?

catch
return _UnivariateOperator(f, f′, nothing)

Check warning on line 358 in src/Nonlinear/operators.jl

View check run for this annotation

Codecov / codecov/patch

src/Nonlinear/operators.jl#L358

Added line #L358 was not covered by tests
end
end

function _UnivariateOperator(::Symbol, f::Function, f′::Function, f′′::Function)
Expand Down
17 changes: 17 additions & 0 deletions test/Nonlinear/ReverseAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,23 @@ function test_varying_length_x()
return
end

function test_univariate_operator_with_no_second_order()
f(x::Float64) = x^2
df(x::Float64) = 2 * x
model = MOI.Nonlinear.Model()
MOI.Nonlinear.register_operator(model, :op_f, 1, f, df)
x = MOI.VariableIndex(1)
MOI.Nonlinear.add_constraint(model, :(op_f($x)), MOI.LessThan(2.0))
evaluator =
MOI.Nonlinear.Evaluator(model, MOI.Nonlinear.SparseReverseMode(), [x])
@test !(:Hess in MOI.features_available(evaluator))
MOI.initialize(evaluator, [:Grad, :Jac])
J = zeros(length(MOI.jacobian_structure(evaluator)))
MOI.eval_constraint_jacobian(evaluator, J, [2.0])
@test J == [4.0]
return
end

end # module

TestReverseAD.runtests()
Loading