-
Notifications
You must be signed in to change notification settings - Fork 6
/
matmul.py
168 lines (135 loc) · 5.16 KB
/
matmul.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# Copyright 2018 Jason Zaman <jason AT perfinion.com> 2018
# Licensed under Apache-2
#
# Additional changes for project Thoth by Thoth team.
#
import logging
import os
import sys
import numpy as np
import json
import tensorflow as tf
from timeit import time
_LOGGER = logging.getLogger(__name__)
# Datatype used.
# Options:
# float16
# float32
# float64
# int32
_ARGS_DTYPE = os.getenv('MATMUL_DTYPE', 'float32')
if _ARGS_DTYPE not in ('float16', 'float32', 'float64', 'int32'):
raise ValueError("Unknown MATMUL_DTYPE")
print("DTYPE set to %s" % _ARGS_DTYPE, file=sys.stderr)
# Run on CPU or GPU.
# Options:
# cpu
# gpu
_ARGS_DEVICE = os.getenv('MATMUL_DEVICE', 'cpu')
print("DEVICE set to %s" % _ARGS_DEVICE, file=sys.stderr)
# Number of repetitions.
# Options:
# A positive integer.
_ARGS_REPS = int(os.getenv('MATMUL_REPS', 50))
print("REPS set to %s" % _ARGS_REPS, file=sys.stderr)
# Number of calls to the op per repetition.
# Options:
# A positive integer.
_ARGS_MINI_BATCH = int(os.getenv("MATMUL_MINI_BATCH", 40))
print("MINI_BATCH set to %s" % _ARGS_MINI_BATCH, file=sys.stderr)
# Size of matrix.
# Options:
# A positive integer.
_ARGS_MATRIX_SIZE = int(os.getenv('MATMUL_MATRIX_SIZE', 512))
print("MATRIX size set to %s" % _ARGS_MATRIX_SIZE, file=sys.stderr)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
if _ARGS_DEVICE == 'cpu':
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
def _get_aicoe_tensorflow_build_info():
"""Try to obtain information of AICoE TensorFlow builds.
Do whatever is needed in this function, if there is an error, the reported build information is
set to None (e.g. AICoE TensorFlow is not installed and such).
"""
try:
path = os.path.dirname(os.path.dirname(tf.__file__))
build_info_path = os.path.join(path, 'tensorflow-' + tf.__version__ + '.dist-info', 'build_info.json')
with open(build_info_path, 'r') as build_info_file:
build_info = json.load(build_info_file)
return build_info
except Exception:
_LOGGER.exception("Failed to obtain AICoE specific build information for TensorFlow")
return None
def _get_tensorflow_build_info():
"""Get tensorflow build info provided by tensorflow 2.3 and above."""
try:
return tf.sysconfig.get_build_info()
except AttributeError:
return None
def bench_v1(n: int):
times = []
tf.reset_default_graph()
with tf.device("/%s:0" % (_ARGS_DEVICE)):
matrix1 = tf.Variable(tf.ones((n, n), dtype=_ARGS_DTYPE))
matrix2 = tf.Variable(tf.ones((n, n), dtype=_ARGS_DTYPE))
product = tf.matmul(matrix1, matrix2)
config = tf.ConfigProto()
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
# warmup
sess.run(product.op)
for i in range(_ARGS_REPS):
start = time.monotonic()
for j in range(_ARGS_MINI_BATCH):
sess.run(product.op)
times.append(time.monotonic() - start)
times_ms = 1000 * np.array(times) # in seconds, convert to ms
elapsed_ms = np.median(times_ms)
ops = n ** 3 + (n - 1) * n ** 2 * _ARGS_MINI_BATCH # n^2*(n-1) additions, n^3 multiplications
rate = ops / elapsed_ms / 10 ** 6 # in GFLOPS. (/ milli / 10**6) == (/ 10 ** 9)
print('%d x %d matmul took: \t%.4f ms,\t %.2f GFLOPS' % (n, n, elapsed_ms, rate,), file=sys.stderr)
return rate, elapsed_ms
def bench_v2(n: int):
times = []
with tf.device("/%s:0" % (_ARGS_DEVICE)):
matrix1 = tf.Variable(tf.ones((n, n), dtype=_ARGS_DTYPE))
matrix2 = tf.Variable(tf.ones((n, n), dtype=_ARGS_DTYPE))
for i in range(_ARGS_REPS):
start = time.monotonic()
for j in range(_ARGS_MINI_BATCH):
product = tf.matmul(matrix1, matrix2)
times.append(time.monotonic() - start)
times_ms = 1000 * np.array(times) # in seconds, convert to ms
elapsed_ms = np.median(times_ms)
ops = n ** 3 + (n - 1) * n ** 2 * _ARGS_MINI_BATCH # n^2*(n-1) additions, n^3 multiplications
rate = ops / elapsed_ms / 10 ** 6 # in GFLOPS. (/ milli / 10**6) == (/ 10 ** 9)
print('%d x %d matmul took: \t%.4f ms,\t %.2f GFLOPS' % (n, n, elapsed_ms, rate,), file=sys.stderr)
return rate, elapsed_ms
def main():
np.set_printoptions(suppress=True)
tf_version = tf.__version__
print("# Version: %s, path: %s" % (tf_version, tf.__path__), file=sys.stderr)
if int(tf_version[0]) >= 2:
rate, elapsed = bench_v2(_ARGS_MATRIX_SIZE)
else:
rate, elapsed = bench_v1(_ARGS_MATRIX_SIZE)
result = {
"component": "tensorflow",
"name": "PiMatmul",
"@parameters": {
"dtype": _ARGS_DTYPE,
"device": _ARGS_DEVICE,
"reps": _ARGS_REPS,
"mini_batch": _ARGS_MINI_BATCH,
"matrix_size": _ARGS_MATRIX_SIZE,
},
"@result": {
"rate": rate,
"elapsed": elapsed,
},
"tensorflow_aicoe_buildinfo": _get_aicoe_tensorflow_build_info(),
"tensorflow_upstream_buildinfo": _get_tensorflow_build_info(),
}
json.dump(result, sys.stdout, indent=2)
if __name__ == '__main__':
main()