Skip to content

Fix(ad): accesing per-trajectory fields - #1380

Closed
LucasGrasso wants to merge 2 commits into
SciML:masterfrom
LucasGrasso:fix_ensemble_traj_access
Closed

LucasGrasso wants to merge 2 commits into
SciML:masterfrom
LucasGrasso:fix_ensemble_traj_access

Conversation

@LucasGrasso

Copy link
Copy Markdown

The issue arises when trying to iterate sol.u and read per-trajectory fields on a loss fn. Zygote reverse pass produces a cotangent for the ensemble that is an EnsembleSolution that wraps a 3D VectorOfArray, and when calling getindex it yields a scalar value. using Array(sol) bypasses this and works.

The error was

ERROR: LoadError: BoundsError: attempt to access Tuple{} at index [0]
Stacktrace:
  [1] getindex(t::Tuple, i::Int64)
    @ Base ./tuple.jl:31
  [2] (::SciMLSensitivity.var"#df_iip#338"{Float32, Colon})(_out::Vector{Float32}, u::Vector{Float32}, p::ComponentVector{Float32, Vector{Float32}, Tuple{Axis{(weight = ViewAxis(1:16, ShapedAxis((4, 4))), bias = ViewAxis(17:20, Shaped1DAxis((4,))))}}}, t::Float32, i::Int64)
    @ SciMLSensitivity ~/.julia/packages/SciMLSensitivity/9RPKK/src/concrete_solve.jl:839
  [3] ReverseLossCallback
    @ ~/.julia/packages/SciMLSensitivity/9RPKK/src/adjoint_common.jl:744 [inlined]
...

The cotangent Δ that arrives at ∇responsible_map_internal is:

Δ :: EnsembleSolution{Float32, 1, VectorOfArray{Float32, 3, Vector{DiffEqArray{...}}}}
Δ.u :: VectorOfArray{Float32, 3, Vector{DiffEqArray{...}}}
Δ.u.u :: Vector{DiffEqArray{...}}   # length = n_traj, one per trajectory
Δ.u.u[i] :: DiffEqArray{...}        # trajectory i's cotangent (state × time)

This PR adds a case for __getIndex that fixes this issue. It also provides downstream tests for it. All Core and Downstream (except Autodiff Remake which failed before the update) tests passing.

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Closes SciML/SciMLSensitivity.jl#1478

Probably related to #1347

@ChrisRackauckas

Copy link
Copy Markdown
Member

The __getindex was actually using the old behavior of RAT v3, so it's better to more clearly just not use the linear indexing type there, i.e. #1384. Should be good now. Thanks for pointing in this direction, that helped simplify it a lot.

@LucasGrasso

Copy link
Copy Markdown
Author

thanks @ChrisRackauckas for helping out, I will try this out and close the discourse thread.

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.

GaussAdjoint and QuadratureAdjoint fail on EnsembleProblem with Callbacks

2 participants