Skip to content

Cover out-of-place init in the Jacobian cache inference tests - #1137

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:test-oop-init-inference
Aug 3, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:test-oop-init-inference

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Fixes #1092.

Please ignore until reviewed by @ChrisRackauckas.

⚠️ Blocked on a FiniteDiff release. This bumps FiniteDiff to 2.32.1, which carries
the actual fix (JuliaDiff/FiniteDiff.jl#229) and is not registered
yet. CI here will fail to resolve until it is.

Diagnosis

The reported instability is not in NonlinearSolveFirstOrder/NonlinearSolveBase. Walking
the cache construction:

  • DoglegCache and NewtonDescentCache infer fully concretely when handed a concrete
    J — I checked Base.return_types on InternalAPI.init(prob, alg.descent, J, fu, u; …)
    in isolation and got the exact runtime type.
  • The open where-bounds start at construct_jacobian_cache, which infers
    JacobianCache{_A, _B, …} where {_A, _B} — J and J_destination.
  • @report_opt puts the origin at jacobian.jl:129:
    runtime dispatch detected: NonlinearSolveBase.JacobianCache(%56::Any, %56::Any, …)
    
    %56 is the AD-computed J. With AutoFiniteDiff, DI.jacobian bottoms out in
    FiniteDiff's out-of-place finite_difference_jacobian, whose per-color closures assign
    variables that also live in the enclosing function's scope. That boxes the captures, so the
    function returns Any, and the Any propagates up through the Jacobian cache into the
    descent caches — exactly the shape reported in the issue.

Once JuliaDiff/FiniteDiff.jl#229 lands, the issue's reproducer is
clean:

julia> f(u, p) = u .^ 2 .- 2.0
julia> prob = NonlinearProblem(f, [1.0])
julia> cache = @inferred SciMLBase.init(prob, TrustRegion(autodiff = AutoFiniteDiff()));   # OK

What this PR changes

inference_tests__item1.jl only ever built in-place problems, which is why this went
unnoticed — the in-place FiniteDiff path in DI was already inferrable. This adds the
out-of-place problem to the Jacobian-cache testset and folds the two Full init testsets
into one loop over {in-place, out-of-place} × {TrustRegion, NewtonRaphson}. I confirmed
the new cases fail on FiniteDiff 2.32.0 and pass on the patched build.

The FiniteDiff lower bound moves 2.24 → 2.32.1 so Downgrade CI resolves a version where
these tests actually hold.

Not fixed here

Two neighbouring instabilities that are not what #1092 reports, listed so they don't get
conflated:

  • Default AutoForwardDiff() on out-of-place problems. ForwardDiff.JacobianConfig
    encodes the chunk size in its type and pickchunksize(length(u)) is a runtime value, so
    the DI prep — and therefore the cache — cannot be inferred. Passing
    AutoForwardDiff(chunksize = n) makes it inferrable; I verified both. This is inherent to
    ForwardDiff, not something NonlinearSolve can paper over.
  • LevenbergMarquardt, on every AD backend. Its DampedNewtonDescentCache init picks
    mode (Val{:normal_form} / Val{:least_squares} / …) at runtime, leaving
    __T_lincache, __T_JᵀJ_cache, __T_mode and friends as open bounds. Separate root
    cause in damped_newton.jl; happy to open an issue for it.

Testing

GROUP=Core for lib/NonlinearSolveFirstOrder on Julia 1.10.11, against the patched
FiniteDiff:

     Testing NonlinearSolveFirstOrder tests passed

The new inference cases also pass on 1.11.9 and 1.12.6.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MoeiVWqAEFJQK22kiXu48f

`inference_tests__item1.jl` only exercised in-place problems, so the
out-of-place instability in SciML#1092 went unnoticed:

    prob = NonlinearProblem((u, p) -> u .^ 2 .- 2.0, [1.0])
    @inferred SciMLBase.init(prob, TrustRegion(autodiff = AutoFiniteDiff()))

inferred the cache's `jac_cache`/`descent_cache` to open `where`-bounds.
The root cause was FiniteDiff's out-of-place `finite_difference_jacobian`
returning `Any` (JuliaDiff/FiniteDiff.jl#229); the Jacobian cache is typed
by that return value, so the whole solver cache went with it.

Add the out-of-place problem to the existing testset and raise the
FiniteDiff lower bound to the release carrying the fix, so Downgrade CI
resolves a version where these tests hold.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MoeiVWqAEFJQK22kiXu48f
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review August 3, 2026 17:17
@ChrisRackauckas
ChrisRackauckas merged commit c9c259d into SciML:master Aug 3, 2026
16 of 74 checks passed
@JamesWrigley

Copy link
Copy Markdown
Member

I think this is making most of the tests fail and NLSFirstOrder fail to instantiate, because FiniteDiff 2.32.1 hasn't been released yet.

@ChrisRackauckas

Copy link
Copy Markdown
Member

It should be released now though, I was just pushing it through quicker because it was going to be easier than trying to remember the inference test down the line.

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.

init(::NonlinearProblem, TrustRegion) not type-stable: Dogleg/JacobianCache infer to open where-bounds

3 participants