-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
129 lines (107 loc) · 3.88 KB
/
model.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
import pandas as pd
import numpy as np
import pickle
from sklearn.linear_model import Lasso
from cluster import GPU_NAME, CORE_BASE, MEM_BASE
df = pd.read_csv("csvs/%s-dvfs-real-Performance-Power.csv" % GPU_NAME, header = 0)
def get_ci(samples):
mean = np.mean(samples)
std = np.std(samples, ddof=1)
z = 1.96 # 95%
se = std /np.sqrt(len(samples))
lcb = mean - z * se
ucb = mean + z * se
return lcb, ucb
def LR(X, y, alpha = 0.05):
#lin = Lasso(alpha=0.0001,precompute=True,max_iter=1000, positive=True, random_state=9999, selection='random', fit_intercept=False)
lin = Lasso(alpha=0.01, positive=True, fit_intercept=False)
lin.fit(X,y)
#print(lin.coef_, lin.intercept_)
w = lin.coef_
#w = np.array([lin.intercept_, lin.coef_[0], lin.coef_[1]])
#print(w)
#inv_item = np.linalg.inv((np.dot(X.T, X)) + alpha * np.identity(X.shape[1]))
#w = np.dot((inv_item @ X.T), y)
return w
def main():
kernels = df['appName'].drop_duplicates()
kernels.sort_values(inplace=True)
para_dict = {}
vp = 0.5
time_err = 0
power_err = 0
time_mape = 0
power_mape = 0
p_basic = []
gamma = []
cg = []
print("Benchmarks:")
for kernel in kernels:
tmp_df = df[df.appName == kernel]
coreFs = tmp_df["coreF"].values * 1.0 / CORE_BASE
coreVs = (coreFs - vp) ** 2 * 2 + vp
memFs = tmp_df["memF"].values * 1.0 / MEM_BASE
# fitting performance
x1 = 1 / coreFs
x2 = 1 / memFs
X = np.stack((np.ones(x1.shape[0]), x1, x2)).T
y = tmp_df["time/ms"].values
#print(X, y)
tw = LR(X, y)
t0 = tw[0] if tw[0] > 0 else 0.0 # t0 < 0.0 is impossible
D = tw[1] + tw[2]
delta = tw[1] / D
print("--%s: t0: %f, D: %f, delta: %f." % (kernel, t0, D, delta))
rec_y = D * (delta * x1 + (1 - delta) * x2) + t0
#rec_y = tw.predict(X)
time_err += np.mean((y - rec_y) ** 2)
time_mape += np.mean(np.abs(y - rec_y)/y)
# fitting power
VFs = coreVs ** 2 * coreFs
X = np.stack((np.ones(memFs.shape[0]), memFs, VFs)).T
y = tmp_df["power/W"].values
#print(X, y)
pw = LR(X, y)
p0 = pw[0]
gamma = pw[1]
cg = pw[2]
rec_y = p0 + gamma*memFs + cg*VFs
#rec_y = pw.predict(X)
power_err += np.mean((y - rec_y) ** 2)
power_mape += np.mean(np.abs(y - rec_y)/y)
para_dict[kernel] = {"p_star":(p0+gamma+cg), "p0":p0, "gamma":gamma, "cg":cg, "t_star":(t0+D), "t0":t0, "D":D, "delta":delta}
print("Error Measurement:")
print("--RMSE of performance:", time_err / len(kernels))
print("--RMSE of power:", power_err / len(kernels))
print("--MAPE of performance: %f%%" % (time_mape / len(kernels)*100))
print("--MAPE of power: %f%%" % (power_mape / len(kernels)*100))
#print(para_dict)
p0s = [v["p0"] for v in para_dict.values()]
p_stars = [v["p_star"] for v in para_dict.values()]
gammas = [v["gamma"] for v in para_dict.values()]
cgs = [v["cg"] for v in para_dict.values()]
t0s = [v["t0"] for v in para_dict.values()]
t_stars = [v["t_star"] for v in para_dict.values()]
Ds = [v["D"] for v in para_dict.values()]
deltas = [v["delta"] for v in para_dict.values()]
p0s.sort()
p_stars.sort()
gammas.sort()
cgs.sort()
t0s.sort()
t_stars.sort()
Ds.sort()
deltas.sort()
print("Range:")
print("--p0", get_ci(p0s))
print("--p_star", get_ci(p_stars))
print("--gamma", get_ci(gammas))
print("--cg", get_ci(cgs))
print("--t0", get_ci(t0s))
print("--t_star", get_ci(t_stars))
print("--D", get_ci(Ds))
print("--delta", get_ci(deltas))
with open("apps.pkl", "wb") as fp:
pickle.dump(para_dict, fp, protocol = pickle.HIGHEST_PROTOCOL)
if __name__ == '__main__':
main()