Skip to content

Make AutoSpecialize infer through functor residuals; zero-alloc homotopy stepping - #1046

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:autospecialize-homotopy-zeroalloc
Jul 12, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:autospecialize-homotopy-zeroalloc

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Note: This PR should be ignored until reviewed by @ChrisRackauckas.

Summary

The homotopy continuation drivers (HomotopySweep, ArcLengthContinuation) wrap their residual in a functor — FixLambda for the sweep, AugmentedHomotopy/HomotopyResidual for arclength — and build the inner NonlinearFunction at the AutoSpecialize default. AutoSpecialize wraps an in-place residual in a chunksize-1 FunctionWrappersWrapper, whose fixed dual signatures should let init(inner_prob, inner) infer a concrete cache so the driver's reused inner cache and every per-step solve! are allocation-free.

For a functor residual the inferred wrapper type instead widened to Any, so every interior solve! and every .u/.resid/.retcode/.stats read boxed. Measured on Julia 1.11, N = 50:

driver before after
HomotopySweep (inner NewtonRaphson) 96 B/step 0 B/step
ArcLengthContinuation (inner NewtonRaphson) 320 B/step 0 B/step

Root cause

FunctionWrappersWrapper's (f, argtypes, rettypes) convenience constructor builds its wrapper tuple with map(argtypes, rettypes) do A, R; FunctionWrapper{R, A}(f); end. The closure leaves A/R inferred as the join of the heterogeneous argtypes tuple, so typeof(fwt) — and hence the wrapper, and every cache built from the wrapped problem — widens to abstract whenever the wrapped callable is one inference cannot see through. A functor is exactly such a callable (DiffEqBase hit the identical problem with many-parameter ODEFunctions). The runtime wrapper value is concrete and identical to the plain-function case; only its inferred type differed.

Fix

wrapfun_iip (ForwardDiff extension) now builds the wrapper through a _make_fww_iip helper that binds each arglist as a ::Type{A} type parameter, exactly mirroring DiffEqBase's _make_fww. That keeps typeof(fwt) concrete, so the wrapped functor problem's init infers concretely and the drivers step allocation-free.

With that in place the sweep no longer needs to opt into FullSpecialize (which had been added only to dodge the Any-widening at the cost of extra specialization / no wrapper reuse): the sweep and arclength both use the AutoSpecialize default again, and arclength drops from 320 → 0 B/step with no arclength source change — purely from the wrapfun_iip fix.

The wrapfun_iip change is a pure inference improvement (the runtime FunctionWrappersWrapper value/type is unchanged), so it benefits every in-place AutoSpecialize functor residual, not just the homotopy drivers.

Scope / notes

  • 0 B/step is on Julia 1.11+; v1.10 may still allocate the LinearSolve pivot vector (out of scope here).
  • SimpleNewtonRaphson as a homotopy inner remains ~100 KB/step — it implements no cache-reuse interface and cold-re-solves each step (a SimpleNonlinearSolve limitation, scoped to scalar/StaticArray problems; unaffected by this PR).

Tests

  • homotopy_alloc_tests__item1.jl (new): pins the inference guarantee (the functor's wrapper no longer widens init to Any; deterministic) and the zero-alloc stepping for both drivers with a clean NewtonRaphson inner (gated on v1.11+).
  • homotopy_jac_tests__item1.jl: updated to assert the inner function is now built AutoSpecialize.
  • Full Core homotopy/arclength suite (incl. *_jac, *_tests__item22, polyalg warm-handoff) passes locally on Julia 1.11.

🤖 Generated with Claude Code

…opy stepping

The homotopy continuation drivers wrap their residual in a functor (`FixLambda`
for the sweep; `AugmentedHomotopy`/`HomotopyResidual` for arclength) and build the
inner `NonlinearFunction` at the `AutoSpecialize` default. `AutoSpecialize` wraps
an in-place residual in a chunksize-1 `FunctionWrappersWrapper`, which should make
`init(inner_prob, inner)` infer a concrete cache — but for a functor residual the
inferred wrapper type widened to `Any`, so every reused-cache `solve!` and every
field read of the interior solves boxed. Measured on Julia 1.11 (N = 50): the sweep
allocated 96 B/step and arclength 320 B/step.

Root cause is `FunctionWrappersWrapper`'s `(f, argtypes, rettypes)` convenience
constructor: it builds its wrapper tuple with `map(argtypes, rettypes) do A, R; ...`,
whose closure leaves `A`/`R` inferred as the join of the heterogeneous `argtypes`
tuple, so `typeof(fwt)` — and the wrapper — widens to abstract whenever the wrapped
callable is one inference cannot see through (a functor here; DiffEqBase hit the same
with many-parameter `ODEFunction`s). The runtime value is concrete; only its inferred
type differed.

Fix `wrapfun_iip` (ForwardDiff ext) to build the wrapper through a `_make_fww_iip`
helper that binds each arglist as a `::Type{A}` type parameter, exactly mirroring
DiffEqBase's `_make_fww`. This keeps `typeof(fwt)` concrete, so the wrapped functor
problem's `init` infers concretely and the drivers step allocation-free. With that in
place the sweep no longer needs to opt into `FullSpecialize` (added only to dodge the
`Any`-widening): it and arclength now use the `AutoSpecialize` default, whose chunk-1
wrapper is what makes the inner cache concrete — and arclength drops from 320 to 0
B/step with no arclength source change.

Both drivers measure 0 B/step on Julia 1.11 (v1.10 may still allocate the LinearSolve
pivot vector, which is out of scope here). `homotopy_alloc_tests__item1.jl` pins the
inference guarantee (deterministic) and the zero-alloc stepping (gated on v1.11+);
`homotopy_jac_tests__item1.jl` is updated to assert the inner function is now
`AutoSpecialize`.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Chris Rackauckas <[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