-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimer.py
85 lines (67 loc) · 2.22 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
"""
Timing utilities, by Robert-Jan Bruintjes.
"""
import torch
import time
import numpy as np
import datetime
def start_timing(cuda=True):
if cuda:
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()
return (start_event, end_event)
else:
start_time = time.time()
return start_time
def end_timing(timing_data, cuda=True):
"""Returns time in seconds."""
if cuda:
start_event, end_event = timing_data
end_event.record()
torch.cuda.synchronize()
return start_event.elapsed_time(end_event) * 1000 # ms or *1000: us or s: / 1000.0
else:
start_time = timing_data
end_time = time.time()
elapsed_time = end_time - start_time
return elapsed_time
class PyTorchTimer(object):
def __init__(self, cuda=True, name=None, keep_n=10):
self.cuda = cuda
self.name = name
self.times = []
self.anomaly = False
self.keep = np.zeros((keep_n), dtype="float32")
self.keep_n = keep_n
self.keep_idx = 0
def write(self, time):
self.keep[self.keep_idx] = time
self.keep_idx = (self.keep_idx + 1) % self.keep_n
def __enter__(self):
self.timing = start_timing(cuda=self.cuda)
def __exit__(self, exc_type, exc_value, exc_traceback):
if not self.anomaly:
time = end_timing(self.timing, cuda=self.cuda)
self.write(time)
else:
self.anomaly = False
def start(self):
self.__enter__()
def stop(self):
self.__exit__(None, None, None)
def manual_entry(self, start_time, end_time):
if not self.anomaly:
elapsed_time = end_time - start_time
self.write(elapsed_time)
else:
self.anomaly = False
def mark_anomaly(self):
self.anomaly = True
def last(self):
return self.keep[(self.keep_idx - 1) % self.keep_n]
def mean(self):
return np.mean(self.keep)
def __str__(self):
name_display = f"{self.name}" if self.name is not None else "Time"
return f"Average time over {self.keep_n:d}: {name_display} (us): {self.mean():.4f}"