Tutorial
We present a typical workflow with DifferentiationInterfaceTest.jl, building on the tutorial of the DifferentiationInterface.jl documentation (which we encourage you to read first).
julia> using DifferentiationInterface, DifferentiationInterfaceTest
julia> import ForwardDiff, Enzyme
Introduction
The AD backends we want to compare are ForwardDiff.jl and Enzyme.jl.
backends = [AutoForwardDiff(), AutoEnzyme(; mode=Enzyme.Reverse)]
2-element Vector{ADTypes.AbstractADType}:
AutoForwardDiff()
AutoEnzyme(mode=EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false, false}())
To do that, we are going to take gradients of a simple function:
f(x::AbstractArray) = sum(sin, x)
f (generic function with 1 method)
Of course we know the true gradient mapping:
∇f(x::AbstractArray) = cos.(x)
∇f (generic function with 1 method)
DifferentiationInterfaceTest.jl relies with so-called "scenarios", in which you encapsulate the information needed for your test:
- the function
f
- the input
x
and outputy
of the functionf
- the reference output of the operator (here
grad
) - the number of arguments for
f
(either1
or2
) - the behavior of the operator (either
:inplace
or:outofplace
)
There is one scenario constructor per operator, and so here we will use GradientScenario
:
xv = rand(Float32, 3)
xm = rand(Float64, 3, 2)
scenarios = [
GradientScenario(f; x=xv, y=f(xv), grad=∇f(xv), nb_args=1, place=:inplace),
GradientScenario(f; x=xm, y=f(xm), grad=∇f(xm), nb_args=1, place=:inplace)
];
Testing
The main entry point for testing is the function test_differentiation
. It has many options, but the main ingredients are the following:
julia> test_differentiation( backends, # the backends you want to compare scenarios, # the scenarios you defined, correctness=true, # compares values against the reference type_stability=false, # checks type stability with JET.jl detailed=true, # prints a detailed test set )
Test Summary: | Pass Total Time Testing correctness | 108 108 9.4s AutoForwardDiff() | 54 54 2.3s gradient | 54 54 2.3s Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | 27 27 1.4s Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | 27 27 0.8s AutoEnzyme(mode=EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false, false}()) | 54 54 7.0s gradient | 54 54 7.0s Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | 27 27 5.8s Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | 27 27 1.2s
If you are too lazy to manually specify the reference, you can also provide an AD backend as the ref_backend
keyword argument, which will serve as the ground truth for comparison.
Benchmarking
Once you are confident that your backends give the correct answers, you probably want to compare their performance. This is made easy by the benchmark_differentiation
function, whose syntax should feel familiar:
df = benchmark_differentiation(backends, scenarios);
Row | backend | scenario | operator | calls | samples | evals | time | allocs | bytes | gc_fraction | compile_fraction |
---|---|---|---|---|---|---|---|---|---|---|---|
Abstract… | Scenario… | Symbol | Int64 | Int64 | Int64 | Float64 | Float64 | Float64 | Float64 | Float64 | |
1 | AutoForwardDiff() | Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | prepare_gradient | 0 | 1 | 1 | 7.452e-6 | 11.0 | 528.0 | 0.0 | 0.0 |
2 | AutoForwardDiff() | Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | value_and_gradient! | 1 | 151090 | 1 | 1.46e-7 | 1.0 | 32.0 | 0.0 | 0.0 |
3 | AutoForwardDiff() | Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | gradient! | 1 | 154207 | 1 | 1.34e-7 | 0.0 | 0.0 | 0.0 | 0.0 |
4 | AutoForwardDiff() | Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | prepare_gradient | 0 | 1 | 1 | 7.748e-6 | 11.0 | 1776.0 | 0.0 | 0.0 |
5 | AutoForwardDiff() | Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | value_and_gradient! | 1 | 89913 | 1 | 3.43e-7 | 5.0 | 192.0 | 0.0 | 0.0 |
6 | AutoForwardDiff() | Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | gradient! | 1 | 102915 | 1 | 3.27e-7 | 4.0 | 160.0 | 0.0 | 0.0 |
7 | AutoEnzyme(mode=ReverseMode{false, FFIABI, false, false}()) | Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | prepare_gradient | 0 | 1 | 1 | 1.37e-7 | 0.0 | 0.0 | 0.0 | 0.0 |
8 | AutoEnzyme(mode=ReverseMode{false, FFIABI, false, false}()) | Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | value_and_gradient! | 1 | 63679 | 1 | 9.74e-7 | 10.0 | 208.0 | 0.0 | 0.0 |
9 | AutoEnzyme(mode=ReverseMode{false, FFIABI, false, false}()) | Scenario{:gradient,1,:inplace} f : Vector{Float32} -> Float32 | gradient! | 1 | 147910 | 1 | 1.05e-7 | 0.0 | 0.0 | 0.0 | 0.0 |
10 | AutoEnzyme(mode=ReverseMode{false, FFIABI, false, false}()) | Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | prepare_gradient | 0 | 1 | 1 | 1.3e-7 | 0.0 | 0.0 | 0.0 | 0.0 |
11 | AutoEnzyme(mode=ReverseMode{false, FFIABI, false, false}()) | Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | value_and_gradient! | 1 | 66814 | 1 | 9.73e-7 | 10.0 | 208.0 | 0.0 | 0.0 |
12 | AutoEnzyme(mode=ReverseMode{false, FFIABI, false, false}()) | Scenario{:gradient,1,:inplace} f : Matrix{Float64} -> Float64 | gradient! | 1 | 173266 | 1 | 1.59e-7 | 0.0 | 0.0 | 0.0 | 0.0 |
The resulting object is a DataFrame
from DataFrames.jl, whose columns correspond to the fields of DifferentiationBenchmarkDataRow
: