Skip to content

Fix getproperty(::NonlinearProblem) rrule to emit full-width cotangents - #1382

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix-nonlinearproblem-getproperty-rrule
Jun 11, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix-nonlinearproblem-getproperty-rrule

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

Important

This PR should be ignored until reviewed by @ChrisRackauckas.

Fixes the test/downstream/remake_autodiff.jl ("split = false") failure currently on master (also visible as the red Downstream CI on master):

ArgumentError: @NamedTuple{f::Nothing, u0::Nothing, p::Vector{Float64}, problem_type::Nothing, lb::Nothing, ub::Nothing, kwargs::Nothing} keys must be a subset of @NamedTuple{p::Vector{Float64}} keys

Root cause

The getproperty(::NonlinearProblem) rrule in SciMLBaseChainRulesCoreExt returned a single-field NamedTuple like (p = dp,) as the problem cotangent. Zygote.accum cannot combine such partial NamedTuples with full-width cotangents for the same problem produced by other pullbacks (or with a partial for a different field) — it requires one side's keys to be a subset of the other's and throws the ArgumentError above otherwise.

This was latent until ModelingToolkitBase 1.42.0 changed initializeprobmap (SciML/ModelingToolkit.jl commit 5236ce3d7), after which the trivial-initialization NonlinearProblem receives cotangents from two pullback paths in SciMLBase.get_initial_values during Zygote differentiation of remaked MTK ODEProblems — i.e. the failure is exposed by a dependency update, not by a SciMLBase commit (the same SciMLBase v3.17.0 source fails with current deps, verified).

Fix

Return a Tangent{typeof(x)} instead: Zygote canonicalizes Tangents to full-width NamedTuples over all struct fields, which accumulate cleanly in any order. A der === nothing / AbstractZero cotangent now returns ZeroTangent() instead of zero_tangent(x), since a MutableTangent also cannot be accumulated with NamedTuple cotangents.

How this was pinned down

  • The partial-NamedTuple rrule was introduced in aced3e87b (Jul 2025, v2.103.1) but stayed latent.
  • CI archaeology: the LTS Downstream "Autodiff Remake" job was 6-pass/1-broken through the May 31 run and first failed June 1 (v3.17.0); the only dependency delta was ModelingToolkitBase 1.41.0 → 1.42.0. The SciMLBase code delta in that window was unrelated (ForwardDiff ext only) — in particular this is not caused by the v3.18.0 lb/ub remake changes (v3.17.0 source + current Manifest fails identically).
  • Direct trigger confirmation: unfixed master + ModelingToolkitBase pinned to 1.41.0 passes with gradients identical to the fixed run; unfixed master + 1.42.x fails.
  • Why CI didn't flag it loudly: the Downstream group runs with continue_on_error = true, and the Julia "1"/"pre" jobs skip this testset (VERSION < v"1.12" gate — only the LTS job runs it).

Verification

  • Minimal repro (MTK lotka-volterra + remake + BacksolveAdjoint(autojacvec=ZygoteVJP()) via DI AutoZygote, extracted from the failing testset): fails on unmodified master, succeeds with this fix.
  • New unit tests (test/nonlinearproblem_zygote.jl) covering cotangent shape and multi-field/multi-remake accumulation: pass on Julia 1.10, 1.11, and 1.12.
  • Full remake_autodiff.jl "split = false" downstream testset passes with the fix (see CI / verification log).

Related: #1381 (ensemble Zygote adjoint fixes) documents encountering this same failure on master while validating that PR; the two PRs are independent.

Side note for maintainers: the same partial-cotangent hazard would apply to NonlinearLeastSquaresProblem if it ever gains a similar getproperty rrule (it currently has none).

🤖 Generated with Claude Code

ChrisRackauckas and others added 2 commits June 10, 2026 01:50
…rrule

The rrule returned a single-field NamedTuple like (p = dp,) as the problem
cotangent. Zygote.accum cannot combine such partial NamedTuples with
full-width cotangents for the same problem produced by other pullbacks
(or with partials for a different field), throwing
'ArgumentError: ... keys must be a subset of ... keys'.

This broke test/downstream/remake_autodiff.jl ('split = false') once
ModelingToolkitBase 1.42.0 changed 'initializeprobmap' (SciML/ModelingToolkit.jl
commit 5236ce3d7), making the trivial-initialization NonlinearProblem receive
cotangents from two pullback paths in SciMLBase.get_initial_values.

Returning a Tangent{typeof(x)} instead lets Zygote canonicalize it to a
full-width NamedTuple over all problem fields, which accumulates cleanly
in any order. A der === nothing / AbstractZero cotangent now returns
ZeroTangent() instead of zero_tangent(x), since a MutableTangent also
cannot be accumulated with NamedTuple cotangents.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Chris Rackauckas <[email protected]>
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review June 11, 2026 08:33
@ChrisRackauckas
ChrisRackauckas merged commit 83a34fb into SciML:master Jun 11, 2026
26 of 62 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.

3 participants