TuringBenchmarking.jl
A useful package for benchmarking and checking Turing.jl models.
Example
julia> using TuringBenchmarking, Turingjulia> @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.benchmark_modelTuringBenchmarking.extract_stan_dataTuringBenchmarking.make_stan_suiteTuringBenchmarking.make_turing_suiteTuringBenchmarking.stan_model_string
TuringBenchmarking.benchmark_model — Methodbenchmark_model(model::Turing.Model; suite_kwargs..., kwargs...)Create and run a benchmark suite for model.
The benchmarking suite will be created using make_turing_suite. See make_turing_suite for the available keyword arguments and more information.
Keyword arguments
suite_kwargs: Keyword arguments passed tomake_turing_suite.kwargs: Keyword arguments passed toBenchmarkTools.run.
TuringBenchmarking.extract_stan_data — Functionextract_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
- A JSON string representing a dictionary with the data.
- A path to a data file ending in
.json.
TuringBenchmarking.make_stan_suite — Functionmake_stan_suite(model::Turing.Model; kwargs...)Create default benchmark suite for the Stan model corresponding to model.
TuringBenchmarking.make_turing_suite — Methodmake_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: iftrue, 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: iftrue, the log-density evaluations and the gradients will be compared against each other to ensure that they are consistent. Note that this will forcerun_once=true.error_on_failed_check=false: iftrue, 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: iftrue, 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: theVarInfoto use. Defaults toDynamicPPL.VarInfo(model).sampler: theSamplerto use. Defaults tonothing(i.e. no sampler).context: theContextto use. Defaults toDynamicPPL.DefaultContext().θ: the parameters to use. Defaults torand(Vector, model).θ_linked: the linked parameters to use. Defaults torandn(d)wheredis 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 oneadbackendat the time.
TuringBenchmarking.stan_model_string — Functionstan_model_string(model::DynamicPPL.Model)Return a string defining the Stan model corresponding to model.