This repository was archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiming_utils.py
More file actions
145 lines (118 loc) · 4.29 KB
/
timing_utils.py
File metadata and controls
145 lines (118 loc) · 4.29 KB
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
"""Utilities for timing functions.
"""
from time import time
import datetime
from functools import lru_cache
import os
import numpy as np
import pandas as pd
import tensorflow as tf
from matplotlib import pyplot as plt
from IPython import display
import qkeras
def get_time_info(filepath):
# get the time that this file was most recently modified
__mtime__ = int(os.path.getmtime(filepath))
# get the current time
__atime__ = int(time())
# convert __atime__ and __mtime__ to a human-readable format using datetime module
__atime__ = datetime.datetime.fromtimestamp(__atime__)
__mtime__ = datetime.datetime.fromtimestamp(__mtime__)
time_since_mod = __atime__ - __mtime__
# convert time_since_mod to a human-readable format using datetime module
time_since_mod = datetime.timedelta(seconds=time_since_mod.total_seconds())
# get basename of file
current_file = os.path.basename(filepath)
print(f"File {current_file} Updated. Last modified {time_since_mod} seconds ago")
def hardware_timeit(func, *, hardware, data_shapes, max_iterations=1, max_time=None):
assert hardware in ("/CPU:0", "/GPU:0", "/TPU:0")
max_time = float("inf") if max_time is None else max_time
res = []
for shape in data_shapes:
sub_res = {}
iterations = 0
total_time = 0
total_sq_time = 0
times = []
while total_time < max_time and iterations < max_iterations:
with tf.device(hardware):
x = tf.random.normal(shape)
start_time = time()
_ = func(x)
run_time = time() - start_time
total_time += run_time
total_sq_time += run_time**2
iterations += 1
times.append(run_time)
sub_res["iterations"] = len(times)
sub_res["avg_time"] = np.mean(times)
sub_res["time_std"] = np.std(times)
sub_res["median_time"] = np.median(times)
sub_res["shape"] = str(shape)
sub_res["size"] = np.prod(shape)
res.append(sub_res)
return pd.DataFrame(res)
@lru_cache()
def _quantized_bits_speed_test_data(
alt_quantized_bits,
*,
hardware,
data_shapes,
max_iterations=1,
max_time=None,
quantized_bits_kwargs=None,
):
kwargs = {
"hardware": hardware,
"data_shapes": data_shapes,
"max_iterations": max_iterations,
"max_time": max_time,
}
quantized_bits_kwargs = (
{} if quantized_bits_kwargs is None else dict(quantized_bits_kwargs)
)
baseline_times = hardware_timeit(
qkeras.quantized_bits(**quantized_bits_kwargs), **kwargs
)
alt_times = hardware_timeit(alt_quantized_bits(**quantized_bits_kwargs), **kwargs)
return baseline_times, alt_times
def quantized_bits_speed_tests(
*args, plot=False, error_bars=False, field="avg_time", **kwargs
):
baseline_times, alt_times = _quantized_bits_speed_test_data(*args, **kwargs)
if plot:
label = {"median_time": "Median Time", "avg_time": "Average Time"}.get(
field, field
)
plt.errorbar(
np.log(baseline_times["size"]),
baseline_times[field],
2 * baseline_times["time_std"] if error_bars else None,
label=f"Baseline {label}",
)
plt.errorbar(
np.log(alt_times["size"]),
alt_times[field],
2 * alt_times["time_std"] if error_bars else None,
label=f"New implementation {label}",
)
plt.xlabel("Log input size")
plt.ylabel(f"{label} (seconds)")
plt.title("Runtime for quantized_bits implementation")
plt.ylim(bottom=0)
plt.legend()
plt.show()
else:
print("Current implementation Times")
display(baseline_times)
print("Alternative implementation Times")
display(alt_times)
improvement = 1 - alt_times[field] / baseline_times[field]
print(f"\nAverage improvement: {improvement.mean():.2%}\n")
get_time_info(__file__)
if __name__ == "__main__":
def func(x):
return tf.linalg.svd(x)
data_shapes = [(100, 100), (1000, 1000), (10000, 10000)]
print(hardware_timeit(func, hardware="/CPU:0", data_shapes=data_shapes, max_time=1))
print(hardware_timeit(func, hardware="/GPU:0", data_shapes=data_shapes, max_time=1))