Testing and benchmarking
These tools check a new modifier, coupling or depletion form, and time the package.
A new type's forward! is checked against PieceInterface, and its pullback! against ForwardDiff and plain AD.
Comparing with plain automatic differentiation
NoAdjoint gives the plain automatic differentiation route to compare with.
using ComposableRecurrences
using ComposableRecurrences: NoAdjoint, Depletion
using ForwardDiff, Chairmarks
r = Recurrence([0.2, 0.5, 0.3]; modifiers = (Depletion(1000.0),))
loss(op) = R -> sum(op(R; history = fill(5.0, 3)))
R = fill(1.4, 60)
(
operator = @b(ForwardDiff.gradient($(loss(r)), $R), seconds = 0.2).time,
no_adjoint = @b(ForwardDiff.gradient($(loss(NoAdjoint(r))), $R), seconds = 0.2).time,
)(operator = 1.1856000000000001e-5, no_adjoint = 1.1888e-5)Time a rule on a reverse-mode backend, as forward-mode backends never take it.
Rule timings
A rule is kept only where it beats plain AD by about 10% in reverse mode on Mooncake or Enzyme. The table gives the rule time over the NoAdjoint time, from benchmark matrix cases at T 200 and L 20 where one covers it, else from the small CI scenarios (marked ¹).
| Rule switched on by | Mooncake reverse | Enzyme reverse | Decision |
|---|---|---|---|
core recurrence (kernel, I or dense coupling) | 0.44 | 0.84 | keep |
Depletion, hazard form | 0.16 | 0.31 | keep |
Depletion, Floor(), dense coupling | 0.32 | 0.39 | keep |
Depletion, Truncate(), dense coupling | 0.20³ | 0.24³ | keep |
Depletion, Truncate(κ), dense coupling | 0.20³ | 0.32³ | keep |
Depletion with two pools and a count flow | 0.28 | 0.40 | keep |
Redistribute | 0.07 | 0.35 | keep |
Depletion without a draw | 0.43 | 0.43 | keep |
Depletion with two pools and waning | 0.40 | 0.32 | keep |
Allocate | 0.78¹ | 0.78¹ | keep |
Allocate with imports and spill, 64 strata | 0.05 | 0.11 | keep |
Depletion filled up to a capacity, flows before the draw | 0.26 | 0.32 | keep |
Transform | 0.18 | 0.43 | keep |
Primary() kernel | 0.51 | 0.66 | keep |
Pairwise kernel | 0.68¹ | 0.61¹ | keep |
Pairwise routes, dense and sparse, 50 strata | 0.11 | 0.45 | keep |
Diagonal coupling | 0.84¹ | 0.98¹ | keep |
| sparse coupling | 0.75¹ | 0.28¹ | keep |
TimeVarying kernel and coupling | 0.71¹ | 0.85¹ | keep |
Convolution | 0.48 | 0.64 | keep |
pointwise modifier without a pullback! | 0.33⁸ | 0.75⁸ | keep |
pointwise modifier without a pullback!, PerStratum parameter | 0.52² | 0.95² | keep |
coupling without a pullback!, up to 24 strata and scalars | 0.47² | 0.96² | keep |
coupling without a pullback!, 25 to 48 strata and scalars | 0.81² | 1.84² | keep on Mooncake |
pointwise modifier without a pullback!, float vector read by time | 0.39⁴ | 0.86⁴ | keep |
whole-step modifier without a pullback!, 25 to 48 values, state and parameters | 0.84⁷ | plain AD | keep on Mooncake |
whole-step modifier without a pullback!, more than 48 | 11.7 to 157⁹ | plain AD | plain AD |
coupling without a pullback!, holding a matrix | 0.39⁴ | 0.55⁴ | keep |
Pairwise routes with a route coupling without a pullback!, up to 24 strata and scalars | 0.35⁵ | 0.83⁵ | keep |
| the same with 25 to 48 strata and scalars | 0.59⁵ | plain AD | keep on Mooncake |
| a sparse route coupling under plain AD | plain AD | 0.48 to 0.57⁶ | keep on Enzyme |
² The worst within the row's limit, at T 200 and L 20, for the user types of the custom_modifier_strata, custom_coupling and custom_coupling_dense cases. The modifier was timed at 3, 5, 10, 20, 23 and 50 strata, the couplings at 5, 13, 20, 23, 26, 30, 36, 40 and 44.
³ A local timing, not a matrix case: 5 strata, a dense coupling, T 200 and L 20.
⁴ The worst within the route's limit, at T 200 and L 20, for the user types of the custom_modifier_vector and custom_coupling_matrix cases. The modifier was timed at 1, 5 and 20 strata, the coupling at 2 to 5. On Enzyme the modifier's local step was 0.79 to 0.86 times plain AD, at 1, 5 and 20 strata, so it takes the rule there too.
⁵ A local timing, not a matrix case: the worst within the row's limit against plain AD of the same model, at T 100 and L 6. The model has two routes, a dense user coupling with one scalar and a dense matrix. It was timed at 5, 11, 20, 22 and 23 strata, one pass, and at 30, 46 and 47, two passes. At 60 strata the call takes plain AD, within noise of the time before: 1.02 times it on Mooncake and 0.98 on Enzyme.
⁶ A local timing, not a matrix case: a NoAdjoint call of the strata_routes routes without the depletion, at T 200 and L 20, timed with and without the rule. It was timed at 5 and 50 strata, with the sparse route coupling constant and as a parameter.
⁷ The worst of two rounds for the user type of the custom_modifier_whole case, at T 200, L 20 and S 20. On Enzyme the rule took 1.25 to 2.4 times plain AD, depending on the machine and the Julia version, so the call takes plain AD there.
⁸ The custom modifier block of benchmark/results/docs.csv, the docs tier at 48f2396e: median times of the rule with forward! only over plain AD at T 200 and L 20. At T 100 and L 6 they are 0.60 on Mooncake and 0.61 on Enzyme.
⁹ The rule's time over plain AD before the call took plain AD past two passes, for the user type of the custom_modifier_matrix case, which holds an S × S matrix, at T 200 and L 20: 11.7 at 20 strata and 157 at 50. It now takes plain AD there, at 0.93 to 1.01 times the NoAdjoint time.
Every rule is faster than plain AD on each backend that takes it; where the decision names one backend, the other takes plain AD. The limits sit where a coupling whose cost grows with the strata squared stops beating plain AD, with 24 inputs per pass. On Enzyme it is 0.96 times plain AD at 23 strata, one pass, and 1.58 at 26, two passes. On Mooncake it is 0.77 at 44 strata, two passes, and 1.08 at 50, three. A coupling whose cost grows with the strata alone is 0.23 times plain AD on Mooncake at 50 strata, but the limit sends it to plain AD, so give such a coupling a pullback!.
The benchmark matrix
benchmark/matrix.jl times every case in test/ADFixtures/src/matrix_cases.jl at every size of a tier, on the primal run and each gradient backend, with and without NoAdjoint. Each backend runs in its own Julia process.
task -t benchmark/Taskfile.yml matrix -- --tier=ci --targets=primal
task -t benchmark/Taskfile.yml matrix-rules
task -t benchmark/Taskfile.yml matrix-report -- matrix-resultsThe report lists the median time of every cell, and the gain of each hand-written gradient as the NoAdjoint median over the operator's median. A new case is a Case entry in matrix_cases.jl whose loss takes a wrap argument (identity or NoAdjoint), so the matrix times both arms.
Some cases add arms:
Cases in
LOCAL_CASESrun alocalarm, which replaces thepullback!of each pointwise modifier and user coupling with the localForwardDiffstep.The
bvd_renewal,bvd_patchesandbvd_delaycases time the model of the patch model case study, in therealisticanddocstiers. They add alooparm, a hand loop in the style of the BVDOutbreakSize backend. They also add ahandrulearm, the same loop with the hand-written Mooncake rules inbenchmark/bvd_handrule.jl.The cases in
TUTORIAL_HANDRULE_CASESadd ahandrulearm beside theirlooparm, with the rules inbenchmark/tutorial_handrule.jl.
The handrule arm runs on Mooncake reverse only, since on any other target it is the loop arm. A test item in test/ad/handrules.jl checks that each of these rules is a Mooncake primitive and that its gradient matches ForwardDiff on the case's hand loop.
--executor and --threads time the operators under an executor. --threads=1,2,4 runs each target once per thread count, and --executor=threaded runs the rule arm under Threaded() (the default is serial). Each run writes <tier>-<run>.tsv and <tier>-<run>.log, where <run> is the target with the thread count and any executor other than serial appended, such as primal_t4 or primal_Threaded_t4.
task -t benchmark/Taskfile.yml matrix -- --tier=realistic --targets=primal
task -t benchmark/Taskfile.yml matrix -- --tier=realistic --targets=primal \
--executor=threaded --threads=2,4The report adds an executor table with each threaded run's speed-up over the serial one-thread run of the same target. Write the serial one-thread run to the same directory first, or the report leaves the table out and says why. Serial runs on more than one thread are labelled <target> @ t<n> and stay out of the executor table. Under the Threaded() executor, ForwardDiff gradients run threaded, and Mooncake and Enzyme thread only a rule's forward and reverse passes. Each row records the load average when its cell started, so a busy machine shows in the results.
Gradient tests and use-case tests
test/ADFixtures holds the scenarios the gradient tests run on every backend, each checked against a ForwardDiff reference. The tolerances are:
Each gradient entry matches its reference to a relative
1e-8, or1e-4for a scenario with single-precision parameters. Entries that are zero in exact arithmetic have an absolute floor of1e-12. The plain call and the prepared, value-and-gradient and in-place calls are all checked this way.The ForwardDiff job checks each reference against central differences to a relative
1e-5, so a wrong reference cannot pass every backend. Each entry may also differ by the rounding of its central difference,100 eps |f| / hfor a steph. Each entry is tried at a step relative to it and at a step of at leastcbrt(eps()); a wrong reference misses by far more.The rule testers that
test_adjointruns use a relative and absolute1e-8, or1e-3in single precision, where their finite differences lose more.
A scenario is a name, a scalar loss of a flat parameter vector and its starting value, added to _SCENARIOS.
("Recurrence renewal", _renewal, () -> _flat(G0, zeros(L), LOGR[1, :]))Run one backend with TAG=mooncake_reverse task test-ad-backend.
Where a new gradient test goes
Each backend job compiles every scenario and test item it runs, so a check in the AD jobs costs minutes on each backend. Put each check at the cheapest level that runs its code path.
The maths of a
pullback!goes intest/unit, against a ForwardDiff Jacobian on small inputs. For a modifier step, depletion form or coupling, declare thepullbackcomponent ofPieceInterfaceand test it withInterfaces.test(PieceInterface{(:pullback,)}, T, objs). For a whole call, usepullback_matches(test/unit/adjoints.jl). These need no AD backend.A new rule path, such as a new operator, a new kind of field or a new wrapper of the call, gets one small scenario in
_SCENARIOS. Every backend job runs it through its rule against ForwardDiff.Every scenario has a
NoAdjointtwin, which Mooncake reverse and Enzyme reverse run: there the twin is the only check that the backend differentiates a built-in's own forward code. ForwardDiff, ReverseDiff and the forward modes take the same plain path for a scenario and its twin, so they run the scenarios alone (category = :rules). A new method of a call onNoAdjointitself needs a check on each of these backends, in the item "Without a rule: the NoAdjoint wrapper's own calls". The benchmarks time every twin.test_adjointchecks one registry operator of each kind of field and wrapper, listed inADJOINT_SCENARIOSintest/ad/adjoints.jl. Add a scenario there only when it brings a kind of field or wrapper the list lacks, or a branch for a constant part that Enzyme's pass with the operator constant reaches.A failure on one backend gets an item tagged for that backend only, at the smallest size that shows it. Name the failure in a comment, so the item stays while the failure can come back.
Do not add a scenario that repeats a covered path at a larger size or in another combination. The benchmark matrix times realistic sizes.
test/usecases rebuilds real models from modelling packages with the operators and compares values and gradients with reference code in test/usecases/references. Run them with julia --project=test test/runtests.jl usecase_only.
Threads, older Julia versions and Reactant in CI
The Threads workflow runs the tests with four threads, so Threaded loops run on separate threads. On Julia 1.11 it runs the whole unit suite; on the current release it runs only the items tagged :threads, as the Test workflow runs the rest there. Tag an item :threads when it uses Threaded, EXECUTOR or Threads, so it runs with four threads on the current release too. A quality check in test/package/test_tags.jl fails on an item that uses them without the tag. To do the same locally, add --threads=4 to a subset run, such as julia --threads=4 --project=test test/runtests.jl threads_only.
The AD workflow runs every backend on the current Julia release only. The AD versions workflow runs every Enzyme reverse and Mooncake reverse item on Julia 1.11 and 1.12, weekly and on demand. To run one of these locally, use julia +1.11 --project=test/ad test/ad/runtests.jl enzyme_reverse.
The Reactant support matrix in test/reactant/ runs weekly and on main, and fails when a cell's status differs from the committed results.tsv. It runs every AD registry scenario, forward and reverse, and the hand-written cases in cases.jl. Those cover what the registry cannot: a resumed call, a joined traced history, a call across 1,000 strata and the growth of compile time with the number of steps. A scenario Reactant cannot run is listed in the registry under the Reactant backend, with the cause. On pull requests that change src/ or ext/, a smoke run checks a few cases and scenarios against the same file. After a change, rerun it on Julia 1.12 from the repository root:
julia +1.12 --project=test/reactant -e 'using Pkg; Pkg.instantiate()'
julia +1.12 --project=test/reactant --startup-file=no test/reactant/runtests.jlThen commit results.tsv and RESULTS.md, and update expected.jl or the registry's Reactant lists. REACTANT_BACKENDS picks the backends that run, and REACTANT_RESULTS only names the results files. A run on another backend, such as a GPU, sets both: REACTANT_BACKENDS=gpu runs it, and REACTANT_RESULTS=gpu writes results-gpu.tsv and RESULTS-gpu.md, so it leaves the CPU results the weekly job compares against unchanged. The main tests check that the hand-written cases' rows agree with expected.jl, which lists CPU cells only; the Reactant workflow checks the registry rows against the registry.