Repository navigation
Erase and wrap the callbacks of AutoRespecialize problems (stacked on #4599) - #4604
SebastianM-C wants to merge 5 commits into
Conversation
`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
`_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
`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
`_erases_callback_types` erased callbacks for `AutoSpecialize`, `AutoDespecialize` and `NoSpecialize` but not for `AutoRespecialize`, which extends `AutoSpecialize`. The gap was hidden because SciMLBase's `constructorof` reset the specialization to the default on every `@set`: an `AutoRespecialize` problem with a `jac_prototype`, or with a `jac`/`tgrad` on a ForwardDiff-based solver, came out of `promote_f` as `AutoSpecialize` and had its callbacks erased, while one without them kept its callbacks typed. With the specialization kept on `@set` (SciML/SciMLBase.jl#1606) those problems stay `AutoRespecialize` and lose the erasure, e.g. ModelingToolkit's `ODEProblem{true, AutoRespecialize}(sys, ...; jac = true)` with events. Erase the callbacks of every `AutoRespecialize` problem, so its callback handling no longer depends on which auxiliary functions it carries. `FullSpecialize` keeps its callbacks typed; `NoSpecialize` was already erased. Test that the erasure follows the specialization of the concretized function, with and without a `jac_prototype`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP
`_despecialize_callbacks!` wraps the conditions and affects of the erased callbacks of `AutoSpecialize` and `AutoDespecialize` integrators so that the callback machinery compiles once per integrator type. `AutoRespecialize` extends `AutoSpecialize` and now has its callbacks erased by DiffEqBase, so wrap them too. Before SciMLBase kept the specialization on `@set`, an `AutoRespecialize` problem with a `jac_prototype` (or a `jac`/`tgrad` on a ForwardDiff-based solver) reached the integrator as `AutoSpecialize` and got this wrapping by accident. `NoSpecialize` still leaves its entries unwrapped, as it leaves `f`. Run the existing callback erasure and wrapping testsets for `AutoRespecialize`, and test that the callbacks stay erased and wrapped when `promote_f` wraps a Jacobian, and that `FullSpecialize` keeps them typed. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP
| # level erases them, `FullSpecialize` keeps them typed. `AutoRespecialize` extends | ||
| # `AutoSpecialize` and erases them with or without a `jac_prototype`, whose `@set` in | ||
| # `promote_f` rebuilds the function. | ||
| @static if VERSION >= v"1.12" |
There was a problem hiding this comment.
why is this version dependent?
There was a problem hiding this comment.
it mirrors
OrdinaryDiffEq.jl/lib/DiffEqBase/src/solve.jl
Line 101 in 91d6176
which says that it's related to older Enzyme versions aborting on Vector{Any}
PR: #4604 Reviewed head: de91b60 Risk assessment
Review reportVERDICT: CHANGES Riskmedium-high (MH1: the patch changes callback dispatch, cache use, and function promotion on core The affected users select Blocking findings
Nonblocking findings
What I ran
What I did not verify
Head link: de91b60 🤖 Posted by an AI agent — harness: codex-cli 0.172.0 · model: gpt-6.2-sol |
Stacked on #4599. This branch contains #4599's commits; only the last two are new here:
Erase the callback types of AutoRespecialize problems(DiffEqBase)Wrap the erased callbacks of AutoRespecialize integrators(OrdinaryDiffEqCore)Why: SciML/SciMLBase.jl#1606 makes
@seton a SciML function keep its specialization. Before it,promote_f's@set f.jac_prototype/ jac / tgrad rebuilds reset every problem toAutoSpecialize, so callback erasure (_erases_callback_types) and integrator callback wrapping (_despecialize_callbacks!) applied to any problem with those auxiliary functions, whatever its specialization level. With the specialization kept:AutoDespecializeproblems are unaffected: they are in both lists. Checked forjac = true,sparse = true,tgrad, mass-matrix ODEs andDAEProblem.AutoRespecialize(=== AutoDePSpecialize) is in neither list, so its problems lose the erasure/wrapping they only got through the reset (jacon a ForwardDiff solver, orsparse).FullSpecialize+jac_prototypenow keeps typed callbacks. That's intended and not restored here.Change: add
AutoRespecializeto_erases_callback_typesand to the_despecialize_callbacks!gate. SciMLBase documents it as extendingAutoSpecialize, so its callbacks now followAutoSpecializeregardless of which auxiliary functions the problem carries.NoSpecializestays erased but unwrapped, as designed: wrapping measured +0.2 s on the first solve and no gain afterwards.Compile-time impact on MTK-generated problems (Julia 1.12.5, MTK 11.45.1). Each cell is the median of 5 fresh processes: first solve of model A / first solve of a different model B with the same structure, in the same process. "released" = SciMLBase 3.55.0 + DiffEqBase 7.21.2; "#1606 + #4599" = those two PRs; "+ this" = plus this PR.
AutoRespecializenow gets erasure too: model B is 0.8–1.9 s faster, and the first solve costs about 0.2–0.35 s more (the same cost appears without callbacks).AutoRespecialize+jac+ a non-ForwardDiff solver (e.g. Tsit5) costs about +0.3 s per model. On that pathpromote_fdoesn't wrapjac, so the integrator type depends on the model's jac and every model recompiles anyway. Wrapping jac/tgrad on the non-ForwardDiff path would be a separate follow-up.Numerics: final states match bit for bit across the three combos for all 48 model/setting/solver/specialization cells. DDE/SDE smoke tests give identical values;
AutoRespecializeSDE callbacks are now erased, DDEs unchanged.Tests:
test/high_level_solve.jl: a new block, gated onVERSION >= v"1.12". Erasure follows the concretized specialization for 5 levels, with and withoutjac_prototype;AutoRespecializeis always erased;FullSpecializewithout a prototype stays typed.test/InterfaceII/despecialized_parameters.jl:AutoRespecializeadded to the existing loops, plus testsets for callbacks staying erased and wrapped when Rosenbrock23 wraps a jac, and forFullSpecializestaying typed. Without this change: 23 failed / 2 errored.No compat bumps: with only DiffEqBase updated,
AutoRespecializecallbacks are erased but not wrapped, likeNoSpecialize. Enzyme stays gated off by the existing Julia ≥ 1.12 condition. This change adds new specialization/erasure combinations, not new type paths; it was not tested with Enzyme.Written by Claude Code on Sebastian's behalf.
🤖 Generated with Claude Code
https://claude.ai/code/session_01JNAfKDURNdkubVjmftigPP