Repository navigation
Add a specialization marker to OptimizationFunction - #1641
Merged
ChrisRackauckas merged 3 commits intoOct 13, 2026
Merged
Conversation
`OptimizationFunction` had no specialization marker: its second type parameter is
the AD type, and `specialization(::OptimizationFunction)` returned that instead of
an `AbstractSpecialization`. The marker is now stored as the last type parameter,
after the field types, so code that spells out the existing parameters keeps
working. It defaults to `FullSpecialize`, so existing functions are unchanged.
`OptimizationFunction{iip, specialize}(f, adtype; kwargs...)` selects it, and
`specialization` returns it. `remake`, `ConstructionBase.setproperties` (and with
it `@set`) and `OptimizationProblem` keep it. The spelling that passes the AD
type as the second type parameter (`OptimizationFunction{iip, AD}(f; adtype)`)
still builds a `FullSpecialize` function.
What `AutoSpecialize` and `AutoDespecialize` do is up to the solver packages,
as for the other SciML functions.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Storage typed for despecialized parameters could not be assigned a new parameter object. For example, an optimization cache built from an `AutoDespecialize` problem stores `p` as a `DespecializedParameters`, and `reinit!(cache; p = new_p)` threw `Cannot convert an object of type ... to an object of type SciMLBase.DespecializedParameters`. `convert` now wraps the object, and returns an already wrapped one unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
SebastianM-C
marked this pull request as draft
October 12, 2026 15:27
ChrisRackauckas
marked this pull request as ready for review
October 13, 2026 16:26
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
OptimizationFunctionis the only SciML function without a specialization marker. Its second type parameter is the AD type, andspecialization(::OptimizationFunction)returned that AD type instead of anAbstractSpecialization. So there was no way to ask the optimization solvers for theAutoSpecialize/AutoDespecializebehaviour the ODE solvers already offer.This PR adds the marker as the last type parameter, after the field types, so code that spells out the existing parameters (
OptimizationFunction{iip, AD, F, ...}) keeps working:OptimizationFunction{iip, specialize}(f, adtype = NoAD(); kwargs...)picks it. Every other constructor defaults toFullSpecialize, so existing functions are unchanged.specialization(f)andspecialization(typeof(f))return the marker.remake,ConstructionBase.setproperties(and with it@set) andOptimizationProblemkeep it.ConstructionBase.constructorofgets anOptimizationFunctionmethod, because the type parameters list the AD type before the objective while the fields store them the other way around.OptimizationFunction{iip, AD}(f; adtype), still works and builds aFullSpecializefunction.What
AutoSpecializeandAutoDespecializeactually do is left to the solver packages, as for the other SciML functions. The docstring describes the intended meaning. WithAutoSpecialize, solvers hide the function types behind wrappers so the solver code compiles once for all objectives. WithAutoDespecialize,pis also stored in aDespecializedParameters.A second commit adds
convert(::Type{DespecializedParameters}, p), which wrapsp. Without it, storage typed for despecialized parameters can't be given a new parameter object. For example,reinit!(cache; p = new_p)on a cache built from anAutoDespecializeproblem threwCannot convert an object of type ... to an object of type SciMLBase.DespecializedParameters. Already wrapped parameters are returned unchanged.The consumer is a follow-up PR to SciML/Optimization.jl that despecializes
OptimizationCache(and the OptimizationMOI NLP cache) for these markers. It needs a release that includes this PR, so the version is bumped to 3.58.0.Tests: new
test/optimization_function_specialization.jl(in the Core group) and a conversion testset intest/despecialized_parameters.jl. Locally,GROUP=Corepasses on Julia 1.10.12 and 1.13.1, andGROUP=QApasses on 1.13.1.Written by Claude Code on Sebastian's behalf.
🤖 Generated with Claude Code · model: claude-opus-5-5[1m]
Review: unreviewed