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] )┌ Warning: The AD backend ADTypes.AutoZygote() is not officially supported by DynamicPPL. Gradient calculations may still work, but compatibility is not guaranteed. └ @ DynamicPPL ~/.julia/packages/DynamicPPL/xVVvM/src/logdensityfunction.jl:123 ┌ Warning: The AD backend ADTypes.AutoZygote() is not officially supported by DynamicPPL. Gradient calculations may still work, but compatibility is not guaranteed. └ @ DynamicPPL ~/.julia/packages/DynamicPPL/xVVvM/src/logdensityfunction.jl:123 2-element BenchmarkTools.BenchmarkGroup: tags: [] "evaluation" => 2-element BenchmarkTools.BenchmarkGroup: tags: [] "linked" => Trial(460.000 ns) "standard" => Trial(360.000 ns) "gradient" => 4-element BenchmarkTools.BenchmarkGroup: tags: [] "ADTypes.AutoForwardDiff(chunksize=0)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ForwardDiff"] "linked" => Trial(692.000 ns) "standard" => Trial(581.000 ns) "ADTypes.AutoReverseDiff()" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ReverseDiff"] "linked" => Trial(15.209 μs) "standard" => Trial(13.585 μs) "ADTypes.AutoReverseDiff(compile=true)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ReverseDiff [compiled]"] "linked" => Trial(1.703 μs) "standard" => Trial(1.632 μs) "ADTypes.AutoZygote()" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["Zygote"] "linked" => Trial(812.550 μs) "standard" => Trial(641.803 μ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(chunksize=0), ADTypes.AutoReverseDiff(), ADTypes.AutoReverseDiff(compile=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