@@ -5,8 +5,9 @@ using BenchmarkTools
5
5
# ###################################################################
6
6
7
7
function benchmark_driver! (f, x... ; f_displayname= string (f))
8
+ x = (x... , nothing )
8
9
println (" benchmarking $(f_displayname) ..." )
9
- tf = Libtask. TapedFunction (f, x)
10
+ tf = Libtask. TapedFunction (f, x... );
10
11
11
12
print (" Run Original Function:" )
12
13
@btime $ f ($ (x). .. )
@@ -20,30 +21,43 @@ function benchmark_driver!(f, x...; f_displayname=string(f))
20
21
print (" Run TapedFunction (compiled):" )
21
22
@btime $ ctf ($ (x). .. )
22
23
GC. gc ()
24
+
25
+ print (" Run TapedTask:" )
26
+ x = (x[1 : end - 1 ]. .. , produce)
27
+ tf = Libtask. TapedFunction (f, x... );
28
+ @btime begin tt = TapedTask ($ tf, $ x... ); while consume (tt)!= = nothing end ; end
29
+ # show the number of produce calls inside `f`
30
+ tt = TapedTask (tf, x... );
31
+ c = 0 ; while consume (tt)!= = nothing c+= 1 end ;
32
+ println (f_displayname, " : #produce=" , c);
33
+ GC. gc ()
23
34
end
24
35
25
36
# ###################################################################
26
37
27
38
28
- function rosenbrock (x)
39
+ function rosenbrock (x, callback = nothing )
29
40
i = x[2 : end ]
30
41
j = x[1 : end - 1 ]
31
- return sum ((1 .- j). ^ 2 + 100 * (i - j.^ 2 ). ^ 2 )
42
+ ret = sum ((1 .- j). ^ 2 + 100 * (i - j.^ 2 ). ^ 2 )
43
+ callback === nothing || callback (ret);
44
+ return ret
32
45
end
33
46
34
47
x = rand (100000 )
35
48
benchmark_driver! (rosenbrock, x)
36
49
37
50
# ###################################################################
38
51
39
- function ackley (x:: AbstractVector )
52
+ function ackley (x:: AbstractVector , callback = nothing )
40
53
a, b, c = 20.0 , - 0.2 , 2.0 * π
41
54
len_recip = inv (length (x))
42
55
sum_sqrs = zero (eltype (x))
43
56
sum_cos = sum_sqrs
44
57
for i in x
45
58
sum_cos += cos (c* i)
46
59
sum_sqrs += i^ 2
60
+ callback === nothing || callback (sum_sqrs);
47
61
end
48
62
return (- a * exp (b * sqrt (len_recip* sum_sqrs)) -
49
63
exp (len_recip* sum_cos) + a + MathConstants. e)
@@ -54,11 +68,13 @@ benchmark_driver!(ackley, x)
54
68
55
69
# ###################################################################
56
70
function generate_matrix_test (n)
57
- return x -> begin
71
+ return (x, callback = nothing ) -> begin
58
72
# @assert length(x) == 2n^2 + n
59
73
a = reshape (x[1 : n^ 2 ], n, n)
60
74
b = reshape (x[n^ 2 + 1 : 2 n^ 2 ], n, n)
61
- return log .((a * b) + a - b)
75
+ ret = log .((a * b) + a - b)
76
+ callback === nothing || callback (ret);
77
+ return ret
62
78
end
63
79
end
64
80
@@ -71,10 +87,12 @@ benchmark_driver!(matrix_test, x; f_displayname="matrix_test")
71
87
relu (x) = log .(1.0 .+ exp .(x))
72
88
sigmoid (n) = 1. / (1. + exp (- n))
73
89
74
- function neural_net (w1, w2, w3, x1)
90
+ function neural_net (w1, w2, w3, x1, callback = nothing )
75
91
x2 = relu (w1 * x1)
76
92
x3 = relu (w2 * x2)
77
- return sigmoid (LinearAlgebra. dot (w3, x3))
93
+ ret = sigmoid (LinearAlgebra. dot (w3, x3))
94
+ callback === nothing || callback (ret);
95
+ return ret
78
96
end
79
97
80
98
xs = (randn (10 ,10 ), randn (10 ,10 ), randn (10 ), rand (10 ))
0 commit comments