Skip to content

Add benchmarks #133

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 4 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
85 changes: 85 additions & 0 deletions perf/benchmark.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Libtask
using LinearAlgebra
using BenchmarkTools

####################################################################

function benchmark_driver!(f, x...; f_displayname=string(f))
println("benchmarking $(f_displayname)...")
tf = Libtask.TapedFunction(f, x)

print(" Run Original Function:")
@btime $f($(x)...)
GC.gc()

print(" Run TapedFunction:")
@btime $tf($(x)...)
GC.gc()

ctf = Libtask.compile(tf)
print(" Run TapedFunction (compiled):")
@btime $ctf($(x)...)
GC.gc()
end

####################################################################


function rosenbrock(x)
i = x[2:end]
j = x[1:end-1]
return sum((1 .- j).^2 + 100*(i - j.^2).^2)
end

x = rand(100000)
benchmark_driver!(rosenbrock, x)

####################################################################

function ackley(x::AbstractVector)
a, b, c = 20.0, -0.2, 2.0*π
len_recip = inv(length(x))
sum_sqrs = zero(eltype(x))
sum_cos = sum_sqrs
for i in x
sum_cos += cos(c*i)
sum_sqrs += i^2
end
return (-a * exp(b * sqrt(len_recip*sum_sqrs)) -
exp(len_recip*sum_cos) + a + MathConstants.e)
end

x = rand(100000)
benchmark_driver!(ackley, x)

####################################################################
function generate_matrix_test(n)
return x -> begin
# @assert length(x) == 2n^2 + n
a = reshape(x[1:n^2], n, n)
b = reshape(x[n^2 + 1:2n^2], n, n)
return log.((a * b) + a - b)
end
end

n = 100
matrix_test = generate_matrix_test(n)
x = collect(1.0:(2n^2 + n))
benchmark_driver!(matrix_test, x; f_displayname="matrix_test")

####################################################################
relu(x) = log.(1.0 .+ exp.(x))
sigmoid(n) = 1. / (1. + exp(-n))

function neural_net(w1, w2, w3, x1)
x2 = relu(w1 * x1)
x3 = relu(w2 * x2)
return sigmoid(LinearAlgebra.dot(w3, x3))
end

xs = (randn(10,10), randn(10,10), randn(10), rand(10))
benchmark_driver!(neural_net, xs...)

####################################################################

println("done")
29 changes: 11 additions & 18 deletions perf/p0.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,26 @@ 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..."
println("Directly call...")
@btime f(args...)
# (2.0, VarInfo (2 variables (μ, σ), dimension 2; logp: -6.162))

@show "TapedTask construction..."
println("TapedTask construction...")
t = @btime TapedTask(f, args...)
# schedule(t.task) # work fine!
# @show Libtask.result(t.tf)
@show "Run a tape..."
println("Run a tape...")
@btime t.tf(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]...)
f = m.evaluator[1];
args = m.evaluator[2:end];

@show "TapedTask construction..."
t = @btime TapedTask(m.evaluator[1], m.evaluator[2:end]...);
# schedule(t.task)
# @show Libtask.result(t.tf.tape)
@show "Run a tape..."
@btime t.tf(m.evaluator[2:end]...)
println("Directly call...")
@btime f(args...)
println("TapedTask construction...")
t = @btime TapedTask(f, args...)
println("Run a tape...")
@btime t.tf(args...)
1 change: 1 addition & 0 deletions perf/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include("benchmark.jl")
include("p0.jl")
include("p1.jl")
include("p2.jl")