@@ -5,8 +5,10 @@ using BenchmarkTools
55# ###################################################################
66
77function benchmark_driver! (f, x... ; f_displayname= string (f))
8+ x = (x... , nothing )
9+
810 println (" benchmarking $(f_displayname) ..." )
9- tf = Libtask. TapedFunction (f, x)
11+ tf = Libtask. TapedFunction (f, x... );
1012
1113 print (" Run Original Function:" )
1214 @btime $ f ($ (x). .. )
@@ -20,30 +22,48 @@ function benchmark_driver!(f, x...; f_displayname=string(f))
2022 print (" Run TapedFunction (compiled):" )
2123 @btime $ ctf ($ (x). .. )
2224 GC. gc ()
25+
26+ print (" Run TapedTask: " )
27+ x = (x[1 : end - 1 ]. .. , produce);
28+ # show the number of produce calls inside `f`
29+ f_task = (tf, x; verbose= false ) -> begin
30+ tt = TapedTask (tf, x... );
31+ c = 0
32+ while consume (tt)!= = nothing
33+ c+= 1
34+ end
35+ verbose && print (" #produce=" , c, " ; " );
36+ end
37+ f_task (tf, x; verbose= true ) # print #produce calls.
38+ @btime $ f_task ($ tf, $ x)
39+ GC. gc ()
2340end
2441
2542# ###################################################################
2643
2744
28- function rosenbrock (x)
45+ function rosenbrock (x, callback = nothing )
2946 i = x[2 : end ]
3047 j = x[1 : end - 1 ]
31- return sum ((1 .- j). ^ 2 + 100 * (i - j.^ 2 ). ^ 2 )
48+ ret = sum ((1 .- j). ^ 2 + 100 * (i - j.^ 2 ). ^ 2 )
49+ callback != = nothing && callback (ret)
50+ return ret
3251end
3352
3453x = rand (100000 )
3554benchmark_driver! (rosenbrock, x)
3655
3756# ###################################################################
3857
39- function ackley (x:: AbstractVector )
58+ function ackley (x:: AbstractVector , callback = nothing )
4059 a, b, c = 20.0 , - 0.2 , 2.0 * π
4160 len_recip = inv (length (x))
4261 sum_sqrs = zero (eltype (x))
4362 sum_cos = sum_sqrs
4463 for i in x
4564 sum_cos += cos (c* i)
4665 sum_sqrs += i^ 2
66+ callback != = nothing && callback (sum_sqrs)
4767 end
4868 return (- a * exp (b * sqrt (len_recip* sum_sqrs)) -
4969 exp (len_recip* sum_cos) + a + MathConstants. e)
@@ -54,11 +74,13 @@ benchmark_driver!(ackley, x)
5474
5575# ###################################################################
5676function generate_matrix_test (n)
57- return x -> begin
77+ return (x, callback = nothing ) -> begin
5878 # @assert length(x) == 2n^2 + n
5979 a = reshape (x[1 : n^ 2 ], n, n)
6080 b = reshape (x[n^ 2 + 1 : 2 n^ 2 ], n, n)
61- return log .((a * b) + a - b)
81+ ret = log .((a * b) + a - b)
82+ callback != = nothing && callback (ret)
83+ return ret
6284 end
6385end
6486
@@ -71,10 +93,12 @@ benchmark_driver!(matrix_test, x; f_displayname="matrix_test")
7193relu (x) = log .(1.0 .+ exp .(x))
7294sigmoid (n) = 1. / (1. + exp (- n))
7395
74- function neural_net (w1, w2, w3, x1)
96+ function neural_net (w1, w2, w3, x1, callback = nothing )
7597 x2 = relu (w1 * x1)
7698 x3 = relu (w2 * x2)
77- return sigmoid (LinearAlgebra. dot (w3, x3))
99+ ret = sigmoid (LinearAlgebra. dot (w3, x3))
100+ callback != = nothing && callback (ret)
101+ return ret
78102end
79103
80104xs = (randn (10 ,10 ), randn (10 ,10 ), randn (10 ), rand (10 ))
0 commit comments