Repository navigation
Make AutoSpecialize infer through functor residuals; zero-alloc homotopy stepping - #1046
Merged
ChrisRackauckas merged 1 commit intoJul 12, 2026
Conversation
…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]>
ChrisRackauckas
marked this pull request as ready for review
July 12, 2026 11:15
This was referenced Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: This PR should be ignored until reviewed by @ChrisRackauckas.
Summary
The homotopy continuation drivers (
HomotopySweep,ArcLengthContinuation) wrap their residual in a functor —FixLambdafor the sweep,AugmentedHomotopy/HomotopyResidualfor arclength — and build the innerNonlinearFunctionat theAutoSpecializedefault.AutoSpecializewraps an in-place residual in a chunksize-1FunctionWrappersWrapper, whose fixed dual signatures should letinit(inner_prob, inner)infer a concrete cache so the driver's reused inner cache and every per-stepsolve!are allocation-free.For a functor residual the inferred wrapper type instead widened to
Any, so every interiorsolve!and every.u/.resid/.retcode/.statsread boxed. Measured on Julia 1.11, N = 50:HomotopySweep(innerNewtonRaphson)ArcLengthContinuation(innerNewtonRaphson)Root cause
FunctionWrappersWrapper's(f, argtypes, rettypes)convenience constructor builds its wrapper tuple withmap(argtypes, rettypes) do A, R; FunctionWrapper{R, A}(f); end. The closure leavesA/Rinferred as the join of the heterogeneousargtypestuple, sotypeof(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-parameterODEFunctions). 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_iiphelper that binds each arglist as a::Type{A}type parameter, exactly mirroring DiffEqBase's_make_fww. That keepstypeof(fwt)concrete, so the wrapped functor problem'sinitinfers 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 theAny-widening at the cost of extra specialization / no wrapper reuse): the sweep and arclength both use theAutoSpecializedefault again, and arclength drops from 320 → 0 B/step with no arclength source change — purely from thewrapfun_iipfix.The
wrapfun_iipchange is a pure inference improvement (the runtimeFunctionWrappersWrappervalue/type is unchanged), so it benefits every in-placeAutoSpecializefunctor residual, not just the homotopy drivers.Scope / notes
SimpleNewtonRaphsonas 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 widensinittoAny; deterministic) and the zero-alloc stepping for both drivers with a cleanNewtonRaphsoninner (gated on v1.11+).homotopy_jac_tests__item1.jl: updated to assert the inner function is now builtAutoSpecialize.Corehomotopy/arclength suite (incl.*_jac,*_tests__item22, polyalg warm-handoff) passes locally on Julia 1.11.🤖 Generated with Claude Code