Skip to content

No silent plain AD: the rule takes ranges and lazy wrappers, and names what it does not - #955

Merged
seabbs-bot merged 13 commits into
mainfrom
claude/visible-plain-ad
Dec 2, 2026
Merged

seabbs-bot merged 13 commits into
mainfrom
claude/visible-plain-ad

Conversation

@seabbs-bot

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

Copy link
Copy Markdown
Collaborator

Follow-up to #952: no call takes plain AD silently, the cause of the trace gap, two design proposals, and the runs #952 left out.

1. Silent routes to plain AD

The rule's gate is a test on types, and a call that failed it took plain AD with no message.
I listed every way a call can fail it and decided each on its merits.

What stops the gate Before Now
A range (StepRangeLen, LinRange) as a kernel, a modifier's parameter, an input, a history or a Diagonal coupling's diagonal plain AD, silent the rule
A view, reshape, transpose or adjoint of a range plain AD, silent the rule
A PermutedDimsArray, Symmetric or Hermitian of an Array plain AD, silent the rule
Any other array type: a static array, a Fill, a user's array type, a symmetric wrapper of a sparse matrix plain AD, silent plain AD, named
A field of abstract type, or a struct whose field type is fixed to a lazy type plain AD, silent plain AD, named
Device arrays with a part that has no rule on a device (a user's coupling or whole-step modifier, a change-point kernel) plain AD, silent plain AD, named
A part with no pullback! plain AD, named at construction unchanged
Dual numbers, BigFloat, any real number that is not an IEEE float plain AD by design unchanged, no message
NoAdjoint plain AD, asked for unchanged, no message

How the rule takes a range.
The rule writes cotangents into arrays, and a backend's tangent of a range is not an array.
So a call with a range or one of the wrappers above takes a third route, :dense: each such leaf is copied to an Array outside the rule, and the rule runs on the copy.
The backend differentiates the copy, so a range built from parameters keeps their gradients (tested: range(θ[1], θ[2]; length = n) as kernel, parameter, input, history and coupling, on Mooncake and Enzyme reverse, against ForwardDiff).
The operator is rebuilt with the new field types from its type parameters, without its constructors.
A struct that cannot be rebuilt that way is left as it is and named.
The route is decided from the types, so a call that already took the rule compiles to what it did before.

Only Base and LinearAlgebra types whose values collect gives are copied.
Any other array type is not: it may be a device or traced array, or sparse, where a copy would be wrong or dense.
StaticArrays and FillArrays are in no test environment, so SVector and Fill are covered by the rule for unknown types, tested with a stand-in.

What the copy costs, on the model of the parity table (5 strata, 20 lags, 200 steps, Mooncake reverse, one process each, μs):

Model Forward, main Forward, here Gradient, main Gradient, here
all Vector 18.7 18.9 62.3 62.1
kernel a range 30.2 19.2 524.4 63.2
parameter a range 20.1 19.2 459.6 63.0
input a PermutedDimsArray 19.1 20.6 420.5 87.7

A forward call with no AD pays the copy too: 8% for an input of 1,000 entries behind a PermutedDimsArray.
A range kernel is faster copied than read in place.

How the rest is named.
The plain route already calls a no-op hook that Mooncake and Enzyme replace.
In reverse mode the hook now logs the existing one-time @info, with the field path or argument position and its type:

Recurrence gradients use plain AD of the whole operator: Recurrence.kernel is a
MyVec{Float64}, which the rule does not take: pass an Array

It is logged from the backend's rule, so only when a rule was there to take: a forward call, forward mode and ForwardDiff log nothing.
The hook is skipped, as before, for a call that holds a number type no rule runs, so dual numbers pay nothing.
After the first call of a signature it costs one lookup under a lock, on a route that is already plain AD.

2. The trace gap

I had the cause wrong.
#952 said the gap was the generic trace pullback evaluating step again.
I made the change asked for: the pullback now reads the step's value from the next modifier's record or the output, and its state from the next step's record or the final state, which the traced record carries.
Calls that record no trace build no traced record, so they read nothing more.
It works on the host and on a device, and it did not move the time: 79.0 to 81.0 μs on main against 80.1 to 81.2 μs here, in alternating fresh processes, against 72 to 74 μs for the built-in.

The cause, measured.
I swapped one part of the user's copy at a time, in one process, eight rounds:

Trace pullback of the user's copy Gradient through the trace (μs)
built-in Depletion, for reference 73.6
generic, as on main 81.2
generic, parameters left out 79.2
closed form, as the built-in's 75.1

So about 6 μs of the 7.6 is the dual-number pass over trace, 4 for the four step values and 2 for the parameters.
trace here is (s′, v′, v - v′): every partial is a constant 0, 1 or -1.
The pass sums cotangent times partial, and the compiler cannot drop a product with a constant zero, because the cotangent may be NaN.

A fix I made and then dropped.
In the contraction I made a zero partial add nothing, whatever its cotangent, so the compiler could drop the terms it knows are zero.
It brought the ratio from 1.10 to 1.03 to 1.04, and benchmark/ab.jl found no cell outside noise over 36 cells.
It also changed a result: zero times a NaN or infinite cotangent became zero.
The test was on the partial's value, as dual numbers cannot tell a partial that is zero by structure from one that is zero at the point.
So I compared the routes on a step with two inputs (x, θ) and two outputs, with a non-finite cotangent on the first output.
Each cell is the cotangent of (x, θ).

Case Dual pass, IEEE (main) Dual pass, zero skipped Hand pullback Mooncake reverse Enzyme reverse ForwardDiff
A. (2x, 3θ), cotangent (Inf, 1): the first output does not read θ (Inf, NaN) (Inf, 3) (Inf, 3) (Inf, 3) (Inf, 3) (Inf, NaN)
B. (max(x, 0), θ) at x = -1, cotangent (Inf, 1) (NaN, NaN) (0, 1) (0, 1) (NaN, 1) (0, 1) (NaN, NaN)
C. (x^2, θ) at x = 0, cotangent (Inf, 1) (NaN, NaN) (0, 1) (NaN, 1) (NaN, 1) (NaN, 1) (NaN, NaN)
C. (θ x, θ) at x = 0, cotangent (NaN, 1) (NaN, NaN) (NaN, 1) (NaN, NaN) (NaN, NaN) (NaN, NaN) (NaN, NaN)
D. (x (x/θ)^0.5, θ) at x = 0, the empty pool, cotangent (1, 1) (NaN, NaN) (NaN, NaN) (0, 1) (0, 1) (NaN, NaN) (NaN, NaN)

What the table says:

  • The dual pass on main gives what ForwardDiff gives in every row. That is its contract: the local derivative is the step on dual numbers, and it never drops a non-finite cotangent.
  • Skipping zeros matched the hand pullback and the reverse backends in A and B, and matched no route in C. In the last C row it returned a clean cotangent for θ where every other route returns NaN. So it could hide a NaN that marks a real fault upstream, which the IEEE pass cannot.
  • It did nothing for the documented case, D. There the NaN is made inside the dual arithmetic (zero times an infinite slope), before the contraction, so Hazard's hand step_pullback is needed either way and returns what it did.
  • The routes already disagree with each other on where a NaN lands (B and D split Mooncake from Enzyme). That holds on main and is not new here; parity of the user's copy with the built-in is claimed for finite cotangents, and the page does not claim more.

A few percent does not justify a result that no other route gives, so the contraction is as it was on main, and a test now pins the IEEE behaviour.
The commits are in the branch's history (ba759a1, undone in 69ba041) if the owner wants to look.

Where that leaves the gap.
It stays at about 1.10.
Closing it without changing a result takes a trace_pullback beside step_pullback, with which the user's copy times as the closed-form row above.
That is a design addition and I have not made it.
I also tried skipping a zero parameter cotangent before the store; in two alternating pairs of runs it was no faster, so it is not here either.

I kept the record change: it removes one evaluation of step per stratum and step from a traced reverse pass, which a costly step would pay for, though this step does not show it.

3. Two design questions for the owner

Nothing below is implemented.

(a) θ does not reach nested parts

What happens now.
θ is the modifier's fields, each parameter read at the stratum and time.
The read goes into tuples and named tuples and stops at any other struct, which reaches step as it is.
The local derivative seeds the float leaves of θ, so a float inside a nested struct is out of its reach, and a modifier that holds one takes plain AD ("holds floats in a field that is not a parameter").
Depletion is a tuple of draws and flows, each a struct with its own form, population, weights or demand.
So it cannot be a pure step: it has private _step, pullback and trace methods, and a form gets its parameters through a private call that appends N to the form's fields.

Proposal.
Let a type declare that it is a part: a struct whose fields are read as a modifier's are.
θ then holds each part as a value of the same type with its parameters read, so step can still dispatch on the part's type (a form).
The cotangent side follows the same shape: the mirror of a part is already the named tuple of its fields' mirrors.

What it would take.

  • One declaration, say ispart(::Type{<:MyDraw}) = true, false by default.
    Without it a nested struct would change meaning for existing user types, which is why it is not automatic.
  • _read_param and _add_leaf! in src/interface/canonical.jl gain a method for a part, rebuilt without its constructor as the rule's leaves are (_rebuild_leaves).
    _rule_field and the seeding in src/interface/duals.jl (_flat, _unflat, _shaped) walk into a part.
    About 100 to 150 lines, with tests, and the device kernels need no change as the read stays per stratum.
  • The step check (test_step) and the developer pages gain the nested case.
  • Rewriting Depletion over it is a second, larger change: src/depletion/pool.jl and flows.jl are about 1,700 lines of private methods.
    The parity table is the guard: the one-pool hazard copy already matches the built-in through the public route.

What it would cost.

  • A wider θ for the compiler: a Depletion with three pools and four flows would read about a dozen parts per stratum and step.
    The risk is inference depth and compile time, not run time; it needs measuring on the largest registry scenario before the rewrite is committed to.
  • It does not by itself give a user's form the things the audit lists as private to the built-in forms: draws by pool name, weights, into, overflow, a form inside a Flow, and a state record that is skipped when nothing reads it.
    Those are properties of Depletion's pool layout, and would become public only when Depletion itself is written through the route.
  • One more public name to keep.

Recommendation.
Do the declaration and the nested read first, on its own, with a user type of two nested parts as the test.
Decide on the Depletion rewrite after its compile time is measured.

(b) A user's coupling or whole-step modifier has no route to the device rule or the fused reverse

What happens now.
A recurrence takes the rule on device arrays when _device_ready holds: its kernel, coupling and every modifier must each have a reverse step that runs on the device.
A pure step always has one, as the package runs it inside its own kernels.
A coupling has one only if it is I, a Diagonal, or a matrix on the device.
A whole-step modifier has one only if it is built in.
A user's forward! and pullback! are assumed to index their arrays one entry at a time, which a device array does not allow, so the whole recurrence takes plain AD.
With this PR that call is named in the notice ("X has no rule on device arrays").
The fused reverse is narrower still: it is one kernel per step that does a dense matrix coupling's transposed product and the delay kernel's reverse together, and it exists for a device matrix and a device sparse matrix only.

Proposal, in two steps.

  1. A declaration that a type's methods run on device arrays.
    Say ondevice(::MyCoupling) = true: a promise that forward! and pullback! use whole-array operations (broadcasts, products, reductions) and no scalar indexing.
    _device_coupling and _device_modifier then return it for a user's type.
    The rule calls the user's methods once per step on device vectors, as it calls the built-in Redistribute.
  2. A linear coupling as its matrix on the device.
    A coupling declared islinear already runs as the matrix its forward! gives, built once per call.
    On a device that matrix can be built on the host and moved, and it is then a device matrix: it takes the device rule and the fused reverse with no further code.

What it would take.

  • Step 1 is the two methods and the declaration, with tests on JLArray.
    I tried it with the two internal methods in place of the declaration: a broadcast-only whole-step modifier and a broadcast-only nonlinear coupling each took the rule on JLArray with scalar indexing off, and their outputs and cotangents matched the host's.
    So the reverse pass needs no change for step 1.
  • Step 2 is the call-time build (it exists on the host) plus one adapt to the input's array type.
  • A page in docs/src/developer/ with a tested example of each, and the Adapt.adapt_structure method the user already writes for a pure step.

What it would cost.

  • Step 1 reaches the device rule and not the fused reverse.
    Each step makes one kernel launch per array operation in the user's method, where a fused step makes one.
    That is the cost the built-in whole-step modifiers already pay, and it is far below plain AD of the whole recurrence on a device.
  • A wrong promise fails loudly where scalar indexing is disallowed, and silently slowly where it is allowed.
    The check function for a new type should run it with scalar indexing off.
  • Step 2 holds an S × S dense matrix on the device and builds it once per call.
    That is as the host does now, and it loses any sparsity the user's forward! had.
  • A nonlinear user coupling cannot reach the fused reverse without a per-stratum protocol for couplings, a counterpart of the pure step for a function that reads every stratum.
    I would not design that until a model needs it: the fused path is worth most for dense matrices, which are linear.

Recommendation.
Step 1 and step 2 together are small and cover what the audit found.
The per-stratum coupling protocol is the open design question and can wait.

4. Runs

The branch is main merged in at 9f60eb1; it has no conflicts.
All runs are local, on Julia 1.13.4.

On 9f60eb1:

  • Unit (skip_quality): 41,452 passed, 1 broken (already marked), 0 failed.
  • Quality (quality_only): 33 items passed.
  • Enzyme reverse, slice 2 of 4, with "NoAdjoint Recurrence capacity, beds with overflow" held out by a command-line filter: 61 passed and 2 broken (already marked: "Recurrence modifier chosen at run time" and its twin), 0 failed; each gradient entry by entry, and the rule check.
    The held-out scenario hangs or segfaults in LLVM while Enzyme compiles it, on main as well; it is entry 10 of Enzyme.jl: upstream reports #931, and Carry one unit in registers and hold short-kernel caches strata × time #1008 makes it compile again.

On the earlier merge eabe769 (main as of #1005), by test/ad tag:

Not run: the AD tags on 9f60eb1 (only slice 2 above), Enzyme forward on the full registry, the Reactant suite (this PR changes none of its rows), and a local docs build.

https://claude.ai/code/session_01WwC7ivYt11uRBnyJ1NF4ow


Generated by Claude Code

A call whose kernel, coupling, parameter, input or history is a range, or a
PermutedDimsArray, Symmetric or Hermitian of an Array, took plain AD with no
message. Such a call now takes a third route that copies those leaves to
Arrays outside the rule, so the backend differentiates the copy.

A call the gate still stops (another array type, a field of abstract type, a
part with no rule on device arrays) is named by the one-time notice, logged
from the plain-AD hook that reverse-mode backends replace. Calls on number
types no rule runs skip the hook as before.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixMLmTv9ZNyQj17Pp8
The pullback of a user's trace evaluated step once more at each stratum and
time to get the step's outputs. The reverse pass already holds them: the
value is the next modifier's record or the output, and the state is the next
step's record or the final state, which the traced record now carries. Calls
that record no trace build no traced record and read nothing more.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixMLmTv9ZNyQj17Pp8
The local derivative of a step or trace is a sum of cotangents times
partials. A partial the compiler knows to be zero still cost a product, as
the cotangent may be NaN. A zero partial now adds nothing, as in a pullback
written by hand, so those terms fold away.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixMLmTv9ZNyQj17Pp8
@seabbs-bot
seabbs-bot requested a review from seabbs as a code owner December 2, 2026 08:26
Skipping zero partials made a zero partial times a NaN or infinite cotangent
zero. Where the partial is zero by value, as for x^2 at zero, every other
route gives NaN there, so the change could hide a NaN that a hand pullback,
Mooncake, Enzyme and ForwardDiff all show. The contraction is as it was: the
local derivative is the step on dual numbers and follows forward mode.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixMLmTv9ZNyQj17Pp8
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

On the review's point about _contract: the zero-partial rule is no longer in this PR. It could hide a NaN that every other route shows, so the contraction is as on main and a test pins that; the description has the comparison.


Generated by Claude Code

@github-actions

github-actions Bot commented Dec 2, 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/visible-plain-ad")
using ComposableRecurrences

Option 2: Local Checkout

If you have the repo locally:

git checkout claude/visible-plain-ad
julia --project=. -e "using Pkg; Pkg.instantiate()"

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

Copy link
Copy Markdown
Collaborator Author

downgrade-compat is red here and it is not this PR's: the same job fails the same way on main at cc74265 (job 143283281872). In the downgraded environment on Julia 1.13, REPLExt cannot load Markdown, so GPUCompiler, AllocCheck, DifferentiationInterfaceTest and then ADFixtures fail to precompile, and the registry test items error before they run. No test of this PR's code fails in that job (31,146, 2,831 and 4,348 passed on its three workers). The fix belongs to the downgrade job's environment on main; I have not changed it here.


Generated by Claude Code

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Benchmark comparison vs base

Minimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:

Group 🟢 <50% 🟢 50–75% 🟢 75–95% ⚪ 95–105% 🔴 105–125% 🔴 125–150% 🔴 >150%
Evaluation 1 5 17 · · · 1
ForwardDiff · · 8 3 5 5 ·
Mooncake reverse · · 20 1 · · ·
Enzyme reverse · · 18 3 · · ·
Evaluation — 24 benchmarks (by time change)
Benchmark base PR time memory
Evaluation / Matrix tv_piecewise_secondary_k5 T200_L20_S1 1.51 μs 2.74 μs 🔴 1.81× ⚪ 1.0×
Evaluation / Matrix conv_fixed T200_L20_S1 2.12 μs 550.0 ns 🟢 0.26× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 4.3 μs 3.04 μs 🟢 0.71× ⚪ 1.0×
Evaluation / Matrix strata_mixing T200_L20_S5 30.21 μs 22.04 μs 🟢 0.73× ⚪ 1.0×
Evaluation / Matrix renewal T200_L20_S1 3.57 μs 2.6 μs 🟢 0.73× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 6.31 μs 4.62 μs 🟢 0.73× ⚪ 1.0×
Evaluation / Matrix bvd_patch T200_L20_S5 43.03 μs 31.69 μs 🟢 0.74× ⚪ 1.0×
Evaluation / Matrix bvd_patches_call T200_L20_S5 56.81 μs 43.36 μs 🟢 0.76× ⚪ 1.0×
Evaluation / Matrix overview T200_L20_S3 18.15 μs 14.04 μs 🟢 0.77× ⚪ 1.0×
Evaluation / Matrix bvd_occupancy_kernel T200_L20_S1 7.56 μs 5.88 μs 🟢 0.78× ⚪ 1.0×
Evaluation / Matrix random_walk T30_L1_S1 90.0 ns 70.0 ns 🟢 0.78× ⚪ 1.0×
Evaluation / Matrix custom_modifier T200_L20_S1 4.2 μs 3.28 μs 🟢 0.78× ⚪ 1.0×
Evaluation / Matrix bvd_census T200_L20_S1 2.5 μs 1.96 μs 🟢 0.78× ⚪ 1.0×
Evaluation / Matrix custom_modifier_pullback T200_L20_S1 4.17 μs 3.33 μs 🟢 0.8× ⚪ 1.0×
Evaluation / Matrix delay_fixed T200_L20_S1 731.0 ns 590.0 ns 🟢 0.81× ⚪ 1.0×
Evaluation / Matrix renewal_depletion T200_L20_S1 8.41 μs 6.87 μs 🟢 0.82× ⚪ 1.0×
Evaluation / Matrix bvd_renewal_call T200_L20_S1 8.72 μs 7.15 μs 🟢 0.82× ⚪ 1.0×
Evaluation / Matrix user_depletion T200_L20_S1 8.4 μs 6.93 μs 🟢 0.83× ⚪ 1.0×
Evaluation / Matrix user_depletion_pullback T200_L20_S1 8.32 μs 6.9 μs 🟢 0.83× ⚪ 1.0×
Evaluation / Matrix bvd_patches_call T200_L20_S1 18.61 μs 15.45 μs 🟢 0.83× ⚪ 1.0×
Convolution body / native axpy per lag 491.2 ns 408.1 ns 🟢 0.83× —
Convolution body / BLAS axpy per lag 777.7 ns 694.4 ns 🟢 0.89× —
Convolution body / package 334.8 ns 310.4 ns 🟢 0.93× —
Evaluation / Matrix tv_piecewise_primary_k5 T200_L20_S1 2.09 μs 1.97 μs 🟢 0.94× ⚪ 1.0×
AD gradients — 63 benchmarks (by time change)
Benchmark base PR time memory
AD gradients / Matrix renewal T200_L20_S1 / ForwardDiff 356.22 μs 532.95 μs 🔴 1.5× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / ForwardDiff 595.34 μs 842.93 μs 🔴 1.42× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / ForwardDiff 1.62 μs 2.23 μs 🔴 1.38× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / ForwardDiff 332.3 μs 432.15 μs 🔴 1.3× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / ForwardDiff 1.48 ms 1.88 ms 🔴 1.26× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / ForwardDiff 272.46 μs 330.72 μs 🔴 1.21× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / Enzyme reverse 141.11 μs 111.26 μs 🟢 0.79× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / Mooncake reverse 15.58 μs 12.31 μs 🟢 0.79× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / Mooncake reverse 132.18 μs 105.1 μs 🟢 0.8× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / Mooncake reverse 155.44 μs 123.67 μs 🟢 0.8× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / Enzyme reverse 49.64 μs 39.75 μs 🟢 0.8× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / Mooncake reverse 195.42 μs 157.2 μs 🟢 0.8× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / Enzyme reverse 103.69 μs 84.36 μs 🟢 0.81× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / Mooncake reverse 68.43 μs 55.76 μs 🟢 0.81× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / Enzyme reverse 17.57 μs 14.33 μs 🟢 0.82× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / Mooncake reverse 25.35 μs 20.7 μs 🟢 0.82× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / Enzyme reverse 220.0 ns 180.0 ns 🟢 0.82× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / Mooncake reverse 1.11 μs 911.0 ns 🟢 0.82× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / Enzyme reverse 82.73 μs 67.81 μs 🟢 0.82× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / Mooncake reverse 21.7 μs 17.82 μs 🟢 0.82× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / Enzyme reverse 17.62 μs 14.48 μs 🟢 0.82× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / Mooncake reverse 23.14 μs 19.13 μs 🟢 0.83× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / Enzyme reverse 10.74 μs 8.9 μs 🟢 0.83× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / Enzyme reverse 8.46 μs 7.02 μs 🟢 0.83× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / Enzyme reverse 12.81 μs 10.65 μs 🟢 0.83× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / Mooncake reverse 26.68 μs 22.24 μs 🟢 0.83× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / ForwardDiff 504.82 μs 588.3 μs 🔴 1.17× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / Enzyme reverse 9.39 μs 7.84 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / Mooncake reverse 24.99 μs 20.88 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / Mooncake reverse 20.69 μs 17.32 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / Mooncake reverse 15.31 μs 12.82 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / Mooncake reverse 28.14 μs 23.64 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / Mooncake reverse 71.27 μs 59.88 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / Enzyme reverse 19.42 μs 16.35 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / Enzyme reverse 18.96 μs 16.0 μs 🟢 0.84× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / Enzyme reverse 47.32 μs 40.37 μs 🟢 0.85× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / Enzyme reverse 16.9 μs 14.53 μs 🟢 0.86× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / Mooncake reverse 16.55 μs 14.36 μs 🟢 0.87× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / ForwardDiff 524.47 μs 593.25 μs 🔴 1.13× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / ForwardDiff 5.52 ms 6.2 ms 🔴 1.12× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / Enzyme reverse 9.16 μs 8.11 μs 🟢 0.89× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / Mooncake reverse 10.6 μs 9.39 μs 🟢 0.89× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / Mooncake reverse 15.28 μs 13.82 μs 🟢 0.9× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / Mooncake reverse 26.37 μs 23.93 μs 🟢 0.91× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / Enzyme reverse 15.9 μs 14.43 μs 🟢 0.91× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / Mooncake reverse 11.4 μs 10.44 μs 🟢 0.92× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / ForwardDiff 511.53 μs 472.72 μs 🟢 0.92× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / ForwardDiff 509.22 μs 472.0 μs 🟢 0.93× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / ForwardDiff 475.76 μs 441.52 μs 🟢 0.93× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / ForwardDiff 3.26 ms 3.04 ms 🟢 0.93× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / Enzyme reverse 32.21 μs 30.05 μs 🟢 0.93× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / ForwardDiff 11.07 ms 11.8 ms 🔴 1.07× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / ForwardDiff 480.52 μs 449.2 μs 🟢 0.93× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / ForwardDiff 49.57 ms 46.42 ms 🟢 0.94× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / ForwardDiff 603.64 μs 567.46 μs 🟢 0.94× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / ForwardDiff 1.63 ms 1.53 ms 🟢 0.94× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / Mooncake reverse 32.3 μs 30.79 μs ⚪ 0.95× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / ForwardDiff 602.97 μs 629.55 μs ⚪ 1.04× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / ForwardDiff 512.22 μs 532.5 μs ⚪ 1.04× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / ForwardDiff 13.78 ms 14.28 ms ⚪ 1.04× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / Enzyme reverse 7.84 μs 7.62 μs ⚪ 0.97× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / Enzyme reverse 2.68 μs 2.74 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / Enzyme reverse 2.4 μs 2.36 μs ⚪ 0.98× ⚪ 1.0×

Copy link
Copy Markdown
Collaborator Author

Documenter / Documentation is red here and it is not this PR's: the same 17 errors are on main at cc74265 (job 143283280831), all in docs/src/getting-started/three-towns-forward.md, which this PR does not touch. Its @setup forward block calls pkgdir(ComposableRecurrences) before any using ComposableRecurrences in that block's module, so the include fails and every later block of the page fails with it. A likely fix is using ComposableRecurrences as the first line of that @setup block; I have not made it here, as I have not found why the page built before. No other page reports an error in this job, so the pages this PR changes (explanation/adjoints-and-backends.md, developer/extending-performance.md) built.


Generated by Claude Code

The registry scenario had no rows in the results file, so the registry
check of the smoke and reactant jobs failed. Both modes work.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7ixMLmTv9ZNyQj17Pp8
Its pool was the population less the seed, negative at the zero vector where
Mooncake prepares its rule, so the loss took the log of a negative number.
The pool is now the parameter and the population is the pool plus the seed.

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

Copy link
Copy Markdown
Collaborator Author

Hand-over note

This session stops here. The branch claude/visible-plain-ad is pushed at 42b1532. It is not ready to merge: it conflicts with main, and two AD tags fail on a fixture that the last commit changes but that has not been re-run.

What each commit does

Commit What it does
585d550 Item 1. A third route, :dense, in src/adjoints/adjoints.jl: a range, or a PermutedDimsArray, Symmetric or Hermitian of an Array, is copied to an Array outside the rule, so the rule takes it and the backend differentiates the copy. Any other call the gate stops is named by the one-time @info, logged from _plain_hook in Mooncake and Enzyme reverse (_note_stopped). Explanation page updated.
0684fc1 Item 2. The generic trace pullback reads a step's outputs from the records (_state_after, field F of the traced record) and no longer runs step again. No cost to untraced calls. It did not change the time.
ba759a1 Skipped zero partials in the dual pass's contraction. Undone in 69ba041.
680f026 A test for ba759a1 (replaced in 69ba041) and a correction to the performance page's sentence on the trace row.
69ba041 Restores the IEEE contraction and pins it with a test in test/unit/pure_step.jl.
7e6061d Review fix: a lazy array the rule would copy is not named as what stops a call; comment wording.
7e230a7 benchmark/results/parity.csv regenerated at 7e6061d: 15 of 15 rows agree; gradient through the trace 1.06.
695a6fc, 47038d3 Reactant rows and report for the registry scenario "Recurrence user copy of the hazard depletion". main has since added its own rows, so on merge take main's test/reactant/results.tsv and RESULTS.md.
42b1532 The same scenario made valid at every parameter value (see "What the AD runs owe"). Not verified.

What was verified

All on Julia 1.13.4, on the tree at 7e230a7, before main moved:

Run Result
Unit suite, skip_quality 41,543 passed, 0 failed, 1 broken (already marked)
Quality suite passed, exit 0
New AD item "Lazy host arrays take the rule; other containers are named once", Mooncake and Enzyme reverse 71 of 71
Mooncake reverse tag 8,902 passed, 1 failed, 4 errored: all five are the one scenario below
Mooncake forward tag 3,547 passed, 2 errored: the same scenario
Reactant registry check with the scenario's rows agrees
benchmark/ab.jl on the contraction change (since undone) no cell outside noise

What the AD runs still owe

  • Mooncake reverse and forward fail on "Recurrence user copy of the hazard depletion" and its NoAdjoint twin. The fault is the fixture, added in Parity of a user's extension with a built-in: audit, fixes in shared code, tests and a table #952: its pool is the population less the seed, which is negative at the zero vector, where Mooncake prepares its rule, so log.(y) throws a DomainError. 42b1532 takes the pool as the parameter and builds the population as pool plus seed. That commit has not been run on any backend.
  • main has moved the registry to test/ADFixtures/src/registry.jl and still has the negative pool there (line 1657, pool = N .- vec(sum(seed; dims = 2))), so the fix has to be made in that file when merging.
  • The scenario's Reactant rows were recorded with the old definition, on this branch and on main. They need a re-run once the fixture changes.
  • Not run at all: Enzyme forward (started, not finished), Enzyme reverse, and the docs build locally. In CI at 7e230a7 the docs job reported errors only in three-towns-forward.md, which main has since fixed (Load the package in the three-towns page's setup block #956), so the two pages this PR changes built there.
  • After the merge with main, every run above is owed again on the merged tree.

Conflicts with main

main is 20 commits ahead of the branch point. git merge-tree reports three conflicts:

  • test/ADFixtures/src/ADFixtures.jl: main moved the scenarios to registry.jl. Take main's file and port the _hazard_copy change from 42b1532 into registry.jl.
  • test/reactant/results.tsv and test/reactant/RESULTS.md: take main's, then re-run the one scenario.

CI at the last pushed head

  • downgrade-compat is red here and on main for the same reason (REPLExt cannot load Markdown in the downgraded environment); see the earlier comment.
  • smoke was red because the scenario had no Reactant rows; 695a6fc added them.
  • The benchmark comment shows ForwardDiff rows 20 to 50% slower. Locally, in alternating fresh processes, a renewal's ForwardDiff gradient is 157.4 and 157.9 μs on main against 157.0 and 157.7 μs here, so I read those rows as differences between the two runners.

Commands

export PATH=$HOME/.juliaup/bin:$PATH
# unit, then quality
CR_TEST_WORKERS=3 julia --project=. -e 'using Pkg; Pkg.test(test_args=["skip_quality"])'
julia --project=. -e 'using Pkg; Pkg.test(test_args=["quality_only"])'
# one AD tag: mooncake_reverse, mooncake_forward, enzyme_forward, enzyme_reverse
julia --heap-size-hint=6G --project=test/ad test/ad/runtests.jl mooncake_reverse
# one Reactant scenario, Julia 1.12, then the registry check
REACTANT_SUITES=registry REACTANT_BACKENDS=cpu \
  REACTANT_SCENARIOS="Recurrence user copy of the hazard depletion" \
  julia +1.12 --project=test/reactant --startup-file=no test/reactant/runtests.jl
julia +1.12 --project=test/reactant --startup-file=no test/reactant/registry.jl test/reactant/results.tsv
# parity table and formatter
julia --project=benchmark --threads=4 benchmark/parity.jl
julia --project=test/formatter -e 'using Runic; Runic.main(["-i","src","test","ext","benchmark"])'

Enzyme reverse ran out of memory in one process on a 15 GB machine; run its four registry slices and the rest as separate processes. Mooncake reverse took about 100 minutes.

Open design questions for the owner

Each is written up in the description; nothing is implemented.

  1. Nested parts and θ. θ holds a modifier's fields with each parameter read, and stops at a nested struct. So a type built of parts with their own parameters, as Depletion is, cannot be a pure step and takes private methods. Proposal: a declaration that a struct is a part, read as a modifier's fields are, about 100 to 150 lines. Rewriting Depletion over it is a second, larger change, to decide after measuring compile time.
  2. A device route for a user's coupling or whole-step modifier. These send the whole recurrence to plain AD on a device, because their methods are assumed to index entry by entry. Proposal: a declaration that a type's forward! and pullback! use whole-array operations only, and running a coupling declared linear as its matrix on the device. A probe with the two internal methods showed the reverse pass needs no change. It reaches the device rule, not the fused reverse.
  3. An optional trace_pullback. A gradient through a user's trace costs about 1.06 to 1.10 of the built-in's. The measured cause is the dual-number pass over trace, not a second evaluation of step. A change to the contraction closed most of it but returned zero where every other route returns NaN, so it was dropped. A trace_pullback beside step_pullback would close the gap with no change of result: with a closed form the user's copy timed within 2% of the built-in.

One review thread is left open on purpose: the set of seen call signatures in _note_stopped, with my reply on it.


Generated by Claude Code

Take main's split of the AD fixtures and its Reactant results, and make
the user copy of the hazard depletion valid at the zero vector in
registry.jl.

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

# Conflicts:
#	docs/src/explanation/adjoints-and-backends.md
@seabbs-bot
seabbs-bot merged commit 03f2a7c into main Dec 2, 2026
1 of 27 checks passed
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.

2 participants