-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* temporarily add some pkgs to do testing * simple benchmarks * use ir and tape cache * use LRUCache instead of Dict * partially copy tape * fix a TArray bug * add Project.toml for perf dir * minor update * Update src/tapedtask.jl Co-authored-by: David Widmann <devmotion@users.noreply.github.com> * remove redundant module * Catch and print error while re-running a (cached) tape. * put `new` onto tape * copy NewInstruction * update docs/comments * give a warning when find an unknown ir code * refactor new instruction, add test cases * new CI job * Update Project.toml Co-authored-by: Hong Ge <hg344@cam.ac.uk> Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
- Loading branch information
1 parent
d27401a
commit ccc293c
Showing
13 changed files
with
361 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 | ||
|
||
@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 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 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 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 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.
ccc293c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
ccc293c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/52746
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: