Skip to content

Commit 2f561e4

Browse files
KDr2yebai
andauthored
Add benchmarks (#133)
* add benchmarks * update old benchmarks * new benchmark * Apply suggestions from code review Co-authored-by: Hong Ge <[email protected]>
1 parent eacff8c commit 2f561e4

File tree

3 files changed

+97
-18
lines changed

3 files changed

+97
-18
lines changed

perf/benchmark.jl

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Libtask
2+
using LinearAlgebra
3+
using BenchmarkTools
4+
5+
####################################################################
6+
7+
function benchmark_driver!(f, x...; f_displayname=string(f))
8+
println("benchmarking $(f_displayname)...")
9+
tf = Libtask.TapedFunction(f, x)
10+
11+
print(" Run Original Function:")
12+
@btime $f($(x)...)
13+
GC.gc()
14+
15+
print(" Run TapedFunction:")
16+
@btime $tf($(x)...)
17+
GC.gc()
18+
19+
ctf = Libtask.compile(tf)
20+
print(" Run TapedFunction (compiled):")
21+
@btime $ctf($(x)...)
22+
GC.gc()
23+
end
24+
25+
####################################################################
26+
27+
28+
function rosenbrock(x)
29+
i = x[2:end]
30+
j = x[1:end-1]
31+
return sum((1 .- j).^2 + 100*(i - j.^2).^2)
32+
end
33+
34+
x = rand(100000)
35+
benchmark_driver!(rosenbrock, x)
36+
37+
####################################################################
38+
39+
function ackley(x::AbstractVector)
40+
a, b, c = 20.0, -0.2, 2.0*π
41+
len_recip = inv(length(x))
42+
sum_sqrs = zero(eltype(x))
43+
sum_cos = sum_sqrs
44+
for i in x
45+
sum_cos += cos(c*i)
46+
sum_sqrs += i^2
47+
end
48+
return (-a * exp(b * sqrt(len_recip*sum_sqrs)) -
49+
exp(len_recip*sum_cos) + a + MathConstants.e)
50+
end
51+
52+
x = rand(100000)
53+
benchmark_driver!(ackley, x)
54+
55+
####################################################################
56+
function generate_matrix_test(n)
57+
return x -> begin
58+
# @assert length(x) == 2n^2 + n
59+
a = reshape(x[1:n^2], n, n)
60+
b = reshape(x[n^2 + 1:2n^2], n, n)
61+
return log.((a * b) + a - b)
62+
end
63+
end
64+
65+
n = 100
66+
matrix_test = generate_matrix_test(n)
67+
x = collect(1.0:(2n^2 + n))
68+
benchmark_driver!(matrix_test, x; f_displayname="matrix_test")
69+
70+
####################################################################
71+
relu(x) = log.(1.0 .+ exp.(x))
72+
sigmoid(n) = 1. / (1. + exp(-n))
73+
74+
function neural_net(w1, w2, w3, x1)
75+
x2 = relu(w1 * x1)
76+
x3 = relu(w2 * x2)
77+
return sigmoid(LinearAlgebra.dot(w3, x3))
78+
end
79+
80+
xs = (randn(10,10), randn(10,10), randn(10), rand(10))
81+
benchmark_driver!(neural_net, xs...)
82+
83+
####################################################################
84+
85+
println("done")

perf/p0.jl

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,26 @@ end
1313

1414

1515
# Case 1: Sample from the prior.
16-
1716
m = Turing.Core.TracedModel(gdemo(1.5, 2.), SampleFromPrior(), VarInfo())
18-
1917
f = m.evaluator[1];
20-
2118
args = m.evaluator[2:end];
2219

23-
@show "Directly call..."
20+
println("Directly call...")
2421
@btime f(args...)
2522
# (2.0, VarInfo (2 variables (μ, σ), dimension 2; logp: -6.162))
26-
27-
@show "TapedTask construction..."
23+
println("TapedTask construction...")
2824
t = @btime TapedTask(f, args...)
29-
# schedule(t.task) # work fine!
30-
# @show Libtask.result(t.tf)
31-
@show "Run a tape..."
25+
println("Run a tape...")
3226
@btime t.tf(args...)
3327

3428
# Case 2: SMC sampler
35-
3629
m = Turing.Core.TracedModel(gdemo(1.5, 2.), Sampler(SMC(50)), VarInfo());
37-
@show "Directly call..."
38-
@btime m.evaluator[1](m.evaluator[2:end]...)
30+
f = m.evaluator[1];
31+
args = m.evaluator[2:end];
3932

40-
@show "TapedTask construction..."
41-
t = @btime TapedTask(m.evaluator[1], m.evaluator[2:end]...);
42-
# schedule(t.task)
43-
# @show Libtask.result(t.tf.tape)
44-
@show "Run a tape..."
45-
@btime t.tf(m.evaluator[2:end]...)
33+
println("Directly call...")
34+
@btime f(args...)
35+
println("TapedTask construction...")
36+
t = @btime TapedTask(f, args...)
37+
println("Run a tape...")
38+
@btime t.tf(args...)

perf/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include("benchmark.jl")
12
include("p0.jl")
23
include("p1.jl")
34
include("p2.jl")

0 commit comments

Comments
 (0)