-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmarking.py
More file actions
140 lines (116 loc) · 4.93 KB
/
Benchmarking.py
File metadata and controls
140 lines (116 loc) · 4.93 KB
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
import time
from tqdm import tqdm
import numpy as np
import pandas as pd
from numpy.random import RandomState
from Datasets import split_transpose
import tracemalloc
RNG_SEED = 6553
class Benchmark:
def __init__(self, X, y, n_runs=1000, warmup=100, mem_runs=100, test_sz=0.3, rng_seed=RNG_SEED, same_splits=True):
self.X = X
self.y = y
self.n = n_runs
self.warmup = warmup
self.mem_runs = mem_runs
self.test_sz = test_sz
self.det = same_splits
if self.det:
self.rng_seed = rng_seed
else:
self.rng = RandomState(rng_seed)
self.data = dict()
print("Benching params:")
print("Total runs:", self.warmup+self.mem_runs+self.n)
print("Warmup runs:", self.warmup)
print("Peak Memory usage runs:", self.mem_runs)
print("Running time runs:", self.n)
approx_test_sz = int(self.y.size * self.test_sz)
print("Train size rows (approx):", self.y.size - approx_test_sz)
print("Test size rows (approx):", approx_test_sz)
print("Test size fraction:", self.test_sz)
def bench(self, model_class, **kwargs):
name = model_class.__name__
# train_time, test_time, accuracy
time_data = np.empty((self.n, 3), dtype=float)
# train_peak_mem, test_peak_mem
mem_data = np.empty((self.mem_runs, 2), dtype=float)
rng = RandomState(self.rng_seed) if self.det else self.rng
for i in range(self.warmup):
# Instantiate model with error check for unsupported parameters
model = model_class(**kwargs)
# Generate current train-test split
X_train, X_test, y_train, y_test = split_transpose(
self.X, self.y,
test_size=self.test_sz,
random_state=rng
)
# Run training and prediction (timing or memory measurement not recorded)
model.fit(X_train, y_train)
model.predict(X_test)
for i in tqdm(range(self.mem_runs), total=self.mem_runs, desc=f"{name} (MEM)"):
model = model_class(**kwargs)
X_train, X_test, y_train, y_test = split_transpose(
self.X, self.y,
test_size=self.test_sz,
random_state=rng
)
tracemalloc.start()
t1 = time.perf_counter()
model.fit(X_train, y_train)
t2 = time.perf_counter()
_, train_peak = tracemalloc.get_traced_memory()
tracemalloc.reset_peak()
model.predict(X_test)
t3 = time.perf_counter()
_, test_peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
mem_data[i,] = (
train_peak / (1024 * 1024),
test_peak / (1024 * 1024)
)
for i in tqdm(range(self.n), total=self.n, desc=f"{name} (TIME)"):
model = model_class(**kwargs)
X_train, X_test, y_train, y_test = split_transpose(
self.X, self.y,
test_size=self.test_sz,
random_state=rng
)
t1 = time.perf_counter()
model.fit(X_train, y_train)
t2 = time.perf_counter()
preds = model.predict(X_test)
t3 = time.perf_counter()
time_data[i,] = (
(t2 - t1) * 1000,
(t3 - t2) * 1000,
(y_test.flatten() == preds.flatten()).mean()
)
self.data[name] = (time_data, mem_data)
def summary(self, baseline=None):
aux = []
for name, (time_data, mem_data) in self.data.items():
result = {
'model': name,
'train_median_ms': np.median(time_data[:, 0]),
'train_std_ms': time_data[:, 0].std(),
'test_median_ms': np.median(time_data[:, 1]),
'test_std_ms': time_data[:, 1].std(),
'mean_accuracy': time_data[:, 2].mean(),
'train_mem_median_mb': np.median(mem_data[:, 0]),
'train_mem_std_mb': mem_data[:, 0].std(),
'test_mem_median_mb': np.median(mem_data[:, 1]),
'test_mem_std_mb': mem_data[:, 1].std()
}
aux.append(result)
df = pd.DataFrame(aux).set_index('model')
if baseline is not None and baseline in self.data:
df['train_speedup'] = df.loc[baseline,
'train_median_ms'] / df['train_median_ms']
df['test_speedup'] = df.loc[baseline,
'test_median_ms'] / df['test_median_ms']
df['train_mem_reduction'] = df.loc[baseline,
'train_mem_median_mb'] / df['train_mem_median_mb']
df['test_mem_reduction'] = df.loc[baseline,
'test_mem_median_mb'] / df['test_mem_median_mb']
return df