-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.py
205 lines (162 loc) · 5.33 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
""" https://github.com/flytocc/debug-timer
"""
import time
from collections import defaultdict, deque
from functools import partial
from typing import Callable
import numpy as np
class Timer(object):
"""
A timer which computes the time elapsed since the tic/toc of the timer.
"""
def __init__(self, window_size=20):
self.deque = deque(maxlen=window_size)
self.start_time = 0.
self.total_time = 0.
self.calls = 0
def tic(self):
# using time.time instead of time.clock because time time.clock
# does not normalize for multithreading
self.start_time = time.time()
def toc(self):
diff = time.time() - self.start_time
self.total_time += diff
self.calls += 1
self.deque.append(diff)
return diff
@property
def median(self):
return np.median(self.deque) if self.deque else 0.
@property
def avg(self):
return np.mean(self.deque) if self.deque else 0.
@property
def global_avg(self):
return self.total_time / self.calls
@property
def max(self):
return max(self.deque) if self.deque else 0.
@property
def value(self):
return self.deque[-1] if self.deque else 0.
class _DebugTimer(object):
"""
Track vital debug statistics.
Usage:
1. from timer import debug_timer
2. with debug_timer("timer1"):
code1
debug_timer.tic("timer2")
code2
debug_timer.toc("timer2")
debug_timer.timer3_tic()
code3
debug_timer.timer3_toc()
@debug_timer("timer4")
def func(*args, **kwargs):
code4
3. debug_timer.log()
"""
__TIMER__ = None
prefix = ""
window_size = 50
def __new__(cls, *args, **kwargs):
if cls.__TIMER__ is None:
cls.__TIMER__ = super(_DebugTimer, cls).__new__(cls)
return cls.__TIMER__
def __init__(self, num_warmup=0):
super(_DebugTimer, self).__init__()
self.num_warmup = num_warmup
self.context_stacks = []
self.calls = 0
self.timers = defaultdict(Timer)
self.set_sync_func(lambda: None)
self.set_window_size(self.window_size)
def __getattr__(self, name):
if name in self.__dict__:
return self.__dict__[name]
elif name.endswith("_tic"):
return lambda: self.tic(name[:-4])
elif name.endswith("_toc"):
return lambda: self.toc(name[:-4])
raise AttributeError(name)
def __call__(self, name_or_func):
if isinstance(name_or_func, str):
self.context_stacks.append(name_or_func)
return self
name = self.context_stacks.pop(-1)
def func_wrapper(*args, **kwargs):
with self(name):
return name_or_func(*args, **kwargs)
return func_wrapper
def __enter__(self):
name = self.context_stacks[-1]
self.tic(name)
return self
def __exit__(self, exc_type, exc_value, traceback):
name = self.context_stacks.pop(-1)
self.toc(name)
if exc_type is not None:
raise exc_value
def set_sync_func(self, func):
assert isinstance(func, Callable)
self.sync = func
def set_window_size(self, window_size):
assert isinstance(window_size, int)
assert len(self.timers) == 0
self.window_size = window_size
timer_obj = partial(Timer, window_size=self.window_size) \
if window_size > 0 else Timer
self.timers = defaultdict(timer_obj)
def reset_timer(self):
for timer in self.timers.values():
timer.reset()
def tic(self, name):
timer = self.timers[name]
self.sync()
timer.tic()
return timer
def toc(self, name):
timer = self.timers.get(name, None)
if timer is None:
raise ValueError(
f"Trying to toc a non-existent Timer which is named '{name}'!")
if self.calls >= self.num_warmup:
self.sync()
return timer.toc()
def log(self, logperiod=10, prefix="", log_func=print):
"""
Log the tracked statistics.
Eg.: | timer1: xxxs | timer2: xxxms | timer3: xxxms |
"""
self.calls += 1
if self.calls % logperiod == 0 and self.timers:
lines = [prefix or self.prefix]
for name, timer in self.timers.items():
avg_time = timer.avg if self.window_size > 0 else timer.global_avg
suffix = "s"
if avg_time < 0.01:
avg_time *= 1000
suffix = "ms"
lines.append(" {}: {:.3f}{} ".format(name, avg_time, suffix))
lines.append("")
log_func("|".join(lines))
debug_timer = _DebugTimer()
if __name__ == "__main__":
# debug_timer.set_sync_func(torch.cuda.synchronize)
@debug_timer('timer0')
def code():
s = 0
for i in range(100000):
s += i
return True
for iter_i in range(1000):
with debug_timer('timer1'):
code()
debug_timer.tic("timer2")
code()
debug_timer.toc("timer2")
debug_timer.timer3_tic()
code()
debug_timer.timer3_toc()
debug_timer.log(prefix=f"{iter_i}:")