-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
178 lines (154 loc) · 5.9 KB
/
run.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
from singleton import Singleton
from resource_manager import ResourceManager
from random import randint
from itertools import product
from time import sleep
from rllite import SAC
import multiprocessing as mp
def run(lock, shared_eps_num, shared_eps_reward, hyper_param):
model = SAC(
env_name = 'Pendulum-v0',
load_dir = './ckpt/ckpt_'+str(hyper_param[0])+'_'+str(hyper_param[1]),
log_dir = './log/log_'+str(hyper_param[0])+'_'+str(hyper_param[1]),
buffer_size = 1e6,
seed = hyper_param[1],
max_episode_steps = 500, # manual set
batch_size = hyper_param[0],
discount = 0.99,
learning_starts = 500,
tau = 0.005,
save_eps_num = 100
)
timesteps = 0
total_timesteps = 1e5
max_eps_steps = 100
# train
while timesteps < total_timesteps:
episode_reward = 0
done = False
eps_steps = 0
obs = model.env.reset()
while not done and eps_steps < max_eps_steps:
action = model.predict(obs)
new_obs, reward, done, info = model.env.step(action)
model.replay_buffer.push(obs, action, reward, new_obs, done)
obs = new_obs
episode_reward += reward
eps_steps += 1
timesteps += 1
if timesteps > model.learning_starts :
model.train_step()
model.episode_num += 1
model.writer.add_scalar('episode_reward', episode_reward, model.episode_num)
lock.acquire()
shared_eps_num.value = model.episode_num
shared_eps_reward.value = episode_reward
lock.release()
class ATR(Singleton):
def __init__(self, hyper_params, max_num=9999, random=True):
self.hp_name = list(hyper_params.keys())
self.hp = self.get_hp(hyper_params)
self.max_num = max_num
self.random = random
self.resource_manager = ResourceManager(mem_limit=1, cpu_limit = 0.1, gpu_limit=0.5, max_instances=self.max_num)
self.waiting_pool = [item for item in self.hp]
self.finished_pool = []
self.working_pool = []
self.working_process = []
self.lock_list = []
self.shared_eps_num_list = []
self.shared_eps_reward_list = []
def get_hp(self, hyper_params):
hp_list = list(hyper_params.values())
if len(hp_list) < 2:
return hp_list
hp = hp_list.pop(0)
for i in range(len(hp_list)):
hp = list(product(hp, hp_list[i]))
return hp
def start(self):
while True:
self.auto_tune()
sleep(1.0)
def report(self):
self.resource_manager.report()
def auto_tune(self):
self.ask_result()
self.auto_kill()
self.auto_gen()
# for test
def ask_result(self):
if len(self.working_pool) == 0: return
for i in range(len(self.working_pool)):
lock = self.lock_list[i]
lock.acquire()
shared_eps_num = self.shared_eps_num_list[i].value
shared_eps_reward = self.shared_eps_reward_list[i].value
lock.release()
print(i, shared_eps_num, shared_eps_reward)
# for test
def auto_kill(self):
if len(self.working_pool) == 0:
if len(self.waiting_pool) == 0:
print('All Job Finished !')
return
index = -999
min_eps_reward = -99999
for i in range(len(self.working_pool)):
lock = self.lock_list[i]
lock.acquire()
shared_eps_num = self.shared_eps_num_list[i].value
shared_eps_reward = self.shared_eps_reward_list[i].value
lock.release()
if shared_eps_num > 1000:
if shared_eps_reward < min_eps_reward:
min_eps_reward = shared_eps_reward
index = i
if index < 0:
return
process = self.working_process.pop(index)
process.terminate()
hyper_param = self.working_pool.pop(index)
self.finished_pool.append(hyper_param)
self.lock_list.pop(index)
self.shared_eps_num_list.pop(index)
self.shared_eps_reward_list.pop(index)
def auto_gen(self):
while True:
if len(self.waiting_pool) == 0: return
if len(self.working_pool) >= self.max_num: return
if not self.resource_manager.get_memory_access(): return
if not self.resource_manager.get_cpu_access(): return
gpu_id = self.resource_manager.get_gpu_access()
if gpu_id < 0: return
if(self.random):
index = 0
if len(self.waiting_pool) > 1:
index = randint(0, len(self.waiting_pool)-1)
hyper_param = self.waiting_pool.pop(index)
self.working_pool.append(hyper_param)
else:
hyper_param = self.waiting_pool.pop(0)
self.working_pool.append(hyper_param)
self.create_process(hyper_param)
def create_process(self, hyper_param):
ctx = mp.get_context('spawn')
lock = ctx.Lock()
shared_eps_num = ctx.Value('l', 0)
shared_eps_reward = ctx.Value('d', 0.0)
process = ctx.Process(target=run, args=(lock, shared_eps_num, shared_eps_reward, hyper_param))
process.start()
self.lock_list.append(lock)
self.shared_eps_num_list.append(shared_eps_num)
self.shared_eps_reward_list.append(shared_eps_reward)
self.working_process.append(process)
print('Start:', hyper_param)
def listener(self, event):
if event.exception: print('The job crashed :(')
if __name__ == '__main__':
hyper_params = {
'batch_size':[32, 64, 128],
'seed':[1,2,3]
}
atr = ATR(hyper_params, max_num=4)
atr.start()