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 end
demo (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(370.000 ns) "standard" => Trial(300.000 ns) "gradient" => 4-element BenchmarkTools.BenchmarkGroup: tags: [] "ADTypes.AutoForwardDiff{0, Nothing}(nothing)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ForwardDiff"] "linked" => Trial(541.000 ns) "standard" => Trial(461.000 ns) "ADTypes.AutoReverseDiff(true)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ReverseDiff [compiled]"] "linked" => Trial(1.442 μs) "standard" => Trial(1.442 μs) "ADTypes.AutoReverseDiff(false)" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["ReverseDiff"] "linked" => Trial(13.456 μs) "standard" => Trial(13.145 μs) "ADTypes.AutoZygote()" => 2-element BenchmarkTools.BenchmarkGroup: tags: ["Zygote"] "linked" => Trial(585.281 μs) "standard" => Trial(565.234 μs)
API
TuringBenchmarking.benchmark_model
TuringBenchmarking.extract_stan_data
TuringBenchmarking.make_stan_suite
TuringBenchmarking.make_turing_suite
TuringBenchmarking.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{0, Nothing}(nothing), ADTypes.AutoReverseDiff(false), ADTypes.AutoReverseDiff(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
: theVarInfo
to use. Defaults toDynamicPPL.VarInfo(model)
.sampler
: theSampler
to use. Defaults tonothing
(i.e. no sampler).context
: theContext
to use. Defaults toDynamicPPL.DefaultContext()
.θ
: the parameters to use. Defaults torand(Vector, model)
.θ_linked
: the linked parameters to use. Defaults torandn(d)
whered
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 oneadbackend
at the time.
TuringBenchmarking.stan_model_string
— Functionstan_model_string(model::DynamicPPL.Model)
Return a string defining the Stan model corresponding to model
.