-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofiler.py
55 lines (44 loc) · 1.57 KB
/
profiler.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
from typing import Callable, Optional, Sequence, Any, Dict
import time
import torch
import numpy as np
class Profiler:
def __init__(self, func: Callable, name: Optional[str] = None) -> None:
self.func = func
self.name = func.__name__ if name is None else name
self.output = None
self.records = []
def execute(
self, args: Sequence[Any], times: int, warmup: int = 0, is_cuda: bool = False
):
if warmup == 0:
self.output = self.func(*args)
else:
for _ in range(warmup):
self.output = self.func(*args)
if is_cuda:
torch.cuda.synchronize()
for _ in range(times):
start = time.time()
self.func(*args)
if is_cuda:
torch.cuda.synchronize()
self.records.append(time.time() - start)
return self
def statistic(self) -> Dict:
times = len(self.records)
stat = {}
stat["times"] = times
if times == 0:
return stat
stat["avg"] = np.mean(self.records).item() * 100
stat["min"] = np.min(self.records).item() * 100
stat["max"] = np.max(self.records).item() * 100
stat["std"] = np.std(self.records).item() * 100
stat["sum"] = np.sum(self.records).item() * 100
return stat
def summary(self) -> None:
stat = self.statistic()
print(
f"'{self.name}' execute statistics:\n\tavg = {stat['avg']:.3f} ms, min/max/std = {stat['min']:.3f}/{stat['max']:.3f}/{stat['std']:.3f} ms"
)