-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimer.py
163 lines (142 loc) · 5.68 KB
/
timer.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
import time
import json
from contextlib import contextmanager
from io import StringIO
import numpy as np
import torch
class Timer:
"""
Timer for PyTorch code
Comes in the form of a contextmanager:
Example:
>>> timer = Timer()
... for i in range(10):
... with timer("expensive operation"):
... x = torch.randn(100)
... print(timer.summary())
"""
def __init__(self, verbosity_level=1, skip_first=True, on_cuda=True):
self.verbosity_level = verbosity_level
self.log_fn = self.log_info
self.skip_first = skip_first
self.cuda_available = torch.cuda.is_available() and on_cuda
self.reduce_times = []
self.reset()
def reset(self):
"""Reset the timer"""
self.totals = {} # Total time per label
self.first_time = {} # First occurrence of a label (start time)
self.last_time = {} # Last occurence of a label (end time)
self.call_counts = {} # Number of times a label occurred
@contextmanager
def __call__(self, label, epoch=-1.0, verbosity=1):
# Don't measure this if the verbosity level is too high
if verbosity > self.verbosity_level:
yield
return
# Measure the time
self._cuda_sync()
start = time.time()
yield
self._cuda_sync()
end = time.time()
if label == "batch.reduce":
self.reduce_times.append(end - start)
# Update first and last occurrence of this label
if label not in self.first_time:
self.first_time[label] = start
self.last_time[label] = end
# Update the totals and call counts
if label not in self.totals and self.skip_first:
self.totals[label] = 0.0
del self.first_time[label]
self.call_counts[label] = 0
elif label not in self.totals and not self.skip_first:
self.totals[label] = end - start
self.call_counts[label] = 1
else:
self.totals[label] += end - start
self.call_counts[label] += 1
if self.call_counts[label] > 0:
# We will reduce the probability of logging a timing
# linearly with the number of time we have seen it.
# It will always be recorded in the totals, though.
if np.random.rand() < 1 / self.call_counts[label]:
self.log_fn("timer", {"epoch": epoch, "value": end - start}, {"event": label})
def summary(self):
"""
Return a summary in string-form of all the timings recorded so far
"""
if len(self.totals) > 0:
with StringIO() as buffer:
# total_avg_time = 0
total_time = 0
print(
"--- Timer summary -----------------------------------------------",
file=buffer,
)
print(
" Event | Count | Average time | Frac.",
file=buffer,
)
for event_label in sorted(self.totals):
total = self.totals[event_label]
count = self.call_counts[event_label]
if count == 0:
continue
avg_duration = total / count
total_runtime = self.last_time[event_label] - self.first_time[event_label]
runtime_percentage = 100 * total / total_runtime
# total_avg_time += avg_duration if "." not in event_label else 0
total_time += total if event_label == "batch" else 0
print(
f"- {event_label:30s} | {count:6d} | {avg_duration:11.5f}s | {runtime_percentage:5.1f}%",
file=buffer,
)
print(
"-----------------------------------------------------------------",
file=buffer,
)
event_label = "total_time"
print(
f"- {event_label:30s} | {count:6d} | {total_time:11.5f}s |",
file=buffer,
)
print(
"-----------------------------------------------------------------",
file=buffer,
)
return buffer.getvalue()
def save_summary(self, json_file_path):
data = {}
for event_label in sorted(self.totals):
total = self.totals[event_label]
count = self.call_counts[event_label]
if count == 0:
continue
avg_duration = total / count
data[event_label] = {
"label": event_label,
"average_duration": avg_duration,
"n_events": count,
"total_time": total,
}
# Uncomment to store reduce times
# data["reduce_times"] = self.reduce_times
with open(json_file_path, "w") as fp:
json.dump(data, fp)
def _cuda_sync(self):
"""Finish all asynchronous GPU computations to get correct timings"""
if torch.cuda.is_available():
torch.cuda.synchronize()
def log_info(self, name, values, tags={}):
value_list = []
for key in sorted(values.keys()):
value = values[key]
value_list.append(f"{key}:{value:7.3f}")
values = ", ".join(value_list)
tag_list = []
for key, tag in tags.items():
tag_list.append(f"{key}:{tag}")
tags = ", ".join(tag_list)
print("{name:20s} - {values} ({tags})".format(name=name, values=values, tags=tags))