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
julia> import DataFrames, Markdown, PrettyTables, Printf

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{nothing, Nothing}(nothing)
 AutoEnzyme{EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}}(EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, 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 output y for mutating functions)
  • optionally a reference ref to check against

There is one scenario per operator, and so here we will use GradientScenario:

scenarios = [
    GradientScenario(f; x=rand(Float32, 3), ref=∇f, place=:inplace),
    GradientScenario(f; x=rand(Float64, 3, 2), ref=∇f, 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                                               |   56     56  38.8s
  ForwardDiff (forward)                                           |   28     28   4.3s
    GradientScenario                                              |   28     28   4.3s
      GradientScenario{1,inplace}(f : Vector{Float32} -> Float32) |   14     14   2.2s
      GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64) |   14     14   2.1s
  Enzyme (reverse)                                                |   28     28  34.5s
    GradientScenario                                              |   28     28  34.5s
      GradientScenario{1,inplace}(f : Vector{Float32} -> Float32) |   14     14  32.9s
      GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64) |   14     14   1.6s

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:

benchmark_result = benchmark_differentiation(backends, scenarios);
12-element Vector{DifferentiationBenchmarkDataRow}:
 DifferentiationBenchmarkDataRow(AutoForwardDiff{nothing, Nothing}(nothing), GradientScenario{1, :inplace, typeof(Main.f), Vector{Float32}, Float32, typeof(Main.∇f)}(Main.f, Float32[0.2428816, 0.29749817, 0.8754151], 1.3014393f0, Main.∇f), :prepare_gradient, 0, 1, 1, 9.287000000000001e-6, 11.0, 528.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoForwardDiff{nothing, Nothing}(nothing), GradientScenario{1, :inplace, typeof(Main.f), Vector{Float32}, Float32, typeof(Main.∇f)}(Main.f, Float32[0.2428816, 0.29749817, 0.8754151], 1.3014393f0, Main.∇f), :value_and_gradient!, 1, 37870, 1, 8.0e-8, 1.0, 32.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoForwardDiff{nothing, Nothing}(nothing), GradientScenario{1, :inplace, typeof(Main.f), Vector{Float32}, Float32, typeof(Main.∇f)}(Main.f, Float32[0.2428816, 0.29749817, 0.8754151], 1.3014393f0, Main.∇f), :gradient!, 1, 47825, 1, 6.900000000000001e-8, 0.0, 0.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoForwardDiff{nothing, Nothing}(nothing), GradientScenario{1, :inplace, typeof(Main.f), Matrix{Float64}, Float64, typeof(Main.∇f)}(Main.f, [0.12338306993592874 0.5562674215678101; 0.9466456250080322 0.28810647129729705; 0.5479436996463277 0.6056799420946917], 2.836941593183788, Main.∇f), :prepare_gradient, 0, 1, 1, 8.285000000000001e-6, 11.0, 1776.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoForwardDiff{nothing, Nothing}(nothing), GradientScenario{1, :inplace, typeof(Main.f), Matrix{Float64}, Float64, typeof(Main.∇f)}(Main.f, [0.12338306993592874 0.5562674215678101; 0.9466456250080322 0.28810647129729705; 0.5479436996463277 0.6056799420946917], 2.836941593183788, Main.∇f), :value_and_gradient!, 1, 35974, 1, 1.9e-7, 5.0, 192.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoForwardDiff{nothing, Nothing}(nothing), GradientScenario{1, :inplace, typeof(Main.f), Matrix{Float64}, Float64, typeof(Main.∇f)}(Main.f, [0.12338306993592874 0.5562674215678101; 0.9466456250080322 0.28810647129729705; 0.5479436996463277 0.6056799420946917], 2.836941593183788, Main.∇f), :gradient!, 1, 31061, 1, 1.8000000000000002e-7, 4.0, 160.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoEnzyme{EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}}(EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}()), GradientScenario{1, :inplace, typeof(Main.f), Vector{Float32}, Float32, typeof(Main.∇f)}(Main.f, Float32[0.2428816, 0.29749817, 0.8754151], 1.3014393f0, Main.∇f), :prepare_gradient, 0, 1, 1, 1.3e-7, 0.0, 0.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoEnzyme{EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}}(EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}()), GradientScenario{1, :inplace, typeof(Main.f), Vector{Float32}, Float32, typeof(Main.∇f)}(Main.f, Float32[0.2428816, 0.29749817, 0.8754151], 1.3014393f0, Main.∇f), :value_and_gradient!, 1, 54527, 1, 7.71e-7, 9.0, 192.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoEnzyme{EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}}(EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}()), GradientScenario{1, :inplace, typeof(Main.f), Vector{Float32}, Float32, typeof(Main.∇f)}(Main.f, Float32[0.2428816, 0.29749817, 0.8754151], 1.3014393f0, Main.∇f), :gradient!, 1, 114430, 1, 6.900000000000001e-8, 0.0, 0.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoEnzyme{EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}}(EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}()), GradientScenario{1, :inplace, typeof(Main.f), Matrix{Float64}, Float64, typeof(Main.∇f)}(Main.f, [0.12338306993592874 0.5562674215678101; 0.9466456250080322 0.28810647129729705; 0.5479436996463277 0.6056799420946917], 2.836941593183788, Main.∇f), :prepare_gradient, 0, 1, 1, 2.21e-7, 0.0, 0.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoEnzyme{EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}}(EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}()), GradientScenario{1, :inplace, typeof(Main.f), Matrix{Float64}, Float64, typeof(Main.∇f)}(Main.f, [0.12338306993592874 0.5562674215678101; 0.9466456250080322 0.28810647129729705; 0.5479436996463277 0.6056799420946917], 2.836941593183788, Main.∇f), :value_and_gradient!, 1, 54677, 1, 8.410000000000001e-7, 9.0, 192.0, 0.0, 0.0)
 DifferentiationBenchmarkDataRow(AutoEnzyme{EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}}(EnzymeCore.ReverseMode{false, EnzymeCore.FFIABI, false}()), GradientScenario{1, :inplace, typeof(Main.f), Matrix{Float64}, Float64, typeof(Main.∇f)}(Main.f, [0.12338306993592874 0.5562674215678101; 0.9466456250080322 0.28810647129729705; 0.5479436996463277 0.6056799420946917], 2.836941593183788, Main.∇f), :gradient!, 1, 113777, 1, 9.000000000000001e-8, 0.0, 0.0, 0.0, 0.0)

The resulting object is a Vector of DifferentiationBenchmarkDataRow, which can easily be converted into a DataFrame from DataFrames.jl:

df = DataFrames.DataFrame(benchmark_result)
12×11 DataFrame
Rowbackendscenariooperatorcallssamplesevalstimeallocsbytesgc_fractioncompile_fraction
Abstract…Gradient…SymbolInt64Int64Int64Float64Float64Float64Float64Float64
1AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)prepare_gradient0119.287e-611.0528.00.00.0
2AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)value_and_gradient!13787018.0e-81.032.00.00.0
3AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)gradient!14782516.9e-80.00.00.00.0
4AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)prepare_gradient0118.285e-611.01776.00.00.0
5AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)value_and_gradient!13597411.9e-75.0192.00.00.0
6AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)gradient!13106111.8e-74.0160.00.00.0
7AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)prepare_gradient0111.3e-70.00.00.00.0
8AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)value_and_gradient!15452717.71e-79.0192.00.00.0
9AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)gradient!111443016.9e-80.00.00.00.0
10AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)prepare_gradient0112.21e-70.00.00.00.0
11AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)value_and_gradient!15467718.41e-79.0192.00.00.0
12AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)gradient!111377719.0e-80.00.00.00.0

Here's what the resulting DataFrame looks like with all its columns. Note that we only compare (possibly) in-place operators, because they are always more efficient.

table = PrettyTables.pretty_table(
    String,
    df;
    backend=Val(:markdown),
    header=names(df),
)

Markdown.parse(table)
backendscenariooperatorcallssamplesevalstimeallocsbytesgc_fractioncompile_fraction
AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)prepare_gradient0119.287e-611.0528.00.00.0
AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)value_and_gradient!13787018.0e-81.032.00.00.0
AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)gradient!14782516.9e-80.00.00.00.0
AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)prepare_gradient0118.285e-611.01776.00.00.0
AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)value_and_gradient!13597411.9e-75.0192.00.00.0
AutoForwardDiff{nothing, Nothing}(nothing)GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)gradient!13106111.8e-74.0160.00.00.0
AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)prepare_gradient0111.3e-70.00.00.00.0
AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)value_and_gradient!15452717.71e-79.0192.00.00.0
AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Vector{Float32} -> Float32)gradient!111443016.9e-80.00.00.00.0
AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)prepare_gradient0112.21e-70.00.00.00.0
AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)value_and_gradient!15467718.41e-79.0192.00.00.0
AutoEnzyme{ReverseMode{false, FFIABI, false}}(ReverseMode{false, FFIABI, false}())GradientScenario{1,inplace}(f : Matrix{Float64} -> Float64)gradient!111377719.0e-80.00.00.00.0