-
Notifications
You must be signed in to change notification settings - Fork 37
Benchmarking #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Benchmarking #248
Changes from 116 commits
Commits
Show all changes
128 commits
Select commit
Hold shift + click to select a range
e32b59f
initial stuff
torfjelde e83a671
moved benchmark folder and added README
torfjelde 3ab2bee
unwrap distributions and varnames at model-level
torfjelde a549d1f
removed _tilde and renamed tilde_assume and others
torfjelde e0f77bc
formatting
torfjelde 8e4fa91
updated compiler for new tilde-methods
torfjelde 1c9a2d5
fixed calls to dot_assume
torfjelde d70e1be
added sampling context and unwrap_childcontext
torfjelde f743990
updated tilde methods
torfjelde 3d2e7e2
updated model call signature
torfjelde 4f1d396
updated compiler
torfjelde b187d74
formatting
torfjelde ee99f8c
added getsym for vectors
torfjelde c4845d0
Update src/varname.jl
torfjelde a0c05f3
fixed some signatures for Model
torfjelde 307cd7e
fixed a method call
torfjelde 5972771
fixed method signatures
torfjelde c4ecd0e
sort of fixed the matchingvalue functionality for model
torfjelde a34b51c
formatting
torfjelde b89ff7e
removed redundant _tilde method
torfjelde 6368282
Merge branch 'tor/tilde-simplification' into tor/sampler-context
torfjelde e4a2cf8
removed left-over acclogp! that should not be here anymore
torfjelde 7605785
export SamplingContext
torfjelde 354ac52
use context instead of ctx to refer to contexts
torfjelde b7a2b3b
formatting
torfjelde 9e0fc9a
use context instead of ctx for variables
torfjelde 7a4a1a3
use context instead of ctx to refer to contexts
torfjelde 7899473
Update src/compiler.jl
torfjelde 1630476
Update src/context_implementations.jl
torfjelde 6892d2b
Apply suggestions from code review
torfjelde 13da1b4
added some whitespace to some docstrings
torfjelde d76e5b3
deprecated tilde and dot_tilde plus exported new versions
torfjelde 8059669
formatting
torfjelde 4343896
Merge branch 'master' into tor/tilde-simplification
yebai 43ef8d1
minor version bump
torfjelde fec00b6
Merge branch 'master' into tor/sampler-context
torfjelde ffb4933
Merge branch 'master' into tor/sampler-context
torfjelde 43356a2
Merge branch 'master' into tor/tilde-simplification
torfjelde 671e0b7
Merge branch 'master' into tor/tilde-simplification
torfjelde 30c8345
Merge branch 'tor/tilde-simplification' into tor/sampling-context-simple
torfjelde 1015f0e
added impl of matchingvalue for contexts
torfjelde 23c86a7
reverted the change that makes assume always resample
torfjelde 17f5abe
removed the inds arguments from assume and dot_assume to stay non-bre…
torfjelde dbd61f0
Update src/context_implementations.jl
yebai b10ba3f
added missing sampler arg to tilde_observe
torfjelde 43779e8
Merge branch 'tor/sampling-context-simple' of github.com:TuringLang/D…
torfjelde bc5029f
added missing sampler argument in dot_tilde_observe
torfjelde 7eac33d
fixed order of arguments in some dot_assume calls
torfjelde 8599481
formatting
torfjelde 90a8c45
formatting
torfjelde f9d4ff8
added missing sampler argument in tilde_observe for SamplingContext
torfjelde e424fe7
added missing word in a docstring
torfjelde 70957d2
updated submodel macro
torfjelde d00cdcf
removed unwrap_childcontext and related since its not needed for this PR
torfjelde 639fd6e
updated submodel macro
torfjelde c9a06fb
fixed evaluation implementations of dot_assume
torfjelde 2fe5f40
updated pointwise_loglikelihoods and related
torfjelde b532ca6
added proper tests for pointwise_loglikelihoods
torfjelde 4e2274e
updated DPPL tests to reflect recent changes
torfjelde ef6da43
bump minor version since this will be breaking
torfjelde dbb48c5
Merge branch 'tor/tilde-simplification' into tor/sampling-context-simple
torfjelde 10899f3
formatting
torfjelde 1f21ce4
formatting
torfjelde 7004506
renamed mean_of_mean_models used in tests
torfjelde fa6c4d6
bumped dppl version in integration tests
torfjelde 684d829
Apply suggestions from code review
torfjelde 07bb284
Apply suggestions from code review
torfjelde c7c6a3c
fixed ambiguity error
torfjelde c259999
Merge branch 'tor/sampling-context-simple' of github.com:TuringLang/D…
torfjelde 06d319c
Introduction of `SamplingContext`: keeping it simple (#259)
torfjelde cb996c6
Update src/DynamicPPL.jl
torfjelde 300f4b6
Merge branch 'master' into tor/tilde-simplification
torfjelde 0437d39
Merge branch 'tor/tilde-simplification' into tor/simple-varinfo-v2
torfjelde 03c9285
added initial impl of SimpleVarInfo
torfjelde f91952d
remove unnecessary debug statements to be compat with Zygote
torfjelde 4d4b489
make reconstruct slightly more generic
torfjelde 1506773
Merge branch 'master' into tor/simple-varinfo-v2
torfjelde a68c045
added a couple of convenience constructors
torfjelde 9766aec
formatting
torfjelde 46b1c78
small fix
torfjelde 3a645d6
return var_info from tilde-statements, allowing impl of immutable ver…
torfjelde a2ec0bd
allow usage of non-Ref types in SimpleVarInfo
torfjelde 1d9bc37
update submodel-macro
torfjelde cfd7f21
formatting and docstring for submodel-macro
torfjelde c200e73
attempt at supporting implicit returns too
torfjelde efeb812
added a small comment
torfjelde 14b9495
simplifed submodel macro a bit
torfjelde c3d9e7b
formatting
torfjelde 416e773
fixed typo
torfjelde ac2b68d
Merge branch 'tor/simple-varinfo-v2' of github.com:TuringLang/Dynamic…
torfjelde b4b8b03
use bang-bang convention
torfjelde a725a27
updated PointwiseLikelihoodContext
torfjelde 5512670
fixed issue where we unnecessarily replace the return-statement
torfjelde 4c1ee70
check subtype in the retval
torfjelde 26590b5
formatting
torfjelde 42fd414
fixed type-instability in retval check
torfjelde f219545
introduced evaluate method for model
torfjelde ce13566
remove unnecessary type-requirement
torfjelde 3556b11
make return-value check much nicer
torfjelde 599d094
removed redundant creation of anonymous function
torfjelde 22b170c
dont use UnionAll in return_values
torfjelde 4606f16
updated tests for submodel to reflect new syntax
torfjelde 68cb021
moved to using BangBang-convention for most methods
torfjelde cb1fd8b
remove SimpleVarInfo from this branch
torfjelde 5936dd0
added a comment
torfjelde 426c465
reverted submodel macro to use = rather than ~
torfjelde 208b62c
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde a8e55bd
updated SimpleVarInfo impl
torfjelde 149229f
added a couple of missing deprecations
torfjelde 809d23f
updated tests
torfjelde 8ea80d7
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde 07f684b
updated implementations of logjoint and others
torfjelde d317bd8
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde b00ae47
formatting
torfjelde a88f8ea
Merge branch 'tor/immutable-varinfo-support' into tor/simple-varinfo-v2
torfjelde bfd7c78
added eltype impl for SimpleVarInfo
torfjelde acb15eb
formatting
torfjelde 4828aab
fixed eltype for SimpleVarInfo
torfjelde f25afa3
Merge branch 'master' into tor/benchmarks
torfjelde e67ca2a
updated to work with master
torfjelde 6ac63b8
changed the output structure a bit
torfjelde 74a3f97
make benchmarks a proper project
torfjelde 5742f90
forgot to include src
torfjelde 34cfabc
updated jmd files
torfjelde abb1768
added some docs
torfjelde 4ea7bfc
updated README
torfjelde 1147f64
formatting
torfjelde cfb8635
make sure we are evaluating rather than sampling
torfjelde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| To run the benchmarks, simply do: | ||
| ```sh | ||
| julia --project -e 'using Weave; Weave.weave("benchmarks.jmd", doctype="github", args=Dict(:benchmarkbody => "benchmark_body.jmd"));' | ||
| ``` | ||
|
|
||
| Furthermore: | ||
| - If you want to save the output of `code_typed` for the evaluator of the different models, add a `:prefix => "myprefix"` to the `args`. | ||
| - If `:prefix_old` is specified in `args`, a `diff` of the `code_typed` loaded using `:prefix_old` and the output of `code_typed` for the current run will be included in the weaved document. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| module DynamicPPLBenchmarks | ||
|
|
||
| using DynamicPPL | ||
| using BenchmarkTools | ||
|
|
||
| import Weave | ||
| import Markdown | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import LibGit2, Pkg | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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) | ||
| return suite | ||
| end | ||
|
|
||
| function benchmark_typed_varinfo!(suite, m) | ||
| # Populate. | ||
| vi = VarInfo(m) | ||
| # Evaluate. | ||
| suite["evaluation_typed"] = @benchmarkable $m($vi) | ||
| return suite | ||
| end | ||
|
|
||
| function typed_code(m, vi = VarInfo(m)) | ||
| rng = DynamicPPL.Random.MersenneTwister(42); | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
|
|
||
| function make_suite(m) | ||
| suite = BenchmarkGroup() | ||
| benchmark_untyped_varinfo!(suite, m) | ||
| benchmark_typed_varinfo!(suite, m) | ||
|
|
||
| return suite | ||
| end | ||
|
|
||
| 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...) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rendered = Weave.render_doc(doc) | ||
| return display(Markdown.parse(rendered)) | ||
| end | ||
|
|
||
| function pkgversion(m::Module) | ||
| projecttoml_path = joinpath(dirname(pathof(m)), "..", "Project.toml") | ||
| return Pkg.TOML.parsefile(projecttoml_path)["version"] | ||
| end | ||
|
|
||
| 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 | ||
|
|
||
| function weave_benchmarks( | ||
| ; | ||
| benchmarkbody=joinpath(dirname(pathof(DynamicPPLBenchmarks)), "..", "benchmark_body.jmd"), | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| include_commit_id=false, | ||
| name=default_name(include_commit_id=include_commit_id), | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| name_old=nothing, | ||
| include_typed_code=false, | ||
| doctype="github", | ||
| outpath="results/$(name)/", | ||
| kwargs... | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| args = Dict( | ||
| :benchmarkbody => benchmarkbody, | ||
| :name => name, | ||
| :include_typed_code => include_typed_code | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| if !isnothing(name_old) | ||
| args[:name_old] = name_old | ||
| end | ||
| @info "Storing output in $(outpath)" | ||
| mkpath(outpath) | ||
| Weave.weave("benchmarks.jmd", doctype; out_path=outpath, args=args, kwargs...) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| end # module | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.