forked from tlkh/tf-metal-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unified_mem_benchmark.py
79 lines (67 loc) · 2.15 KB
/
unified_mem_benchmark.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--iterations", default=30, type=int,
help="Number of iterations to run within each benchmark")
parser.add_argument("--device1", default="/CPU:0", type=str)
parser.add_argument("--device2", default="/GPU:0", type=str)
args = parser.parse_args()
import os
import time
from tqdm import tqdm
import tensorflow as tf
@tf.function(experimental_autograph_options=tf.autograph.experimental.Feature.ALL)
def do_op(a, b):
with tf.device(args.device1):
x = a * b + b
with tf.device(args.device2):
x = tf.linalg.matmul(a, x)
with tf.device(args.device1):
x = a * x + b
with tf.device(args.device2):
x = tf.linalg.matmul(b, x)
with tf.device(args.device1):
x = a * b + x
with tf.device(args.device2):
x = tf.linalg.matmul(a, x)
with tf.device(args.device1):
x = a * b + x
with tf.device(args.device2):
x = tf.linalg.matmul(b, x)
return x
def benchmark_matmul(M, dtype=tf.float32, iterations=30):
# generate data and warm-up iteration
A = tf.random.normal([M, M], mean=0, stddev=1, dtype=dtype)
B = tf.random.normal([M, M], mean=0, stddev=1, dtype=dtype)
C = do_op(A, B)
C.numpy()
C = do_op(A, B)
C.numpy()
# run benchmark
st = time.time()
for _ in range(iterations+1):
C = do_op(A, B)
C.numpy()
et = time.time()
duration = (et-st)
return iterations/duration
fp16_matmul, fp32_matmul, fp64_matmul = [], [], []
fp16_tflops, fp32_tflops, fp64_tflops = [], [], []
M_list = [2048] * 30
print("\nStarting burn...\n")
burn_start = time.time()
for M in tqdm(M_list):
print("FP32", M, end=" : ")
ret = benchmark_matmul(M, dtype=tf.float32, iterations=args.iterations)
tflops = 4 * (ret * 2 * M**3 + 2*M*M)/ 1e12
fp32_matmul.append(ret)
fp32_tflops.append(tflops)
print(tflops)
#time.sleep(1)
burn_end = time.time()
print("\nFinished in", int(burn_end-burn_start), "seconds\n")
title = "Max TFLOPS achieved"
print("")
print(title)
print("="*len(title))
print("* FP32:", round(max(fp32_tflops),1), "TFLOPS")
print("")