Repository navigation
Keep the specialization when rebuilding a SciML function - #1606
Merged
ChrisRackauckas merged 2 commits intoSep 23, 2026
Merged
ChrisRackauckas merged 2 commits into
ChrisRackauckas merged 2 commits into
Conversation
`ConstructionBase.constructorof` for SciML functions always rebuilt with
`DEFAULT_SPECIALIZATION`, so `@set f.x = ...` (or `@set prob.f.x = ...`)
turned a `FullSpecialize` function into an `AutoSpecialize` one, and an
`AutoDespecialize` one (e.g. MTK's default) into `AutoSpecialize` as well.
With DiffEqBase's callback type erasure for `AutoSpecialize` problems, the
silent change made a downstream package's first calibration compile ~8x longer.
Keep the specialization of the type being rebuilt; the default applies only
when the type leaves it unbound (e.g. `constructorof(ODEFunction{true})`). The
field parameters are still narrowed to the new values' types, which DiffEqBase
relies on after `widen_bounded_type_params`.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP
SebastianM-C
force-pushed
the
smc/constructorof-keep-specialization
branch
from
September 23, 2026 11:29
dbda0c1 to
5adcd4a
Compare
…)` in remake `_remake_odeproblem` rewrapped an out-of-place `FunctionWrapperSpecialize` function with the in-place argument tuple `(u, u, p, t)`, so calling the remade function (or solving the remade problem) failed with "No matching function wrapper". The problem constructor and the BVP remake already use `(u, p, t)`. Previously `get_concrete_problem` rarely reached this branch, because the `@set`s in DiffEqBase's `promote_f` reset the specialization to `AutoSpecialize`; with the specialization kept, `solve` goes through it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP
ChrisRackauckas
marked this pull request as ready for review
September 23, 2026 21:36
Merged
ChrisRackauckas
referenced
this pull request
Sep 26, 2026
Agent-Harness: Claude Code Agent-Model: claude-opus-5-5[1m] Agent-Session: local Claude Code session 3cd6500a-1f81-46b5-ac0b-c466e15b6a53 on Chris's Mac (session ID, no URL) Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
ChrisRackauckas
pushed a commit
to SciML/OrdinaryDiffEq.jl
that referenced
this pull request
Sep 27, 2026
#4599) * Don't re-despecialize an already concretized AutoDespecialize function `promote_f` widens the bounded type parameters of an `AutoDespecialize` function (`_despecialize_auxiliary_functions`) and relies on the `@set`s of the wrapping step to narrow them back. For a function that is already wrapped the wrapping step is skipped, so concretizing a concretized problem returned the widened type (`initialization_data::Union{Nothing, OverrideInitData}`). This was masked because SciMLBase's `constructorof` reset the specialization to the default on every `@set`, so a concretized problem was no longer `AutoDespecialize`. Once the specialization survives (SciML/SciMLBase.jl#1606), `despecialized_p_test.jl` catches it. Skip the auxiliary despecialization for an `f` whose RHS is already wrapped; this works with either SciMLBase. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP * Handle functions without an `f` field and SDEs in `_is_concretized` `_is_concretized` read `f.f`, which `DynamicalODEFunction` (`f1`/`f2`) does not have, so every `AutoDespecialize` `DynamicalODEProblem` threw a `FieldError` when concretized. It also excluded `SDEFunction` outright, so an SDE wrapped by the non-ForwardDiff path was despecialized again on a second concretization, re-widening its `ID` parameter. Treat a function without an `f` field as not wrapped, and an `SDEFunction` as wrapped when both drift and diffusion are, matching the skip condition of the wrapping step. Add an idempotency testset covering a plain `AutoDespecialize` ODE (no jac/tgrad, which the existing test relies on), an SDE on the non-ForwardDiff path and a `DynamicalODEProblem`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP * Qualify `NoRecompileArgumentError` in the ForwardDiff extension `wrapfun_oop` threw `NoRecompileArgumentError(IT)` unqualified, but the error type lives in DiffEqBase, so the unsupported-signature path raised an `UndefVarError` instead of the intended error. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP * Test the unsupported-signature error of the ForwardDiff extension An out-of-place `FunctionWrapperSpecialize` problem with argument types that have no precompiled wrapper signature must raise `NoRecompileArgumentError`. Before the previous commit it raised an `UndefVarError`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * Check the idempotency of `promote_f` without `remake` SciMLBase 3.56.1 (SciML/SciMLBase.jl#1616) makes `remake` keep the narrowed metadata types when a concretized `AutoDespecialize` function is replaced by a re-widened one. That hides a non-idempotent `promote_f` from the problem-level checks, so these now pass without the fix. Also compare the output of the `promote_f` call of `get_concrete_problem` on an already concretized problem directly, for the plain ODE and the SDE cases. The `DynamicalODEFunction` case is left at the problem level: there `promote_f` widens `ID` on every pass and `remake` narrows it back, so its output never matches the concretized function. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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.
ConstructionBase.constructoroffor the SciML function types rebuilds withDEFAULT_SPECIALIZATION. So any@set f.x = .../@set prob.f.x = ...(Accessors/Setfield,setproperties) silently changes the specialization: aFullSpecializefunction comes backAutoSpecialize, and so does anAutoDespecializeone (e.g. what MTK builds by default).Since DiffEqBase 7.21.2 type-erases the callbacks of
AutoSpecializeproblems, this has a real cost. A downstream package strippedinitialization_datafrom itsFullSpecializeshooting problems with@set. Its first exact-Hessian (SecondOrder(AutoForwardDiff(), AutoForwardDiff())) calibration then compiled the erased callback path for every Float64/Dual/Dual{Dual} integrator: 282 s instead of 40 s. With this PR alone (no downstream change) it is back to 41 s.History: the reset dates back to 2022. Callback erasure is simply the first change that made it expensive.
constructorof, but by the time it merged,constructorofrebuilt with a hardcodedFullSpecialize. From then on, every@seton a SciML function returned aFullSpecializeone. Nobody noticed, because most functions wereFullSpecializeanyway.NonlinearFunctionconstructors toDEFAULT_SPECIALIZATION(AutoSpecialize), and its sweep changedconstructorofalong with them. From then on,@setturnedFullSpecializeandAutoDespecializefunctions intoAutoSpecializeones. That was still free, sinceAutoSpecializedidn't change anything that mattered for callbacks yet.AutoSpecializeproblems on Julia ≥ 1.12. That made the reset costly: 40 s → 282 s in the downstream package above.Change: a single
constructorofmethod that keeps the specialization of the type being rebuilt. The default applies only when the type leaves it unbound (e.g.constructorof(ODEFunction{true})). The field parameters are still narrowed to the new values' types, as before; DiffEqBase relies on that afterwiden_bounded_type_params, and a test covers it.Related PRs. These came out of tracking this down. In merge order:
promote_fidempotent for concretizedAutoDespecializefunctions. Needed before this PR (details below).AutoDespecialize. Needed before this PR (details below).AutoRespecializeproblems. Those problems only got that through the reset; without it they lose model-to-model reuse once this PR lands (MTK, jac + Rodas5P: second model's first solve 0.37 s → 1.85 s on Julia 1.13.0). It can merge any time after 1.Depends on a DiffEqBase fix. With the specialization kept, the downstream DiffEqBase test
despecialized_p_test.jl("AutoDespecialize uses the dynamic parameter barrier") fails:promote_fis not idempotent for an already concretizedAutoDespecializefunction.@sets reset the problem toAutoSpecialize, so the second pass never takes the despecialize branch.despecialized_p_test.jlpasses with both this branch and released SciMLBase 3.55.0.Also depends on a NonlinearSolveBase fix. SciMLSensitivity Core8 "Mooncake through init" (
desauty_dae_mwe.jl:163) fails with this PR.solve_uprule strips the residual wrapper with@set prob.f.f = .... The old reset made that problemAutoSpecialize.AutoDespecialize, so when the adjoint concretizes the unwrapped problem again,pis wrapped back intoDespecializedParameters. The parameter tangent gains an extraparamsfield, and Mooncake rejects it.AutoSpecializeexplicitly. With it, that test passes with this branch and with released SciMLBase alike. It should be released first as well; until then Core8 is expected to fail on that test here.Other CI failures here are unrelated. They also fail on master, at the same test locations with the same messages:
SCCNonlinearProblem has no field u0) and QA (Base.Callablenon-public access)._vecjacobian(::SVector)).desauty_dae_mwe.jl:123,EnzymeNoTypeError). This is Core8's other failure; unlike "Mooncake through init" above, it also fails on master and with released SciMLBase.test/extensions/Project.toml: Permission deniedon the runner).Also fixed here, since this PR exposes it:
_remake_odeproblemrewrapped an out-of-placeFunctionWrapperSpecializefunction with the in-place argument tuple(u, u, p, t)instead of(u, p, t). So the remade function threw "No matching function wrapper". Previously the specialization reset keptget_concrete_problemout of that branch; with the specialization kept,solvegoes through it. A new downstream test (test/downstream/function_wrapper_remake.jl) fails on 3.55.0 and passes with the fix.Local verification (Julia 1.12.5; the tests were re-run on 1.12.7 and 1.13.0 with the same results):
test/problem_building_test.jlpasses,detect_ambiguitiesreports nothing forconstructorof, and the Core group passes (including Remake).Written by Claude Code on Sebastian's behalf.
🤖 Generated with Claude Code
https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP