Skip to content

NonlinearSolveBase: rebuild a pre-wrapped function when its signatures do not match u0/p - #1226

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:nlsbase-unwrap-dual-prewrapped
Sep 5, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:nlsbase-unwrap-dual-prewrapped

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

Draft — please ignore until reviewed by @ChrisRackauckas.

What changed and why

maybe_wrap_nonlinear_f returned an already-wrapped AutoSpecializeCallable unchanged. A wrapper built by get_concrete_problem carries FunctionWrappersWrapper signatures for exactly the state and parameter types it was wrapped with, so a problem that was wrapped at construction time and then remaked with different types (another value eltype, a different array type, an outer-AD Dual) was handed a wrapper with no matching signature: the call throws NoFunctionWrapperFoundError (or falls into the slow fallback path, depending on the policy).

The check is now general: wrapper_accepts(f, (u0, u0, p)) asks whether the stored wrapper has a signature for the current arguments. If it does, the wrapper is reused; if not, the raw callable is recovered with get_raw_f and goes through the normal wrapping path, which rebuilds the wrapper for the new types, or leaves the function raw when u0 is Dual-typed (the existing rule for outer AD). _despecialize_parameters also skips its remake when the problem is already despecialized and wrapped, so concretizing an already-concretized problem is a no-op.

Motivation: SciML/ModelingToolkit.jl#5062 stores initialization problems pre-wrapped; ReconstructInitializeprob / remake later change their u0/p types. That PR does not require this one (the AllowNonIsBits fallback keeps non-isbits cases working), but benefits from it.

Verification

New testset maybe_wrap_nonlinear_f rebuilds a pre-wrapped f whose signatures do not match u0 in lib/NonlinearSolveBase/test/runtests.jl: wrap at construction, then remake with (a) Float32 u0/p — expects a rebuilt wrapper that accepts the new types and computes the right residual, and (b) an outer-AD Dual u0/p — expects the raw callable and correct values/partials; both for AutoSpecialize and AutoDespecialize.

Against master NonlinearSolveBase (78b1484f), same testset (wrapper_accepts does not exist there, so those assertions error; the Float32 call itself throws):

maybe_wrap_nonlinear_f rebuilds a pre-wrapped f whose signatures do not match u0: Error During Test at newtest_only.jl:17
  Expression: NonlinearSolveBase.wrapper_accepts(wrapped.f.f, (wrapped.u0, wrapped.u0, wrapped.p))
...
maybe_wrap_nonlinear_f rebuilds a pre-wrapped f whose signatures do not match u0: Error During Test at newtest_only.jl:2
  No matching function wrapper was found!
Test Summary:                                                                    | Pass  Error  Total  Time
maybe_wrap_nonlinear_f rebuilds a pre-wrapped f whose signatures do not match u0 |    3      4      7  3.8s

With this PR, NONLINEARSOLVE_TEST_GROUP=Core julia --project=lib/NonlinearSolveBase -e 'using Pkg; Pkg.test()' (Julia 1.12.7):

maybe_wrap_nonlinear_f rebuilds a pre-wrapped f whose signatures do not match u0 |   20     20  2.7s
...
     Testing NonlinearSolveBase tests passed

NONLINEARSOLVE_TEST_GROUP=QA: Testing NonlinearSolveBase tests passed. Runic and typos clean on the changed files.

Not verified

  • Downstream solver-package groups (NonlinearSolveFirstOrder etc.), Enzyme paths, GPU arrays; only NonlinearSolveBase Core and QA were run locally.
  • Julia 1.10.

For the reviewer

  • wrapper_accepts compares against the base signature only ((u0, u0, p)); the inner-AD Dual signatures the ForwardDiff extension adds are keyed off that base type, so a base match implies the Dual ones match too.
  • _despecialize_parameters' early return assumes that when f.f is already wrapped and p is already DespecializedParameters, the other callbacks (jac, …) were wrapped in the same earlier pass. That holds for anything produced by maybe_wrap_f.
  • Version bumped to 2.49.3.

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

`maybe_wrap_nonlinear_f` returned an already-wrapped `AutoSpecializeCallable`
unchanged. A problem that was wrapped at construction and then `remake`d with
an outer-AD Dual `u0`/`p` (e.g. ForwardDiff through an ODE initialization) kept
a `FunctionWrappersWrapper` whose signatures only cover the value eltype, and
the solve failed with `NoFunctionWrapperFoundError`. Hand back the raw callable
in that case, mirroring the existing rule that Dual-eltype state is never
wrapped. `_despecialize_parameters` also skips its `remake` when the problem is
already despecialized and wrapped, so re-concretizing such a problem is free.

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
…atch u0/p

A function wrapped at construction carries signatures for the state and
parameter types it was wrapped with. Instead of special-casing a Dual `u0`,
check whether the stored wrapper has a signature for `(u0, u0, p)`
(`wrapper_accepts`): if it does, reuse it; if not, unwrap and go through the
normal wrapping path, which rebuilds the wrapper for a new value eltype and
leaves the raw callable for an outer-AD Dual eltype.

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-Claude ChrisRackauckas-Claude changed the title NonlinearSolveBase: unwrap a pre-wrapped function when u0 is promoted to Dual NonlinearSolveBase: rebuild a pre-wrapped function when its signatures do not match u0/p Sep 4, 2026
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Release ordering: #1228 needs to own NonlinearSolveBase 2.49.3 because SciML/ModelingToolkit.jl#5062 now requires its reverse-mode AD fixes. This PR currently also claims 2.49.3. After pull request 1228 is reviewed, merged, and released, this branch should rebase on the updated base and bump NonlinearSolveBase to 2.49.4 before merge. I have not rewritten or force-pushed this branch.

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 5, 2026 08:24
@ChrisRackauckas
ChrisRackauckas merged commit 07011de into SciML:master Sep 5, 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)



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 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]>
ChrisRackauckas added a commit that referenced this pull request Sep 10, 2026
- Require all components to stall before resetting Broyden (#1184)
- Add adaptive Jacobian reuse to first-order solvers (#1072)
- Release 4.29.1 (#1220)
- Release 2.49.1 (#1221)
- Release 1.15.3 (#1222)
- Fix @static_timeit for TimerOutputs ≥ 0.5.27 (#1224) (#1225)
- 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)



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 10, 2026
- Remove name duplicates in keyword arguments/named tuples (#1238)
- Add 32-bit Core CI lane via test_groups.toml (#1239)
- Add a native bounded trust-region solver (#1219)



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.

2 participants