forked from DeNA/HandyRL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
executable file
·636 lines (521 loc) · 26.4 KB
/
train.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# Copyright (c) 2020 DeNA Co., Ltd.
# Licensed under The MIT License [see LICENSE for details]
# training
import os
import time
import copy
import threading
import random
import bz2
import pickle
import warnings
import queue
from collections import deque
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.distributions as dist
import torch.optim as optim
import psutil
from .environment import prepare_env, make_env
from .util import map_r, bimap_r, trimap_r, rotate
from .model import to_torch, to_gpu, ModelWrapper
from .losses import compute_target
from .connection import MultiProcessJobExecutor
from .worker import WorkerCluster, WorkerServer
def make_batch(episodes, args):
"""Making training batch
Args:
episodes (Iterable): list of episodes
args (dict): training configuration
Returns:
dict: PyTorch input and target tensors
Note:
Basic data shape is (B, T, P, ...) .
(B is batch size, T is time length, P is player count)
"""
obss, datum = [], []
def replace_none(a, b):
return a if a is not None else b
for ep in episodes:
moments_ = sum([pickle.loads(bz2.decompress(ms)) for ms in ep['moment']], [])
moments = moments_[ep['start'] - ep['base']:ep['end'] - ep['base']]
players = list(moments[0]['observation'].keys())
if not args['turn_based_training']: # solo training
players = [random.choice(players)]
# template for padding
obs_zeros = map_r(moments[0]['observation'][moments[0]['turn'][0]], lambda o: np.zeros_like(o))
amask_zeros = np.zeros_like(moments[0]['action_mask'][moments[0]['turn'][0]])
# data that is changed by training configuration
if args['turn_based_training'] and not args['observation']:
obs = [[m['observation'][m['turn'][0]]] for m in moments]
prob = np.array([[[m['selected_prob'][m['turn'][0]]]] for m in moments])
act = np.array([[m['action'][m['turn'][0]]] for m in moments], dtype=np.int64)[..., np.newaxis]
amask = np.array([[m['action_mask'][m['turn'][0]]] for m in moments])
else:
obs = [[replace_none(m['observation'][player], obs_zeros) for player in players] for m in moments]
prob = np.array([[[replace_none(m['selected_prob'][player], 1.0)] for player in players] for m in moments])
act = np.array([[replace_none(m['action'][player], 0) for player in players] for m in moments], dtype=np.int64)[..., np.newaxis]
amask = np.array([[replace_none(m['action_mask'][player], amask_zeros + 1e32) for player in players] for m in moments])
# reshape observation
obs = rotate(rotate(obs)) # (T, P, ..., ...) -> (P, ..., T, ...) -> (..., T, P, ...)
obs = bimap_r(obs_zeros, obs, lambda _, o: np.array(o))
# datum that is not changed by training configuration
v = np.array([[replace_none(m['value'][player], [0]) for player in players] for m in moments], dtype=np.float32).reshape(len(moments), len(players), -1)
rew = np.array([[replace_none(m['reward'][player], [0]) for player in players] for m in moments], dtype=np.float32).reshape(len(moments), len(players), -1)
ret = np.array([[replace_none(m['return'][player], [0]) for player in players] for m in moments], dtype=np.float32).reshape(len(moments), len(players), -1)
oc = np.array([ep['outcome'][player] for player in players], dtype=np.float32).reshape(1, len(players), -1)
emask = np.ones((len(moments), 1, 1), dtype=np.float32) # episode mask
tmask = np.array([[[m['selected_prob'][player] is not None] for player in players] for m in moments], dtype=np.float32)
omask = np.array([[[m['observation'][player] is not None] for player in players] for m in moments], dtype=np.float32)
progress = np.arange(ep['start'], ep['end'], dtype=np.float32)[..., np.newaxis] / ep['total']
# pad each array if step length is short
batch_steps = args['burn_in_steps'] + args['forward_steps']
if len(tmask) < batch_steps:
pad_len_b = args['burn_in_steps'] - (ep['train_start'] - ep['start'])
pad_len_a = batch_steps - len(tmask) - pad_len_b
obs = map_r(obs, lambda o: np.pad(o, [(pad_len_b, pad_len_a)] + [(0, 0)] * (len(o.shape) - 1), 'constant', constant_values=0))
prob = np.pad(prob, [(pad_len_b, pad_len_a), (0, 0), (0, 0), (0, 0)], 'constant', constant_values=1)
v = np.concatenate([np.pad(v, [(pad_len_b, 0), (0, 0), (0, 0)], 'constant', constant_values=0), np.tile(oc, [pad_len_a, 1, 1])])
act = np.pad(act, [(pad_len_b, pad_len_a), (0, 0), (0, 0), (0, 0)], 'constant', constant_values=0)
rew = np.pad(rew, [(pad_len_b, pad_len_a), (0, 0), (0, 0)], 'constant', constant_values=0)
ret = np.pad(ret, [(pad_len_b, pad_len_a), (0, 0), (0, 0)], 'constant', constant_values=0)
emask = np.pad(emask, [(pad_len_b, pad_len_a), (0, 0), (0, 0)], 'constant', constant_values=0)
tmask = np.pad(tmask, [(pad_len_b, pad_len_a), (0, 0), (0, 0)], 'constant', constant_values=0)
omask = np.pad(omask, [(pad_len_b, pad_len_a), (0, 0), (0, 0)], 'constant', constant_values=0)
amask = np.pad(amask, [(pad_len_b, pad_len_a), (0, 0), (0, 0), (0, 0)], 'constant', constant_values=1e32)
progress = np.pad(progress, [(pad_len_b, pad_len_a), (0, 0)], 'constant', constant_values=1)
obss.append(obs)
datum.append((prob, v, act, oc, rew, ret, emask, tmask, omask, amask, progress))
obs = to_torch(bimap_r(obs_zeros, rotate(obss), lambda _, o: np.array(o)))
prob, v, act, oc, rew, ret, emask, tmask, omask, amask, progress = [to_torch(np.array(val)) for val in zip(*datum)]
return {
'observation': obs,
'selected_prob': prob,
'value': v,
'action': act, 'outcome': oc,
'reward': rew, 'return': ret,
'episode_mask': emask,
'turn_mask': tmask, 'observation_mask': omask,
'action_mask': amask,
'progress': progress,
}
def forward_prediction(model, hidden, batch, args):
"""Forward calculation via neural network
Args:
model (torch.nn.Module): neural network
hidden: initial hidden state (..., B, P, ...)
batch (dict): training batch (output of make_batch() function)
Returns:
tuple: batch outputs of neural network
"""
observations = batch['observation'] # (..., B, T, P or 1, ...)
batch_shape = batch['action'].size()[:3] # (B, T, P or 1)
if hidden is None:
# feed-forward neural network
obs = map_r(observations, lambda o: o.flatten(0, 2)) # (..., B * T * P or 1, ...)
outputs = model(obs, None)
outputs = map_r(outputs, lambda o: o.unflatten(0, batch_shape)) # (..., B, T, P or 1, ...)
else:
# sequential computation with RNN
outputs = {}
for t in range(batch_shape[1]):
obs = map_r(observations, lambda o: o[:, t].flatten(0, 1)) # (..., B * P or 1, ...)
omask_ = batch['observation_mask'][:, t]
omask = map_r(hidden, lambda h: omask_.view(*h.size()[:2], *([1] * (h.dim() - 2))))
hidden_ = bimap_r(hidden, omask, lambda h, m: h * m) # (..., B, P, ...)
if args['turn_based_training'] and not args['observation']:
hidden_ = map_r(hidden_, lambda h: h.sum(1)) # (..., B * 1, ...)
else:
hidden_ = map_r(hidden_, lambda h: h.flatten(0, 1)) # (..., B * P, ...)
if t < args['burn_in_steps']:
model.eval()
with torch.no_grad():
outputs_ = model(obs, hidden_)
else:
if not model.training:
model.train()
outputs_ = model(obs, hidden_)
outputs_ = map_r(outputs_, lambda o: o.unflatten(0, (batch_shape[0], batch_shape[2]))) # (..., B, P or 1, ...)
for k, o in outputs_.items():
if k == 'hidden':
next_hidden = o
else:
outputs[k] = outputs.get(k, []) + [o]
hidden = trimap_r(hidden, next_hidden, omask, lambda h, nh, m: h * (1 - m) + nh * m)
outputs = {k: torch.stack(o, dim=1) for k, o in outputs.items() if o[0] is not None}
for k, o in outputs.items():
if k == 'policy':
o = o.view(*o.size()[:3], *batch['action_mask'].size()[-2:])
o = o.mul(batch['turn_mask'].unsqueeze(-1))
if o.size(2) > 1 and batch_shape[2] == 1: # turn-alternating batch
o = o.sum(2, keepdim=True) # gather turn player's policies
outputs[k] = o - batch['action_mask']
else:
# mask valid target values and cumulative rewards
outputs[k] = o.mul(batch['observation_mask'])
return outputs
def compose_losses(outputs, log_selected_policies, total_advantages, targets, batch, args):
"""Caluculate loss value
Returns:
tuple: losses and statistic values and the number of training data
"""
tmasks = batch['turn_mask'].unsqueeze(-1).repeat(1, 1, 1, log_selected_policies.size(-2), 1)
omasks = batch['observation_mask']
losses = {}
dcnt = tmasks.sum().item()
losses['p'] = (-log_selected_policies * total_advantages).mul(tmasks).sum()
if 'value' in outputs:
losses['v'] = ((outputs['value'] - targets['value']) ** 2).mul(omasks).sum() / 2
if 'return' in outputs:
losses['r'] = F.smooth_l1_loss(outputs['return'], targets['return'], reduction='none').mul(omasks).sum()
entropy = dist.Categorical(logits=outputs['policy']).entropy().mul(tmasks.squeeze(-1))
losses['ent'] = entropy.sum()
base_loss = losses['p'] + losses.get('v', 0) + losses.get('r', 0)
entropy_loss = entropy.mul(1 - batch['progress'].unsqueeze(-1) * (1 - args['entropy_regularization_decay'])).sum() * -args['entropy_regularization']
losses['total'] = base_loss + entropy_loss
return losses, dcnt
def compute_loss(batch, model, hidden, args):
outputs = forward_prediction(model, hidden, batch, args)
if args['burn_in_steps'] > 0:
batch = map_r(batch, lambda v: v[:, args['burn_in_steps']:] if v.size(1) > 1 else v)
outputs = map_r(outputs, lambda v: v[:, args['burn_in_steps']:])
actions = batch['action']
emasks = batch['episode_mask']
clip_rho_threshold, clip_c_threshold = 1.0, 1.0
log_selected_b_policies = torch.log(torch.clamp(batch['selected_prob'], 1e-16, 1)) * emasks.unsqueeze(-1)
log_selected_t_policies = F.log_softmax(outputs['policy'], dim=-1).gather(-1, actions) * emasks.unsqueeze(-1)
# thresholds of importance sampling
log_rhos = log_selected_t_policies.detach() - log_selected_b_policies
rhos = torch.exp(log_rhos)
clipped_rhos = torch.clamp(rhos, 0, clip_rho_threshold)
cs = torch.clamp(rhos, 0, clip_c_threshold)
outputs_nograd = {k: o.detach() for k, o in outputs.items()}
if 'value' in outputs_nograd:
values_nograd = outputs_nograd['value']
if args['turn_based_training'] and values_nograd.size(2) == 2: # two player zerosum game
values_nograd_opponent = -torch.stack([values_nograd[:, :, 1], values_nograd[:, :, 0]], dim=2)
values_nograd = (values_nograd + values_nograd_opponent) / (batch['observation_mask'].sum(dim=2, keepdim=True) + 1e-8)
outputs_nograd['value'] = values_nograd * emasks + batch['outcome'] * (1 - emasks)
# compute targets and advantage
targets = {}
advantages = {}
value_args = outputs_nograd.get('value', None), batch['outcome'], None, args['lambda'], 1, clipped_rhos, cs
return_args = outputs_nograd.get('return', None), batch['return'], batch['reward'], args['lambda'], args['gamma'], clipped_rhos, cs
targets['value'], advantages['value'] = compute_target(args['value_target'], *value_args)
targets['return'], advantages['return'] = compute_target(args['value_target'], *return_args)
if args['policy_target'] != args['value_target']:
_, advantages['value'] = compute_target(args['policy_target'], *value_args)
_, advantages['return'] = compute_target(args['policy_target'], *return_args)
# compute policy advantage
total_advantages = clipped_rhos * sum(advantages.values()).unsqueeze(-1)
return compose_losses(outputs, log_selected_t_policies, total_advantages, targets, batch, args)
class Batcher:
def __init__(self, args, episodes):
self.args = args
self.episodes = episodes
self.executor = MultiProcessJobExecutor(self._worker, self._selector(), self.args['num_batchers'])
def _selector(self):
while True:
yield [self.select_episode() for _ in range(self.args['batch_size'])]
def _worker(self, conn, bid):
print('started batcher %d' % bid)
while True:
episodes = conn.recv()
batch = make_batch(episodes, self.args)
conn.send(batch)
print('finished batcher %d' % bid)
def run(self):
self.executor.start()
def select_episode(self):
while True:
ep_count = min(len(self.episodes), self.args['maximum_episodes'])
ep_idx = random.randrange(ep_count)
accept_rate = 1 - (ep_count - 1 - ep_idx) / ep_count
if random.random() < accept_rate:
break
ep = self.episodes[ep_idx]
turn_candidates = 1 + max(0, ep['steps'] - self.args['forward_steps']) # change start turn by sequence length
train_st = random.randrange(turn_candidates)
st = max(0, train_st - self.args['burn_in_steps'])
ed = min(train_st + self.args['forward_steps'], ep['steps'])
st_block = st // self.args['compress_steps']
ed_block = (ed - 1) // self.args['compress_steps'] + 1
ep_minimum = {
'args': ep['args'], 'outcome': ep['outcome'],
'moment': ep['moment'][st_block:ed_block],
'base': st_block * self.args['compress_steps'],
'start': st, 'end': ed, 'train_start': train_st, 'total': ep['steps'],
}
return ep_minimum
def batch(self):
return self.executor.recv()
class Trainer:
def __init__(self, args, model):
self.episodes = deque()
self.args = args
self.gpu = torch.cuda.device_count()
self.model = model
self.default_lr = 3e-8
self.data_cnt_ema = self.args['batch_size'] * self.args['forward_steps']
self.params = list(self.model.parameters())
lr = self.default_lr * self.data_cnt_ema
self.optimizer = optim.Adam(self.params, lr=lr, weight_decay=1e-5) if len(self.params) > 0 else None
self.steps = 0
self.batcher = Batcher(self.args, self.episodes)
self.update_flag = False
self.update_queue = queue.Queue(maxsize=1)
self.wrapped_model = ModelWrapper(self.model)
self.trained_model = self.wrapped_model
if self.gpu > 1:
self.trained_model = nn.DataParallel(self.wrapped_model)
def update(self):
self.update_flag = True
model, steps = self.update_queue.get()
return model, steps
def train(self):
if self.optimizer is None: # non-parametric model
time.sleep(0.1)
return self.model
batch_cnt, data_cnt, loss_sum = 0, 0, {}
if self.gpu > 0:
self.trained_model.cuda()
self.trained_model.train()
while data_cnt == 0 or not self.update_flag:
batch = self.batcher.batch()
batch_size = batch['value'].size(0)
player_count = batch['value'].size(2)
hidden = self.wrapped_model.init_hidden([batch_size, player_count])
if self.gpu > 0:
batch = to_gpu(batch)
hidden = to_gpu(hidden)
losses, dcnt = compute_loss(batch, self.trained_model, hidden, self.args)
self.optimizer.zero_grad()
losses['total'].backward()
nn.utils.clip_grad_norm_(self.params, 4.0)
self.optimizer.step()
batch_cnt += 1
data_cnt += dcnt
for k, l in losses.items():
loss_sum[k] = loss_sum.get(k, 0.0) + l.item()
self.steps += 1
print('loss = %s' % ' '.join([k + ':' + '%.3f' % (l / data_cnt) for k, l in loss_sum.items()]))
self.data_cnt_ema = self.data_cnt_ema * 0.8 + data_cnt / (1e-2 + batch_cnt) * 0.2
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.default_lr * self.data_cnt_ema / (1 + self.steps * 1e-5)
self.model.cpu()
self.model.eval()
return copy.deepcopy(self.model)
def run(self):
print('waiting training')
while len(self.episodes) < self.args['minimum_episodes']:
time.sleep(1)
if self.optimizer is not None:
self.batcher.run()
print('started training')
while True:
model = self.train()
self.update_flag = False
self.update_queue.put((model, self.steps))
print('finished training')
class Learner:
def __init__(self, args, net=None, remote=False):
train_args = args['train_args']
env_args = args['env_args']
train_args['env'] = env_args
args = train_args
self.args = args
random.seed(args['seed'])
self.env = make_env(env_args)
eval_modify_rate = (args['update_episodes'] ** 0.85) / args['update_episodes']
self.eval_rate = max(args['eval_rate'], eval_modify_rate)
self.shutdown_flag = False
self.flags = set()
# trained datum
self.model_epoch = self.args['restart_epoch']
self.model = net if net is not None else self.env.net()
if self.model_epoch > 0:
self.model.load_state_dict(torch.load(self.model_path(self.model_epoch)), strict=False)
# generated datum
self.generation_results = {}
self.num_episodes = 0
self.num_returned_episodes = 0
# evaluated datum
self.results = {}
self.results_per_opponent = {}
self.num_results = 0
# multiprocess or remote connection
self.worker = WorkerServer(args) if remote else WorkerCluster(args)
# thread connection
self.trainer = Trainer(args, self.model)
def model_path(self, model_id):
return os.path.join('models', str(model_id) + '.pth')
def latest_model_path(self):
return os.path.join('models', 'latest.pth')
def update_model(self, model, steps):
# get latest model and save it
print('updated model(%d)' % steps)
self.model_epoch += 1
self.model = model
os.makedirs('models', exist_ok=True)
torch.save(model.state_dict(), self.model_path(self.model_epoch))
torch.save(model.state_dict(), self.latest_model_path())
def feed_episodes(self, episodes):
# analyze generated episodes
for episode in episodes:
if episode is None:
continue
for p in episode['args']['player']:
model_id = episode['args']['model_id'][p]
outcome = episode['outcome'][p]
n, r, r2 = self.generation_results.get(model_id, (0, 0, 0))
self.generation_results[model_id] = n + 1, r + outcome, r2 + outcome ** 2
self.num_returned_episodes += 1
if self.num_returned_episodes % 100 == 0:
print(self.num_returned_episodes, end=' ', flush=True)
# store generated episodes
self.trainer.episodes.extend([e for e in episodes if e is not None])
mem_percent = psutil.virtual_memory().percent
mem_ok = mem_percent <= 95
maximum_episodes = self.args['maximum_episodes'] if mem_ok else int(len(self.trainer.episodes) * 95 / mem_percent)
if not mem_ok and 'memory_over' not in self.flags:
warnings.warn("memory usage %.1f%% with buffer size %d" % (mem_percent, len(self.trainer.episodes)))
self.flags.add('memory_over')
while len(self.trainer.episodes) > maximum_episodes:
self.trainer.episodes.popleft()
def feed_results(self, results):
# store evaluation results
for result in results:
if result is None:
continue
for p in result['args']['player']:
model_id = result['args']['model_id'][p]
res = result['result'][p]
n, r, r2 = self.results.get(model_id, (0, 0, 0))
self.results[model_id] = n + 1, r + res, r2 + res ** 2
if model_id not in self.results_per_opponent:
self.results_per_opponent[model_id] = {}
opponent = result['opponent']
n, r, r2 = self.results_per_opponent[model_id].get(opponent, (0, 0, 0))
self.results_per_opponent[model_id][opponent] = n + 1, r + res, r2 + res ** 2
def update(self):
# call update to every component
print()
print('epoch %d' % self.model_epoch)
if self.model_epoch not in self.results:
print('win rate = Nan (0)')
else:
def output_wp(name, results):
n, r, r2 = results
mean = r / (n + 1e-6)
name_tag = ' (%s)' % name if name != '' else ''
print('win rate%s = %.3f (%.1f / %d)' % (name_tag, (mean + 1) / 2, (r + n) / 2, n))
keys = self.results_per_opponent[self.model_epoch]
if len(self.args.get('eval', {}).get('opponent', [])) <= 1 and len(keys) <= 1:
output_wp('', self.results[self.model_epoch])
else:
output_wp('total', self.results[self.model_epoch])
for key in sorted(list(self.results_per_opponent[self.model_epoch])):
output_wp(key, self.results_per_opponent[self.model_epoch][key])
if self.model_epoch not in self.generation_results:
print('generation stats = Nan (0)')
else:
n, r, r2 = self.generation_results[self.model_epoch]
mean = r / (n + 1e-6)
std = (r2 / (n + 1e-6) - mean ** 2) ** 0.5
print('generation stats = %.3f +- %.3f' % (mean, std))
model, steps = self.trainer.update()
if model is None:
model = self.model
self.update_model(model, steps)
# clear flags
self.flags = set()
def server(self):
# central conductor server
# returns as list if getting multiple requests as list
print('started server')
prev_update_episodes = self.args['minimum_episodes']
# no update call before storing minimum number of episodes + 1 epoch
next_update_episodes = prev_update_episodes + self.args['update_episodes']
while self.worker.connection_count() > 0 or not self.shutdown_flag:
try:
conn, (req, data) = self.worker.recv(timeout=0.3)
except queue.Empty:
continue
multi_req = isinstance(data, list)
if not multi_req:
data = [data]
send_data = []
if req == 'args':
if self.shutdown_flag:
send_data = [None] * len(data)
else:
for _ in data:
args = {'model_id': {}}
# decide role
if self.num_results < self.eval_rate * self.num_episodes:
args['role'] = 'e'
else:
args['role'] = 'g'
if args['role'] == 'g':
# genatation configuration
args['player'] = self.env.players()
for p in self.env.players():
if p in args['player']:
args['model_id'][p] = self.model_epoch
else:
args['model_id'][p] = -1
self.num_episodes += 1
elif args['role'] == 'e':
# evaluation configuration
args['player'] = [self.env.players()[self.num_results % len(self.env.players())]]
for p in self.env.players():
if p in args['player']:
args['model_id'][p] = self.model_epoch
else:
args['model_id'][p] = -1
self.num_results += 1
send_data.append(args)
elif req == 'episode':
# report generated episodes
self.feed_episodes(data)
send_data = [None] * len(data)
elif req == 'result':
# report evaluation results
self.feed_results(data)
send_data = [None] * len(data)
elif req == 'model':
for model_id in data:
model = self.model
if model_id != self.model_epoch and model_id > 0:
try:
model = copy.deepcopy(self.model)
model.load_state_dict(torch.load(self.model_path(model_id)), strict=False)
except:
# return latest model if failed to load specified model
pass
send_data.append(pickle.dumps(model))
if not multi_req and len(send_data) == 1:
send_data = send_data[0]
self.worker.send(conn, send_data)
if self.num_returned_episodes >= next_update_episodes:
prev_update_episodes = next_update_episodes
next_update_episodes = prev_update_episodes + self.args['update_episodes']
self.update()
if self.args['epochs'] >= 0 and self.model_epoch >= self.args['epochs']:
self.shutdown_flag = True
print('finished server')
def run(self):
# open training thread
threading.Thread(target=self.trainer.run, daemon=True).start()
# open generator, evaluator
self.worker.run()
self.server()
def train_main(args):
prepare_env(args['env_args']) # preparing environment is needed in stand-alone mode
learner = Learner(args=args)
learner.run()
def train_server_main(args):
learner = Learner(args=args, remote=True)
learner.run()