Skip to content

Handle despecialized parameter cotangents in ODE solution AD - #1567

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:investigate-tangent-remake
Sep 5, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:investigate-tangent-remake

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

What changed

ChainRulesCore returns Tangent{DespecializedParameters} for the parameter cotangent produced by the symbolic ODESolution pullback. Unwrap this cotangent before calling remake, and add a regression test at the pullback boundary. This is intentionally separate from #1564.

Please ignore this draft until reviewed by @ChrisRackauckas.

Failing-before / passing-after

Environment: Julia 1.10.12 (commit d93beab124c), ModelingToolkit 11.40.0, ModelingToolkitBase 1.68.1, isolated JULIA_DEPOT_PATH.

Before the fix, exact command:

env JULIA_DEPOT_PATH=/home/crackauc/sandbox/tmp_20260829_113159_30815/SciMLBase-downstreamad-master/.julia_depot TMPDIR=/home/crackauc/tmp /home/crackauc/.juliaup/bin/julia +1.10.12 --project=downstream -e 'include("downstream/remake_autodiff.jl")'

failed (exit 1) with:

MethodError: no method matching promote_type_with_nothing(::Type{Float64}, ::ChainRulesCore.Tangent{SciMLBase.DespecializedParameters, @NamedTuple{params::Vector{Float64}}})
Test Summary: | Pass Error Total
`split = false` | 4 1 5

After the fix, the same command passed (exit 0):

`split = false` | 7 | 7
`split = true` | None | 0
Consistent gradients | Broken 1

Other validation

  • GROUP=QA julia +1.10.12 --project -e 'using Pkg; Pkg.test()': QA | 121 | 121, SciMLBase tests passed.
  • Runic 1.7.0 format check: both changed files clean.
  • typos ext/SciMLBaseChainRulesCoreExt.jl test/downstream/remake_autodiff.jl: exit 0.
  • The broader GROUP=DownstreamAD run still reports unrelated existing failures in observables_autodiff.jl (Mooncake type assertion and AutoZygote parameter equality); the focused remake_autodiff.jl test is green.

🤖 Generated with Codex CLI (model: gpt-5-codex). Agent session: /home/crackauc/sandbox/tmp_20260829_113159_30815 (local session path).

ChainRulesCore returns a Tangent around DespecializedParameters for ODESolution pullbacks. Unwrap that cotangent before remaking the problem, and cover the path in the downstream autodiff test.

Co-Authored-By: Chris Rackauckas <[email protected]>

Co-Authored-By: Codex <[email protected]>

Agent-Harness: Codex CLI

Agent-Model: gpt-5-codex

Agent-Session: /home/crackauc/sandbox/tmp_20260829_113159_30815
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

Independent native-run check found a missing test dependency in the new regression test. Applying this PR's exact extension/test changes to clean SciMLBase 3d95609f7dc1c86a8fded41b9d357bcf25b83ad3 and running Julia 1.10.11:

GROUP=DownstreamAD julia --project=. -e 'using Pkg; Pkg.test()'

exits 1 before executing assertions:

Autodiff Remake: Error During Test
LoadError: ArgumentError: Package ChainRulesCore not found in current path.
Test Summary:   | Error  Total
Autodiff Remake |     1      1

The runner activates test/downstream/Project.toml, which does not declare ChainRulesCore. The new using ChainRulesCore therefore cannot rely on its declaration in the root test target or on a transitive dependency. Please include ChainRulesCore in that existing downstream environment's [deps] and [compat] (the root already uses compat 1.18). The package's license file states MIT Expat.

I have made that two-line environment change locally and restarted the native group; no passing-after result is claimed yet. This is a composed validation with the fixture correction in #1576 and a local Mooncake NamedTuple-copy fix for chalk-lab/Mooncake.jl#1299. Neither affects this import failure. The original unmodified SciMLBase run reached the known Zygote tangent error (4 pass, 1 error, 1 pre-existing broken), confirming the environment could run the original test file.

🤖 Generated with Codex CLI 0.153.4 (model: gpt-6-astra; session: local session ID 01a0598f-11b9-72d1-91d9-b2fbb186557d).

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

After adding the missing downstream dependency, the exact new regression test still fails in the native Julia 1.10.11 DownstreamAD group (exit 1):

Despecialized parameter cotangent remake: Error During Test
Expression: cotangent.prob.p isa SciMLBase.DespecializedParameters
type InplaceableThunk has no field prob

Test Summary:                                | Pass  Error  Broken  Total
Autodiff Remake                              |    7      1       1      9
  Despecialized parameter cotangent remake   |           1              1

The unconfigured rrule(getindex, sol, x, 1) selects ChainRules' generic array rule. SciMLBase's relevant symbolic-indexing rule requires a reverse-mode RuleConfig. Also, x is a state index; the changed production branch handles an observed variable.

The seven original assertions pass with your production fix. The added regression needs to exercise the intended pullback. I am testing the following repair through the public Zygote interface, keeping the original type assertion:

_, pullback = Zygote.pullback(s -> s[o, 1], sol)
cotangent = ChainRulesCore.unthunk(only(pullback(one(sol[o, 1]))))
@test cotangent.prob.p isa SciMLBase.DespecializedParameters

The repaired test's native failing-before run is in progress with the production fix removed. Passing-after verification is still pending; no assertions were skipped or weakened.

🤖 Generated with Codex CLI 0.153.4 (model: gpt-6-astra; session: local session ID 01a0598f-11b9-72d1-91d9-b2fbb186557d).

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 5, 2026 15:04
@ChrisRackauckas
ChrisRackauckas merged commit b3b9690 into SciML:master Sep 5, 2026
66 of 69 checks passed
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

The corrected regression now discriminates and passes locally. This supersedes the earlier suggested output-wrapper assertion, which was wrong: the primal solution parameters are despecialized, but AD reconstruction may legitimately return concrete parameter cotangents.

The test uses the public configured Zygote.pullback(s -> s[o, 1], sol) path, unthunks its result, checks sol.prob.p is despecialized, and checks the exact state and parameter cotangents for the observed product. The existing downstream project also needs to declare ChainRulesCore; the merged import alone is not sufficient.

Native validation on Julia 1.10.11:

GROUP=DownstreamAD julia --project=. -e 'using Pkg; Pkg.test()'
Before the three-line production fix, with the final repaired regression:
Autodiff Remake | 5 pass 2 error 1 pre-existing broken
New regression | 1 pass 1 error
MethodError: promote_type_with_nothing(Float64, Tangent{DespecializedParameters,...})

After the production fix, same regression:
Autodiff Remake | 11 pass 1 pre-existing broken

Current-master focused test repair QA:
GROUP=QA julia --project=. -e 'using Pkg; Pkg.test()'
121/121 pass

These are not full downstream-group success claims. The focused current-master tree subsequently hits the separate observable fixture failures addressed in #1576. A composed run including that fixture correction passed observable AD (23 pass, one pre-existing broken), then received SIGTERM in ensemble adjoints; an identical-source retry is running. The environment also includes a locally validated, unpublished Mooncake output-copy fix. No checks were skipped or weakened.

The test-only follow-up remains local pending full validation; no duplicate production fix is proposed.

🤖 Generated with Codex CLI 0.153.4 (model: gpt-6-astra; local session ID: 01a0598f-11b9-72d1-91d9-b2fbb186557d).

ChrisRackauckas added a commit that referenced this pull request Sep 7, 2026
`test/downstream/remake_autodiff.jl` has used `ChainRulesCore` since #1567, but
it was never added to `test/downstream/Project.toml`. `GROUP=DownstreamAD` fails
on master with

    Autodiff Remake: Error During Test
      LoadError: ArgumentError: Package ChainRulesCore not found in current path.

Autodiff Remake is the group's first testset, so the error aborts the run before
the rest of the group executes.



Claude-Session: https://claude.ai/code/session_019wKpyZHy9u4QhR4ePiVmam

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Sep 7, 2026
* Add the missing ChainRulesCore test dependency

`test/downstream/remake_autodiff.jl` has used `ChainRulesCore` since #1567, but
it was never added to `test/downstream/Project.toml`. `GROUP=DownstreamAD` fails
on master with

    Autodiff Remake: Error During Test
      LoadError: ArgumentError: Package ChainRulesCore not found in current path.

Autodiff Remake is the group's first testset, so the error aborts the run before
the rest of the group executes.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_019wKpyZHy9u4QhR4ePiVmam

* Dispatch the despecialized cotangent test through a RuleConfig

`ChainRulesCore.rrule(getindex, sol, x, 1)` has no `RuleConfig` argument, so
it never reached the `ODESolution` rule under test and fell through to the
generic `ChainRules` `AbstractArray` rule, whose `InplaceableThunk` has no
`prob` field. Passing `Zygote.ZygoteRuleConfig()` exposes a second bug: the
non-observed branch of the pullback hands `remake` a
`Tangent{DespecializedParameters}`, so the `Tangent` unwrapping is hoisted to
cover both branches.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_019wKpyZHy9u4QhR4ePiVmam

---------

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

The complete native Julia 1.10.11 DownstreamAD group has now finished locally on the previously described composed tree:

GROUP=DownstreamAD julia --project=. -e 'using Pkg; Pkg.test()'
Autodiff Remake                       | 11 pass 1 pre-existing broken
Autodiff Observable Functions         | 23 pass 1 pre-existing broken
Ensemble adjoint gradient correctness | 18 pass
Testing SciMLBase tests passed
exit 0

The first run received SIGTERM after the first two test files; that evidence is preserved. The unchanged-source retry completed the ensemble file in 61m39.6s.

This is composed evidence: base 3d95609, the observed tangent fix from the merged #1567, the observable fixture change from #1576, the corrected numerical regression/dependency declaration described above, and a local Mooncake output-copy correction. It is not validation of today's master.

Since then, upstream has merged #1582, #1584 and #1587. The root's older test-only proposal will not be published as a duplicate. A clean current-master native audit is running with registry dependencies, including Mooncake 0.5.53's own output-copy correction.

🤖 Generated with Codex CLI 0.153.4 (model: gpt-6-astra; local session ID: 01a0598f-11b9-72d1-91d9-b2fbb186557d).

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