TuringBenchmarking.jl

A useful package for benchmarking and checking Turing.jl models.

Example

julia> using TuringBenchmarking, Turing
julia> @model function demo(x) s ~ InverseGamma(2, 3) m ~ Normal(0, sqrt(s)) for i in 1:length(x) x[i] ~ Normal(m, sqrt(s)) end enddemo (generic function with 2 methods)
julia> model = demo([1.5, 2.0]);
julia> benchmark_model( model; # Check correctness of computations check=true, # Automatic differentiation backends to check and benchmark adbackends=[:forwarddiff, :reversediff, :reversediff_compiled, :zygote] )2-element BenchmarkTools.BenchmarkGroup: tags: [] "evaluation" => 2-element BenchmarkTools.BenchmarkGroup: tags: [] "linked" => Trial(380.000 ns) "standard" => Trial(340.000 ns) "gradient" => 4-element BenchmarkTools.BenchmarkGroup: tags: [] "ADTypes.AutoForwardDiff{0, Nothing}(nothing)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ForwardDiff"] "linked" => Trial(571.000 ns) "standard" => Trial(530.000 ns) "ADTypes.AutoReverseDiff(true)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ReverseDiff [compiled]"] "linked" => Trial(1.512 μs) "standard" => Trial(1.482 μs) "ADTypes.AutoReverseDiff(false)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ReverseDiff"] "linked" => Trial(9.186 μs) "standard" => Trial(8.916 μs) "ADTypes.AutoZygote()" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["Zygote"] "linked" => Trial(524.058 μs) "standard" => Trial(499.422 μs)

API

TuringBenchmarking.extract_stan_dataFunction
extract_stan_data(model::DynamicPPL.Model)

Return the data in model in a format consumable by the corresponding Stan model.

The Stan model requires the return data to be either

  1. A JSON string representing a dictionary with the data.
  2. A path to a data file ending in .json.
source
TuringBenchmarking.make_turing_suiteMethod
make_turing_suite(model::Turing.Model; kwargs...)

Create default benchmark suite for model.

Keyword arguments

  • adbackends: a collection of adbackends to use, specified either as a type from

ADTypes.jl or using a Symbol. Defaults to ADTypes.AbstractADType[ADTypes.AutoForwardDiff{0, Nothing}(nothing), ADTypes.AutoReverseDiff(false), ADTypes.AutoReverseDiff(true), ADTypes.AutoZygote()].

  • run_once=true: if true, the body of each benchmark will be run once to avoid compilation to be included in the timings (this may occur if compilation runs longer than the allowed time limit).
  • check=false: if true, the log-density evaluations and the gradients will be compared against each other to ensure that they are consistent. Note that this will force run_once=true.
  • error_on_failed_check=false: if true, an error will be thrown if the check fails rather than just printing a warning, as is done by default.
  • error_on_failed_backend=false: if true, an error will be thrown if the evaluation of the log-density or the gradient fails for any of the backends rather than just printing a warning, as is done by default.
  • varinfo: the VarInfo to use. Defaults to DynamicPPL.VarInfo(model).
  • sampler: the Sampler to use. Defaults to nothing (i.e. no sampler).
  • context: the Context to use. Defaults to DynamicPPL.DefaultContext().
  • θ: the parameters to use. Defaults to rand(Vector, model).
  • θ_linked: the linked parameters to use. Defaults to randn(d) where d is the length of the linked parameters..
  • atol: the absolute tolerance to use for comparisons.
  • rtol: the relative tolerance to use for comparisons.

Notes

  • A separate "parameter" instance (DynamicPPL.VarInfo) will be created for each test. Hence if you have a particularly large model, you might want to only pass one adbackend at the time.
source