Repository navigation
AutoDePSpecialize: de-specialize non-isbits parameters via OpaqueRef - #4005
ChrisRackauckas merged 4 commits into
Conversation
Follow-up to SciML#3692 (which shipped AutoDePSpecialize for isbits parameters). Extends the opaque-p path to non-isbits parameters: should_opaque_p now accepts any non-NullParameters, non-already-packed p, and pack_auto/opaque_container_type route isbits payloads into OpaqueParams (byte copy) and everything else into RespecializeParams.OpaqueRef (by reference). The FD ext substitutes opaque_container_type(P) rather than hardcoding OpaqueParams, and an idempotency guard prevents re-wrapping an already-opaque problem (no nested OpaqueVoid). This extends the shared-compilation TTFX benefit to problems whose parameter type is a non-isbits struct (e.g. a param struct holding arrays). It remains a forward-solve latency tool: de-specializing makes sol.prob.p an opaque container, so reverse-mode AD of p (SciMLSensitivity) and symbolic parameter indexing (getp/setp/observed) — which read the concrete parameter — are not supported. Reverse-mode support (unwrap the RHS + unpack p in the adjoint, as AutoSpecialize already does for its function wrapper) is a planned follow-up. Recover the payload with RespecializeParams.unpack(sol.prob.p, typeof(p)). DiffEqBase 7.8.0 -> 7.9.0. Verified: GROUP=Core green (Opaque-p Hook 25/25 incl. OpaqueRef + idempotency + inference; ForwardDiff Dual Detection); full-stack non-isbits solves match FullSpecialize across Tsit5/Rodas5P/FBDF. Co-Authored-By: Claude Fable 5 <[email protected]> Co-Authored-By: Chris Rackauckas <[email protected]>
The AutoDePSpecialize precompile workload only exercised an isbits `p` (the `lorenz_p_params` NamedTuple), so only the `OpaqueParams` container was precompiled. A non-isbits `p` routes through `OpaqueRef`, a distinct wrapper signature that nothing precompiled — so it compiled on first use and saw no TTFX benefit (measured: first Tsit5 solve 2.40s, same as FullSpecialize's 2.13s). Add a non-isbits variant (`lorenz_pref` with a `Vector` `p`) to the shared setup and to each algorithm's `@compile_workload`. Like the isbits case, one precompiled `OpaqueRef` solve is shared across all non-isbits parameter types, since the wrapped signature carries `OpaqueRef` rather than `typeof(p)`. After this the non-isbits first solve drops to 0.37s, matching the isbits AutoDePSpecialize path. Patch-bumps the touched precompile-carrying sublibraries. Co-Authored-By: Chris Rackauckas <[email protected]>
|
Added precompilation of the non-isbits ( The existing Measured (fresh process, first
So the non-isbits path now gets the same ~5.8× first-solve TTFX win as the isbits path. Patch-bumped the touched precompile-carrying sublibraries. |
SciML#1107 merged AutoDePSpecialize with an isbits-only policy (`should_opaque_p(p) = isbits(p) && !NullParameters`), so a non-isbits `p` was left concrete and got no de-specialization. Relax the policy to de-specialize any non-`NullParameters`, non-already-packed `p`, and pack via `pack_auto` so isbits payloads go to `OpaqueParams` (byte copy) and non-isbits payloads to `OpaqueRef` (by reference). Mirrors the ODE DiffEqBase path (SciML/OrdinaryDiffEq.jl#4005). This is what makes the non-isbits problem in the precompile workloads actually take the opaque path. Reverse-mode AD of `p` and symbolic parameter indexing remain unsupported under the opaque path (forward-solve latency tool); recover the payload with `RespecializeParams.unpack(sol.prob.p, typeof(p))`. Minor-bumps NonlinearSolveBase (extends the opt-in AutoDePSpecialize feature). Co-Authored-By: Chris Rackauckas <[email protected]>
The non-isbits path packs any non-isbits `p` into an OpaqueRef, which swept in `MTKParameters`. A ModelingToolkit problem opting into AutoDePSpecialize then errored at solve time in `promote_u0_p(::OpaqueRef)` (MTK's initialization pipeline needs a concrete parameter object), and even had it solved, `getp`/observed/symbolic indexing read the concrete `p` and would break. Guard both `promote_f` opaque branches with `!SciMLBase.has_sys(f)`: a function carrying a symbolic system falls back to the normal specialization. This is a pure no-op for such problems anyway — `MTKParameters` is non-isbits and already type-uniform, so there is no shared-compilation win to be had. Verified end-to-end with a real MTK ODEProblem (solve + `sol[x]` + `getp` all succeed) and a `SymbolCache`-backed regression test; plain non-isbits/isbits problems still de-specialize as before. Co-Authored-By: Chris Rackauckas <[email protected]>
|
Added an MTK-safety guard, because the non-isbits path had a real footgun here.
Fix: guard both Verified with a real MTK
Plain non-isbits/isbits problems (no |
The Default workload only swept `solve(prob, solver)`, so it covered `solve(prob, DefaultODEAlgorithm())` but not the no-algorithm `solve(prob)` entry, which runs default algorithm *selection* — a separate code path, and the one most user code and MTK tutorials actually write. The opaque-p precompilation therefore never reached it. Measured on an AutoDePSpecialize problem with an isbits struct `p`: solve(prob, DefaultODEAlgorithm()) 0.42s (already precompiled) solve(prob) before this change 11.25s solve(prob) after this change 0.45s FullSpecialize is 13.3s on the same no-alg path, so this is a ~30x first-solve win on the most common entry point rather than the ~1.2x it was getting before. Co-Authored-By: Chris Rackauckas <[email protected]>
|
Found and fixed a further precompilation gap, this time on the default solver — and it was the biggest one. The workload only swept Measured (fresh process, isbits struct
So the most common entry point went from a ~1.2× win to ~30×. Worth noting the polyalgorithm payoff is far larger than for a single method ( Fix keeps the AutoDePSpecialize problems in their own list so they drive both the |
…1110) * Precompile the AutoDePSpecialize opaque-p path #1107 added the AutoDePSpecialize specialization level but no workload exercised it, so the opaque-p solve compiled on first use and struct/array parameters saw no TTFX benefit. Add an AutoDePSpecialize problem (isbits `p` -> OpaqueParams, non-isbits `p` -> OpaqueRef) to the FirstOrder, QuasiNewton, and SpectralMethods `@compile_workload`s. Each container is a single wrapped-residual signature shared across all parameter types of its kind, so one precompiled solve per container serves first solves with any struct/array parameter. Measured (fresh process, first NewtonRaphson solve of a never-seen non-isbits parameter struct): FullSpecialize 3.02s, AutoDePSpecialize 0.88s after this change (vs 3.1s before, when the opaque path was not precompiled). Broyden and DFSane first solves likewise drop to ~1s. Patch-bumps the three touched sublibraries. Co-Authored-By: Chris Rackauckas <[email protected]> * Add the non-isbits (OpaqueRef) AutoDePSpecialize path #1107 merged AutoDePSpecialize with an isbits-only policy (`should_opaque_p(p) = isbits(p) && !NullParameters`), so a non-isbits `p` was left concrete and got no de-specialization. Relax the policy to de-specialize any non-`NullParameters`, non-already-packed `p`, and pack via `pack_auto` so isbits payloads go to `OpaqueParams` (byte copy) and non-isbits payloads to `OpaqueRef` (by reference). Mirrors the ODE DiffEqBase path (SciML/OrdinaryDiffEq.jl#4005). This is what makes the non-isbits problem in the precompile workloads actually take the opaque path. Reverse-mode AD of `p` and symbolic parameter indexing remain unsupported under the opaque path (forward-solve latency tool); recover the payload with `RespecializeParams.unpack(sol.prob.p, typeof(p))`. Minor-bumps NonlinearSolveBase (extends the opt-in AutoDePSpecialize feature). Co-Authored-By: Chris Rackauckas <[email protected]> * Decline the opaque path for symbolic-system (MTK) problems Mirror of the DiffEqBase guard: the non-isbits path packs any non-isbits `p` (including MTKParameters) into an OpaqueRef, which breaks MTK's initialization pipeline and symbolic parameter indexing. Guard `maybe_opaque_wrap` with `!SciMLBase.has_sys(prob.f)` so a NonlinearProblem carrying a symbolic system falls back to the normal specialization — a no-op for such problems, which gain nothing from de-specialization. Adds a `SymbolCache`-backed regression test. Co-Authored-By: Chris Rackauckas <[email protected]> * Precompile the polyalgorithm and no-kwarg solve entries for AutoDePSpecialize Two gaps, both on the paths users actually hit: 1. The AutoDePSpecialize problems were only added to the FirstOrder, QuasiNewton and SpectralMethods workloads, which sweep individual algorithms. The polyalgorithm / no-algorithm sweep lives in the main NonlinearSolve workload, so `solve(prob)` (default algorithm selection via FastShortcutNonlinearPolyalg) never saw an opaque-p problem. 2. The workload's default sweep passes `abstol`/`verbose`, and the keyword NamedTuple's type participates in the specialization — so precompiling `solve(prob; kwargs...)` does not cover the bare `solve(prob)` that most user code writes. Add the opaque-p problems (isbits -> OpaqueParams, non-isbits -> OpaqueRef) to the main workload, and give them their own bare no-kwarg `solve(prob)` sweep. Measured (fresh process, non-isbits struct `p`, `solve(prob)` no algorithm): FullSpecialize 5.04s AutoDePSpecialize, before 3.11s AutoDePSpecialize, + polyalg probs 1.69s AutoDePSpecialize, + bare sweep 0.61s That matches the single-algorithm figure (0.72s), i.e. the polyalgorithm path now gets the same first-solve win. Verified: NonlinearSolveBase AutoDePSpecialize suite 23/23; polyalg/NewtonRaphson/Broyden/DFSane all converge on a non-isbits opaque-p problem. Co-Authored-By: Chris Rackauckas <[email protected]> --------- Co-authored-by: ChrisRackauckas-Claude <[email protected]>
… workloads (#4080) #4005 added a non-isbits precompile workload built on OrdinaryDiffEqCore.lorenz_pref / lorenz_pref_params, and referenced it from eight sublibraries' @compile_workload blocks without raising their OrdinaryDiffEqCore floors. It bumped Core to 4.10.1, but 4.10.1 was never registered — the next registered Core after 4.10.0 is 4.11.0 — so the first released Core containing the symbols is 4.11.0. Tsit5 2.1.1, Verner 2.2.1, SSPRK 2.2.1 and Rosenbrock 2.6.1 are all registered declaring OrdinaryDiffEqCore = "4.10", so any environment holding Core at 4.10.0 resolves a combination that fails to precompile with UndefVarError: `lorenz_pref` not defined in `OrdinaryDiffEqCore` Reproduced locally with OrdinaryDiffEqTsit5 2.1.1 + OrdinaryDiffEqCore 4.10.0; the same pair with Core 4.11.0 precompiles and solves. Audited every sublibrary's qualified OrdinaryDiffEqCore accesses and explicit import lists against the registered Core trees for 4.0.0 - 4.11.0. Exactly these eight are affected. BDF and SDIRK were additionally below the floor for AutoDePSpecialize / lorenz_p / lorenz_p_params (first released in 4.8.1) under their "4.8" bound; that is subsumed by 4.11. No other sublibrary references a Core name absent at its declared floor. Metadata-only; the source in this repo is already correct. Fixes #4079 Claude-Session: https://claude.ai/code/session_017ZqxphdTA1MydR1WXye9Jc Co-authored-by: ChrisRackauckas-Claude <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Note
Draft PR — please ignore until reviewed by @ChrisRackauckas.
Follow-up to #3692 (which shipped
AutoDePSpecializefor isbits parameters, now on master). This extends the opaque-ppath to non-isbits parameters.What changed
should_opaque_pnow de-specializes anypexceptNullParametersand already-packed containers (previously isbits-only).RespecializeParams.pack_auto/opaque_container_typeroute the payload: isbits →OpaqueParams(byte copy, as before), everything else →OpaqueRef(by reference). The ForwardDiff extension substitutesopaque_container_type(P)instead of hardcodingOpaqueParams, and an idempotency guard stops a re-concretized opaque problem from being re-wrapped (no nestedOpaqueVoid).The RespecializeParams machinery (
OpaqueRef,OpaqueVoid'sOpaqueRefmethods,pack_auto) already existed — this just stops gating it out.DiffEqBase 7.8.0 → 7.9.0.Value + scope
Extends the shared-compilation TTFX benefit to problems whose parameter type is a non-isbits struct (e.g. a parameter struct holding arrays — different model types then share one precompiled solve).
It remains a forward-solve latency tool. De-specializing makes
sol.prob.pan opaque container, so:p(SciMLSensitivity) and symbolic parameter indexing (getp/setp/observed) — which read the concrete parameter — are not supported. Recover the payload explicitly withRespecializeParams.unpack(sol.prob.p, typeof(p)).unwrapped_f, extended to see throughOpaqueVoid) and unpackp, the same way it already unwraps the plainAutoSpecializefunction wrapper. (Prototyped; it's a multi-path SciMLSensitivity change, so it's separated out.)pis unaffected — the existingisdualtype(u0)guard skips the opaque path wheneverpcarries duals.Verified
GROUP=Coregreen: Opaque-p Hook 25/25 (adds OpaqueRef + idempotency + inference cases), ForwardDiff Dual Detection unchanged.FullSpecializeacrossTsit5/Rodas5P/FBDF(sol.prob.p isa OpaqueRef, unpack roundtrips, different non-isbits types sharetypeof(prob.f)).🤖 Generated with Claude Code
https://claude.ai/code/session_01UuAbA8hSnrirMrJjFqxwn5