fix(zygote ext): handle NamedTuple cotangents in vofa_u_adjoint (EnsembleSolution end-time grads)#623
Draft
ChrisRackauckas-Claude wants to merge 1 commit into
Conversation
`vofa_u_adjoint` rewrapped the per-element cotangents of an `AbstractVectorOfArray`/`AbstractDiffEqArray` in a `VectorOfArray`/`DiffEqArray`. For an `EnsembleSolution` whose elements are solution objects, differentiating only a scalar field (e.g. `sol.t[end]`) makes Zygote produce structural `NamedTuple` cotangents per trajectory. These have no `ndims`, so the reconstruction threw `MethodError: no method matching ndims(::NamedTuple)`. Detect non-array cotangents and pass them through as a plain vector instead. This extends the earlier `ZeroTangent`/`AbstractZero` handling (SciML#606) to the structural-tangent case. Fixes SciML/DifferentialEquations.jl#1149 Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
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.
Problem
Reverse-mode (Zygote) differentiation through an
EnsembleSolutioncrashes when the loss reads only a scalar field of each trajectory's solution — e.g. the halting time produced by aterminate!callback:with
ForwardDiff works; Zygote does not.
Cause
EnsembleSolution <: AbstractVectorOfArray, so accessing.uhits theliteral_getproperty(::AbstractVectorOfArray, ::Val{:u})adjoint →vofa_u_adjoint, which rewraps the per-element cotangents in aVectorOfArray. When only a scalar field of each per-trajectoryODESolutionis differentiated, Zygote's cotangent for each element is a structuralNamedTuple((u = nothing, t = […])), not an array.VectorOfArray(::Vector{NamedTuple})then callsndimson aNamedTuple, which has no method.This is the structural-tangent analogue of the
ZeroTangent/size(::ZeroTangent)case fixed in #606 (same function, sibling failure mode) — which is likely why the report looked handled but wasn't.Fix
In both
vofa_u_adjointmethods, detect when the mapped cotangents are not array-like and pass them through as a plain vector instead of forcing aVectorOfArray/DiffEqArrayreconstruction.Test
Added to
test/AD/adjoints.jl, mirroring the #606 test: feedsNamedTuplecotangents throughvofa_u_adjointfor bothVectorOfArrayandDiffEqArray, and a mixedNamedTuple/nothing/ZeroTangentcase. Verified locally that the new test reproduces the exactndims(::NamedTuple)error on the unmodified source and passes with the fix; the fullAD/adjoints.jlsuite passes.Scope / not addressed
This fixes the crash so the cotangent flows through. Whether
GaussAdjoint(or othersolveadjoints) then propagates the event-time (sol.t[end]) sensitivity correctly through theContinuousCallback/terminate!is a separate, downstream concern (the substance of SciML/DifferentialEquations.jl#1149) and is not claimed to be solved here.Fixes SciML/DifferentialEquations.jl#1149
🤖 Generated with Claude Code