Skip to content

NonlinearSolveBase: report the wrapped arity for despecialization wrappers - #1230

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nlsbase-wrapper-isinplace
Sep 7, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:nlsbase-wrapper-isinplace

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Draft — please ignore until reviewed by @ChrisRackauckas.

What changed and why

ParameterDespecializationWrapper and AutoSpecializeCallable forward through a single (args...) method, so SciMLBase.numargs reported arity [1] for every wrapped residual/callback. When a problem that was wrapped at construction (ModelingToolkit's initialization problems after SciML/ModelingToolkit.jl#5062) is remaked, remake(::NonlinearProblem) rebuilds the NonlinearFunction, whose constructor re-derives isinplace for jac/jvp/vjp. Arity 1 pushes that check into SciMLBase.isinplace's method-table introspection fallback (any(isequal(Vararg{Any}), methods(f).ms[1].sig.parameters)), which Enzyme reverse mode cannot differentiate through:

BoundsError: attempt to access Core.SimpleVector at index [2]
  [3] _any @ ./anyall.jl:130
  ...
  [13] isinplace @ SciMLBase/src/utils.jl:368
  [15] NonlinearFunction @ SciMLBase/src/scimlfunctions.jl:4711
  [17] remake @ SciMLBase/src/remake.jl:368
  [19] remake @ SciMLBase/src/remake.jl:1377

(from https://github.com/SciML/ModelingToolkit.jl/actions/runs/33963346747/job/101298982372, SciMLSensitivity Core8 against MTK PR 5062.)

The fix defines SciMLBase.numargs (public, documented as the arity hook for "foreign-function wrappers") for both wrapper types, delegating to the wrapped function, so the arity fast path is taken. Version bumped to 2.49.4.

Verification

Failing before (registry NonlinearSolveBase 2.49.3, Enzyme 0.13.200, Julia 1.12.7; the concretized initialization problem of an MTK DAE built with jac = true):

jac :: NonlinearSolveBase.ParameterDespecializationWrapper numargs = [1]
remake(initializeprob) under Enzyme: FAILED: BoundsError: attempt to access Core.SimpleVector at index [2]

Passing after (this branch dev'd into the same environment):

jac :: NonlinearSolveBase.ParameterDespecializationWrapper numargs = (2, 3)
remake(initializeprob) under Enzyme: OK du=[0.0] (expected 0.0)

The new testset in test/autodespecialize.jl asserts numargs of the wrapped f/jac equals that of the raw functions (this is [1] != [3] on master, so it fails without the fix) and runs Enzyme.autodiff(Reverse, ...) through remake of a get_concrete_problem result.

GROUP=Core julia +1.12 --project=lib/NonlinearSolveBase -e 'using Pkg; Pkg.test()'
AutoDespecialize dynamic-p |   32     32  1m21.2s     (26 before this PR)
     Testing NonlinearSolveBase tests passed

GROUP=QA   julia +1.12 --project=lib/NonlinearSolveBase -e 'using Pkg; Pkg.test()'
     Testing NonlinearSolveBase tests passed

Runic (--check --diff, Runic 1.10.0) and typos on the diff: clean.

Not verified

Only the NonlinearSolveBase sublibrary was tested locally; downstream NonlinearSolve groups and the SciMLSensitivity Core8 run against the fixed version are still in progress locally and will be reported on the MTK PR.

🤖 Generated with Claude Code (model: claude-fable-5-1)
https://claude.ai/code/session_01FeJYXni9MkJhPFA9yxYvfn

…ppers

`ParameterDespecializationWrapper` and `AutoSpecializeCallable` forward through a
single `(args...)` method, so `SciMLBase.numargs` reported arity 1 for every wrapped
callback. `remake` of a wrapped problem rebuilds its `NonlinearFunction`, whose
constructor re-derives `isinplace` for each callback; arity 1 pushed that check
into the method-table introspection fallback, which Enzyme reverse mode cannot
differentiate through (`BoundsError: attempt to access Core.SimpleVector at
index [2]`). Delegating `numargs` to the wrapped function keeps the arity fast path.

Release 2.49.4.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Agent-Harness: Claude Code 2.1.259
Agent-Model: claude-fable-5-1
Agent-Session: https://claude.ai/code/session_01FeJYXni9MkJhPFA9yxYvfn
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 7, 2026 06:16
@ChrisRackauckas
ChrisRackauckas merged commit a91a70c into SciML:master Sep 7, 2026
96 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 7, 2026
- NonlinearSolveBase: rebuild a pre-wrapped function when its signatures do not match u0/p (#1226)
- Fix reverse AD through despecialized parameters (#1228)
- Preserve Jacobian cache parameter representation (#1227)
- Preserve TrustRegion cache parameter representation on reinit (#1229)
- Run runic on docstrings and markdown files (#1231)
- Release NonlinearSolveFirstOrder 2.5.1 (#1232)
- NonlinearSolveBase: report the wrapped arity for despecialization wrappers (#1230)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
ChrisRackauckas referenced this pull request Sep 7, 2026
- Drop stale [compat] majors older than one year (#1198)
- Drop @fastmath from L2_NORM/Linf_NORM so isfinite guards survive (#1182)
- Release 4.28.1 (#1202)
- Release 2.48.1 (#1206)
- Align QA NLsolve compat with root project (#1205)
- Widen NonlinearSolveBase LogExpFunctions compat to 0.3.28–1 (#1203)
- Continue polyalgorithm after stalled least-squares solves (#1204)
- Align Wrappers NLsolve compat with root project (#1208)
- Release 2.48.2 (#1210)
- Preserve despecialized parameters during reinit (#1199)



Agent-Harness: Claude Code
Agent-Model: claude-opus-5[1m]
Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R

Co-authored-by: Claude Opus 5 (1M context) <[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.

3 participants