Skip to content

Build the growth Jacobian without hcat, so ReverseDiff can take it, and replay compiled tapes at a second point - #1006

Closed
seabbs-bot wants to merge 5 commits into
mainfrom
claude/reversediff-growth
Closed

seabbs-bot wants to merge 5 commits into
mainfrom
claude/reversediff-growth

Conversation

@seabbs-bot

@seabbs-bot seabbs-bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1003.

What changes

  • growth builds the Jacobian of its root in _eigen_jacobian block by block in an array like M (similar(M, T, S + 1, S + 1)), in place of vcat(hcat(...), hcat(...)).
    Under ReverseDiff the blocks are a Matrix{TrackedReal} and a TrackedArray, and ReverseDiff's hcat of that pair is ambiguous once SparseArrays is loaded (ReverseDiff.jl: faults found #1007), so the two growth-of-mixing-strata scenarios failed on both ReverseDiff backends.
  • Nothing is stripped for ReverseDiff's tracked numbers: the root search and the inverse are recorded on the tape.
    The first version of this PR added a ReverseDiff extension that took their values, as _primal_value does for dual numbers; a compiled tape then held the root and the Jacobian as constants and gave a wrong gradient at any other point (see the comment below), so it was reverted.
  • The compiled-tape check (test/ad/checks.jl) also records each registry scenario at its parameters and replays it at a point about 1% away (0.01 where an entry is zero), against the ForwardDiff gradient there.
    The existing check records at zero and replays at the parameters, which gives no second point for the scenarios whose parameters are zero.

Checks run

Julia 1.13.4, ReverseDiff 1.18.11.

  • The two scenarios, against ForwardDiff, called directly: ReverseDiff tape 3.7e-16 and 2.7e-16 at θ and θ + 0.05; ReverseDiff compiled, recorded at θ, 3.7e-16 at θ and 2.7e-16 at θ + 0.05.
  • test/ad item ReverseDiff (compiled) gradients with the replay: 3120 passed, 13 failed. Every scenario passes the replay at a second point.
    The 13 failures are one scenario, "Growth rate and profile of strata that mix": the kit's check records the compiled tape at the zero vector, where the coupling matrix is zero and no growth rate exists, so the tape records the branch that returns NaN and replays it at the parameters (12 failures in the kit's correctness check and 1 in the entry-by-entry check).
  • Runic on the changed files: no change.

Not run here

  • The ForwardDiff, Mooncake, Enzyme and Reactant growth rows before and after, in value and time: in progress.
  • The ReverseDiff (tape) item after the last change.

https://claude.ai/code/session_01WwC7ixGWZNbRbSXXsSPbAs


Generated by Claude Code

…s a quantity fixed

growth found its root and Jacobian on tracked numbers under ReverseDiff,
as _primal_value had no method for them, and ReverseDiff's hcat of a
Matrix{TrackedReal} with a TrackedArray is ambiguous with SparseArrays
loaded. A ReverseDiff extension gives _primal_value and _fixed their
values, as for ForwardDiff's dual numbers. Fixes #1003.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixGWZNbRbSXXsSPbAs
@seabbs-bot
seabbs-bot requested a review from seabbs as a code owner December 2, 2026 14:36
seabbs-review-bot[bot]

This comment was marked as resolved.

@seabbs-review-bot seabbs-review-bot Bot added the llm-reviewed Reviewed by seabbs-review-bot label Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author

For the owner, two questions from the coordinating session, measured on Julia 1.13.4 with ReverseDiff 1.18.11.

(a) No stripping, with the Jacobian in _eigen_jacobian built entry by entry instead of with hcat and vcat.
The gradient is right on both ReverseDiff backends, and it stays right when a compiled tape is replayed at another point; this PR's extension does not.
With the extension, the stripped root and Jacobian are constants on a compiled tape, so a tape recorded at θ and replayed at θ + 0.05 gives a wrong gradient, including for the one-series growth scenario, which is right on main today.
The cost of not stripping is about 1.6 times the time on the mixing-strata scenario, since the root search and the inverse are recorded too.

Scenario Backend Point Extension (this PR) No stripping, no hcat
Growth rate and profile of strata that mix tape θ 2.7e-16, 1364 µs 3.7e-16, 2236 µs
tape θ + 0.05 1.8e-16 2.7e-16
compiled θ 2.7e-16, 43.6 µs 3.7e-16, 70.8 µs
compiled θ + 0.05 0.54 2.7e-16
Growth rate from a reproduction number compiled θ 9.9e-17, 2.7 µs 2e-16, 4.0 µs
compiled θ + 0.05 0.031 1.6e-16

Each cell is the largest difference from the ForwardDiff gradient at that point over the largest entry of it, then the minimum time; the compiled tape is recorded at θ in both columns.
The registry checks a compiled tape only at the point it was recorded, so the tests pass with the extension.

(b) The docs page.
The extensions-table row alone would do: the page only restates the row and has no example the docs build runs.
With the formulation in (a) there is no extension, so neither the row nor the page is needed.


Generated by Claude Code

…age holds a quantity fixed"

This reverts commit 4428bc2.
…piled tapes at a second point

The Jacobian was joined with hcat and vcat, and ReverseDiff's hcat of
a Matrix{TrackedReal} with a TrackedArray is ambiguous with SparseArrays
loaded. It is now filled block by block in an array like M, which takes
tracked numbers, and nothing is stripped for them: a compiled tape that
takes the root as a constant gives a wrong gradient at another point.
The compiled-tape check replays each registry scenario at a point about
1% from the one it was recorded at. Fixes #1003.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixGWZNbRbSXXsSPbAs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixGWZNbRbSXXsSPbAs
@seabbs-bot seabbs-bot changed the title ReverseDiff: take a tracked number's value where the package holds a quantity fixed Build the growth Jacobian without hcat, so ReverseDiff can take it, and replay compiled tapes at a second point Dec 2, 2026
The coupling of "Growth rate and profile of strata that mix" is `K0`
scaled entry by entry by the exponential of its parameters, so it mixes
the strata at the zero vector too.

A tape recorded near the point it replays at runs the same steps of a
loop, so the second-point replay check is `@test` for every scenario and
no longer reads the broken list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixGWZNbRbSXXsSPbAs

Copy link
Copy Markdown
Collaborator Author

Merged through #1022, which carries this branch with main merged in and the compiled-tape entry.


Generated by Claude Code

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Try this Pull Request!

Option 1: Julia Package Manager

Open Julia and type:

import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableRecurrences.jl", rev="claude/reversediff-growth")
using ComposableRecurrences

Option 2: Local Checkout

If you have the repo locally:

git checkout claude/reversediff-growth
julia --project=. -e "using Pkg; Pkg.instantiate()"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-reviewed Reviewed by seabbs-review-bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReverseDiff: growth of a stratified recurrence stops at an ambiguous hcat

2 participants