Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix Mooncake gradient crash with ComponentArray#1565

Open
AstitvaAggarwal wants to merge 2 commits into
SciML:masterSciML/SciMLSensitivity.jl:masterfrom
AstitvaAggarwal:fix/1548-componentarray-mooncake-gradientAstitvaAggarwal/SciMLSensitivity.jl:fix/1548-componentarray-mooncake-gradientCopy head branch name to clipboard
Open

Fix Mooncake gradient crash with ComponentArray#1565
AstitvaAggarwal wants to merge 2 commits into
SciML:masterSciML/SciMLSensitivity.jl:masterfrom
AstitvaAggarwal:fix/1548-componentarray-mooncake-gradientAstitvaAggarwal/SciMLSensitivity.jl:fix/1548-componentarray-mooncake-gradientCopy head branch name to clipboard

Conversation

@AstitvaAggarwal

@AstitvaAggarwal AstitvaAggarwal commented Jul 22, 2026

Copy link
Copy Markdown
Member

Summary

concrete_solve.jl's ForwardDiffSensitivity/ForwardSensitivity adjoint pullback assumed a wrapped-array tangent always exposes its data via a .x field. That assumption only held for the package's own internal test wrapper type — for ComponentArrays.ComponentVector, it grabbed the wrong backing field (a ZeroTangent), causing:

MethodError: no method matching vec(::ChainRulesCore.ZeroTangent)

when computing a Mooncake gradient of an ODE solve with a ComponentVector u0 (see #1548 for the full MRE/stacktrace).

This is a generic ChainRulesCore.Tangent-consuming code path shared by every AD backend that calls into it via ChainRulesCore.rrule (Mooncake included, via its generic rrule-wrapping bridge). Empirically, Zygote/ReverseDiff never construct a Tangent with this exact field-mirroring shape for this scenario, so Mooncake is the only backend that currently trips it, but the fix itself is backend-agnostic.

Added _tangent_array_data(v::Tangent), which finds the tangent's one non-zero backing field instead of assuming a fixed name (recursing if that field is itself a Tangent), and used it at both call sites that previously did v.x.

Test plan

  • Added test/Core1/mooncake_componentarray_gradient.jl, mirroring the existing mooncake_vjp_prob_kwargs.jl style, registered in test/runtests.jl
  • Verified the issue's exact MRE now passes with Mooncake (correct finite gradient)
  • Verified Zygote and ReverseDiff still produce identical correct gradients on the same scenario (no regression)

Closes #1548

Cc: @ChrisRackauckas

`concrete_solve.jl` assumed a wrapped-array tangent always exposes its
data via a `.x` field, which only held for the package's own internal
test wrapper type. For `ComponentArrays.ComponentVector`, that grabbed
the wrong backing field (a `ZeroTangent`), causing
`MethodError: no method matching vec(::ChainRulesCore.ZeroTangent)`.

Add `_tangent_array_data` to find the tangent's one non-zero field
instead of assuming a fixed name.

Fixes SciML#1548
@AstitvaAggarwal AstitvaAggarwal changed the title Fix Mooncake gradient crash with ComponentArray u0 Fix Mooncake gradient crash with ComponentArray Jul 22, 2026
@AstitvaAggarwal
AstitvaAggarwal marked this pull request as draft July 22, 2026 00:51
Tangent overloads getproperty to mirror the primal's fields, so
`v.backing` doesn't return the Tangent's own backing field - it
returns ZeroTangent() (the overload's fallback for a missing
property). Use getfield(v, :backing) instead, which bypasses the
overload safely and doesn't require importing the non-public
ChainRulesCore.backing function (which was failing Aqua's
ExplicitImports public-API check in CI).
@AstitvaAggarwal
AstitvaAggarwal marked this pull request as ready for review July 22, 2026 12:28
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.

Mooncake gradient of ODE solve fails with ComponentArray u0

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.