Skip to content

Run strata loops as kernels for a Reactant gradient of a kernel per stratum with a depletion - #1072

Open
seabbs-bot wants to merge 3 commits into
mainfrom
fix/reactant-capacity-reverse-error
Open

seabbs-bot wants to merge 3 commits into
mainfrom
fix/reactant-capacity-reverse-error

Conversation

@seabbs-bot

Copy link
Copy Markdown
Collaborator

Closes #983.
Under Reactant, the reverse gradient of the four capacity scenarios was wrong or did not compile.
The cause is not the capacity: it is a kernel per stratum (PerStratum) together with a Depletion modifier, when the strata loops are written out one block a stratum.
Run as kernels, those loops give the right gradient, so under Reactant's AD such a call now runs every strata loop as a kernel, and throws an error naming what it needs where a loop cannot run as one.
The four scenarios now give the right gradient.

Opus 5.5

What was wrong

Probes on Julia 1.12.7, Reactant 0.2.311, ReactantCore 0.1.28, comparing the compiled Enzyme.gradient(Reverse, …) with ForwardDiff on the parameters of the _capacity_drop scenario (3 strata):

Recurrence Reverse gradient under Reactant
PerStratum(GCAP), pool and beds (the scenario) wrong, entry relerr 1.7e+02
PerStratum(GCAP), the pool alone (a plain truncated draw, no pools) wrong
PerStratum(GCAP), the beds alone (Truncate(0.2; into = :O)) wrong
PerStratum(GCAP), a plain Hazard depletion wrong
PerStratum(GCAP), a Hazard depletion, coupling K0 CompilationError (post_op_transpose_reshape)
PerStratum(GCAP), a stateless Transform, a Recent cap or a Redistribute right
PerStratum(GCAP), no modifiers right
shared kernel G0 with no coupling, a Hazard depletion right
shared kernel G0 with coupling K0, the pool, the beds or a depletion right
PerStratum(GCAP), a Hazard depletion, with TRACED_MIN_STRATA => 1 right

So the trigger is a kernel per stratum with a Depletion modifier, not the capacity.
With 3 strata, below the default TRACED_MIN_STRATA of 32, the strata loops are written out one block a stratum, and that program gets the wrong derivative.
Run as kernels (TRACED_MIN_STRATA => 1), the same call gives the right one.

The change

ext/ComposableRecurrencesReactantExt.jl:

  • The traced _run checks Reactant.Enzyme.within_autodiff(), which Reactant sets while it traces a gradient and leaves false for a plain @compile (checked in the probes).
    When it is true and the recurrence has a PerStratum kernel and a Depletion modifier, the call runs with Traced(; min_strata = 1, kernels_only = true).
  • Traced gains a kernels_only field. With it set, a strata loop that would be written out throws an ArgumentError instead: without KernelAbstractions loaded it says to load KernelAbstractions and CUDA, and otherwise that the loop could not run as a kernel. The message names the PerStratum kernel and the Depletion modifier, and the alternatives: a kernel shared by the strata, or a gradient taken outside Reactant.
  • The primal traced call keeps the usual executor.
  • It also applies under Reactant forward-mode AD, as within_autodiff does not tell the modes apart.

Which calls it touches

A plain Float64 run of every registry scenario and every hand-written case in test/reactant/cases.jl, recording the predicate at the call, hit exactly the four capacity scenarios and no row that works today:

  • Recurrence capacity, dropped overflow after a truncated draw
  • Recurrence capacity, beds with overflow
  • Recurrence capacity, weekly budget with a queue
  • Recurrence trace of a weekly budget with a queue

Recurrence with a value for each stratum as its history stays listed: it has a coupling and no kernel per stratum, and still does not compile.

Tests and results

  • test/reactant/extension_check.jl gains two testsets: the compiled gradient of a PerStratum kernel with a depletion matches plain Enzyme, and the primal compiles; and, in a process without KernelAbstractions, the gradient throws an error naming PerStratum, Depletion and using KernelAbstractions, CUDA, while the primal compiles. Red before the change (the gradient did not compile), green after.
  • The four names leave REACTANT_BROKEN_REVERSE in test/ADFixtures/src/registry.jl.
  • Their reverse rows in test/reactant/results.tsv come from a rerun of probe.jl on the four scenarios with the change (all works; relative errors 1.6e-06, 2.9e-14, 1.4e-14 and 5.8e-15), and RESULTS.md is rebuilt from it with REACTANT_REUSE=1.
  • test/reactant/registry.jl results.tsv passes, and REACTANT_REUSE=1 test/reactant/runtests.jl passes.
  • The :quality standards items and pre-commit pass.

This was opened by a bot. Please ping @seabbs for any questions.

seabbs-bot and others added 3 commits December 5, 2026 19:03
Under Reactant's AD the gradient of a recurrence with a PerStratum kernel
and a Depletion modifier is wrong or does not compile (#983). The first
test expects the right gradient with the kernel extension loaded; the
second expects an error naming what is needed without it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under Reactant's AD, a recurrence with a PerStratum kernel and a
Depletion modifier gets a wrong gradient, or one that does not compile,
when its strata loops are written out one block a stratum (#983). Run as
kernels they give the right gradient, so such a call runs every strata
loop as a kernel there, and a loop that cannot run as one throws an
error naming what it needs rather than give a wrong gradient. The primal
call keeps the usual executor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With their strata loops run as kernels under Reactant's AD, the four
capacity scenarios take the right reverse gradient (#983). Their names
leave REACTANT_BROKEN_REVERSE, and their reverse rows in results.tsv
come from a rerun of the probe on them, with RESULTS.md rebuilt from it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@seabbs-bot
seabbs-bot requested a review from seabbs as a code owner December 5, 2026 19:42
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

📖 Documentation preview is ready!

View the docs for this PR at: https://EpiAware.github.io/ComposableRecurrences.jl/previews/PR1072/

This preview will be updated automatically when you push new commits.

@github-actions

github-actions Bot commented Dec 5, 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 · · · 21 3 · ·
ForwardDiff · · · 21 · · ·
Mooncake reverse · · · 21 · · ·
Enzyme reverse · · · 20 1 · ·
Evaluation — 24 benchmarks (by time change)
Benchmark base PR time memory
Convolution body / BLAS axpy per lag 396.3 ns 480.5 ns 🔴 1.21× —
Evaluation / Matrix bvd_census T200_L20_S1 920.0 ns 1.06 μs 🔴 1.15× ⚪ 1.0×
Evaluation / Matrix bvd_occupancy_kernel T200_L20_S1 4.27 μs 4.63 μs 🔴 1.08× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_secondary_k5 T200_L20_S1 915.0 ns 934.0 ns ⚪ 1.02× ⚪ 1.0×
Evaluation / Matrix delay_fixed T200_L20_S1 536.0 ns 546.0 ns ⚪ 1.02× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_primary_k5 T200_L20_S1 1.31 μs 1.33 μs ⚪ 1.02× ⚪ 1.0×
Evaluation / Matrix random_walk T30_L1_S1 76.0 ns 75.0 ns ⚪ 0.99× ⚪ 1.0×
Evaluation / Matrix overview T200_L20_S3 11.98 μs 12.09 μs ⚪ 1.01× ⚪ 1.0×
Evaluation / Matrix bvd_patches_call T200_L20_S1 11.11 μs 11.18 μs ⚪ 1.01× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 3.82 μs 3.8 μs ⚪ 0.99× ⚪ 1.0×
Evaluation / Matrix renewal T200_L20_S1 2.23 μs 2.24 μs ⚪ 1.01× ⚪ 1.0×
Evaluation / Matrix bvd_patch T200_L20_S5 26.13 μs 26.0 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix conv_fixed T200_L20_S1 536.0 ns 534.0 ns ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 2.59 μs 2.58 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix custom_modifier_pullback T200_L20_S1 2.98 μs 2.99 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix bvd_patches_call T200_L20_S5 34.98 μs 35.07 μs ⚪ 1.0× ⚪ 1.0×
Convolution body / package 317.4 ns 318.3 ns ⚪ 1.0× —
Evaluation / Matrix bvd_renewal_call T200_L20_S1 6.85 μs 6.83 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix user_depletion T200_L20_S1 6.36 μs 6.38 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix strata_mixing T200_L20_S5 19.22 μs 19.19 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix custom_modifier T200_L20_S1 2.98 μs 2.98 μs ⚪ 1.0× ⚪ 1.0×
Convolution body / native axpy per lag 408.0 ns 408.3 ns ⚪ 1.0× —
Evaluation / Matrix renewal_depletion T200_L20_S1 6.73 μs 6.73 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix user_depletion_pullback T200_L20_S1 6.36 μs 6.37 μs ⚪ 1.0× ⚪ 1.0×
AD gradients — 63 benchmarks (by time change)
Benchmark base PR time memory
AD gradients / Matrix conv_fixed T200_L20_S1 / Enzyme reverse 2.52 μs 2.68 μs 🔴 1.07× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / Enzyme reverse 8.95 μs 9.26 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / Enzyme reverse 7.84 μs 8.08 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / ForwardDiff 1.26 ms 1.22 ms ⚪ 0.97× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / Mooncake reverse 15.34 μs 14.97 μs ⚪ 0.98× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / ForwardDiff 1.4 μs 1.43 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / ForwardDiff 484.1 μs 476.08 μs ⚪ 0.98× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / Mooncake reverse 27.23 μs 26.82 μs ⚪ 0.98× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / Mooncake reverse 24.44 μs 24.12 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / ForwardDiff 3.27 ms 3.23 ms ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / Enzyme reverse 2.64 μs 2.66 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / Enzyme reverse 26.11 μs 26.39 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / ForwardDiff 45.35 ms 45.81 ms ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / ForwardDiff 473.03 μs 477.68 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / Enzyme reverse 9.25 μs 9.16 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / Mooncake reverse 7.38 μs 7.45 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / Mooncake reverse 20.77 μs 20.58 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / Mooncake reverse 26.28 μs 26.05 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / ForwardDiff 600.51 μs 595.41 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / ForwardDiff 5.43 ms 5.38 ms ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / Mooncake reverse 23.01 μs 22.83 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / Mooncake reverse 7.78 μs 7.84 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / ForwardDiff 460.23 μs 456.95 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / Enzyme reverse 12.92 μs 13.01 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / Enzyme reverse 43.77 μs 43.48 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / Enzyme reverse 138.33 μs 137.43 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / Mooncake reverse 67.87 μs 67.44 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / Enzyme reverse 9.04 μs 9.09 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / ForwardDiff 470.45 μs 472.99 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / Mooncake reverse 8.09 μs 8.04 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / Mooncake reverse 16.3 μs 16.38 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / Mooncake reverse 26.5 μs 26.37 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / Enzyme reverse 16.92 μs 17.0 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / Enzyme reverse 17.01 μs 16.93 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / ForwardDiff 504.09 μs 506.44 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / Enzyme reverse 220.0 ns 221.0 ns ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / Mooncake reverse 20.46 μs 20.37 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / Mooncake reverse 194.24 μs 195.1 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / Enzyme reverse 9.46 μs 9.42 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / ForwardDiff 13.82 ms 13.77 ms ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / ForwardDiff 321.72 μs 320.43 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / ForwardDiff 592.51 μs 594.57 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / Enzyme reverse 104.62 μs 104.98 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / Enzyme reverse 18.1 μs 18.16 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / Enzyme reverse 78.9 μs 78.69 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / Mooncake reverse 15.25 μs 15.21 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / Enzyme reverse 19.11 μs 19.06 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / ForwardDiff 597.94 μs 596.61 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / Mooncake reverse 156.93 μs 157.27 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / ForwardDiff 506.56 μs 507.58 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / Mooncake reverse 66.64 μs 66.53 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / ForwardDiff 11.09 ms 11.1 ms ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / Enzyme reverse 46.98 μs 46.91 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / ForwardDiff 254.84 μs 255.21 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / Mooncake reverse 24.24 μs 24.26 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / Mooncake reverse 25.99 μs 26.01 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / ForwardDiff 358.08 μs 357.83 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / Enzyme reverse 16.87 μs 16.88 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / ForwardDiff 1.56 ms 1.56 ms ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / ForwardDiff 506.8 μs 506.94 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / Mooncake reverse 129.12 μs 129.13 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / Mooncake reverse 1.12 μs 1.12 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / Enzyme reverse 8.14 μs 8.14 μs ⚪ 1.0× ⚪ 1.0×

@codecov

codecov Bot commented Dec 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
ad-enzyme-forward 43.00% <ø> (ø)
ad-enzyme-reverse 76.78% <ø> (ø)
ad-forwarddiff 41.88% <ø> (ø)
ad-mooncake-forward 54.69% <ø> (ø)
ad-mooncake-reverse 76.17% <ø> (ø)
ad-reversediff 42.88% <ø> (ø)
ad-reversediff-compiled 42.04% <ø> (ø)
unit 95.79% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

Reactant: wrong reverse gradient for a recurrence with a capacity on current ReactantCore

1 participant