Repository navigation
Fix integer time-step indexing under AD (#1325) - #1327
Merged
ChrisRackauckas merged 2 commits intoApr 24, 2026
Merged
ChrisRackauckas merged 2 commits into
ChrisRackauckas merged 2 commits into
Conversation
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]>
4 tasks done
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.
Summary
sol[i::Integer]under reverse-mode AD. The broadgetindexrrules in the Mooncake, Zygote, and ChainRulesCore extensions were catching integer time-step indexing and mis-treating the integer as a state-variable index, producingBoundsErroron problems wherelength(t) > length(u[1])— the surface reported in 2.155 breaks Mooncake when iterating overODESolution#1325.::Integermethods in each AD extension. Julia dispatch picks them over the broad symbolic rules for integer inputs; the symbolic path (sol[sym]) is unchanged.ODESolution#1325 (Mooncake + Zygote gradients of a Lotka–Volterralogpthat iterateseachindex(sol)and indexessol[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-symbolicsymthrough_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 theiterate/tuplesoverloads, 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Δintosol_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: newrrule(::typeof(getindex), VA::ODESolution, i::Integer).Companion PR for v2 line
A narrower backport targeting
v2-backportis opened separately: Mooncake + CRC only (v2-backport still carries the pre-v3::IntZygote adjoint, so no Zygote change needed there).Test plan
ODESolution#1325BoundsErroron unpatched v2.155.0 (BoundsError: attempt to access 2-element Vector{Float64} at index [101]).runicformat-check clean.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