-
Notifications
You must be signed in to change notification settings - Fork 0
/
muzero.py
709 lines (633 loc) · 26.4 KB
/
muzero.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import copy
import importlib
import json
import math
import pathlib
import pickle
import sys
import time
import nevergrad
import numpy
import ray
import torch
from torch.utils.tensorboard import SummaryWriter
from model import diagnose_model, models, replay_buffer, self_play, shared_storage, trainer, cross_play
class MuZero:
"""
Main class to manage MuZero.
Args:
game_name (str): Name of the game module, it should match the name of a .py file
in the "./games" directory.
config (dict, MuZeroConfig, optional): Override the default config of the game.
split_resources_in (int, optional): Split the GPU usage when using concurent muzero instances.
Example:
>>> muzero = MuZero("cartpole")
>>> muzero.train()
>>> muzero.test(render=True)
"""
def __init__(self, game_name, config=None, split_resources_in=1):
# Load the game and the config from the module with the game name
try:
game_module = importlib.import_module("games." + game_name)
self.Game = game_module.Game
self.config = game_module.MuZeroConfig()
self.cross_config = game_module.MuZeroConfig()
self.cross_config.results_path = pathlib.Path(__file__).resolve().parents[1] / "results" / "cross_play"
except ModuleNotFoundError as err:
print(
f'{game_name} is not a supported game name, refer to the documentation for adding a new game.'
)
raise err
# Overwrite the config
if config:
if type(config) is dict:
for param, value in config.items():
if hasattr(self.config, param):
setattr(self.config, param, value)
else:
raise AttributeError(
f"{game_name} config has no attribute '{param}'. Check the config file for the complete list of parameters."
)
else:
self.config = config
# Fix random generator seed
numpy.random.seed(self.config.seed)
torch.manual_seed(self.config.seed)
# Manage GPUs
if self.config.max_num_gpus == 0 and (
self.config.selfplay_on_gpu
or self.config.train_on_gpu
or self.config.reanalyse_on_gpu
):
raise ValueError(
"Inconsistent MuZeroConfig: max_num_gpus = 0 but GPU requested by selfplay_on_gpu or train_on_gpu or reanalyse_on_gpu."
)
if (
self.config.selfplay_on_gpu
or self.config.train_on_gpu
or self.config.reanalyse_on_gpu
):
total_gpus = (
self.config.max_num_gpus
if self.config.max_num_gpus is not None
else torch.cuda.device_count()
)
else:
total_gpus = 0
self.num_gpus = total_gpus / split_resources_in
if 1 < self.num_gpus:
self.num_gpus = math.floor(self.num_gpus)
ray.init(num_gpus=total_gpus, ignore_reinit_error=True)
# Checkpoint and replay buffer used to initialize workers
self.checkpoint = {
"weights": None,
"optimizer_state": None,
"total_reward": 0,
"muzero_reward": 0,
"opponent_reward": 0,
"episode_length": 0,
"mean_value": 0,
"training_step": 0,
"lr": 0,
"total_loss": 0,
"value_loss": 0,
"reward_loss": 0,
"policy_loss": 0,
"num_played_games": 0,
"num_played_steps": 0,
"num_reanalysed_games": 0,
"terminate": False,
}
self.checkpoint2 = copy.deepcopy(self.checkpoint)
self.replay_buffer = {}
cpu_actor = CPUActor.remote()
cpu_weights = cpu_actor.get_initial_weights.remote(self.config)
check_weights = cpu_actor.get_initial_weights.remote(self.cross_config)
self.checkpoint["weights"], self.summary = copy.deepcopy(ray.get(cpu_weights))
self.checkpoint2["weights"], self.summary2 = copy.deepcopy(ray.get(check_weights))
# Workers
self.self_play_workers = None
self.test_worker = None
self.training_worker = None
self.reanalyse_worker = None
self.replay_buffer_worker = None
self.shared_storage_worker = None
def train(self, log_in_tensorboard=True):
"""
Spawn ray workers and launch the training.
Args:
log_in_tensorboard (bool): Start a testing worker and log its performance in TensorBoard.
"""
if log_in_tensorboard or self.config.save_model:
self.config.results_path.mkdir(parents=True, exist_ok=True)
# Manage GPUs
if 0 < self.num_gpus:
num_gpus_per_worker = self.num_gpus / (
self.config.train_on_gpu
+ self.config.num_workers * self.config.selfplay_on_gpu
+ log_in_tensorboard * self.config.selfplay_on_gpu
+ self.config.use_last_model_value * self.config.reanalyse_on_gpu
)
if 1 < num_gpus_per_worker:
num_gpus_per_worker = math.floor(num_gpus_per_worker)
else:
num_gpus_per_worker = 0
# Initialize workers
self.training_worker = trainer.Trainer.options(
num_cpus=0,
num_gpus=num_gpus_per_worker if self.config.train_on_gpu else 0,
).remote(self.checkpoint, self.config)
self.shared_storage_worker = shared_storage.SharedStorage.remote(
self.checkpoint,
self.config,
)
self.shared_storage_worker.set_info.remote("terminate", False)
self.replay_buffer_worker = replay_buffer.ReplayBuffer.remote(
self.checkpoint, self.replay_buffer, self.config
)
if self.config.use_last_model_value:
self.reanalyse_worker = replay_buffer.Reanalyse.options(
num_cpus=0,
num_gpus=num_gpus_per_worker if self.config.reanalyse_on_gpu else 0,
).remote(self.checkpoint, self.config)
self.self_play_workers = [
self_play.SelfPlay.options(
num_cpus=0,
num_gpus=num_gpus_per_worker if self.config.selfplay_on_gpu else 0,
).remote(
self.checkpoint,
self.Game,
self.config,
self.config.seed + seed,
)
for seed in range(self.config.num_workers)
]
# Launch workers
[
self_play_worker.continuous_self_play.remote(
self.shared_storage_worker, self.replay_buffer_worker
)
for self_play_worker in self.self_play_workers
]
self.training_worker.continuous_update_weights.remote(
self.replay_buffer_worker, self.shared_storage_worker
)
if self.config.use_last_model_value:
self.reanalyse_worker.reanalyse.remote(
self.replay_buffer_worker, self.shared_storage_worker
)
if log_in_tensorboard:
self.logging_loop(
num_gpus_per_worker if self.config.selfplay_on_gpu else 0,
)
def logging_loop(self, num_gpus):
"""
Keep track of the training performance.
"""
# Launch the test worker to get performance metrics
self.test_worker = self_play.SelfPlay.options(
num_cpus=0,
num_gpus=num_gpus,
).remote(
self.checkpoint,
self.Game,
self.config,
self.config.seed + self.config.num_workers,
)
self.test_worker.continuous_self_play.remote(
self.shared_storage_worker, None, True
)
# Write everything in TensorBoard
writer = SummaryWriter(self.config.results_path)
print(
"\nTraining...\nRun tensorboard --logdir ./results and go to http://localhost:6006/ to see in real time the training performance.\n"
)
# Save hyperparameters to TensorBoard
hp_table = [
f"| {key} | {value} |" for key, value in self.config.__dict__.items()
]
writer.add_text(
"Hyperparameters",
"| Parameter | Value |\n|-------|-------|\n" + "\n".join(hp_table),
)
# Save model representation
writer.add_text(
"Model summary",
self.summary,
)
# Loop for updating the training performance
counter = 0
keys = [
"total_reward",
"muzero_reward",
"opponent_reward",
"episode_length",
"mean_value",
"training_step",
"lr",
"total_loss",
"value_loss",
"reward_loss",
"policy_loss",
"num_played_games",
"num_played_steps",
"num_reanalysed_games",
]
info = ray.get(self.shared_storage_worker.get_info.remote(keys))
try:
while info["training_step"] < self.config.training_steps:
info = ray.get(self.shared_storage_worker.get_info.remote(keys))
writer.add_scalar(
"1.Total_reward/1.Total_reward",
info["total_reward"],
counter,
)
writer.add_scalar(
"1.Total_reward/2.Mean_value",
info["mean_value"],
counter,
)
writer.add_scalar(
"1.Total_reward/3.Episode_length",
info["episode_length"],
counter,
)
writer.add_scalar(
"1.Total_reward/4.MuZero_reward",
info["muzero_reward"],
counter,
)
writer.add_scalar(
"1.Total_reward/5.Opponent_reward",
info["opponent_reward"],
counter,
)
writer.add_scalar(
"2.Workers/1.Self_played_games",
info["num_played_games"],
counter,
)
writer.add_scalar(
"2.Workers/2.Training_steps", info["training_step"], counter
)
writer.add_scalar(
"2.Workers/3.Self_played_steps", info["num_played_steps"], counter
)
writer.add_scalar(
"2.Workers/4.Reanalysed_games",
info["num_reanalysed_games"],
counter,
)
writer.add_scalar(
"2.Workers/5.Training_steps_per_self_played_step_ratio",
info["training_step"] / max(1, info["num_played_steps"]),
counter,
)
writer.add_scalar("2.Workers/6.Learning_rate", info["lr"], counter)
writer.add_scalar(
"3.Loss/1.Total_weighted_loss", info["total_loss"], counter
)
writer.add_scalar("3.Loss/Value_loss", info["value_loss"], counter)
writer.add_scalar("3.Loss/Reward_loss", info["reward_loss"], counter)
writer.add_scalar("3.Loss/Policy_loss", info["policy_loss"], counter)
print(
f'Last test reward: {info["total_reward"]:.2f}. Training step: {info["training_step"]}/{self.config.training_steps}. Played games: {info["num_played_games"]}. Loss: {info["total_loss"]:.2f}',
end="\r",
)
counter += 1
time.sleep(0.5)
except KeyboardInterrupt:
pass
self.terminate_workers()
if self.config.save_model:
# Persist replay buffer to disk
path = self.config.results_path / "replay_buffer.pkl"
print(f"\n\nPersisting replay buffer games to disk at {path}")
pickle.dump(
{
"buffer": self.replay_buffer,
"num_played_games": self.checkpoint["num_played_games"],
"num_played_steps": self.checkpoint["num_played_steps"],
"num_reanalysed_games": self.checkpoint["num_reanalysed_games"],
},
open(path, "wb"),
)
def terminate_workers(self):
"""
Softly terminate the running tasks and garbage collect the workers.
"""
if self.shared_storage_worker:
self.shared_storage_worker.set_info.remote("terminate", True)
self.checkpoint = ray.get(
self.shared_storage_worker.get_checkpoint.remote()
)
if self.replay_buffer_worker:
self.replay_buffer = ray.get(self.replay_buffer_worker.get_buffer.remote())
print("\nShutting down workers...")
self.self_play_workers = None
self.test_worker = None
self.training_worker = None
self.reanalyse_worker = None
self.replay_buffer_worker = None
self.shared_storage_worker = None
def test(
self, render=True, opponent=None, muzero_player=None, num_tests=1, num_gpus=0, cross=False
):
"""
Test the model in a dedicated thread.
Args:
render (bool): To display or not the environment. Defaults to True.
opponent (str): "self" for self-play, "human" for playing against MuZero and "random"
for a random agent, None will use the opponent in the config. Defaults to None.
muzero_player (int): Player number of MuZero in case of multiplayer
games, None let MuZero play all players turn by turn, None will use muzero_player in
the config. Defaults to None.
num_tests (int): Number of games to average. Defaults to 1.
num_gpus (int): Number of GPUs to use, 0 forces to use the CPU. Defaults to 0.
"""
opponent = opponent if opponent else self.config.opponent
muzero_player = muzero_player if muzero_player else self.config.muzero_player
self_play_worker = self_play.SelfPlay.options(
num_cpus=0,
num_gpus=num_gpus,
).remote(self.checkpoint, self.Game, self.config, numpy.random.randint(10000)) if not cross else cross_play.CrossPlay.options(
num_cpus=0,
num_gpus=num_gpus,
).remote(self.checkpoint, self.checkpoint2, self.Game, self.config, self.cross_config, numpy.random.randint(10000)) # <-- this is where to add ckpt 2
results = []
for i in range(num_tests):
print(f"Testing {i+1}/{num_tests}", end="\r")
results.append(
ray.get(
self_play_worker.play_game.remote(
0,
0,
render,
opponent,
muzero_player,
)
)
)
self_play_worker.close_game.remote()
if len(self.config.players) == 1:
result = numpy.mean([sum(history.reward_history) for history in results])
elif opponent == "cross_play":
result = numpy.mean(
[1 if history.reward_history[-1] == 30 else 0 for history in results]
)
else:
result = numpy.mean(
[
sum(
reward
for i, reward in enumerate(history.reward_history)
if history.to_play_history[i - 1] == muzero_player
)
for history in results
]
)
# reward_histories = [history.reward_history for history in results]
print("Benchmarking result: ", result)
return result
def load_model(self, checkpoint_path=None, replay_buffer_path=None):
"""
Load a model and/or a saved replay buffer.
Args:
checkpoint_path (str): Path to model.checkpoint or model.weights.
replay_buffer_path (str): Path to replay_buffer.pkl
"""
# Load checkpoint
if checkpoint_path:
checkpoint_path = pathlib.Path(checkpoint_path)
self.checkpoint = torch.load(checkpoint_path)
print(f"\nUsing checkpoint from {checkpoint_path}")
# Load replay buffer
if replay_buffer_path:
replay_buffer_path = pathlib.Path(replay_buffer_path)
with open(replay_buffer_path, "rb") as f:
replay_buffer_infos = pickle.load(f)
self.replay_buffer = replay_buffer_infos["buffer"]
self.checkpoint["num_played_steps"] = replay_buffer_infos[
"num_played_steps"
]
self.checkpoint["num_played_games"] = replay_buffer_infos[
"num_played_games"
]
self.checkpoint["num_reanalysed_games"] = replay_buffer_infos[
"num_reanalysed_games"
]
print(f"\nInitializing replay buffer with {replay_buffer_path}")
else:
print(f"Using empty buffer.")
self.replay_buffer = {}
self.checkpoint["training_step"] = 0
self.checkpoint["num_played_steps"] = 0
self.checkpoint["num_played_games"] = 0
self.checkpoint["num_reanalysed_games"] = 0
def diagnose_model(self, horizon):
"""
Play a game only with the learned model then play the same trajectory in the real
environment and display information.
Args:
horizon (int): Number of timesteps for which we collect information.
"""
game = self.Game(self.config.seed)
obs = game.reset()
dm = diagnose_model.DiagnoseModel(self.checkpoint, self.config)
dm.compare_virtual_with_real_trajectories(obs, game, horizon)
input("Press enter to close all plots")
dm.close_all()
@ray.remote(num_cpus=0, num_gpus=0)
class CPUActor:
# Trick to force DataParallel to stay on CPU to get weights on CPU even if there is a GPU
def __init__(self):
pass
def get_initial_weights(self, config):
model = models.MuZeroNetwork(config)
weights = model.get_weights()
summary = str(model).replace("\n", " \n\n")
return weights, summary
def hyperparameter_search(
game_name, parametrization, budget, parallel_experiments, num_tests
):
"""
Search for hyperparameters by launching parallel experiments.
Args:
game_name (str): Name of the game module, it should match the name of a .py file
in the "./games" directory.
parametrization : Nevergrad parametrization, please refer to nevergrad documentation.
budget (int): Number of experiments to launch in total.
parallel_experiments (int): Number of experiments to launch in parallel.
num_tests (int): Number of games to average for evaluating an experiment.
"""
optimizer = nevergrad.optimizers.OnePlusOne(
parametrization=parametrization, budget=budget
)
running_experiments = []
best_training = None
try:
# Launch initial experiments
for i in range(parallel_experiments):
if 0 < budget:
param = optimizer.ask()
print(f"Launching new experiment: {param.value}")
muzero = MuZero(game_name, param.value, parallel_experiments)
muzero.param = param
muzero.train(False)
running_experiments.append(muzero)
budget -= 1
while 0 < budget or any(running_experiments):
for i, experiment in enumerate(running_experiments):
if experiment and experiment.config.training_steps <= ray.get(
experiment.shared_storage_worker.get_info.remote("training_step")
):
experiment.terminate_workers()
result = experiment.test(False, num_tests=num_tests)
if not best_training or best_training["result"] < result:
best_training = {
"result": result,
"config": experiment.config,
"checkpoint": experiment.checkpoint,
}
print(f"Parameters: {experiment.param.value}")
print(f"Result: {result}")
optimizer.tell(experiment.param, -result)
if 0 < budget:
param = optimizer.ask()
print(f"Launching new experiment: {param.value}")
muzero = MuZero(game_name, param.value, parallel_experiments)
muzero.param = param
muzero.train(False)
running_experiments[i] = muzero
budget -= 1
else:
running_experiments[i] = None
except KeyboardInterrupt:
for experiment in running_experiments:
if isinstance(experiment, MuZero):
experiment.terminate_workers()
recommendation = optimizer.provide_recommendation()
print("Best hyperparameters:")
print(recommendation.value)
if best_training:
# Save best training weights (but it's not the recommended weights)
best_training["config"].results_path.mkdir(parents=True, exist_ok=True)
torch.save(
best_training["checkpoint"],
best_training["config"].results_path / "model.checkpoint",
)
# Save the recommended hyperparameters
text_file = open(
best_training["config"].results_path / "best_parameters.txt",
"w",
)
text_file.write(str(recommendation.value))
text_file.close()
return recommendation.value
def load_model_menu(muzero, game_name):
# Configure running options
options = ["Specify paths manually"] + sorted(
(pathlib.Path("results") / game_name).glob("*/")
)
options.reverse()
print()
for i in range(len(options)):
print(f"{i}. {options[i]}")
choice = input("Enter a number to choose a model to load: ")
valid_inputs = [str(i) for i in range(len(options))]
while choice not in valid_inputs:
choice = input("Invalid input, enter a number listed above: ")
choice = int(choice)
if choice == (len(options) - 1):
# manual path option
checkpoint_path = input(
"Enter a path to the model.checkpoint, or ENTER if none: "
)
while checkpoint_path and not pathlib.Path(checkpoint_path).is_file():
checkpoint_path = input("Invalid checkpoint path. Try again: ")
replay_buffer_path = input(
"Enter a path to the replay_buffer.pkl, or ENTER if none: "
)
while replay_buffer_path and not pathlib.Path(replay_buffer_path).is_file():
replay_buffer_path = input("Invalid replay buffer path. Try again: ")
else:
checkpoint_path = options[choice] / "model.checkpoint"
replay_buffer_path = options[choice] / "replay_buffer.pkl"
muzero.load_model(
checkpoint_path=checkpoint_path,
replay_buffer_path=replay_buffer_path,
)
if __name__ == "__main__":
if len(sys.argv) == 2:
# Train directly with: python muzero.py oanquan
muzero = MuZero("oanquan")
muzero.train()
elif len(sys.argv) == 3:
# Train directly with: python muzero.py cartpole '{"lr_init": 0.01}'
config = json.loads(sys.argv[2])
muzero = MuZero("oanquan", config)
muzero.train()
else:
print("\nWelcome to MuZero O An Quan!")
muzero = MuZero("oanquan")
while True:
# Configure running options
options = [
"Train",
"Load pretrained model",
"Diagnose model",
"Render some self play games",
"Play against MuZero",
"Test the game manually",
"Hyperparameter search",
"Benchmark with other model",
"Exit",
]
print()
for i in range(len(options)):
print(f"{i}. {options[i]}")
choice = input("Enter a number to choose an action: ")
valid_inputs = [str(i) for i in range(len(options))]
while choice not in valid_inputs:
choice = input("Invalid input, enter a number listed above: ")
choice = int(choice)
if choice == 0:
muzero.train()
elif choice == 1:
load_model_menu(muzero, "oanquan")
elif choice == 2:
muzero.diagnose_model(30)
elif choice == 3:
muzero.test(render=True, opponent="self", muzero_player=None)
elif choice == 4:
muzero.test(render=True, opponent="human", muzero_player=0)
elif choice == 5:
env = muzero.Game()
env.reset()
env.render()
done = False
while not done:
action = env.human_to_action()
observation, reward, done = env.step(action)
print(f"\nAction: {env.action_to_string(action)}\nReward: {reward}")
env.render()
elif choice == 6:
# Define here the parameters to tune
# Parametrization documentation: https://facebookresearch.github.io/nevergrad/parametrization.html
muzero.terminate_workers()
del muzero
budget = 20
parallel_experiments = 2
lr_init = nevergrad.p.Log(lower=0.0001, upper=0.1)
discount = nevergrad.p.Log(lower=0.95, upper=0.9999)
parametrization = nevergrad.p.Dict(lr_init=lr_init, discount=discount)
best_hyperparameters = hyperparameter_search(
"oanquan", parametrization, budget, parallel_experiments, 20
)
muzero = MuZero("oanquan", best_hyperparameters)
elif choice == 7:
muzero.test(render=False, opponent="cross_play", num_tests=10, muzero_player=0, cross=True)
else:
break
print("\nDone")
ray.shutdown()