Skip to content

Fix integer time-step indexing under AD (#1325) - #1327

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:cc/fix-integer-getindex-ad
Apr 24, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:cc/fix-integer-getindex-ad

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

  • Restores correct behavior of sol[i::Integer] under reverse-mode AD. The broad getindex rrules in the Mooncake, Zygote, and ChainRulesCore extensions were catching integer time-step indexing and mis-treating the integer as a state-variable index, producing BoundsError on problems where length(t) > length(u[1]) — the surface reported in 2.155 breaks Mooncake when iterating over ODESolution #1325.
  • Adds more-specific ::Integer methods in each AD extension. Julia dispatch picks them over the broad symbolic rules for integer inputs; the symbolic path (sol[sym]) is unchanged.
  • Adds a regression test mirroring the MWE in 2.155 breaks Mooncake when iterating over ODESolution #1325 (Mooncake + Zygote gradients of a Lotka–Volterra logp that iterates eachindex(sol) and indexes sol[i]).

Issue: #1325

Root cause

After the Mooncake ext was backported from the v3 line (v2.155.0, PR #1318), the scalar rrule!!(::CoDual{typeof(getindex)}, ::CoDual{<:AbstractTimeseriesSolution}, ::CoDual) unconditionally routed non-symbolic sym through _scatter_symbol_timeseries!, which treats the passed integer as a state-variable index. The same pattern was already latent in the ChainRulesCore extension (rrule(::typeof(getindex), VA::ODESolution, sym)). In v3, the Zygote @adjoint Base.getindex(VA::ODESolution, i::Int) that used to guard this case was removed along with the iterate/tuples overloads, so Zygote on master is also affected (the v2 backport PR only needs Mooncake/CRC fixes because that adjoint still exists there).

Fix

Each AD extension now has a dedicated time-step rrule/adjoint for ::Integer:

  • ext/SciMLBaseMooncakeExt.jl: new @is_primitive + rrule!! for (getindex, AbstractTimeseriesSolution, Integer) that scatters Δ into sol_fdata.data.u[i] in-place.
  • ext/SciMLBaseZygoteExt.jl: restores @adjoint Base.getindex(VA::ODESolution, i::Integer) (widened from the old ::Int).
  • ext/SciMLBaseChainRulesCoreExt.jl: new rrule(::typeof(getindex), VA::ODESolution, i::Integer).

Companion PR for v2 line

A narrower backport targeting v2-backport is opened separately: Mooncake + CRC only (v2-backport still carries the pre-v3 ::Int Zygote adjoint, so no Zygote change needed there).

Test plan

  • Reproduce the 2.155 breaks Mooncake when iterating over ODESolution #1325 BoundsError on unpatched v2.155.0 (BoundsError: attempt to access 2-element Vector{Float64} at index [101]).
  • Verify the patched extension makes Mooncake and Zygote gradients of the MWE match ForwardDiff exactly on the v2-backport env.
  • runic format-check clean.
  • Master-line downstream observables_autodiff.jl — blocked locally on the existing RecursiveArrayTools v4 ↔ OrdinaryDiffEq compat gap. The ext files are byte-identical between v2.155.0 and v3.4.3 for the Mooncake portion, and the Zygote adjoint is a restore of the pre-v3 method; manual verification via the v2 backport covers the same code path.

🤖 Generated with Claude Code

ChrisRackauckas and others added 2 commits April 24, 2026 04:53
The broad `rrule!!(::CoDual{typeof(getindex)}, ::CoDual{<:AbstractTimeseriesSolution}, ::CoDual)`
in the Mooncake extension (and the analogous `rrule` in the ChainRulesCore
extension) caught `sol[i::Integer]` and mis-treated `i` as a state-variable
index, producing a `BoundsError` when the number of time steps exceeded the
state size (the path exercised by `for i in eachindex(sol)` loops).

Add dedicated, more-specific methods for the `Integer` argument that
correctly scatter the output cotangent into the i-th time step. Julia's
dispatch picks these over the broad symbolic methods for integer inputs,
leaving the symbolic path (`sol[sym]`) unchanged. On this v2 backport the
Zygote extension still carries the pre-v3 `::Int` adjoint, so no Zygote
changes are needed here.

Regression test mirrors the reported MWE.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
v3 removed the old `@adjoint Base.getindex(VA::ODESolution, i::Int)` along
with the iterate/tuples overloads (PR <TBD> in the v3 cleanup). Without it,
`sol[i::Int]` under Zygote falls through to the broader
`@adjoint Base.getindex(VA::ODESolution, sym)` method, which treats `i` as
a state-variable index and produces a `BoundsError` when `length(t) >
length(u[1])` — the same surface as the Mooncake/CRC path fixed in the
preceding commit.

Restore the adjoint with a widened `::Integer` signature.

Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas merged commit 0a929da into SciML:master Apr 24, 2026
44 of 61 checks passed
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