-
Notifications
You must be signed in to change notification settings - Fork 0
/
metareasoning.py
210 lines (161 loc) · 7.19 KB
/
metareasoning.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
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
207
208
209
210
#############################
### METAREASONING PROGRAM ###
#############################
# Import packages
import time
# Policy 1
def alg1(net_record, pause_duration_record, pause_adjustment_coef,
temp_record_CPU, threshold_temp, net_accuracies, max_accuracy) :
# Record neural network
net_record.append(net_record[-1])
net_record.append(net_record[-1])
# Assign pause duration value
pause_duration = pause_duration_record[-1] + pause_adjustment_coef*(temp_record_CPU[-1] - threshold_temp)
if pause_duration < 0 :
pause_duration = 0
# Pause
time.sleep(pause_duration)
# "Accuracy" here is actually precision, but I don't want to change every variable name"
# Calculate average expected precision across the last five frames
avg_accuracy = max_accuracy
sum_accuracy = 0
accuracy_window = 5
if len(net_record) > accuracy_window*2+1 :
for i in range(accuracy_window) :
working_net = net_record[-(i*2+1)]
sum_accuracy = sum_accuracy + net_accuracies[working_net].tolist()[0]
avg_accuracy = sum_accuracy/accuracy_window
return pause_duration, avg_accuracy
# Policy 2
def alg2(pause_duration_record, start_temp, temp_record_CPU, threshold_temp,
net_record, net_accuracies, net_durations, max_accuracy, strat, net_names, max_loop_length,
pause_adjustment_coef) :
# Pause
time.sleep(pause_duration_record[-1])
# Define quintile size
quintile = (threshold_temp - start_temp)/5
log_quintile = (threshold_temp - start_temp)/2
# Strategy 1 (gearshifting)
if strat == 1 :
k = net_names.index(net_record[-1])
if temp_record_CPU[-1] > threshold_temp :
if k != 4 :
net_record.append(net_names[k+1])
net_record.append(net_names[k+1])
else :
net_record.append(net_record[-1])
net_record.append(net_record[-1])
elif k != 0 :
net_record.append(net_names[k-1])
net_record.append(net_names[k-1])
else :
net_record.append(net_record[-1])
net_record.append(net_record[-1])
# Strategy 2 (linear)
if strat == 2 :
i = 0
while temp_record_CPU[-1] > (start_temp + quintile) :
i += 1
quintile = ((threshold_temp - start_temp) / 5) * i
if i == len(net_names)-1 : break
net_record.append(net_names[i])
net_record.append(net_names[i])
# Strategy 3 (logarithmic)
if strat == 3 :
i = 0
while temp_record_CPU[-1] > (start_temp + log_quintile) :
i += 1
log_quintile = log_quintile + (threshold_temp - (log_quintile + start_temp)) / 2
if i == len(net_names)-1 : break
net_record.append(net_names[i])
net_record.append(net_names[i])
# Assign next pause duration value
pause_duration = max_loop_length - net_durations[net_record[-1]].tolist()[0]
# Calculate average expected precision across the last five frames
avg_accuracy = max_accuracy
sum_accuracy = 0
accuracy_window = 5
if len(net_record) > accuracy_window*2+1 :
for i in range(accuracy_window) :
working_net = net_record[-(i*2+1)]
sum_accuracy = sum_accuracy + net_accuracies[working_net].tolist()[0]
avg_accuracy = sum_accuracy/accuracy_window
if (avg_accuracy == net_accuracies.min(axis=1).tolist()[0]) and (temp_record_CPU[-1] > threshold_temp) :
pause_duration = pause_duration + pause_duration_record[-1] + pause_adjustment_coef*(temp_record_CPU[-1] - threshold_temp)
print('contingency, pausing for {}'.format(pause_duration))
if pause_duration < 0 :
pause_duration = 0
return pause_duration, avg_accuracy
# Policy 3
def alg3(pause_duration_record, temp_record_CPU, start_temp, threshold_temp,
net_record, max_accuracy, net_accuracies, net_durations, max_throughput,
TAC, strat, net_names, pause_adjustment_coef) :
# Pause
time.sleep(pause_duration_record[-1])
quintile = (threshold_temp - start_temp)/5
log_quintile = (threshold_temp - start_temp)/2
if strat == 1 :
k = net_names.index(net_record[-1])
if temp_record_CPU[-1] > threshold_temp :
if k != 4 :
net_record.append(net_names[k+1])
net_record.append(net_names[k+1])
else :
net_record.append(net_record[-1])
net_record.append(net_record[-1])
elif k != 0 :
net_record.append(net_names[k-1])
net_record.append(net_names[k-1])
else :
net_record.append(net_record[-1])
net_record.append(net_record[-1])
if strat == 2 :
i = 0
while temp_record_CPU[-1] > (start_temp + quintile) :
i += 1
quintile = ((threshold_temp - start_temp) / 5) * i
if i == len(net_names)-1 : break
net_record.append(net_names[i])
net_record.append(net_names[i])
if strat == 3 :
i = 0
while temp_record_CPU[-1] > (start_temp + log_quintile) :
i += 1
log_quintile = log_quintile + (threshold_temp - (log_quintile + start_temp)) / 2
if i == len(net_names)-1 : break
net_record.append(net_names[i])
net_record.append(net_names[i])
# print('using network {}'.format(net_record[-1]))
avg_accuracy = max_accuracy
sum_accuracy = 0
accuracy_window_max = 5
# Calculate average expected precision across the last five frames
accuracy_window = int(len(net_record)/2)
if accuracy_window > accuracy_window_max : accuracy_window = accuracy_window_max
for i in range(accuracy_window) :
working_net = net_record[-(i*2+1)]
sum_accuracy = sum_accuracy + net_accuracies[working_net].tolist()[0]
avg_accuracy = sum_accuracy/accuracy_window
# print('Avg acc: {}'.format(avg_accuracy))
# Calculate desired throughput
desired_throughput = max_throughput - (max_accuracy - avg_accuracy)*TAC
if desired_throughput < 0 : desired_throughput = 0.01
# print('max throughput: {}'.format(max_throughput))
# print('max_accuracy: {}'.format(max_accuracy))
# print('desired throughput: {}'.format(desired_throughput))
desired_loop_length = 1/desired_throughput
# print('desired loop length: {}'.format(desired_loop_length))
# print('using net duration: {}'.format(net_durations[net_record[-1]].tolist()[0]))
pause_duration = desired_loop_length - net_durations[net_record[-1]].tolist()[0]
# print('using tac to adjust, pause for: {}'.format(pause_duration))
if (avg_accuracy == net_accuracies.min(axis=1).tolist()[0]) :
pause_duration = pause_duration + pause_duration_record[-1] + pause_adjustment_coef*(temp_record_CPU[-1] - threshold_temp)
print('contingency, pausing for {}'.format(pause_duration))
if pause_duration < 0 :
pause_duration = 0
# print('pause to low, setting to 0')
# else :
# pause_duration = 0
# # print('waiting for enough loops')
# ADD PAUSE
return pause_duration, avg_accuracy