-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimization.py
More file actions
206 lines (175 loc) · 7.81 KB
/
Copy pathoptimization.py
File metadata and controls
206 lines (175 loc) · 7.81 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import numpy as np
from opt import opti
from opt import optd
from plot import plot
from calc import calc
from scipy.optimize import curve_fit
from sklearn.linear_model import LinearRegression
import pandas as pd
import time
start = time.time()
# =========================
# Data
# =========================
freq_data = np.array([544, 683, 810, 936, 1063, 1202, 1328])
power_data = np.array([65, 76, 85, 92, 103, 127, 167])
perf_data = np.array([380, 470, 560, 640, 730, 810, 880])
pow_coe = np.array([1, 1.02, 1.02, 1.04, 1.04, 1.06, 1.08, 1.08])
perf_coe = np.array([1, 0.98, 0.98, 0.96, 0.96, 0.94, 0.92, 0.92])
def power_model(fre, c1, d1):
return c1 * fre**3 + d1
lower_bound = 544
upper_bound = 1328
N = 8 #Number of GPUs
perf_perc = 0.5
interval = 0
batch_size = 128
size = 1
#size = 128 # Batch size per GPU
#batch_size = int(interval * perf_perc * max(perf_data) * N/size)*size # Total batch size to process in the given interval at the given performance percentage
boot_en = 0 # Energy consumed during bootup for each GPU (in Joules)
n = int(perf_perc * 8) + 1 # Number of active GPUs based on performance coefficient
#n=N
#Linear fits
a, b = np.polyfit(freq_data, perf_data, 1)
#c, d = np.polyfit(freq_data, power_data, 1)
c, d = curve_fit(power_model, freq_data, power_data)[0]
# freq = np.array([
# 1440, 1432, 1425, 1417, 1410, 1402, 1395, 1387, 1380, 1372,
# 1365, 1357, 1350, 1342, 1335, 1327, 1320, 1312, 1305, 1297,
# 1290, 1282, 1275, 1267, 1260, 1252, 1245, 1237, 1230, 1222,
# 1215, 1207, 1200, 1192, 1185, 1177, 1170, 1162, 1155, 1147,
# 1140, 1132, 1125, 1117, 1110, 1102, 1095, 1087, 1080, 1072,
# 1065, 1057, 1050, 1042, 1035, 1027, 1020, 1012, 1005, 997,
# 990, 982, 975, 967, 960, 952, 945, 937, 930, 922,
# 915, 907, 900, 892, 885, 877, 870, 862, 855, 847,
# 840, 832, 825, 817, 810, 802, 795, 787, 780, 772,
# 765, 757, 750, 742, 735, 727, 720, 712, 705, 697,
# 690, 682, 675, 667, 660, 652, 645, 637, 630, 622,
# 615, 607, 600, 592, 585, 577, 570, 562, 555, 547,
# 540, 532, 525, 517, 510, 502, 495, 487, 480, 472,
# 465, 457, 450, 442, 435, 427, 420, 412, 405, 397,
# 390, 382, 375, 367, 360, 352, 345, 337, 330, 322,
# 315, 307, 300, 292, 285, 277, 270, 262, 255, 247,
# 240, 232, 225, 217, 210, 202, 195, 187, 180, 172,
# 165, 157, 150, 142, 135
# ])
# freq_data = np.array([135, 210, 225, 240, 255, 270, 285, 352, 450, 457,
# 472, 487, 495, 517, 540, 570, 660, 720, 787, 900, 1005,
# 1080, 1222, 1320, 1440])
# power_data = np.array([50.66, 51.11, 51.50, 55.68, 56.49, 56.38, 55.92, 56, 55.97, 56.86,
# 56.90, 59.03, 58.87, 58.65, 59.98, 59.69, 59.38, 60.72, 60.34, 61.49,
# 65.20, 67.26, 75.96, 80.82, 90.36])
# perf_data = np.array([24.17, 25.27, 25.3, 41.47, 42.07, 41.67, 41.67, 41.83, 42.07, 43.17,
# 45.7, 44.87, 46.17, 46.2, 46.83, 46.1, 46.57, 47.03, 47.17, 46.3, 46.77,
# 47.07, 46.37, 47, 47.13])
# perf_token_data = np.array([193.33, 202.13, 202.4, 331.73, 336.53, 333.33, 333.33, 334.67,
# 336.53, 345.33, 365.6, 358.93, 369.33, 369.6, 374.67, 368.8, 372.53,
# 376.27, 377.33, 370.4, 374.13, 376.53, 370.93, 376, 377.07])
# freq_data = freq_data[3:]
# power_data = power_data[3:]
# perf_data = perf_data[3:]
# perf_token_data = perf_token_data[3:]
# freq = np.sort(freq)
# freq = freq[14:]
# regions = [
# #freq_data < 240,
# (freq_data >= 240) & (freq_data < 450),
# (freq_data >= 450) & (freq_data < 495),
# freq_data >= 495
# ]
# perf_models=[]
# for region in regions:
# X_region = freq_data[region].reshape(-1, 1)
# y_region = perf_token_data[region]
# model = LinearRegression()
# model.fit(X_region, y_region)
# perf_models.append(model)
# def power_model(freq, c, d):
# return c * freq**3 + d
# (c, d), _ = curve_fit(power_model, freq_data, power_data)
# power = np.zeros(len(freq))
# perf = np.zeros(len(freq))
# for i in range(len(freq)):
# power[i] = c*freq[i]**3+d
# freq_regions = [
# #freq_data < 240,
# (freq >= 240) & (freq < 450),
# (freq >= 450) & (freq < 495),
# freq >= 495
# ]
# for region, model in zip(freq_regions, perf_models):
# perf[region] = model.predict(freq[region].reshape(-1,1))
# perf_min = min(perf)
# power_min = min(power)
# freq = np.append(freq, 0)
# perf = np.append(perf, 0)
# power = np.append(power, 43.6)
# N = 8
# perf_perc = 0.2
# interval = 60
# size = 16*8
# batch_size = int(interval * perf_perc * max(perf) * N/size)*size
# boot_en=0
# n=int(perf_perc*N)+1
# results = np.zeros((N-n+1, 11, 2)) # Store efficiency and energy for each N and weight
results = np.zeros((N-n+1, 1, 2)) # Store efficiency and energy for each N and weight
rows = []
for i in range(n, N+1):
print(f"\n--- Optimization with N={i} ---")
for j in range(5,6):#range(0,11):
print(f"\n--- Optimization with weight={j/10:.1f} ---")
batch, frequency, runtime = opti(
i, gpu_num = N, a=a, b=b, c=c, d=d, weight=j/10, batch_size=batch_size, size=size,
freq_min=lower_bound, freq_max=upper_bound,
perf_min=min(perf_data), perf_max=max(perf_data),
power_min=min(power_data), power_max=max(power_data), perf_perc=perf_perc, interval=interval, power_idle=25)
efficiency, energy = calc(i, a, b, c, d, batch, frequency, power_idle=25, boot_en=boot_en, gpu_count=N, interval=interval)
# batch, frequency, runtime, efficiency, energy = optd(i,freq_set=freq,perf_set=perf,
# pow_set=power,weight=j/10,
# batch_size=batch_size,
# size=size, freq_max=max(freq),
# perf_min=perf_min,
# perf_max=max(perf),power_min=power_min,
# power_max=max(power),
# perf_perc= perf_perc,interval= interval,
# power_idle=43.6)
results[i-n, j-5, 0] = efficiency
results[i-n, j-5, 1] = energy
rows.append({
"N": i,
"weight": j/10,
"efficiency": efficiency,
"energy": energy,
"batch": [batch[k] for k in range(i)],
"freq": [frequency[k] for k in range(i)],
"runtime": runtime
})
print(f"Batch sizes: {[batch[i] for i in range(i)]}")
print(f"Frequencies: {[frequency[i] for i in range(i)]}")
print(f"Time: {runtime} seconds")
end = time.time()
rows.append({
"N": "",
"weight": "",
"efficiency": "",
"energy": "",
"batch": "",
"freq": "",
"runtime": end - start
})
# Save results to CSV
df = pd.DataFrame(rows)
df.to_csv(f"./final_fix_interval/results_{N}_{perf_perc}_{interval}.csv", index=False)
# Plotting results
# For N=8, plot efficiency and energy against weight
# x_data = np.arange(0, 1.1, 0.1)
# y_data_efficiency = results[N-n, :, 0]
# y_data_energy = results[N-n, :, 1]
# plot(x_data, y_data_efficiency, y_data_energy, x_label="Weight", y_label_left="Efficiency(image/J)", y_label_right="Energy(J)", title="Efficiency and Energy vs Weight for N=8")
# # For weight=0.5, plot efficiency and energy against N
# x_data = np.arange(n, N+1)
# y_data_efficiency = results[:, 4, 0]
# y_data_energy = results[:, 4, 1]
# plot(x_data, y_data_efficiency, y_data_energy, x_label="N", y_label_left="Efficiency(image/J)", y_label_right="Energy(J)", title="Efficiency and Energy vs N for Weight=0.5")
print(f"Total time taken: {end - start} seconds")