Skip to content

Add a specialization marker to OptimizationFunction - #1641

Merged
ChrisRackauckas merged 3 commits into
SciML:masterfrom
SebastianM-C:smc/optfunction-specialization
Oct 13, 2026
Merged

ChrisRackauckas merged 3 commits into
SciML:masterfrom
SebastianM-C:smc/optfunction-specialization

Conversation

@SebastianM-C

Copy link
Copy Markdown
Member

OptimizationFunction is the only SciML function without a specialization marker. Its second type parameter is the AD type, and specialization(::OptimizationFunction) returned that AD type instead of an AbstractSpecialization. So there was no way to ask the optimization solvers for the AutoSpecialize/AutoDespecialize behaviour 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 to FullSpecialize, so existing functions are unchanged.
  • specialization(f) and specialization(typeof(f)) return the marker.
  • remake, ConstructionBase.setproperties (and with it @set) and OptimizationProblem keep it. ConstructionBase.constructorof gets an OptimizationFunction method, because the type parameters list the AD type before the objective while the fields store them the other way around.
  • The older spelling that passes the AD type as the second type parameter, OptimizationFunction{iip, AD}(f; adtype), still works and builds a FullSpecialize function.

What AutoSpecialize and AutoDespecialize actually do is left to the solver packages, as for the other SciML functions. The docstring describes the intended meaning. With AutoSpecialize, solvers hide the function types behind wrappers so the solver code compiles once for all objectives. With AutoDespecialize, p is also stored in a DespecializedParameters.

A second commit adds convert(::Type{DespecializedParameters}, p), which wraps p. 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 an AutoDespecialize problem threw Cannot 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 in test/despecialized_parameters.jl. Locally, GROUP=Core passes on Julia 1.10.12 and 1.13.1, and GROUP=QA passes on 1.13.1.

Written by Claude Code on Sebastian's behalf.

🤖 Generated with Claude Code · model: claude-opus-5-5[1m]
Review: unreviewed

SebastianM-C and others added 3 commits October 12, 2026 17:51
`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
SebastianM-C marked this pull request as draft October 12, 2026 15:27
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review October 13, 2026 16:26
@ChrisRackauckas
ChrisRackauckas merged commit 365283c into SciML:master Oct 13, 2026
65 of 73 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants