-
Notifications
You must be signed in to change notification settings - Fork 19
Interface for solver supporting diff natively #356
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
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5ce30a7
Interface for solver supporting diff natively
blegat 5f8ac99
Fixes
blegat 8eb0c2b
Fix format
blegat f29056b
Claude improves coverage
blegat cabc199
Fix format
blegat 463bcda
Add tests
blegat c49c519
fix format
blegat e3c8210
Rename
blegat c8e72f4
Use the attributes in inner models too
blegat 03f4af7
Simplify
blegat b4aee93
Fix format
blegat 6e25d00
Fix
blegat b75c198
Restore keyword option for nonlinear
blegat cbff8af
Fix
blegat 7f652bc
Fix
blegat af656ae
Fixes
blegat fe90ee8
Fix
blegat 3e49830
Fix
blegat 1332d5a
Fix
blegat c8fe60c
Fix
blegat 890fd48
Simpler
blegat 2316a7b
Fix format
blegat f6f3357
Simplify handling of POI layer
blegat 49f824e
Fix format
blegat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -548,7 +548,9 @@ end | |
|
|
||
| MOI.get(model::Optimizer, ::ModelConstructor) = model.model_constructor | ||
|
|
||
| function reverse_differentiate!(model::Optimizer) | ||
| MOI.supports(::Optimizer, ::ReverseDifferentiate) = true | ||
|
|
||
| function MOI.set(model::Optimizer, attr::ReverseDifferentiate, value) | ||
| st = MOI.get(model.optimizer, MOI.TerminationStatus()) | ||
| if !in(st, (MOI.LOCALLY_SOLVED, MOI.OPTIMAL)) | ||
| error( | ||
|
|
@@ -562,17 +564,22 @@ function reverse_differentiate!(model::Optimizer) | |
| "Set `DiffOpt.AllowObjectiveAndSolutionInput()` to `true` to silence this warning." | ||
| end | ||
| end | ||
| diff = _diff(model) | ||
| MOI.set( | ||
| diff, | ||
| NonLinearKKTJacobianFactorization(), | ||
| model.input_cache.factorization, | ||
| ) | ||
| MOI.set( | ||
| diff, | ||
| AllowObjectiveAndSolutionInput(), | ||
| model.input_cache.allow_objective_and_solution_input, | ||
| ) | ||
| diff = _diff(model, attr) | ||
| # Native differentiation interface are allow to not support it | ||
| if MOI.supports(diff, NonLinearKKTJacobianFactorization()) | ||
| MOI.set( | ||
| diff, | ||
| NonLinearKKTJacobianFactorization(), | ||
| model.input_cache.factorization, | ||
| ) | ||
| end | ||
| if MOI.supports(diff, AllowObjectiveAndSolutionInput()) | ||
| MOI.set( | ||
| diff, | ||
| AllowObjectiveAndSolutionInput(), | ||
| model.input_cache.allow_objective_and_solution_input, | ||
| ) | ||
| end | ||
| for (vi, value) in model.input_cache.dx | ||
| MOI.set(diff, ReverseVariablePrimal(), model.index_map[vi], value) | ||
| end | ||
|
|
@@ -593,7 +600,7 @@ function reverse_differentiate!(model::Optimizer) | |
| end | ||
| end | ||
| end | ||
| return reverse_differentiate!(diff) | ||
| return MOI.set(diff, attr, value) | ||
| end | ||
|
|
||
| # Gradient evaluation functions for objective sensitivity fallbacks | ||
|
|
@@ -639,13 +646,12 @@ function _eval_gradient( | |
| end | ||
|
|
||
| function _fallback_set_reverse_objective_sensitivity(model::Optimizer, val) | ||
| diff = _diff(model) | ||
| obj_type = MOI.get(model, MOI.ObjectiveFunctionType()) | ||
| obj_func = MOI.get(model, MOI.ObjectiveFunction{obj_type}()) | ||
| grad = _eval_gradient(model, obj_func) | ||
| for (xi, df_dxi) in grad | ||
| MOI.set( | ||
| diff, | ||
| model.diff, | ||
| ReverseVariablePrimal(), | ||
| model.index_map[xi], | ||
| df_dxi * val, | ||
|
|
@@ -666,24 +672,30 @@ function _copy_forward_in_constraint(diff, index_map, con_map, constraints) | |
| return | ||
| end | ||
|
|
||
| function forward_differentiate!(model::Optimizer) | ||
| MOI.supports(model::Optimizer, attr::ForwardDifferentiate) = true | ||
|
|
||
| function MOI.set(model::Optimizer, attr::ForwardDifferentiate, value) | ||
| st = MOI.get(model.optimizer, MOI.TerminationStatus()) | ||
|
Comment on lines
+675
to
678
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there reasonable precedence for this?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| if !in(st, (MOI.LOCALLY_SOLVED, MOI.OPTIMAL)) | ||
| error( | ||
| "Trying to compute the forward differentiation on a model with termination status $(st)", | ||
| ) | ||
| end | ||
| diff = _diff(model) | ||
| MOI.set( | ||
| diff, | ||
| NonLinearKKTJacobianFactorization(), | ||
| model.input_cache.factorization, | ||
| ) | ||
| MOI.set( | ||
| diff, | ||
| AllowObjectiveAndSolutionInput(), | ||
| model.input_cache.allow_objective_and_solution_input, | ||
| ) | ||
| diff = _diff(model, attr) | ||
| if MOI.supports(diff, NonLinearKKTJacobianFactorization()) | ||
| MOI.set( | ||
| diff, | ||
| NonLinearKKTJacobianFactorization(), | ||
| model.input_cache.factorization, | ||
| ) | ||
| end | ||
| if MOI.supports(diff, AllowObjectiveAndSolutionInput()) | ||
| MOI.set( | ||
| diff, | ||
| AllowObjectiveAndSolutionInput(), | ||
| model.input_cache.allow_objective_and_solution_input, | ||
| ) | ||
| end | ||
| T = Float64 | ||
| list = MOI.get( | ||
| model, | ||
|
|
@@ -700,7 +712,7 @@ function forward_differentiate!(model::Optimizer) | |
| MOI.Parameter(value), | ||
| ) | ||
| end | ||
| return forward_differentiate!(diff) | ||
| return MOI.set(diff, attr, value) | ||
| end | ||
| # @show "func mode" | ||
| if model.input_cache.objective !== nothing | ||
|
|
@@ -733,7 +745,7 @@ function forward_differentiate!(model::Optimizer) | |
| model.input_cache.vector_constraints[F, S], | ||
| ) | ||
| end | ||
| return forward_differentiate!(diff) | ||
| return MOI.set(diff, attr, value) | ||
| end | ||
|
|
||
| function empty_input_sensitivities!(model::Optimizer) | ||
|
|
@@ -782,8 +794,24 @@ function _instantiate_diff(model::Optimizer, constructor) | |
| return model_bridged | ||
| end | ||
|
|
||
| function _diff(model::Optimizer) | ||
| if model.diff === nothing | ||
| # Find the native differentiation solver in the optimizer chain. | ||
| # Cached in `model.diff` to avoid repeated unwrapping. | ||
| function _native_diff_solver(model::Optimizer) | ||
| if isnothing(model.diff) | ||
| model.diff = model.optimizer | ||
| model.index_map = MOI.Utilities.identity_index_map(model.optimizer) | ||
| end | ||
| return model.diff | ||
| end | ||
|
|
||
| function _diff( | ||
| model::Optimizer, | ||
| attr::Union{ForwardDifferentiate,ReverseDifferentiate}, | ||
| ) | ||
| if MOI.supports(model.optimizer, attr) | ||
| model.diff = model.optimizer | ||
| model.index_map = MOI.Utilities.identity_index_map(model.optimizer) | ||
| elseif isnothing(model.diff) | ||
| _check_termination_status(model) | ||
| model_constructor = MOI.get(model, ModelConstructor()) | ||
| if isnothing(model_constructor) | ||
|
|
@@ -1128,15 +1156,7 @@ function MOI.get(model::Optimizer, attr::DifferentiateTimeSec) | |
| return MOI.get(model.diff, attr) | ||
| end | ||
|
|
||
| function MOI.supports( | ||
| ::Optimizer, | ||
| ::NonLinearKKTJacobianFactorization, | ||
| ::Function, | ||
| ) | ||
| return true | ||
| end | ||
|
|
||
| function MOI.supports(::Optimizer, ::AllowObjectiveAndSolutionInput, ::Bool) | ||
| function MOI.supports(::Optimizer, ::NonLinearKKTJacobianFactorization) | ||
| return true | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more type piracy...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's maybe not needed in the end, let me check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #360