Skip to content

Add and precompile the non-isbits (OpaqueRef) AutoDePSpecialize path - #1110

Merged
ChrisRackauckas merged 4 commits into
SciML:masterfrom
ChrisRackauckas-Claude:add-autodepspecialize-precompile
Jul 25, 2026
Merged

ChrisRackauckas merged 4 commits into
SciML:masterfrom
ChrisRackauckas-Claude:add-autodepspecialize-precompile

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

Completes the non-isbits story for AutoDePSpecialize on NonlinearSolve, mirroring the ODE side (SciML/OrdinaryDiffEq.jl#4005).

Two parts, which only make sense together:

  1. Non-isbits code path (NonlinearSolveBase). Add AutoDePSpecialize specialization level (de-specialize p via OpaqueParams) #1107 merged AutoDePSpecialize with an isbits-only policy (should_opaque_p(p) = isbits(p) && !NullParameters), so a non-isbits p (e.g. a struct with a Vector field, or a bare array) was left concrete and got no de-specialization. This relaxes the policy to de-specialize any non-NullParameters, non-already-packed p, packing via pack_auto: isbits → OpaqueParams (byte copy), non-isbits → OpaqueRef (by reference).

  2. Precompilation (FirstOrder, QuasiNewton, SpectralMethods). Add AutoDePSpecialize specialization level (de-specialize p via OpaqueParams) #1107 shipped no workload exercising AutoDePSpecialize at all, so even the isbits opaque path compiled on first use. This adds one AutoDePSpecialize problem per workload covering both containers (isbits p.a, non-isbits p[1]). 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 solve of a never-seen non-isbits parameter struct)

alg FullSpecialize AutoDePSpecialize (this PR)
NewtonRaphson 3.02s 0.88s
Broyden — 0.96s
DFSane — 1.26s

Without part 1, the non-isbits workload problem in part 2 would hit should_opaque_p == false and never take the opaque path. Verified end-to-end: NonlinearSolveBase AutoDePSpecialize test suite (incl. new OpaqueRef + idempotency tests) passes 18/18; the three sublibraries precompile cleanly (workload solves run during precompilation and return Success); fresh-process first solves timed above.

Version bumps: NonlinearSolveBase minor (extends the opt-in feature); FirstOrder/QuasiNewton/SpectralMethods patch.

Caveats (same posture as the isbits path)

Reverse-mode AD of p (SciMLSensitivity) and symbolic parameter indexing read the concrete parameter and are not supported under the opaque path — it is a forward-solve latency tool. Reverse-mode support is a planned follow-up. Recover the payload with RespecializeParams.unpack(sol.prob.p, typeof(p)).

🤖 Generated with Claude Code

https://claude.ai/code/session_01UuAbA8hSnrirMrJjFqxwn5

SciML#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]>
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]>
@ChrisRackauckas-Claude ChrisRackauckas-Claude changed the title Precompile the AutoDePSpecialize opaque-p path Add and precompile the non-isbits (OpaqueRef) AutoDePSpecialize path Jul 23, 2026
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]>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Added the same MTK-safety guard as the ODE side (SciML/OrdinaryDiffEq.jl#4005). MTKParameters is non-isbits, so without a guard the OpaqueRef path would sweep it in and break MTK initialization + symbolic parameter indexing. maybe_opaque_wrap now bails via !SciMLBase.has_sys(prob.f), so a NonlinearProblem carrying a symbolic system falls back to normal specialization (a no-op for it anyway). Regression test uses a SymbolCache sys; NonlinearSolveBase AutoDePSpecialize suite now 23/23.

…ecialize

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]>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Good catch — the polyalgorithms were not covered, and chasing it turned up two separate gaps on the paths users actually hit.

1. Polyalgorithm / no-algorithm entry. My earlier commit only added the opaque-p problems to the FirstOrder/QuasiNewton/SpectralMethods workloads, which sweep individual algorithms. The polyalg + no-alg sweep lives in the main NonlinearSolve workload, so solve(prob) (default selection via FastShortcutNonlinearPolyalg) never saw an AutoDePSpecialize problem.

2. Keyword-argument shape. 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) most user code writes. Measured directly:

solve(prob; abstol=1e-2, verbose=NonlinearVerbosity())   0.61s   (workload shape)
solve(prob)                                              1.68s   (bare — uncovered)

Fix adds the opaque-p problems to the main workload and gives them a bare no-kwarg solve(prob) sweep.

Measured (fresh process, non-isbits struct p, solve(prob) with no algorithm)

first solve
FullSpecialize 5.04s
AutoDePSpecialize — before 3.11s
AutoDePSpecialize — + polyalg problems 1.69s
AutoDePSpecialize — + bare sweep 0.61s

0.61s matches the single-algorithm figure (0.72s), so the polyalgorithm path now gets the full first-solve win — an 8.2× improvement over FullSpecialize rather than the 1.6× it had.

Verified: NonlinearSolveBase AutoDePSpecialize suite 23/23; polyalg / NewtonRaphson / Broyden / DFSane all converge on a non-isbits opaque-p problem. Patch-bumps NonlinearSolve to 4.23.1.

Same class of gap as the ODE side (SciML/OrdinaryDiffEq.jl#4005), where solve(prob) with no algorithm was going 11.25s → 0.45s once added to the Default workload.

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review July 25, 2026 03:46
@ChrisRackauckas
ChrisRackauckas merged commit be0b02e into SciML:master Jul 25, 2026
77 of 101 checks passed
ChrisRackauckas added a commit that referenced this pull request Jul 27, 2026
The workload's default sweep passes `abstol`/`verbose`. The keyword
NamedTuple's type participates in the specialization, so those calls do not
cover the bare `solve(prob)` that most user code writes. #1110 added a bare
sweep for the AutoDePSpecialize problems only; extend it to every nonlinear
and NLLS problem.

Measured (fresh process, default-specialization problems, no algorithm):

                        kwarg form   bare, before   bare, after
  NonlinearProblem         0.53s        1.54s          0.53s
  NLLS problem             0.63s        2.04s          0.63s

Cost, measured by rebuilding both ways:

  precompile   49.8s -> 53.7s   (+7.8%)
  cache .so    37.0MB -> 39.8MB (+7.6%)

So ~8% more build for ~1.0-1.4s off every bare first solve. Load time is
unchanged (2.80s vs 2.81s, within noise).

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
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