Skip to content

Partial implementation of Zygote AD with symbolic indexing - #479

Merged
ChrisRackauckas merged 8 commits into
SciML:masterfrom
contradict:zygote_rules
Aug 12, 2023
Merged

ChrisRackauckas merged 8 commits into
SciML:masterfrom
contradict:zygote_rules

Conversation

@contradict

Copy link
Copy Markdown
Contributor

Symbolic indexing of state and observed variables. Time index must be specified for observed symbols.

This is a rebase and fixup of https://github.com/lamorton/scimlbase.jl/tree/zygote_rules

Symbolic indexing of state and observed variables. Time index must be
specified or observed.

@ai-maintainer ai-maintainer Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-Maintainer Review for PR - Partial implementation of Zygote AD with symbolic indexing

Title and Description 👍

The Title and Description are clear and focused
Details The title and description of the pull request are clear and focused. They effectively communicate the purpose of the changes, which is to implement symbolic indexing of state and observed variables in the context of Zygote AD. However, it would be beneficial to include more specific details about the implementation and motivation behind it.

Scope of Changes 👍

The changes are narrowly focused
Details The changes in this pull request are narrowly focused on implementing symbolic indexing of state and observed variables in the context of Zygote AD. There is no indication that the author is trying to resolve multiple issues simultaneously.

Testing ⚠️

Testing details are missing
Details The description does not mention how the author tested the changes. It would be helpful to have information about the testing approach, such as specific test cases or scenarios that were used to verify the correctness and functionality of the implemented symbolic indexing.

Documentation ⚠️

Docstrings are missing for new functions
Details The following newly added functions, classes, or methods do not have docstrings:
  • ZygoteExt.getindex(VA::ODESolution, i::Int, j::Int)
  • ZygoteExt.getindex(VA::ODESolution, sym, j::Int)
  • ChainRulesCore.rrule(config::ChainRulesCore.RuleConfig, ::typeof(getindex), VA::ODESolution, sym, j::Integer)
  • ChainRulesCore.rrule(::typeof(getindex), VA::ODESolution, sym)
  • ZygoteExt.getindex(VA::ODESolution, i::Int)
  • ZygoteExt.getindex(VA::ODESolution, sym)

These functions, classes, or methods should have docstrings added to describe their behavior, arguments, and return values.

Suggested Changes

  • Please add docstrings to the new functions, classes, or methods to describe their behavior, arguments, and return values.
  • Please provide information about how the changes were tested. This could include specific test cases or scenarios that were used to verify the correctness and functionality of the implemented symbolic indexing.

Reviewed with AI Maintainer

@codecov

codecov Bot commented Aug 11, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #479 (d5d5e37) into master (cce1ce0) will increase coverage by 3.08%.
The diff coverage is 30.00%.

@@            Coverage Diff             @@
##           master     #479      +/-   ##
==========================================
+ Coverage   53.73%   56.82%   +3.08%     
==========================================
  Files          47       50       +3     
  Lines        3584     3657      +73     
==========================================
+ Hits         1926     2078     +152     
+ Misses       1658     1579      -79     
Files Changed Coverage Δ
src/SciMLBase.jl 71.42% <ø> (ø)
src/solutions/chainrules.jl 0.00% <0.00%> (ø)
src/solutions/zygote.jl 46.15% <46.15%> (ø)
ext/ZygoteExt.jl 50.00% <50.00%> (ø)

... and 9 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Comment thread ext/ZygoteExt.jl Outdated
Comment on lines +16 to +42
Δ′ = ODESolution{
T,
N,
typeof(du),
Nothing,
Nothing,
typeof(VA.t),
typeof(VA.k),
typeof(dprob),
typeof(VA.alg),
typeof(VA.interp),
typeof(VA.destats),
typeof(VA.alg_choice),
}(du,
nothing,
nothing,
VA.t,
VA.k,
dprob,
VA.alg,
VA.interp,
VA.dense,
0,
VA.destats,
VA.alg_choice,
VA.retcode)
(Δ′, nothing, nothing)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong formatting.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't revert this if you run the formatter, so put it all to one line and then reformat with the right format.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, maybe fixed?

Comment thread ext/ZygoteExt.jl Resolved
Comment thread src/solutions/chainrules.jl Resolved
Comment thread src/solutions/zygote.jl Resolved
@ChrisRackauckas

Copy link
Copy Markdown
Member

Alright cool this looks good to go if tests pass. I hope we can get sol[sym] working ASAP afterwards since that's probably the most commonly used call, but at least there's a workaround for now.

@contradict

Copy link
Copy Markdown
Contributor Author

Oh, it fails on 1.6 since package extensions don't exist yet. Should I skip the tests on 1.6 or is there a workaround to get the extensions to load?

@ChrisRackauckas

Copy link
Copy Markdown
Member

Just skip v1.6. We're getting close to dropping it (please LTS come soon).

Comment thread test/downstream/remake_autodiff.jl Resolved
@ChrisRackauckas
ChrisRackauckas merged commit 4d926fc into SciML:master Aug 12, 2023
ChrisRackauckas added a commit that referenced this pull request Apr 24, 2026
`solution_new_original_retcode` previously rebuilt the `ODESolution` via
three sequential `@reset` / `@set` calls. Each expands into
`ConstructionBase.setproperties(sol, patch)`, which funnels through
`merge(getproperties(sol), patch)` and then re-calls `ODESolution{T,N}(...)`.
That whole call graph exceeds Julia's inference limits for non-trivial
callers (e.g. `BoundaryValueDiffEqCore.__build_solution`, which annotates
its argument as the bare `::NonlinearSolution` UnionAll). Inference gives
up and widens the rebuilt solution's return type all the way to the bare
`ODESolution` UnionAll, breaking `@inferred solve(bvp, MIRK6(...))` in
BoundaryValueDiffEq.jl (issue #479).

Replace the `@reset` chain with a single direct inner-constructor call
that swaps only the three fields being updated. This keeps the rebuild
fully inferrable: the concrete `typeof(original)` / `typeof(resid)` flow
straight into the new type parameters, and the other parameters are
carried over unchanged from `sol`.

Adds a regression test in test/solution_interface.jl that reproduces the
widening with a bare `::NonlinearSolution` caller annotation.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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