Repository navigation
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
Open
seabbs-bot wants to merge 3 commits into
seabbs-bot wants to merge 3 commits into
Conversation
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>
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. |
Contributor
Benchmark comparison vs baseMinimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:
Evaluation — 24 benchmarks (by time change)
AD gradients — 63 benchmarks (by time change)
|
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 aDepletionmodifier, 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_dropscenario (3 strata):PerStratum(GCAP), pool and beds (the scenario)PerStratum(GCAP), the pool alone (a plain truncated draw, nopools)PerStratum(GCAP), the beds alone (Truncate(0.2; into = :O))PerStratum(GCAP), a plainHazarddepletionPerStratum(GCAP), aHazarddepletion, couplingK0post_op_transpose_reshape)PerStratum(GCAP), a statelessTransform, aRecentcap or aRedistributePerStratum(GCAP), no modifiersG0with no coupling, aHazarddepletionG0with couplingK0, the pool, the beds or a depletionPerStratum(GCAP), aHazarddepletion, withTRACED_MIN_STRATA => 1So the trigger is a kernel per stratum with a
Depletionmodifier, not the capacity.With 3 strata, below the default
TRACED_MIN_STRATAof 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:_runchecksReactant.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
PerStratumkernel and aDepletionmodifier, the call runs withTraced(; min_strata = 1, kernels_only = true).Tracedgains akernels_onlyfield. With it set, a strata loop that would be written out throws anArgumentErrorinstead: withoutKernelAbstractionsloaded it says to loadKernelAbstractionsandCUDA, and otherwise that the loop could not run as a kernel. The message names thePerStratumkernel and theDepletionmodifier, and the alternatives: a kernel shared by the strata, or a gradient taken outside Reactant.within_autodiffdoes 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 drawRecurrence capacity, beds with overflowRecurrence capacity, weekly budget with a queueRecurrence trace of a weekly budget with a queueRecurrence with a value for each stratum as its historystays listed: it has a coupling and no kernel per stratum, and still does not compile.Tests and results
test/reactant/extension_check.jlgains two testsets: the compiled gradient of aPerStratumkernel with a depletion matches plain Enzyme, and the primal compiles; and, in a process withoutKernelAbstractions, the gradient throws an error namingPerStratum,Depletionandusing KernelAbstractions, CUDA, while the primal compiles. Red before the change (the gradient did not compile), green after.REACTANT_BROKEN_REVERSEintest/ADFixtures/src/registry.jl.test/reactant/results.tsvcome from a rerun ofprobe.jlon the four scenarios with the change (allworks; relative errors 1.6e-06, 2.9e-14, 1.4e-14 and 5.8e-15), andRESULTS.mdis rebuilt from it withREACTANT_REUSE=1.test/reactant/registry.jl results.tsvpasses, andREACTANT_REUSE=1 test/reactant/runtests.jlpasses.:qualitystandards items and pre-commit pass.This was opened by a bot. Please ping @seabbs for any questions.