-
Notifications
You must be signed in to change notification settings - Fork 9
Performance and Benchmarks #104
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
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0efe368
temporarily add some pkgs to do testing
463c187
simple benchmarks
7d9eeb0
use ir and tape cache
b78d3f3
use LRUCache instead of Dict
dd211e9
partially copy tape
2e3491d
fix a TArray bug
400571c
add Project.toml for perf dir
3a25240
minor update
f24480e
Update src/tapedtask.jl
yebai 9b548c6
remove redundant module
c6ec201
Catch and print error while re-running a (cached) tape.
yebai e4838e9
put `new` onto tape
e1ae835
copy NewInstruction
c9673cd
update docs/comments
d14936b
give a warning when find an unknown ir code
000ef2b
refactor new instruction, add test cases
1edd718
new CI job
3b4fb2b
Update Project.toml
yebai 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,40 @@ | ||
| name: Benchmarks and MicroIntegration | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - master | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| test: | ||
| name: Benchmarks and MicroIntegration | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| fail-fast: false | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - uses: julia-actions/setup-julia@v1 | ||
| with: | ||
| version: 1 | ||
| arch: x64 | ||
| - uses: julia-actions/julia-buildpkg@latest | ||
| - name: setup enviroment | ||
| shell: julia --color=yes --project=perf {0} | ||
| run: | | ||
| using Pkg | ||
| try | ||
| # force it to use this PR's version of the package | ||
| pkg"add Turing#hg/new-libtask2" # TODO: remove this when Turing is updated | ||
| Pkg.develop(PackageSpec(path=".")) # resolver may fail with main deps | ||
| Pkg.update() | ||
| catch err | ||
| err isa Pkg.Resolve.ResolverError || rethrow() | ||
| # If we can't resolve that means this is incompatible by SemVer and this is fine | ||
| # It means we marked this as a breaking change, so we don't need to worry about | ||
| # Mistakenly introducing a breaking change, as we have intentionally made one | ||
| @info "Not compatible with this release. No problem." exception=err | ||
| exit(0) # Exit immediately, as a success | ||
| end | ||
| - name: run | ||
| run: julia --color=yes --project=perf perf/runtests.jl |
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
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,14 @@ | ||
| [deps] | ||
| AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" | ||
| AdvancedPS = "576499cb-2369-40b2-a588-c64705576edc" | ||
| BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" | ||
| DynamicPPL = "366bfd00-2699-11ea-058f-f148b4cae6d8" | ||
| Libtask = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f" | ||
| Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
| Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0" | ||
|
|
||
| [compat] | ||
| julia = "1.3" | ||
|
|
||
| [targets] | ||
| test = ["Test", "BenchmarkTools"] |
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,47 @@ | ||
| # ]add Turing#hg/new-libtask2 | ||
|
|
||
| using Libtask | ||
| using Turing, DynamicPPL, AdvancedPS | ||
| using BenchmarkTools | ||
|
|
||
| @model gdemo(x, y) = begin | ||
| # Assumptions | ||
| σ ~ InverseGamma(2,3) | ||
| μ ~ Normal(0,sqrt(σ)) | ||
| # Observations | ||
| x ~ Normal(μ, sqrt(σ)) | ||
| y ~ Normal(μ, sqrt(σ)) | ||
| end | ||
|
|
||
|
|
||
| # Case 1: Sample from the prior. | ||
|
|
||
| m = Turing.Core.TracedModel(gdemo(1.5, 2.), SampleFromPrior(), VarInfo()) | ||
|
|
||
| f = m.evaluator[1]; | ||
|
|
||
| args = m.evaluator[2:end]; | ||
|
|
||
| @show "Directly call..." | ||
| @btime f(args...) | ||
| # (2.0, VarInfo (2 variables (μ, σ), dimension 2; logp: -6.162)) | ||
|
|
||
| @show "CTask construction..." | ||
| t = @btime Libtask.CTask(f, args...) | ||
| # schedule(t.task) # work fine! | ||
| # @show Libtask.result(t.tf.tape) | ||
| @show "Step in a tape..." | ||
| @btime Libtask.step_in(t.tf.tape, args) | ||
|
|
||
| # Case 2: SMC sampler | ||
|
|
||
| m = Turing.Core.TracedModel(gdemo(1.5, 2.), Sampler(SMC(50)), VarInfo()); | ||
| @show "Directly call..." | ||
| @btime m.evaluator[1](m.evaluator[2:end]...) | ||
|
|
||
| @show "CTask construction..." | ||
| t = @btime Libtask.CTask(m.evaluator[1], m.evaluator[2:end]...); | ||
| # schedule(t.task) | ||
| # @show Libtask.result(t.tf.tape) | ||
| @show "Step in a tape..." | ||
| @btime Libtask.step_in(t.tf.tape, m.evaluator[2: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,39 @@ | ||
| using Turing, Test, AbstractMCMC, DynamicPPL, Random | ||
|
|
||
| import AbstractMCMC.AbstractSampler | ||
|
|
||
| function check_numerical(chain, | ||
| symbols::Vector, | ||
| exact_vals::Vector; | ||
| atol=0.2, | ||
| rtol=0.0) | ||
| for (sym, val) in zip(symbols, exact_vals) | ||
| E = val isa Real ? | ||
| mean(chain[sym]) : | ||
| vec(mean(chain[sym], dims=1)) | ||
| @info (symbol=sym, exact=val, evaluated=E) | ||
| @test E ≈ val atol=atol rtol=rtol | ||
| end | ||
| end | ||
|
|
||
| function check_MoGtest_default(chain; atol=0.2, rtol=0.0) | ||
| check_numerical(chain, | ||
| [:z1, :z2, :z3, :z4, :mu1, :mu2], | ||
| [1.0, 1.0, 2.0, 2.0, 1.0, 4.0], | ||
| atol=atol, rtol=rtol) | ||
| end | ||
|
|
||
| @model gdemo_d(x, y) = begin | ||
| s ~ InverseGamma(2, 3) | ||
| m ~ Normal(0, sqrt(s)) | ||
| x ~ Normal(m, sqrt(s)) | ||
| y ~ Normal(m, sqrt(s)) | ||
| return s, m | ||
| end | ||
|
|
||
| alg = CSMC(15) | ||
| chain = sample(gdemo_d(1.5, 2.0), alg, 5_000) | ||
|
|
||
| @show chain | ||
|
|
||
| check_numerical(chain, [:s, :m], [49/24, 7/6], atol=0.1) |
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,63 @@ | ||
| using Turing, Test, AbstractMCMC, DynamicPPL, Random, Turing.RandomMeasures, Libtask | ||
KDr2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @model infiniteGMM(x) = begin | ||
| # Hyper-parameters, i.e. concentration parameter and parameters of H. | ||
| α = 1.0 | ||
| μ0 = 0.0 | ||
| σ0 = 1.0 | ||
|
|
||
| # Define random measure, e.g. Dirichlet process. | ||
| rpm = DirichletProcess(α) | ||
|
|
||
| # Define the base distribution, i.e. expected value of the Dirichlet process. | ||
| H = Normal(μ0, σ0) | ||
|
|
||
| # Latent assignment. | ||
| z = tzeros(Int, length(x)) | ||
|
|
||
| # Locations of the infinitely many clusters. | ||
| μ = tzeros(Float64, 0) | ||
|
|
||
| for i in 1:length(x) | ||
|
|
||
| # Number of clusters. | ||
| K = maximum(z) | ||
| nk = Vector{Int}(map(k -> sum(z .== k), 1:K)) | ||
|
|
||
| # Draw the latent assignment. | ||
| z[i] ~ ChineseRestaurantProcess(rpm, nk) | ||
|
|
||
| # Create a new cluster? | ||
| if z[i] > K | ||
| push!(μ, 0.0) | ||
|
|
||
| # Draw location of new cluster. | ||
| μ[z[i]] ~ H | ||
| end | ||
|
|
||
| # Draw observation. | ||
| x[i] ~ Normal(μ[z[i]], 1.0) | ||
| end | ||
| end | ||
|
|
||
| # Generate some test data. | ||
| Random.seed!(1) | ||
|
|
||
| data = vcat(randn(10), randn(10) .- 5, randn(10) .+ 10) | ||
| data .-= mean(data) | ||
| data /= std(data) | ||
|
|
||
| # MCMC sampling | ||
| Random.seed!(2) | ||
| iterations = 500 | ||
| model_fun = infiniteGMM(data) | ||
|
|
||
| m = Turing.Core.TracedModel(model_fun, Sampler(SMC(50)), VarInfo()) | ||
| f = m.evaluator[1] | ||
| args = m.evaluator[2:end] | ||
|
|
||
| t = Libtask.CTask(f, args...) | ||
|
|
||
| Libtask.step_in(t.tf.tape, args) | ||
|
|
||
| @show Libtask.result(t.tf.tape) | ||
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,3 @@ | ||
| include("p0.jl") | ||
| include("p1.jl") | ||
| include("p2.jl") |
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
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
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
Oops, something went wrong.
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.