-
Notifications
You must be signed in to change notification settings - Fork 39
/
matmul_bench.py
52 lines (39 loc) · 1.8 KB
/
matmul_bench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import time
import numpy as np
import tensorflow as tf
from gemm_op import xnor_gemm
N = 4096
N_RUNS = 5
print("Running for %d runs with size %d" % (N_RUNS,N))
A = tf.placeholder(tf.float32, [N, N])
B = tf.placeholder(tf.float32, [N, N])
xnor_gemm = xnor_gemm(A, B)
matmul = tf.matmul(A, B)
# Re-use a for benchmarking on GPU w/only 4GB memory
a_T = tf.sign(tf.random_normal(shape=[N, N], seed=1))
b_T = tf.sign(tf.random_normal(shape=[N, N], seed=2))
xnor_timings = np.zeros(N_RUNS)
base_timings = np.zeros(N_RUNS)
with tf.Session() as sess:
a = sess.run(a_T)
b = sess.run(b_T)
for i in range(N_RUNS):
########### benchmark xnor ############
start_time = time.time()
xnor_gemm_result = sess.run(xnor_gemm, feed_dict={A: a, B: a})
xnor_timings[i] = time.time() - start_time
print("xnor_gemm %d took %f" % (i, xnor_timings[i]))
print(xnor_gemm_result)
#######################################
for i in range(N_RUNS):
########### benchmark matmul ##########
start_time = time.time()
matmul_result = sess.run(matmul, feed_dict={A: a, B: a})
base_timings[i] = time.time() - start_time
print("matmul %d took %f" % (i, base_timings[i]))
print(matmul_result)
#######################################
print("Avg XNOR execution time over %d runs: %f +/- %f" % (N_RUNS-1, xnor_timings[1:].mean(), xnor_timings[1:].std()))
print("Avg MatMul execution time over %d runs: %f +/- %f" % (N_RUNS-1, base_timings[1:].mean(), base_timings[1:].std()))
print("Med XNOR execution time over %d runs: %f +/- %f" % (N_RUNS-1, np.median(xnor_timings[1:]), xnor_timings[1:].std()))
print("Med MatMul execution time over %d runs: %f +/- %f" % (N_RUNS-1, np.median(base_timings[1:]), base_timings[1:].std()))