Spaces:
Runtime error
Runtime error
Upload utils/profiler.py with huggingface_hub
Browse files- utils/profiler.py +45 -0
utils/profiler.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from time import perf_counter as timer
|
2 |
+
from collections import OrderedDict
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
|
6 |
+
class Profiler:
|
7 |
+
def __init__(self, summarize_every=5, disabled=False):
|
8 |
+
self.last_tick = timer()
|
9 |
+
self.logs = OrderedDict()
|
10 |
+
self.summarize_every = summarize_every
|
11 |
+
self.disabled = disabled
|
12 |
+
|
13 |
+
def tick(self, name):
|
14 |
+
if self.disabled:
|
15 |
+
return
|
16 |
+
|
17 |
+
# Log the time needed to execute that function
|
18 |
+
if not name in self.logs:
|
19 |
+
self.logs[name] = []
|
20 |
+
if len(self.logs[name]) >= self.summarize_every:
|
21 |
+
self.summarize()
|
22 |
+
self.purge_logs()
|
23 |
+
self.logs[name].append(timer() - self.last_tick)
|
24 |
+
|
25 |
+
self.reset_timer()
|
26 |
+
|
27 |
+
def purge_logs(self):
|
28 |
+
for name in self.logs:
|
29 |
+
self.logs[name].clear()
|
30 |
+
|
31 |
+
def reset_timer(self):
|
32 |
+
self.last_tick = timer()
|
33 |
+
|
34 |
+
def summarize(self):
|
35 |
+
n = max(map(len, self.logs.values()))
|
36 |
+
assert n == self.summarize_every
|
37 |
+
print("\nAverage execution time over %d steps:" % n)
|
38 |
+
|
39 |
+
name_msgs = ["%s (%d/%d):" % (name, len(deltas), n) for name, deltas in self.logs.items()]
|
40 |
+
pad = max(map(len, name_msgs))
|
41 |
+
for name_msg, deltas in zip(name_msgs, self.logs.values()):
|
42 |
+
print(" %s mean: %4.0fms std: %4.0fms" %
|
43 |
+
(name_msg.ljust(pad), np.mean(deltas) * 1000, np.std(deltas) * 1000))
|
44 |
+
print("", flush=True)
|
45 |
+
|