Benchmarking
@bmodel
The easiest way to benchmark models is to use the @bmodel macro:
using Wren, Wren.Dists@wren function f() x ~ Exponential() z ~ MvNormalScale(zeros(3), abs(x) * I)end@bmodel f()BenchmarkResult
├ model : f
├ n_params : 4
└ lp : 39.497 ns (4 allocs: 160 bytes)When passed a single argument, @bmodel will just benchmark lp: the time it takes to compute the log-joint probability with a vectorised interface (this is the same as e.g. LogDensityProblems.logdensity, if you are familiar with that interface).
Note that benchmarks report the fastest time out of all the samples, since that varies the least between runs.
WrenCore.@bmodel — Macro
@bmodel model [key=value ...]
@bmodel model adtype [key=value ...]Benchmark a model; shorthand for benchmark_model. The one-argument form benchmarks lp only. The two-argument form also benchmarks lp_and_grad with the given AD backend.
Two keyword arguments are accepted, and passed on to benchmark_model:
seconds=5to run each benchmark for roughly 5 secondsextras=trueto also benchmarkparametersandlogjoint
For the other keyword arguments, use benchmark_model directly.
AD
To additionally benchmark gradients, you can pass an AbstractADType as the second argument to @bmodel:
using Enzyme# `Wren.DEFAULT_AD` is the same as `AutoEnzyme(; mode=Reverse)`.@bmodel f() DEFAULT_ADBenchmarkResult
├ model : f
├ backend : AutoEnzyme(mode=EnzymeCore.ReverseMode{false, false, false, EnzymeCore.FFIABI, false, false}())
├ n_params : 4
├ lp : 39.547 ns (4 allocs: 160 bytes)
├ lp_and_grad : 99.947 ns (10 allocs: 416 bytes)
└ grad / lp : 2.527In addition to lp, this will also benchmark lp_and_grad, which is the time taken to compute the log-joint plus its gradient (i.e., LogDensityProblems.logdensity_and_gradient).
We expect that most use cases of @bmodel will fall into this category, since this is essentially the most important piece of information for optimising a model for inference methods like NUTS.
Accessing the results
To access the data in this benchmark, you can directly access the fields of the BenchmarkResult object. The relevant fields are named {function_name}_bench:
br = @bmodel f()br.lp_bench # This is a `Chairmarks.Sample`39.752 ns (4 allocs: 160 bytes)br.lp_bench.time, br.lp_bench.allocs(3.9752025931928686e-8, 4.0)Pitfalls
If you are trying to do comparative benchmarking i.e. comparing performance before and after, you may very well need to fix the number of BLAS threads to get consistent results. You can do so either by calling LinearAlgebra.BLAS.set_num_threads(1), or by setting the OPENBLAS_NUM_THREADS environment variable before starting Julia.
This recommendation in the context of Wren is mostly based on anecdotal evidence, but BenchmarkTools.jl also recommends this: see also this thread on Julia Discourse for more info.
To get more reliable results, you may also want to increase the amount of time spent benchmarking. You can do so by passing seconds=5.0 to either @bmodel or benchmark_model (the default is 1 second).
More control
@bmodel is shorthand for the benchmark_model function, which contains more keyword arguments, including ones to control how the model is vectorised (for example, which parameters to benchmark at).
WrenCore.benchmark_model — Function
benchmark_model(
model::Model,
adtype::Union{Nothing,DI.AbstractADType}=nothing;
lp_function=logjoint_transformed,
latent_source::AbstractLatentSource=FromPrior(Xoshiro(468)),
parameter_space::AbstractParameterSpace=UnconstrainedSpace(),
seconds::Real=1,
extras::Bool=false,
) -> BenchmarkResultBenchmark a model. By default this benchmarks:
lp(log-density of the vectorised model), and- if
adtypeis given,lp_and_grad(log-density and its gradient).
If extras=true, it also benchmarks parameters (sampling from the prior) and logjoint (log-joint density evaluation without vectorisation).
Note that if adtype is given, the corresponding backend package must be loaded first.
The lp_function, latent_source, and parameter_space keywords are forwarded to VecModel. lp_function and parameter_space have the same defaults as for VecModel. latent_source instead defaults to drawing from the prior with a fixed seed, so that every call benchmarks at the same parameters. (The time taken can depend on the parameter values, so random parameters make results vary between calls.)
seconds is the approximate time spent on each of the benchmarks.
See also @bmodel.
Testing AD correctness
To check that an AD backend gives the right log-density and gradient, use test_ad. By default this compares against ForwardDiff, but you can also compare against another backend with WithBackend, or against known values with WithExpectedResult. If the results don't match, an ADIncorrectException is thrown.
Benchmarks over time
Wren has a benchmarking suite which runs several PosteriorDB models, plus their gradients under Enzyme and Mooncake. The code for this is in bench/suite, and you can view the results at https://juliabayes.org/Wren.jl/bench.
This suite is currently run manually by the maintainers in order to guarantee that the results are at least somewhat consistent from run to run. (On GitHub Actions, the results are not consistent enough to be useful, since the underlying hardware is not guaranteed to be the same from run to run.)