Skip to content

Commit 15cb5f5

Browse files
committed
Document BenchmarkToolsExt
1 parent 7eede62 commit 15cb5f5

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

HISTORY.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
# DynamicPPL Changelog
22

3+
## 0.36.0
4+
5+
### BenchmarkTools extension
6+
7+
DynamicPPL now contains a BenchmarkTools extension.
8+
If you have both packages loaded, then it exports a single function `make_benchmark_suite`, which returns a `BenchmarkTools.BenchmarkGroup` object.
9+
Running this will give you information about the time taken to evaluate the log density of the model ("evaluation"), as well as the time taken to evaluate its gradient.
10+
11+
Note that benchmarking both of these is in general much easier since the changes in 0.35.0.
12+
If you just want to run a single model, the easiest way is to do this:
13+
14+
```julia
15+
@model f() = ...
16+
ldf = LogDensityFunction(f(); adtype=AutoMyBackend())
17+
params = ldf.varinfo[:]
18+
@btime LogDensityProblems.logdensity($ldf, params)
19+
@btime LogDensityProblems.logdensity_and_gradient($ldf, params)
20+
```
21+
22+
The `make_benchmark_suite` function is essentially a nice wrapper around this.
23+
324
## 0.35.5
425

526
Several internal methods have been removed:

docs/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[deps]
22
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
3+
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
34
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
45
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
56
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
67
DocumenterMermaid = "a078cd44-4d9c-4618-b545-3ab9d77f9177"
8+
DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8"
79
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
810
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
911
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
@@ -13,6 +15,7 @@ StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
1315

1416
[compat]
1517
Accessors = "0.1"
18+
BenchmarkTools = "1"
1619
DataStructures = "0.18"
1720
Distributions = "0.25"
1821
Documenter = "1"

docs/make.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ using DynamicPPL: AbstractPPL
99
# consistent with that.
1010
using Distributions
1111
using DocumenterMermaid
12-
# load MCMCChains package extension to make `predict` available
12+
13+
# To get docstrings from package extensions
1314
using MCMCChains
15+
using BenchmarkTools
1416

1517
# Doctest setup
1618
DocMeta.setdocmeta!(
@@ -22,7 +24,11 @@ makedocs(;
2224
# The API index.html page is fairly large, and violates the default HTML page size
2325
# threshold of 200KiB, so we double that.
2426
format=Documenter.HTML(; size_threshold=2^10 * 400),
25-
modules=[DynamicPPL, Base.get_extension(DynamicPPL, :DynamicPPLMCMCChainsExt)],
27+
modules=[
28+
DynamicPPL,
29+
Base.get_extension(DynamicPPL, :DynamicPPLMCMCChainsExt),
30+
Base.get_extension(DynamicPPL, :DynamicPPLBenchmarkToolsExt),
31+
],
2632
pages=[
2733
"Home" => "index.md", "API" => "api.md", "Internals" => ["internals/varinfo.md"]
2834
],

docs/src/api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ DynamicPPL.TestUtils.update_values!!
245245
DynamicPPL.TestUtils.test_values
246246
```
247247

248+
## Benchmarking Utilities
249+
250+
If you have `BenchmarkTools` loaded, this function will be available:
251+
252+
```@docs
253+
DynamicPPL.make_benchmark_suite
254+
```
255+
256+
For more fine-grained control over this, you can construct a [`LogDensityFunction`](@ref) yourself and run something along the lines of:
257+
258+
```julia
259+
# set up your model and varinfo here
260+
ldf = LogDensityFunction(model, varinfo; adtype=adtype)
261+
params = varinfo[:]
262+
@benchmark LogDensityProblems.logdensity($ldf, params)
263+
@benchmark LogDensityProblems.logdensity_with_gradient($ldf, params)
264+
```
265+
248266
## Debugging Utilities
249267

250268
DynamicPPL provides a few methods for checking validity of a model-definition.

ext/DynamicPPLBenchmarkToolsExt.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ using Random: Random
1616
1717
Create a benchmark suite for `model` using the selected varinfo type and AD backend.
1818
Available varinfo choices:
19-
• `:untyped` → uses `VarInfo()`
20-
• `:typed` → uses `VarInfo(model)`
21-
• `:simple_namedtuple` → uses `SimpleVarInfo{Float64}(model())`
22-
• `:simple_dict` → builds a `SimpleVarInfo{Float64}` from a Dict (pre-populated with the model’s outputs)
19+
- `:untyped` → uses `VarInfo()`
20+
- `:typed` → uses `VarInfo(model)`
21+
- `:simple_namedtuple` → uses `SimpleVarInfo{Float64}(model())`
22+
- `:simple_dict` → builds a `SimpleVarInfo{Float64}` from a Dict (pre-populated with the model’s outputs)
23+
24+
Note that to run AD with a given backend, you will also need to load the backend yourself. For example,
25+
if `adtype` is `AutoForwardDiff()`, you will need to `import ForwardDiff`.
2326
2427
`islinked` determines whether to link the VarInfo for evaluation.
2528
"""
26-
function make_benchmark_suite(
29+
function DynamicPPL.make_benchmark_suite(
2730
rng::Random.AbstractRNG,
2831
model::Model,
2932
varinfo_choice::Symbol,
@@ -67,10 +70,10 @@ function make_benchmark_suite(
6770

6871
return suite
6972
end
70-
function make_benchmark_suite(
73+
function DynamicPPL.make_benchmark_suite(
7174
model::Model, varinfo_choice::Symbol, adtype::Symbol, islinked::Bool
7275
)
73-
return make_benchmark_suite(
76+
return DynamicPPL.make_benchmark_suite(
7477
Random.default_rng(), model, varinfo_choice, adtype, islinked
7578
)
7679
end

src/DynamicPPL.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export AbstractVarInfo,
125125
value_iterator_from_chain,
126126
check_model,
127127
check_model_and_trace,
128+
# Benchmarking
129+
make_benchmark_suite,
128130
# Deprecated.
129131
@logprob_str,
130132
@prob_str,

0 commit comments

Comments
 (0)