From 765a2285091bc39c82ba056586953b6c87aa0a91 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 25 Mar 2022 19:18:16 +0000 Subject: [PATCH 1/7] add benchmarks for produce --- perf/benchmark.jl | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/perf/benchmark.jl b/perf/benchmark.jl index 692bb9de..4958b3e9 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -5,8 +5,9 @@ using BenchmarkTools #################################################################### function benchmark_driver!(f, x...; f_displayname=string(f)) + x = (x..., nothing) println("benchmarking $(f_displayname)...") - tf = Libtask.TapedFunction(f, x) + tf = Libtask.TapedFunction(f, x...); print(" Run Original Function:") @btime $f($(x)...) @@ -20,15 +21,27 @@ function benchmark_driver!(f, x...; f_displayname=string(f)) print(" Run TapedFunction (compiled):") @btime $ctf($(x)...) GC.gc() + + print(" Run TapedTask:") + x = (x[1:end-1]..., produce) + tf = Libtask.TapedFunction(f, x...); + @btime begin tt = TapedTask($tf, $x...); while consume(tt)!==nothing end; end + # show the number of produce calls inside `f` + tt = TapedTask(tf, x...); + c = 0; while consume(tt)!==nothing c+=1 end; + println(f_displayname, ": #produce=", c); + GC.gc() end #################################################################### -function rosenbrock(x) +function rosenbrock(x, callback=nothing) i = x[2:end] j = x[1:end-1] - return sum((1 .- j).^2 + 100*(i - j.^2).^2) + ret = sum((1 .- j).^2 + 100*(i - j.^2).^2) + callback === nothing || callback(ret); + return ret end x = rand(100000) @@ -36,7 +49,7 @@ benchmark_driver!(rosenbrock, x) #################################################################### -function ackley(x::AbstractVector) +function ackley(x::AbstractVector, callback=nothing) a, b, c = 20.0, -0.2, 2.0*π len_recip = inv(length(x)) sum_sqrs = zero(eltype(x)) @@ -44,6 +57,7 @@ function ackley(x::AbstractVector) for i in x sum_cos += cos(c*i) sum_sqrs += i^2 + callback === nothing || callback(sum_sqrs); end return (-a * exp(b * sqrt(len_recip*sum_sqrs)) - exp(len_recip*sum_cos) + a + MathConstants.e) @@ -54,11 +68,13 @@ benchmark_driver!(ackley, x) #################################################################### function generate_matrix_test(n) - return x -> begin + return (x, callback=nothing) -> 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) + ret = log.((a * b) + a - b) + callback === nothing || callback(ret); + return ret end end @@ -71,10 +87,12 @@ 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) +function neural_net(w1, w2, w3, x1, callback=nothing) x2 = relu(w1 * x1) x3 = relu(w2 * x2) - return sigmoid(LinearAlgebra.dot(w3, x3)) + ret = sigmoid(LinearAlgebra.dot(w3, x3)) + callback === nothing || callback(ret); + return ret end xs = (randn(10,10), randn(10,10), randn(10), rand(10)) @@ -82,4 +100,4 @@ benchmark_driver!(neural_net, xs...) #################################################################### -println("done") +println("done") \ No newline at end of file From cb311790ca79166b1ac500369ae1264e9d8b1376 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 25 Mar 2022 21:10:30 +0000 Subject: [PATCH 2/7] minor fixes. --- perf/benchmark.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/perf/benchmark.jl b/perf/benchmark.jl index 4958b3e9..d733515d 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -23,7 +23,7 @@ function benchmark_driver!(f, x...; f_displayname=string(f)) GC.gc() print(" Run TapedTask:") - x = (x[1:end-1]..., produce) + x = (x[1:end-1]..., produce); tf = Libtask.TapedFunction(f, x...); @btime begin tt = TapedTask($tf, $x...); while consume(tt)!==nothing end; end # show the number of produce calls inside `f` @@ -40,7 +40,7 @@ function rosenbrock(x, callback=nothing) i = x[2:end] j = x[1:end-1] ret = sum((1 .- j).^2 + 100*(i - j.^2).^2) - callback === nothing || callback(ret); + if callback !== nothing; callback(ret); end return ret end @@ -57,7 +57,7 @@ function ackley(x::AbstractVector, callback=nothing) for i in x sum_cos += cos(c*i) sum_sqrs += i^2 - callback === nothing || callback(sum_sqrs); + if callback !== nothing; produce(sum_sqrs); end end return (-a * exp(b * sqrt(len_recip*sum_sqrs)) - exp(len_recip*sum_cos) + a + MathConstants.e) @@ -73,7 +73,7 @@ function generate_matrix_test(n) a = reshape(x[1:n^2], n, n) b = reshape(x[n^2 + 1:2n^2], n, n) ret = log.((a * b) + a - b) - callback === nothing || callback(ret); + if callback !== nothing; callback(ret); end return ret end end @@ -91,7 +91,7 @@ function neural_net(w1, w2, w3, x1, callback=nothing) x2 = relu(w1 * x1) x3 = relu(w2 * x2) ret = sigmoid(LinearAlgebra.dot(w3, x3)) - callback === nothing || callback(ret); + if callback !== nothing; callback(ret); end return ret end From ff797a5b2587518dda4bec2635d97d7391f75f54 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Fri, 25 Mar 2022 22:13:03 +0000 Subject: [PATCH 3/7] minor tweaks --- perf/benchmark.jl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/perf/benchmark.jl b/perf/benchmark.jl index d733515d..02adf5ec 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -6,6 +6,7 @@ using BenchmarkTools function benchmark_driver!(f, x...; f_displayname=string(f)) x = (x..., nothing) + println("benchmarking $(f_displayname)...") tf = Libtask.TapedFunction(f, x...); @@ -22,14 +23,20 @@ function benchmark_driver!(f, x...; f_displayname=string(f)) @btime $ctf($(x)...) GC.gc() - print(" Run TapedTask:") + print(" Run TapedTask: ") x = (x[1:end-1]..., produce); - tf = Libtask.TapedFunction(f, x...); - @btime begin tt = TapedTask($tf, $x...); while consume(tt)!==nothing end; end # show the number of produce calls inside `f` + tf = Libtask.TapedFunction(f, x...); tt = TapedTask(tf, x...); c = 0; while consume(tt)!==nothing c+=1 end; - println(f_displayname, ": #produce=", c); + print("#produce=", c); + f_task = (tf, x) -> begin + # tf = Libtask.TapedFunction(f, x...); + tt = TapedTask(tf, x...); + while consume(tt)!==nothing + end; + end + @btime $f_task($f, $x) GC.gc() end From 3a2e468c661beede659e8fcff33f6e745d3f06cc Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Mon, 28 Mar 2022 14:47:05 +0100 Subject: [PATCH 4/7] Apply suggestions from code review Co-authored-by: Rik Huijzer --- perf/benchmark.jl | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/perf/benchmark.jl b/perf/benchmark.jl index 02adf5ec..e91a1ae7 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -28,7 +28,10 @@ function benchmark_driver!(f, x...; f_displayname=string(f)) # show the number of produce calls inside `f` tf = Libtask.TapedFunction(f, x...); tt = TapedTask(tf, x...); - c = 0; while consume(tt)!==nothing c+=1 end; + c = 0 + while consume(tt) !== nothing + c += 1 + end print("#produce=", c); f_task = (tf, x) -> begin # tf = Libtask.TapedFunction(f, x...); @@ -47,7 +50,7 @@ function rosenbrock(x, callback=nothing) i = x[2:end] j = x[1:end-1] ret = sum((1 .- j).^2 + 100*(i - j.^2).^2) - if callback !== nothing; callback(ret); end + callback !== nothing && callback(ret) return ret end @@ -64,7 +67,7 @@ function ackley(x::AbstractVector, callback=nothing) for i in x sum_cos += cos(c*i) sum_sqrs += i^2 - if callback !== nothing; produce(sum_sqrs); end + callback !== nothing && produce(sum_sqrs) end return (-a * exp(b * sqrt(len_recip*sum_sqrs)) - exp(len_recip*sum_cos) + a + MathConstants.e) @@ -80,7 +83,7 @@ function generate_matrix_test(n) a = reshape(x[1:n^2], n, n) b = reshape(x[n^2 + 1:2n^2], n, n) ret = log.((a * b) + a - b) - if callback !== nothing; callback(ret); end + callback !== nothing && callback(ret) return ret end end @@ -98,7 +101,7 @@ function neural_net(w1, w2, w3, x1, callback=nothing) x2 = relu(w1 * x1) x3 = relu(w2 * x2) ret = sigmoid(LinearAlgebra.dot(w3, x3)) - if callback !== nothing; callback(ret); end + callback !== nothing && callback(ret) return ret end From 172a357332850fe6fcdb19a236759ba630c46374 Mon Sep 17 00:00:00 2001 From: Hong Ge Date: Mon, 28 Mar 2022 14:55:45 +0100 Subject: [PATCH 5/7] minor updates. --- perf/benchmark.jl | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/perf/benchmark.jl b/perf/benchmark.jl index e91a1ae7..1d4b77f2 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -26,19 +26,15 @@ function benchmark_driver!(f, x...; f_displayname=string(f)) print(" Run TapedTask: ") x = (x[1:end-1]..., produce); # show the number of produce calls inside `f` - tf = Libtask.TapedFunction(f, x...); - tt = TapedTask(tf, x...); - c = 0 - while consume(tt) !== nothing - c += 1 - end - print("#produce=", c); - f_task = (tf, x) -> begin - # tf = Libtask.TapedFunction(f, x...); + f_task = (tf, x; verbose=false) -> begin tt = TapedTask(tf, x...); + c = 0 while consume(tt)!==nothing - end; + c+=1 + end + verbose && print("#produce=", c, "; "); end + f_task(f, x; verbose=true) # print #ßproduce calls. @btime $f_task($f, $x) GC.gc() end @@ -67,7 +63,7 @@ function ackley(x::AbstractVector, callback=nothing) for i in x sum_cos += cos(c*i) sum_sqrs += i^2 - callback !== nothing && produce(sum_sqrs) + callback !== nothing && callback(sum_sqrs) end return (-a * exp(b * sqrt(len_recip*sum_sqrs)) - exp(len_recip*sum_cos) + a + MathConstants.e) From a7505b2371763db9acb1da3294fd35821a4be69b Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Mon, 28 Mar 2022 16:41:24 +0100 Subject: [PATCH 6/7] Update benchmark.jl --- perf/benchmark.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/perf/benchmark.jl b/perf/benchmark.jl index 1d4b77f2..fdc75f85 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -34,7 +34,7 @@ function benchmark_driver!(f, x...; f_displayname=string(f)) end verbose && print("#produce=", c, "; "); end - f_task(f, x; verbose=true) # print #ßproduce calls. + f_task(tf, x; verbose=true) # print #produce calls. @btime $f_task($f, $x) GC.gc() end @@ -106,4 +106,4 @@ benchmark_driver!(neural_net, xs...) #################################################################### -println("done") \ No newline at end of file +println("done") From 8e02f7bccc05c8664934e7509f6111e15ffe59dc Mon Sep 17 00:00:00 2001 From: Hong Ge <3279477+yebai@users.noreply.github.com> Date: Tue, 29 Mar 2022 09:34:29 +0100 Subject: [PATCH 7/7] Update benchmark.jl --- perf/benchmark.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perf/benchmark.jl b/perf/benchmark.jl index fdc75f85..2e8de360 100644 --- a/perf/benchmark.jl +++ b/perf/benchmark.jl @@ -35,7 +35,7 @@ function benchmark_driver!(f, x...; f_displayname=string(f)) verbose && print("#produce=", c, "; "); end f_task(tf, x; verbose=true) # print #produce calls. - @btime $f_task($f, $x) + @btime $f_task($tf, $x) GC.gc() end