-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetaTester.py
382 lines (300 loc) · 13.6 KB
/
metaTester.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import torch
import os
import sys
import numpy as np
from sim import load_trace
from sim import env
from maml_rl.policies import ActorNet, CriticNet
from maml_rl.sampler import MrcBatchSampler
S_INFO = 6 # bit_rate, buffer_size, next_chunk_size, bandwidth_measurement(throughput and time), chunk_til_video_end
S_LEN = 8 # take how many frames in the past
A_DIM = 6
ACTOR_LR_RATE = 0.0001
CRITIC_LR_RATE = 0.001
VIDEO_BIT_RATE = [300,750,1200,1850,2850,4300] # Kbps
BUFFER_NORM_FACTOR = 10.0
CHUNK_TIL_VIDEO_END_CAP = 48.0
M_IN_K = 1000.0
REBUF_PENALTY = 4.3 # 1 sec rebuffering -> 3 Mbps
SMOOTH_PENALTY = 1
DEFAULT_QUALITY = 1 # default video quality without agent
RANDOM_SEED = 42
RAND_RANGE = 1000
TEST_LOG_FOLDER = './saves/test_results/'
LOG_FILE = './saves/test_results/log_sim_rl'
TRAIN_TRACES = './meta_train_traces/'
TEST_TRACES = './test_sim_traces/'
def tester(policy, baseline):
np.random.seed(RANDOM_SEED)
assert len(VIDEO_BIT_RATE) == A_DIM
all_cooked_time, all_cooked_bw, all_file_names = load_trace.load_trace(TEST_TRACES)
net_env = env.Environment(all_cooked_time=all_cooked_time,
all_cooked_bw=all_cooked_bw)
log_path = LOG_FILE + '_' + all_file_names[net_env.trace_idx]
log_file = open(log_path, 'w')
with torch.no_grad():
time_stamp = 0
last_bit_rate = DEFAULT_QUALITY
bit_rate = DEFAULT_QUALITY
action_vec = np.zeros(A_DIM)
action_vec[bit_rate] = 1
s_batch = [np.zeros((S_INFO, S_LEN))]
a_batch = [action_vec]
r_batch = []
entropy_record = []
video_count = 0
while True: # serve video forever
# the action is from the last decision
# this is to make the framework similar to the real
delay, sleep_time, buffer_size, rebuf, \
video_chunk_size, next_video_chunk_sizes, \
end_of_video, video_chunk_remain = \
net_env.get_video_chunk(bit_rate)
time_stamp += delay # in ms
time_stamp += sleep_time # in ms
# reward is video quality - rebuffer penalty - smoothness
reward = VIDEO_BIT_RATE[bit_rate] / M_IN_K \
- REBUF_PENALTY * rebuf \
- SMOOTH_PENALTY * np.abs(VIDEO_BIT_RATE[bit_rate] -
VIDEO_BIT_RATE[last_bit_rate]) / M_IN_K
r_batch.append(reward)
last_bit_rate = bit_rate
# log time_stamp, bit_rate, buffer_size, reward
log_file.write((str(time_stamp / M_IN_K) + '\t' +
str(VIDEO_BIT_RATE[bit_rate]) + '\t' +
str(buffer_size) + '\t' +
str(rebuf) + '\t' +
str(video_chunk_size) + '\t' +
str(delay) + '\t' +
str(reward) + '\n'))
log_file.flush()
# retrieve previous state
if len(s_batch) == 0:
state = [np.zeros((S_INFO, S_LEN))]
else:
state = np.array(s_batch[-1], copy=True)
# dequeue history record
state = np.roll(state, -1, axis=1)
# this should be S_INFO number of terms
state[0, -1] = VIDEO_BIT_RATE[bit_rate] / float(np.max(VIDEO_BIT_RATE)) # last quality
state[1, -1] = buffer_size / BUFFER_NORM_FACTOR # 10 sec
state[2, -1] = float(video_chunk_size) / float(delay) / M_IN_K # kilo byte / ms
state[3, -1] = float(delay) / M_IN_K / BUFFER_NORM_FACTOR # 10 sec
state[4, :A_DIM] = np.array(next_video_chunk_sizes) / M_IN_K / M_IN_K # mega byte
state[5, -1] = np.minimum(video_chunk_remain, CHUNK_TIL_VIDEO_END_CAP) / float(CHUNK_TIL_VIDEO_END_CAP)
# compute action probability vector
action_categorical = policy(np.reshape(state, (1, 1, S_INFO, S_LEN)))
bit_rate = int(action_categorical.sample())
# Note: we need to discretize the probability into 1/RAND_RANGE steps,
# because there is an intrinsic discrepancy in passing single state and batch states
s_batch.append(state)
entropy_record.append(action_categorical.entropy())
if end_of_video:
log_file.write('\n')
log_file.close()
last_bit_rate = DEFAULT_QUALITY
bit_rate = DEFAULT_QUALITY # use the default action here
del s_batch[:]
del a_batch[:]
del r_batch[:]
action_vec = np.zeros(A_DIM)
action_vec[bit_rate] = 1
s_batch.append(np.zeros((S_INFO, S_LEN)))
a_batch.append(action_vec)
entropy_record = []
video_count += 1
if video_count >= len(all_file_names):
break
log_path = LOG_FILE + '_' + all_file_names[net_env.trace_idx]
log_file = open(log_path, 'w')
def noMetaTest(actor, critic, batchid):
# run test script
tester(actor, critic)
log_file = open('MetaTestLog.txt', 'a')
# append test performance to the log
rewards = []
test_log_files = os.listdir(TEST_LOG_FOLDER)
for test_log_file in test_log_files:
reward = []
with open(TEST_LOG_FOLDER + test_log_file, 'rb') as f:
for line in f:
parse = line.split()
try:
reward.append(float(parse[-1]))
except IndexError:
break
rewards.append(np.sum(reward[1:]))
rewards = np.array(rewards)
rewards_min = np.min(rewards)
rewards_5per = np.percentile(rewards, 5)
rewards_mean = np.mean(rewards)
rewards_median = np.percentile(rewards, 50)
rewards_95per = np.percentile(rewards, 95)
rewards_max = np.max(rewards)
# if epoch == 100:
# log_file.write("epoch" + '\t' +
# "rewards_min" + '\t' +
# "rewards_5per" + '\t' +
# "rewards_mean" + '\t' +
# "rewards_median" + '\t' +
# "rewards_95per" + '\t' +
# "rewards_max" + '\n')
log_file.write(str(batchid) + '\t' +
str(rewards_min) + '\t' +
str(rewards_5per) + '\t' +
str(rewards_mean) + '\t' +
str(rewards_median) + '\t' +
str(rewards_95per) + '\t' +
str(rewards_max) + '\n')
print(str(batchid) + '\t' +
str(rewards_min) + '\t' +
str(rewards_5per) + '\t' +
str(rewards_mean) + '\t' +
str(rewards_median) + '\t' +
str(rewards_95per) + '\t' +
str(rewards_max) + '\n')
log_file.flush()
def metaTester(policy, task):
np.random.seed(RANDOM_SEED)
assert len(VIDEO_BIT_RATE) == A_DIM
all_cooked_time, all_cooked_bw, all_file_names = load_trace.load_trace(TEST_TRACES)
net_env = env.Environment(all_cooked_time=all_cooked_time,
all_cooked_bw=all_cooked_bw)
log_path = LOG_FILE + '_' + all_file_names[net_env.trace_idx]
log_file = open(log_path, 'w')
with torch.no_grad():
time_stamp = 0
last_bit_rate = DEFAULT_QUALITY
bit_rate = DEFAULT_QUALITY
action_vec = np.zeros(A_DIM)
action_vec[bit_rate] = 1
s_batch = [np.zeros((S_INFO, S_LEN))]
a_batch = [action_vec]
r_batch = []
entropy_record = []
video_count = 0
while True: # serve video forever
# the action is from the last decision
# this is to make the framework similar to the real
delay, sleep_time, buffer_size, rebuf, \
video_chunk_size, next_video_chunk_sizes, \
end_of_video, video_chunk_remain = \
net_env.get_video_chunk(bit_rate)
time_stamp += delay # in ms
time_stamp += sleep_time # in ms
# reward is video quality - rebuffer penalty - smoothness
reward = VIDEO_BIT_RATE[bit_rate] / M_IN_K \
- REBUF_PENALTY * rebuf \
- SMOOTH_PENALTY * np.abs(VIDEO_BIT_RATE[bit_rate] -
VIDEO_BIT_RATE[last_bit_rate]) / M_IN_K
r_batch.append(reward)
last_bit_rate = bit_rate
# log time_stamp, bit_rate, buffer_size, reward
log_file.write((str(time_stamp / M_IN_K) + '\t' +
str(VIDEO_BIT_RATE[bit_rate]) + '\t' +
str(buffer_size) + '\t' +
str(rebuf) + '\t' +
str(video_chunk_size) + '\t' +
str(delay) + '\t' +
str(reward) + '\n'))
log_file.flush()
# retrieve previous state
if len(s_batch) == 0:
state = [np.zeros((S_INFO, S_LEN))]
else:
state = np.array(s_batch[-1], copy=True)
# dequeue history record
state = np.roll(state, -1, axis=1)
# this should be S_INFO number of terms
state[0, -1] = VIDEO_BIT_RATE[bit_rate] / float(np.max(VIDEO_BIT_RATE)) # last quality
state[1, -1] = buffer_size / BUFFER_NORM_FACTOR # 10 sec
state[2, -1] = float(video_chunk_size) / float(delay) / M_IN_K # kilo byte / ms
state[3, -1] = float(delay) / M_IN_K / BUFFER_NORM_FACTOR # 10 sec
state[4, :A_DIM] = np.array(next_video_chunk_sizes) / M_IN_K / M_IN_K # mega byte
state[5, -1] = np.minimum(video_chunk_remain, CHUNK_TIL_VIDEO_END_CAP) / float(CHUNK_TIL_VIDEO_END_CAP)
# compute action probability vector
action_categorical = policy(np.reshape(state, (1, 1, S_INFO, S_LEN)))
bit_rate = int(action_categorical.sample())
# Note: we need to discretize the probability into 1/RAND_RANGE steps,
# because there is an intrinsic discrepancy in passing single state and batch states
s_batch.append(state)
entropy_record.append(action_categorical.entropy())
if end_of_video:
log_file.write('\n')
log_file.close()
last_bit_rate = DEFAULT_QUALITY
bit_rate = DEFAULT_QUALITY # use the default action here
del s_batch[:]
del a_batch[:]
del r_batch[:]
action_vec = np.zeros(A_DIM)
action_vec[bit_rate] = 1
s_batch.append(np.zeros((S_INFO, S_LEN)))
a_batch.append(action_vec)
entropy_record = []
video_count += 1
if video_count >= len(all_file_names):
break
log_path = LOG_FILE + '_' + all_file_names[net_env.trace_idx]
log_file = open(log_path, 'w')
def metaTest(actor, critic, batchid, meta_batch_size, fast_bathc_size):
print("Meta Testing: ")
sampler = MrcBatchSampler('pensieve', batch_size=fast_bathc_size, train_folder=TRAIN_TRACES)
tasks = sampler.sample_tasks(num_tasks=meta_batch_size)
actor_state = actor.state_dict()
critic_state = critic.state_dict()
for task in tasks:
sampler.reset_task(task)
episodes = sampler.sample(actor)
critic.fit(episodes, CRITIC_LR_RATE, isnew=True)
actor.fit(episodes, critic, ACTOR_LR_RATE, isnew=True)
# run test script
tester(actor, critic)
log_file = open('MetaTestLog.txt', 'a')
# append test performance to the log
rewards = []
test_log_files = os.listdir(TEST_LOG_FOLDER)
for test_log_file in test_log_files:
reward = []
with open(TEST_LOG_FOLDER + test_log_file, 'rb') as f:
for line in f:
parse = line.split()
try:
reward.append(float(parse[-1]))
except IndexError:
break
rewards.append(np.sum(reward[1:]))
rewards = np.array(rewards)
rewards_min = np.min(rewards)
rewards_5per = np.percentile(rewards, 5)
rewards_mean = np.mean(rewards)
rewards_median = np.percentile(rewards, 50)
rewards_95per = np.percentile(rewards, 95)
rewards_max = np.max(rewards)
# if epoch == 100:
# log_file.write("epoch" + '\t' +
# "rewards_min" + '\t' +
# "rewards_5per" + '\t' +
# "rewards_mean" + '\t' +
# "rewards_median" + '\t' +
# "rewards_95per" + '\t' +
# "rewards_max" + '\
log_file.write(str(batchid) + '\t' +
str(task) + '\t' +
str(rewards_min) + '\t' +
str(rewards_5per) + '\t' +
str(rewards_mean) + '\t' +
str(rewards_median) + '\t' +
str(rewards_95per) + '\t' +
str(rewards_max) + '\n')
print(str(batchid) + '\t' +
str(task) + '\t' +
str(rewards_min) + '\t' +
str(rewards_5per) + '\t' +
str(rewards_mean) + '\t' +
str(rewards_median) + '\t' +
str(rewards_95per) + '\t' +
str(rewards_max) + '\n')
log_file.flush()
# actor.load_state_dict(actor_state)
# critic.load_state_dict(critic_state)