Skip to content

Return u-field tangent for structural VectorOfArray cotangents - #656

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix/vofa-namedtuple-cotangent
Sep 22, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix/vofa-namedtuple-cotangent

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

Zygote's literal_getproperty adjoint for A.u (vofa_u_adjoint) received per-element cotangents and always rebuilt a VectorOfArray/DiffEqArray from them. When the elements are structural tangents — e.g. the per-solution NamedTuples produced when differentiating ensemble_sol.u for an EnsembleSolution — the VectorOfArray constructor fails on ndims(::NamedTuple), breaking ensemble differentiation (SciML/DifferentialEquations.jl#1149, where Zygote threw MethodError from VectorOfArray(vec::Vector{Any}) while ForwardDiff computed the gradient correctly).

vofa_u_adjoint now detects non-numeric structural elements (anything that is not nothing, an AbstractZero, an AbstractArray or a Number) and returns the u-field tangent (; u = collect(d)) instead. Numeric cotangents keep the existing VectorOfArray/DiffEqArray behavior; mixed vectors with nothing/AbstractZero holes still hit the numeric path as before.

Test plan

  • Regression test added in test/AD/adjoints.jl: vofa_u_adjoint on Any[(; u = ..., t = ...), nothing, (; ...)] for both VectorOfArray and DiffEqArray must return a NamedTuple with the expected .u.

Fails before the fix (revert this patch and run test/AD/adjoints.jl):

MethodError: no method matching ndims(::@NamedTuple{u::Vector{Float64}, t::Float64})
# via VectorOfArray(vec::Vector{Any}) → TypeError in constructor

Passes after: julia --project=test/AD test/AD/adjoints.jl exits 0 (all @tests silent on success).

  • Downstream validation: with this patch dev'd, the ensemble sol.t[end] gradient in SciMLSensitivity (companion PR) matches ForwardDiff to ~1e-5 under both EnsembleSerial and EnsembleThreads.

🤖 Generated with Devin (harness: Devin CLI 3000.10.31, model: SWE-2 Max, session: local transcript /home/crackauc/.local/share/devin/cli/summaries/history_d4203d25c3424828.md)

Zygote's `literal_getproperty` adjoint for `A.u` received per-element
cotangents and always rebuilt a VectorOfArray/DiffEqArray from them.
When the elements are structural tangents — e.g. the per-solution
NamedTuples produced when differentiating `ensemble_sol.u` — the
VectorOfArray constructor fails on `ndims(::NamedTuple)`, breaking
ensemble differentiation
(SciML/DifferentialEquations.jl#1149).

Detect non-numeric structural elements (anything that is not nothing,
an AbstractZero, an AbstractArray or a Number) and return the
`u`-field tangent `(; u = collect(d))` instead. Numeric cotangents keep
the existing VectorOfArray/DiffEqArray behavior.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.10.31
Agent-Model: SWE-2 Max
Agent-Session: local transcript /home/crackauc/.local/share/devin/cli/summaries/history_d4203d25c3424828.md
@ChrisRackauckas
ChrisRackauckas merged commit 87809ee into SciML:master Sep 22, 2026
44 of 46 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 23, 2026
Ships #656: the u-field tangent is returned for structural VectorOfArray cotangents.

Co-Authored-By: Chris Rackauckas <accounts@chrisrackauckas.com>
Co-Authored-By: Claude <noreply@anthropic.com>
Agent-Harness: Claude Code 2.1.273
Agent-Model: claude-opus-5[1m]
Agent-Session: 6a01e420-3436-4518-83cc-6cd853283cfb (local transcript; this harness publishes no conversation URL)
ChrisRackauckas added a commit that referenced this pull request Sep 23, 2026
Ships #656: the u-field tangent is returned for structural VectorOfArray cotangents.



Agent-Harness: Claude Code 2.1.273
Agent-Model: claude-opus-5[1m]
Agent-Session: 6a01e420-3436-4518-83cc-6cd853283cfb (local transcript; this harness publishes no conversation URL)

Co-authored-by: Claude <noreply@anthropic.com>
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