Skip to content

Read the reinit cache with getfield in the optimization cache getproperty - #1618

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
SebastianM-C:smc/optcache-getproperty
Sep 27, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
SebastianM-C:smc/optcache-getproperty

Conversation

@SebastianM-C

@SebastianM-C SebastianM-C commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Base.getproperty(::AbstractOptimizationCache, x) reads u0/p from cache.reinit_cache, and cache.reinit_cache is another call to the same getproperty method. When the cache has an abstractly typed field (OptimizationBase's OptimizationCache has solver_args::NamedTuple), inference of that recursive call falls back to the union of all field types, so cache.p and cache.u0 are inferred as that union instead of the field's type. The fix reads the field with getfield(getfield(cache, :reinit_cache), x).

On an OptimizationCache from init(prob, Adam(1e-3)):

c.p inferred as c.u0 inferred as
master Union{Nothing, …, Adam{…}, OptimizationVerbosity{true}, OptimizationFunction{…}, …} same union
this PR Float64 Vector{Float64}

Every solver method that reads cache.p/cache.u0 gets this union, and its cost grows with the size of the cache type. Measured downstream:

  • A trivial AugLag(inner = Adam) problem pays ~4.4 s of __solve inference for every new callback type on master, and ~0.08 s with this change.
  • In a larger application (an ODE model calibration with AugLag(inner = Adam), where the OptimizationFunction carries 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.jl uses a mock cache with the same shape (a reinit_cache holding u0/p, plus an abstractly typed field). It fails on master, where c.p and c.u0 infer 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

…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]>
@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

The code review of this PR at bbe77ae found nothing blocking. The merge bar is "no test regressions", though, and these checks fail here but pass on the default branch's recent runs. They were re-run once, to rule out a flaky job:

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)
Conversation: https://claude.ai/code/session_01LvC5dTuxGscm48yhAw8Ssn

@ChrisRackauckas
ChrisRackauckas merged commit 08f6f74 into SciML:master Sep 27, 2026
123 of 139 checks passed
@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

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
Conversation: https://claude.ai/code/session_01LvC5dTuxGscm48yhAw8Ssn

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