Repository navigation
Read the reinit cache with getfield in the optimization cache getproperty - #1618
Conversation
…roperty` `getproperty(::AbstractOptimizationCache, :u0 | :p)` read `cache.reinit_cache`, which is another call to the same method. For a cache with an abstractly typed field (OptimizationBase's `OptimizationCache` has `solver_args::NamedTuple`), inference then gives `cache.p` and `cache.u0` the union of all field types instead of the field's type, and every solver method using the cache pays for that union. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The code review of this PR at
Could you check whether the change causes these? Once they pass, or turn out to be unrelated flakes, this PR can be merged. 🤖 Posted by an AI agent — harness: Claude Code · model: claude-opus-5-5[1m] (fleet master) |
Follow-up to the regression note above: a closer look at the failing jobs' logs showed they are unrelated to this PR. They are flakes or failures that also occur on other PRs and on the default branch. Merged. Thanks! 🤖 Posted by an AI agent — harness: Claude Code · model: claude-opus-5-5[1m] (fleet master); failure analysis by Codex CLI / gpt-6-astra |
Base.getproperty(::AbstractOptimizationCache, x)readsu0/pfromcache.reinit_cache, andcache.reinit_cacheis another call to the samegetpropertymethod. When the cache has an abstractly typed field (OptimizationBase'sOptimizationCachehassolver_args::NamedTuple), inference of that recursive call falls back to the union of all field types, socache.pandcache.u0are inferred as that union instead of the field's type. The fix reads the field withgetfield(getfield(cache, :reinit_cache), x).On an
OptimizationCachefrominit(prob, Adam(1e-3)):c.pinferred asc.u0inferred asUnion{Nothing, …, Adam{…}, OptimizationVerbosity{true}, OptimizationFunction{…}, …}Float64Vector{Float64}Every solver method that reads
cache.p/cache.u0gets this union, and its cost grows with the size of the cache type. Measured downstream:AugLag(inner = Adam)problem pays ~4.4 s of__solveinference for every new callback type on master, and ~0.08 s with this change.AugLag(inner = Adam), where theOptimizationFunctioncarries large closure types), a short run dropped from 165 s to 110 s. This was measured as a concurrent pair on the same machine, with SciMLBase 3.56 on both sides.The added test in
test/inference_tests.jluses a mock cache with the same shape (areinit_cacheholdingu0/p, plus an abstractly typed field). It fails on master, wherec.pandc.u0infer as the union, and passes with the change.This PR was written by Claude Code on behalf of @SebastianM-C.
🤖 Generated with Claude Code · model: claude-opus-5-5[1m]
Review: reviewed by @SebastianM-C