|
| 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") |
0 commit comments