Skip to content

[v2-backport] Fix integer time-step indexing under AD (#1325) - #1328

Merged
ChrisRackauckas merged 1 commit into
SciML:v2-backportfrom
ChrisRackauckas-Claude:cc/fix-integer-getindex-ad-v2
Apr 24, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:v2-backportfrom
ChrisRackauckas-Claude:cc/fix-integer-getindex-ad-v2

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

Issue: #1325
Companion: #1327

Verification

  • Reproduced BoundsError: attempt to access 2-element Vector{Float64} at index [101] on unpatched v2.155.0 (exact MWE from 2.155 breaks Mooncake when iterating over ODESolution #1325).
  • After patch, Mooncake gradient matches ForwardDiff exactly: [8.304244028181515, -159.48401961551903, 75.20316229897975, -339.1951631373029].
  • Zygote also passes (no Zygote changes on this branch; verifies no regression via CRC path).
  • runic format-check clean.

🤖 Generated with Claude Code

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]>
@ChrisRackauckas
ChrisRackauckas merged commit fc65dc5 into SciML:v2-backport Apr 24, 2026
33 of 46 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