Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
e32b59f
initial stuff
torfjelde May 18, 2021
e83a671
moved benchmark folder and added README
torfjelde May 18, 2021
3ab2bee
unwrap distributions and varnames at model-level
torfjelde May 31, 2021
a549d1f
removed _tilde and renamed tilde_assume and others
torfjelde May 31, 2021
e0f77bc
formatting
torfjelde May 31, 2021
8e4fa91
updated compiler for new tilde-methods
torfjelde May 31, 2021
1c9a2d5
fixed calls to dot_assume
torfjelde May 31, 2021
d70e1be
added sampling context and unwrap_childcontext
torfjelde May 31, 2021
f743990
updated tilde methods
torfjelde May 31, 2021
3d2e7e2
updated model call signature
torfjelde May 31, 2021
4f1d396
updated compiler
torfjelde May 31, 2021
b187d74
formatting
torfjelde May 31, 2021
ee99f8c
added getsym for vectors
torfjelde May 31, 2021
c4845d0
Update src/varname.jl
torfjelde May 31, 2021
a0c05f3
fixed some signatures for Model
torfjelde May 31, 2021
307cd7e
fixed a method call
torfjelde May 31, 2021
5972771
fixed method signatures
torfjelde Jun 1, 2021
c4ecd0e
sort of fixed the matchingvalue functionality for model
torfjelde Jun 1, 2021
a34b51c
formatting
torfjelde Jun 1, 2021
b89ff7e
removed redundant _tilde method
torfjelde Jun 1, 2021
6368282
Merge branch 'tor/tilde-simplification' into tor/sampler-context
torfjelde Jun 1, 2021
e4a2cf8
removed left-over acclogp! that should not be here anymore
torfjelde Jun 1, 2021
7605785
export SamplingContext
torfjelde Jun 1, 2021
354ac52
use context instead of ctx to refer to contexts
torfjelde Jun 1, 2021
b7a2b3b
formatting
torfjelde Jun 1, 2021
9e0fc9a
use context instead of ctx for variables
torfjelde Jun 1, 2021
7a4a1a3
use context instead of ctx to refer to contexts
torfjelde Jun 1, 2021
7899473
Update src/compiler.jl
torfjelde Jun 2, 2021
1630476
Update src/context_implementations.jl
torfjelde Jun 2, 2021
6892d2b
Apply suggestions from code review
torfjelde Jun 2, 2021
13da1b4
added some whitespace to some docstrings
torfjelde Jun 2, 2021
d76e5b3
deprecated tilde and dot_tilde plus exported new versions
torfjelde Jun 2, 2021
8059669
formatting
torfjelde Jun 2, 2021
4343896
Merge branch 'master' into tor/tilde-simplification
yebai Jun 2, 2021
43ef8d1
minor version bump
torfjelde Jun 2, 2021
fec00b6
Merge branch 'master' into tor/sampler-context
torfjelde Jun 3, 2021
ffb4933
Merge branch 'master' into tor/sampler-context
torfjelde Jun 4, 2021
43356a2
Merge branch 'master' into tor/tilde-simplification
torfjelde Jun 4, 2021
671e0b7
Merge branch 'master' into tor/tilde-simplification
torfjelde Jun 7, 2021
30c8345
Merge branch 'tor/tilde-simplification' into tor/sampling-context-simple
torfjelde Jun 7, 2021
1015f0e
added impl of matchingvalue for contexts
torfjelde Jun 7, 2021
23c86a7
reverted the change that makes assume always resample
torfjelde Jun 7, 2021
17f5abe
removed the inds arguments from assume and dot_assume to stay non-bre…
torfjelde Jun 7, 2021
dbd61f0
Update src/context_implementations.jl
yebai Jun 7, 2021
b10ba3f
added missing sampler arg to tilde_observe
torfjelde Jun 7, 2021
43779e8
Merge branch 'tor/sampling-context-simple' of github.com:TuringLang/D…
torfjelde Jun 7, 2021
bc5029f
added missing sampler argument in dot_tilde_observe
torfjelde Jun 7, 2021
7eac33d
fixed order of arguments in some dot_assume calls
torfjelde Jun 7, 2021
8599481
formatting
torfjelde Jun 7, 2021
90a8c45
formatting
torfjelde Jun 7, 2021
f9d4ff8
added missing sampler argument in tilde_observe for SamplingContext
torfjelde Jun 7, 2021
e424fe7
added missing word in a docstring
torfjelde Jun 7, 2021
70957d2
updated submodel macro
torfjelde Jun 8, 2021
d00cdcf
removed unwrap_childcontext and related since its not needed for this PR
torfjelde Jun 8, 2021
639fd6e
updated submodel macro
torfjelde Jun 8, 2021
c9a06fb
fixed evaluation implementations of dot_assume
torfjelde Jun 8, 2021
2fe5f40
updated pointwise_loglikelihoods and related
torfjelde Jun 8, 2021
b532ca6
added proper tests for pointwise_loglikelihoods
torfjelde Jun 8, 2021
4e2274e
updated DPPL tests to reflect recent changes
torfjelde Jun 8, 2021
ef6da43
bump minor version since this will be breaking
torfjelde Jun 8, 2021
dbb48c5
Merge branch 'tor/tilde-simplification' into tor/sampling-context-simple
torfjelde Jun 8, 2021
10899f3
formatting
torfjelde Jun 8, 2021
1f21ce4
formatting
torfjelde Jun 8, 2021
7004506
renamed mean_of_mean_models used in tests
torfjelde Jun 8, 2021
fa6c4d6
bumped dppl version in integration tests
torfjelde Jun 9, 2021
684d829
Apply suggestions from code review
torfjelde Jun 9, 2021
07bb284
Apply suggestions from code review
torfjelde Jun 9, 2021
c7c6a3c
fixed ambiguity error
torfjelde Jun 9, 2021
c259999
Merge branch 'tor/sampling-context-simple' of github.com:TuringLang/D…
torfjelde Jun 9, 2021
06d319c
Introduction of `SamplingContext`: keeping it simple (#259)
torfjelde Jun 9, 2021
cb996c6
Update src/DynamicPPL.jl
torfjelde Jun 9, 2021
300f4b6
Merge branch 'master' into tor/tilde-simplification
torfjelde Jun 9, 2021
0437d39
Merge branch 'tor/tilde-simplification' into tor/simple-varinfo-v2
torfjelde Jun 9, 2021
03c9285
added initial impl of SimpleVarInfo
torfjelde Jun 10, 2021
f91952d
remove unnecessary debug statements to be compat with Zygote
torfjelde Jun 10, 2021
4d4b489
make reconstruct slightly more generic
torfjelde Jun 10, 2021
1506773
Merge branch 'master' into tor/simple-varinfo-v2
torfjelde Jun 10, 2021
a68c045
added a couple of convenience constructors
torfjelde Jun 18, 2021
9766aec
formatting
torfjelde Jun 18, 2021
46b1c78
small fix
torfjelde Jun 18, 2021
3a645d6
return var_info from tilde-statements, allowing impl of immutable ver…
torfjelde Jun 20, 2021
a2ec0bd
allow usage of non-Ref types in SimpleVarInfo
torfjelde Jun 20, 2021
1d9bc37
update submodel-macro
torfjelde Jun 20, 2021
cfd7f21
formatting and docstring for submodel-macro
torfjelde Jun 20, 2021
c200e73
attempt at supporting implicit returns too
torfjelde Jun 20, 2021
efeb812
added a small comment
torfjelde Jun 20, 2021
14b9495
simplifed submodel macro a bit
torfjelde Jun 20, 2021
c3d9e7b
formatting
torfjelde Jun 20, 2021
416e773
fixed typo
torfjelde Jun 20, 2021
ac2b68d
Merge branch 'tor/simple-varinfo-v2' of github.com:TuringLang/Dynamic…
torfjelde Jun 20, 2021
b4b8b03
use bang-bang convention
torfjelde Jun 20, 2021
a725a27
updated PointwiseLikelihoodContext
torfjelde Jun 20, 2021
5512670
fixed issue where we unnecessarily replace the return-statement
torfjelde Jun 20, 2021
4c1ee70
check subtype in the retval
torfjelde Jun 20, 2021
26590b5
formatting
torfjelde Jun 20, 2021
42fd414
fixed type-instability in retval check
torfjelde Jun 20, 2021
f219545
introduced evaluate method for model
torfjelde Jun 20, 2021
ce13566
remove unnecessary type-requirement
torfjelde Jun 20, 2021
3556b11
make return-value check much nicer
torfjelde Jun 20, 2021
599d094
removed redundant creation of anonymous function
torfjelde Jun 20, 2021
22b170c
dont use UnionAll in return_values
torfjelde Jun 28, 2021
4606f16
updated tests for submodel to reflect new syntax
torfjelde Jun 28, 2021
68cb021
moved to using BangBang-convention for most methods
torfjelde Jun 30, 2021
cb1fd8b
remove SimpleVarInfo from this branch
torfjelde Jun 30, 2021
5936dd0
added a comment
torfjelde Jun 30, 2021
426c465
reverted submodel macro to use = rather than ~
torfjelde Jun 30, 2021
208b62c
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
a8e55bd
updated SimpleVarInfo impl
torfjelde Jun 30, 2021
149229f
added a couple of missing deprecations
torfjelde Jun 30, 2021
809d23f
updated tests
torfjelde Jun 30, 2021
8ea80d7
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
07f684b
updated implementations of logjoint and others
torfjelde Jun 30, 2021
d317bd8
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
b00ae47
formatting
torfjelde Jun 30, 2021
a88f8ea
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde Jun 30, 2021
bfd7c78
added eltype impl for SimpleVarInfo
torfjelde Jul 2, 2021
acb15eb
formatting
torfjelde Jul 2, 2021
4828aab
fixed eltype for SimpleVarInfo
torfjelde Jul 6, 2021
f25afa3
Merge branch 'master' into tor/benchmarks
torfjelde Jul 8, 2021
e67ca2a
updated to work with master
torfjelde Jul 8, 2021
6ac63b8
changed the output structure a bit
torfjelde Jul 8, 2021
74a3f97
make benchmarks a proper project
torfjelde Jul 8, 2021
5742f90
forgot to include src
torfjelde Jul 8, 2021
34cfabc
updated jmd files
torfjelde Jul 8, 2021
abb1768
added some docs
torfjelde Jul 8, 2021
4ea7bfc
updated README
torfjelde Jul 8, 2021
1147f64
formatting
torfjelde Jul 8, 2021
cfb8635
make sure we are evaluating rather than sampling
torfjelde Jul 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions benchmarks/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "DynamicPPLBenchmarks"
uuid = "d94a1522-c11e-44a7-981a-42bf5dc1a001"
version = "0.1.0"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
DiffUtils = "8294860b-85a6-42f8-8c35-d911f667b5f6"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8"
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9"
30 changes: 30 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
To run the benchmarks, simply do:
```sh
julia --project -e 'using DynamicPPLBenchmarks; weave_benchmarks();'
```

```julia
help?> weave_benchmarks
search: weave_benchmarks

weave_benchmarks(input="benchmarks.jmd"; kwargs...)

Weave benchmarks present in benchmarks.jmd into a single file.

Keyword arguments
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡

• benchmarkbody: JMD-file to be rendered for each model.

• include_commit_id=false: specify whether to include commit-id in the default name.

• name: the name of directory in results/ to use as output directory.

• name_old=nothing: if specified, comparisons of current run vs. the run pinted to by name_old
will be included in the generated document.

• include_typed_code=false: if true, output of code_typed for the evaluator of the model will be
included in the weaved document.

• Rest of the passed kwargs will be passed on to Weave.weave.
```
39 changes: 39 additions & 0 deletions benchmarks/benchmark_body.jmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
```julia
@time model_def(data)();
```

```julia
m = time_model_def(model_def, data);
```

```julia
suite = make_suite(m);
results = run(suite)
results
```

```julia; echo=false; results="hidden";
BenchmarkTools.save(joinpath("results", WEAVE_ARGS[:name], "$(m.name)_benchmarks.json"), results)
```

```julia; wrap=false
if WEAVE_ARGS[:include_typed_code]
typed = typed_code(m)
end
```

```julia; echo=false; results="hidden"
if WEAVE_ARGS[:include_typed_code]
# Serialize the output of `typed_code` so we can compare later.
haskey(WEAVE_ARGS, :name) && serialize(joinpath("results", WEAVE_ARGS[:name],"$(m.name).jls"), string(typed));
end
```

```julia; wrap=false; echo=false;
if haskey(WEAVE_ARGS, :name_old)
# We want to compare the generated code to the previous version.
import DiffUtils
typed_old = deserialize(joinpath("results", WEAVE_ARGS[:name_old], "$(m.name).jls"));
DiffUtils.diff(typed_old, string(typed), width=130)
end
```
96 changes: 96 additions & 0 deletions benchmarks/benchmarks.jmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Benchmarks

## Setup

```julia
using BenchmarkTools, DynamicPPL, Distributions, Serialization
```

```julia
import DynamicPPLBenchmarks: time_model_def, make_suite, typed_code, weave_child
```

## Models

### `demo1`

```julia
@model function demo1(x)
m ~ Normal()
x ~ Normal(m, 1)

return (m = m, x = x)
end

model_def = demo1;
data = 1.0;
```

```julia; results="markup"; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
```

### `demo2`

```julia
@model function demo2(y)
# Our prior belief about the probability of heads in a coin.
p ~ Beta(1, 1)

# The number of observations.
N = length(y)
for n in 1:N
# Heads or tails of a coin are drawn from a Bernoulli distribution.
y[n] ~ Bernoulli(p)
end
end

model_def = demo2;
data = rand(0:1, 10);
```

```julia; results="markup"; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
```

### `demo3`

```julia
@model function demo3(x)
D, N = size(x)

# Draw the parameters for cluster 1.
μ1 ~ Normal()

# Draw the parameters for cluster 2.
μ2 ~ Normal()

μ = [μ1, μ2]

# Comment out this line if you instead want to draw the weights.
w = [0.5, 0.5]

# Draw assignments for each datum and generate it from a multivariate normal.
k = Vector{Int}(undef, N)
for i in 1:N
k[i] ~ Categorical(w)
x[:,i] ~ MvNormal([μ[k[i]], μ[k[i]]], 1.)
end
return k
end

model_def = demo3

# Construct 30 data points for each cluster.
N = 30

# Parameters for each cluster, we assume that each cluster is Gaussian distributed in the example.
μs = [-3.5, 0.0]

# Construct the data points.
data = mapreduce(c -> rand(MvNormal([μs[c], μs[c]], 1.), N), hcat, 1:2);
```

```julia; echo=false
weave_child(WEAVE_ARGS[:benchmarkbody], mod = @__MODULE__, args = WEAVE_ARGS)
```
171 changes: 171 additions & 0 deletions benchmarks/src/DynamicPPLBenchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
module DynamicPPLBenchmarks

using DynamicPPL
using BenchmarkTools

using Weave: Weave
using Markdown: Markdown

using LibGit2: LibGit2
using Pkg: Pkg

export weave_benchmarks

function time_model_def(model_def, args...)
return @time model_def(args...)
end

function benchmark_untyped_varinfo!(suite, m)
vi = VarInfo()
# Populate.
m(vi)
# Evaluate.
suite["evaluation_untyped"] = @benchmarkable $m($vi, $(DefaultContext()))
return suite
end

function benchmark_typed_varinfo!(suite, m)
# Populate.
vi = VarInfo(m)
# Evaluate.
suite["evaluation_typed"] = @benchmarkable $m($vi, $(DefaultContext()))
return suite
end

function typed_code(m, vi=VarInfo(m))
rng = DynamicPPL.Random.MersenneTwister(42)
spl = DynamicPPL.SampleFromPrior()
ctx = DynamicPPL.SamplingContext(rng, spl, DynamicPPL.DefaultContext())

results = code_typed(m.f, Base.typesof(m, vi, ctx, m.args...))
return first(results)
end

"""
make_suite(model)

Create default benchmark suite for `model`.
"""
function make_suite(model)
suite = BenchmarkGroup()
benchmark_untyped_varinfo!(suite, model)
benchmark_typed_varinfo!(suite, model)

return suite
end

"""
weave_child(indoc; mod, args, kwargs...)

Weave `indoc` with scope of `mod` into markdown.

Useful for weaving within weaving, e.g.
```julia
weave_child(child_jmd_path, mod = @__MODULE__, args = WEAVE_ARGS)
```
together with `results="markup"` and `echo=false` will simply insert
the weaved version of `indoc`.

# Notes
- Currently only supports `doctype == "github"`. Other outputs are "supported"
in the sense that it works but you might lose niceties such as syntax highlighting.
"""
function weave_child(indoc; mod, args, kwargs...)
# FIXME: Make this work for other output formats than just `github`.
doc = Weave.WeaveDoc(indoc, nothing)
doc = Weave.run_doc(doc; doctype="github", mod=mod, args=args, kwargs...)
rendered = Weave.render_doc(doc)
return display(Markdown.parse(rendered))
end

"""
pkgversion(m::Module)

Return version of module `m` as listed in its Project.toml.
"""
function pkgversion(m::Module)
projecttoml_path = joinpath(dirname(pathof(m)), "..", "Project.toml")
return Pkg.TOML.parsefile(projecttoml_path)["version"]
end

"""
default_name(; include_commit_id=false)

Construct a name from either repo information or package version
of `DynamicPPL`.

If the path of `DynamicPPL` is a git-repo, return name of current branch,
joined with the commit id if `include_commit_id` is `true`.

If path of `DynamicPPL` is _not_ a git-repo, it is assumed to be a release,
resulting in a name of the form `release-VERSION`.
"""
function default_name(; include_commit_id=false)
dppl_path = abspath(joinpath(dirname(pathof(DynamicPPL)), ".."))

# Extract branch name and commit id
local name
try
githead = LibGit2.head(LibGit2.GitRepo(dppl_path))
branchname = LibGit2.shortname(githead)

name = replace(branchname, "/" => "_")
if include_commit_id
gitcommit = LibGit2.peel(LibGit2.GitCommit, githead)
commitid = string(LibGit2.GitHash(gitcommit))
name *= "-$(commitid)"
end
catch e
if e isa LibGit2.GitError
@info "No git repo found for $(dppl_path); extracting name from package version."
name = "release-$(pkgversion(DynamicPPL))"
else
rethrow(e)
end
end

return name
end

"""
weave_benchmarks(input="benchmarks.jmd"; kwargs...)

Weave benchmarks present in `benchmarks.jmd` into a single file.

# Keyword arguments
- `benchmarkbody`: JMD-file to be rendered for each model.
- `include_commit_id=false`: specify whether to include commit-id in the default name.
- `name`: the name of directory in `results/` to use as output directory.
- `name_old=nothing`: if specified, comparisons of current run vs. the run pinted to
by `name_old` will be included in the generated document.
- `include_typed_code=false`: if `true`, output of `code_typed` for the evaluator
of the model will be included in the weaved document.
- Rest of the passed `kwargs` will be passed on to `Weave.weave`.
"""
function weave_benchmarks(
input=joinpath(dirname(pathof(DynamicPPLBenchmarks)), "..", "benchmarks.jmd");
benchmarkbody=joinpath(
dirname(pathof(DynamicPPLBenchmarks)), "..", "benchmark_body.jmd"
),
include_commit_id=false,
name=default_name(; include_commit_id=include_commit_id),
name_old=nothing,
include_typed_code=false,
doctype="github",
outpath="results/$(name)/",
kwargs...,
)
args = Dict(
:benchmarkbody => benchmarkbody,
:name => name,
:include_typed_code => include_typed_code,
)
if !isnothing(name_old)
args[:name_old] = name_old
end
@info "Storing output in $(outpath)"
mkpath(outpath)
return Weave.weave(input, doctype; out_path=outpath, args=args, kwargs...)
end

end # module