Skip to content

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.

julia
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 byMooncake reverseEnzyme reverseDecision
core recurrence (kernel, I or dense coupling)0.440.84keep
Depletion, hazard form0.160.31keep
Depletion, Floor(), dense coupling0.320.39keep
Depletion, Truncate(), dense coupling0.20³0.24³keep
Depletion, Truncate(κ), dense coupling0.20³0.32³keep
Depletion with two pools and a count flow0.280.40keep
Redistribute0.070.35keep
Depletion without a draw0.430.43keep
Depletion with two pools and waning0.400.32keep
Allocate0.78¹0.78¹keep
Allocate with imports and spill, 64 strata0.050.11keep
Depletion filled up to a capacity, flows before the draw0.260.32keep
Transform0.180.43keep
Primary() kernel0.510.66keep
Pairwise kernel0.68¹0.61¹keep
Pairwise routes, dense and sparse, 50 strata0.110.45keep
Diagonal coupling0.84¹0.98¹keep
sparse coupling0.75¹0.28¹keep
TimeVarying kernel and coupling0.71¹0.85¹keep
Convolution0.480.64keep
pointwise modifier without a pullback!0.33⁸0.75⁸keep
pointwise modifier without a pullback!, PerStratum parameter0.52²0.95²keep
coupling without a pullback!, up to 24 strata and scalars0.47²0.96²keep
coupling without a pullback!, 25 to 48 strata and scalars0.81²1.84²keep on Mooncake
pointwise modifier without a pullback!, float vector read by time0.39⁴0.86⁴keep
whole-step modifier without a pullback!, 25 to 48 values, state and parameters0.84⁷plain ADkeep on Mooncake
whole-step modifier without a pullback!, more than 4811.7 to 157⁹plain ADplain AD
coupling without a pullback!, holding a matrix0.39⁴0.55⁴keep
Pairwise routes with a route coupling without a pullback!, up to 24 strata and scalars0.35⁵0.83⁵keep
the same with 25 to 48 strata and scalars0.59⁵plain ADkeep on Mooncake
a sparse route coupling under plain ADplain AD0.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.

bash
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-results

The 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_CASES run a local arm, which replaces the pullback! of each pointwise modifier and user coupling with the local ForwardDiff step.

  • The bvd_renewal, bvd_patches and bvd_delay cases time the model of the patch model case study, in the realistic and docs tiers. They add a loop arm, a hand loop in the style of the BVDOutbreakSize backend. They also add a handrule arm, the same loop with the hand-written Mooncake rules in benchmark/bvd_handrule.jl.

  • The cases in TUTORIAL_HANDRULE_CASES add a handrule arm beside their loop arm, with the rules in benchmark/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.

bash
task -t benchmark/Taskfile.yml matrix -- --tier=realistic --targets=primal
task -t benchmark/Taskfile.yml matrix -- --tier=realistic --targets=primal \
    --executor=threaded --threads=2,4

The 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, or 1e-4 for a scenario with single-precision parameters. Entries that are zero in exact arithmetic have an absolute floor of 1e-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| / h for a step h. Each entry is tried at a step relative to it and at a step of at least cbrt(eps()); a wrong reference misses by far more.

  • The rule testers that test_adjoint runs use a relative and absolute 1e-8, or 1e-3 in 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.

julia
("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 in test/unit, against a ForwardDiff Jacobian on small inputs. For a modifier step, depletion form or coupling, declare the pullback component of PieceInterface and test it with Interfaces.test(PieceInterface{(:pullback,)}, T, objs). For a whole call, use pullback_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 NoAdjoint twin, 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 on NoAdjoint itself 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_adjoint checks one registry operator of each kind of field and wrapper, listed in ADJOINT_SCENARIOS in test/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:

bash
julia +1.12 --project=test/reactant -e 'using Pkg; Pkg.instantiate()'
julia +1.12 --project=test/reactant --startup-file=no test/reactant/runtests.jl

Then 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.