-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexploit.py
1302 lines (1160 loc) · 57.2 KB
/
exploit.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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
from pprint import pformat
from typing import Dict, Optional
import collections
import itertools
import logging
import pathlib
import random
import shutil
import time
import psutil
import pynvml
import torch
import torch.cuda
import torch.distributed
import torch.nn.functional
import torch.nn
import torch.optim
import torch.utils.tensorboard
import fairdiplomacy.selfplay.metrics
import fairdiplomacy.selfplay.pg.data_loader
import fairdiplomacy.selfplay.pg.vtrace
import fairdiplomacy.selfplay.search.data_loader
import fairdiplomacy.selfplay.remote_metric_logger
from fairdiplomacy.models.consts import POWERS, MAX_SEQ_LEN
from fairdiplomacy.models.base_strategy_model.load_model import (
load_base_strategy_model_model_and_args,
)
from fairdiplomacy.models.state_space import EOS_IDX
from fairdiplomacy.agents.base_strategy_model_wrapper import forward_model_with_output_transform
from fairdiplomacy.utils.timing_ctx import TimingCtx
from fairdiplomacy.selfplay.trainer_state import NetTrainingState, TrainerState
from fairdiplomacy.selfplay.execution_context import ExecutionContext
from fairdiplomacy.selfplay.pg.rollout import order_logits_to_action_logprobs
from fairdiplomacy.selfplay.paths import get_rendezvous_path, get_torch_ddp_init_fname
from fairdiplomacy.selfplay.search.rollout import ReSearchRolloutBatch
from fairdiplomacy.selfplay.search.search_utils import (
compute_search_policy_entropy,
compute_search_policy_cross_entropy_sampled,
evs_to_policy,
)
from fairdiplomacy.utils.exception_handling_process import ExceptionHandlingProcess
from fairdiplomacy.utils.multiprocessing_spawn_context import get_multiprocessing_ctx
import conf.conf_cfgs
import heyhi
import nest
mp = get_multiprocessing_ctx()
# Exported checkpoint that could be loaded by agents.
CKPT_MAIN_DIR = pathlib.Path("ckpt")
CKPT_VALUE_DIR = pathlib.Path("ckpt_value")
CKPT_TPL = "epoch%06d.ckpt"
REQUEUE_CKPT = pathlib.Path("requeue.ckpt")
def compute_entropy_loss(logits, mask):
"""Return the entropy loss, i.e., the negative entropy of the policy."""
policy = torch.nn.functional.softmax(logits, dim=-1)
log_policy = torch.nn.functional.log_softmax(logits, dim=-1)
return torch.sum(policy * log_policy * mask.unsqueeze(-1)) / (mask.sum() + 1e-5)
def compute_policy_gradient_loss(action_logprobs, advantages):
return -torch.mean(action_logprobs * advantages.detach())
def compute_sampled_entropy_loss(action_logprobs):
return (action_logprobs * (1 + action_logprobs.detach())).mean()
def sample_and_compute_sampled_entropy_loss(model, obs):
# [T, B, ...] -> [T * B, ...]
flat_obs = nest.map(lambda x: x.flatten(end_dim=1), obs)
global_order_idxs, policy_action_logprobs, _ = forward_model_with_output_transform(
model, flat_obs, temperature=1.0, need_value=False, pad_to_max=True
)
# [T * B, POWERS, SEQ_LEN]
mask = (global_order_idxs != EOS_IDX).any(-1)
policy_action_logprobs = policy_action_logprobs.view(-1)[mask.view(-1)]
loss = compute_sampled_entropy_loss(policy_action_logprobs)
entropy = -policy_action_logprobs.mean()
return loss, entropy
def to_onehot(indices, max_value):
y_onehot = torch.zeros((len(indices), max_value), device=indices.device)
y_onehot.scatter_(1, indices.unsqueeze(1), 1)
return y_onehot
def build_optimizer(net, optimizer_cfg):
opt_name = optimizer_cfg.WhichOneof("optimizer")
assert opt_name, f"Config must define an agent type: {optimizer_cfg}"
opt_class_cfg = getattr(optimizer_cfg, opt_name)
if opt_name == "adam" and optimizer_cfg.adam.weight_decay is not None:
logging.info("Using AdamW optimizer")
optimizer = torch.optim.AdamW(net.parameters(), **heyhi.conf_to_dict(opt_class_cfg))
else:
opt_class = {"adam": torch.optim.Adam, "sgd": torch.optim.SGD}[opt_name]
optimizer = opt_class(net.parameters(), **heyhi.conf_to_dict(opt_class_cfg))
scheduler_list = []
if optimizer_cfg.warmup_epochs is not None and optimizer_cfg.warmup_epochs > 0:
scheduler_list.append(
torch.optim.lr_scheduler.LambdaLR(
optimizer, lambda epoch: min(1.0, (epoch + 1) / optimizer_cfg.warmup_epochs)
)
)
if optimizer_cfg.cosine_decay_epochs:
scheduler_list.append(
torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer, optimizer_cfg.cosine_decay_epochs
)
)
if optimizer_cfg.step_decay_epochs:
scheduler_list.append(
torch.optim.lr_scheduler.StepLR(
optimizer, optimizer_cfg.step_decay_epochs, gamma=optimizer_cfg.step_decay_factor
)
)
if optimizer_cfg.warmup_decay:
scheduler_list.append(
torch.optim.lr_scheduler.LambdaLR(
optimizer, _warmup_decay_schedule(optimizer_cfg.warmup_decay)
)
)
if not scheduler_list:
scheduler = None
elif len(scheduler_list) == 1:
scheduler = scheduler_list[0]
else:
# Only with pytorch 1.10. Hence the stanza above.
scheduler = torch.optim.lr_scheduler.ChainedScheduler(scheduler_list)
return optimizer, scheduler
def vtrace_from_logprobs_no_batch(**kwargs):
kwargs = {k: (v.unsqueeze(1) if v.shape else v.unsqueeze(0)) for k, v in kwargs.items()}
ret = fairdiplomacy.selfplay.pg.vtrace.from_importance_weights(**kwargs)
return type(ret)(*[i.squeeze(1) for i in ret])
def clip_grad_norm_(parameters, max_norm, norm_type=2):
"""Copied from Pytorch 1.5. Faster version for grad norm."""
if isinstance(parameters, torch.Tensor):
parameters = [parameters]
parameters = list(filter(lambda p: p.grad is not None, parameters))
max_norm = float(max_norm)
norm_type = float(norm_type)
total_norm = torch.norm(
torch.stack([torch.norm(p.grad.detach(), norm_type) for p in parameters]), norm_type
)
clip_coef = max_norm / (total_norm + 1e-6)
if clip_coef < 1:
for p in parameters:
p.grad.detach().mul_(clip_coef)
return total_norm
def load_or_create_base_strategy_model_model(model_path: str, *, reset_weights: bool, **kwargs):
"""Thin wrapper over load_base_strategy_model_model_and_args with required reset_weights arg."""
model, args = load_base_strategy_model_model_and_args(
model_path, skip_weight_loading=reset_weights, **kwargs
)
return model, args
class ExploitTrainer:
def __init__(
self, cfg: conf.conf_cfgs.ExploitTask, ectx: ExecutionContext, random_seed: int,
):
# This constructor is called by 3 different sets of processes. See ExecutionContext
# On data generation workers (additional processes directly spawned by slurm), heyhi.is_master() is false.
# These workers just stay permanently stuck in the DataLoader, generating data for the training master.
self.ectx = ectx
if cfg.search_rollout.batch_size:
self.research = True
self.is_policy_being_trained = bool(cfg.search_policy_weight)
self.is_value_being_trained = cfg.critic_weight > 0
else:
self.research = False
self.is_policy_being_trained = self.is_value_being_trained = True
logging.info(f"self.research={self.research}")
logging.info(f"self.is_policy_being_trained={self.is_policy_being_trained}")
logging.info(f"self.is_value_being_trained={self.is_value_being_trained}")
assert cfg.use_distributed_data_parallel == ectx.using_ddp
if ectx.using_ddp:
assert self.research, "DistributedDataParallel only supported for research"
assert cfg.search_rollout.batch_size % self.ectx.ddp_world_size == 0
assert cfg.search_rollout.buffer.capacity % self.ectx.ddp_world_size == 0
if self.ectx.is_training_master:
logging.info(
f"This is a training master process, rank {self.ectx.training_ddp_rank}"
)
elif self.ectx.is_training_helper:
logging.info(
f"This is a training helper process, rank {self.ectx.training_ddp_rank}"
)
else:
assert self.ectx.ddp_world_size == 1
cfg = cfg.to_editable()
if heyhi.conf_is_set(cfg, "optimizer.lr") and not cfg.optimizer.WhichOneof("optimizer"):
heyhi.conf_set(cfg, "optimizer.adam.lr", heyhi.conf_get(cfg, "optimizer.lr"))
if self.research:
assert cfg.bootstrap_offline_targets, "Using online targets is not supported anymore"
if cfg.search_rollout.extra_params.use_ev_targets:
logging.info("Setting cfg.search_rollout.extra_params.collect_search_evs = true")
cfg.search_rollout.extra_params.collect_search_evs = True
logging.info(
"Setting cfg.search_rollout.extra_params.collect_search_policies = true"
)
cfg.search_rollout.extra_params.collect_search_policies = True
if cfg.search_policy_weight:
logging.info(
"Setting cfg.search_rollout.extra_params.collect_search_policies = %s",
self.is_policy_being_trained,
)
cfg.search_rollout.extra_params.collect_search_policies = (
self.is_policy_being_trained
)
if cfg.WhichOneof("maybe_search_ev_loss") is not None:
logging.info("Setting cfg.search_rollout.extra_params.collect_search_evs = True")
cfg.search_rollout.extra_params.collect_search_evs = True
assert (
self.is_policy_being_trained
or not cfg.search_rollout.extra_params.use_trained_policy
)
assert (
self.is_value_being_trained
or not cfg.search_rollout.extra_params.use_trained_value
)
for key in cfg.search_rollout.to_dict():
h2h_eval_cfg = getattr(cfg.search_rollout, key)
if key.startswith("h2h_eval") and h2h_eval_cfg.tag:
assert not h2h_eval_cfg.HasField(
"use_trained_policy"
), f"Do not set use_trained_policy manually: {h2h_eval_cfg}"
h2h_eval_cfg.use_trained_policy = self.is_policy_being_trained
logging.info(
f"Setting cfg.search_rollout.{key}.use_trained_policy = {h2h_eval_cfg.use_trained_policy}"
)
assert not h2h_eval_cfg.HasField(
"use_trained_value"
), f"Do not set use_trained_policy manually: {h2h_eval_cfg}"
h2h_eval_cfg.use_trained_value = self.is_value_being_trained
logging.info(
f"Setting cfg.search_rollout.{key}.use_trained_value = {h2h_eval_cfg.use_trained_value}"
)
if cfg.search_rollout.benchmark_only:
logging.info("Benchmak only mode. Disabling h2h evals and sit checks")
for key in cfg.search_rollout.to_dict():
h2h_eval_cfg = getattr(cfg.search_rollout, key)
if key.startswith("h2h_eval"):
h2h_eval_cfg.tag = ""
cfg.search_rollout.test_situation_eval.do_eval = False
assert cfg.num_train_gpus == 1, "Benchmark mode. Set num_train_gpus to 1"
logging.info("Benchmak only mode. Disabling warmup")
cfg.search_rollout.warmup_batches = 1
logging.info("Benchmak only mode. Disabling buffer saving")
cfg.search_rollout.buffer.ClearField("save_at")
logging.info("Benchmak only mode. Setting overuse to 1")
cfg.search_rollout.enforce_train_gen_ratio = 1.0
cfg = cfg.to_frozen()
if cfg.value_optimizer is not None:
assert self.research
assert self.is_value_being_trained
assert cfg.value_model_path
if cfg.optimizer.cosine_decay_epochs is not None and cfg.trainer.max_epochs:
assert cfg.optimizer.cosine_decay_epochs + 1 >= cfg.trainer.max_epochs
if cfg.seed > 0:
torch.manual_seed(cfg.seed)
assert (
cfg.search_policy_update_prob >= 1.0
or cfg.search_rollout.extra_params.run_do_prob >= 1.0
), "Cannot mix search_policy_update_prob and search_rollout.extra_params.run_do_prob"
self.cfg: conf.conf_cfgs.ExploitTask = cfg
self.device: str
self.state: TrainerState
self.last_epoch_state: Dict
assert self.research or not cfg.value_model_path, "Not supported in PG mode"
self.random = random.Random(random_seed)
# Initialize and prepare the data for training.
# Data generation workers are expected to never return from this function.
def _init_data_loader_workers_may_never_return(self):
if self.research:
# Data generation workers never return from this constructor.
self.data_loader = fairdiplomacy.selfplay.search.data_loader.DataLoader(
self.cfg.model_path,
self.cfg.search_rollout,
num_train_gpus=self.cfg.num_train_gpus,
ectx=self.ectx,
)
assert (
not self.ectx.is_rollouter
), "Data generation machines are expected to stay in DataLoader"
else:
self.data_loader = fairdiplomacy.selfplay.pg.data_loader.DataLoader(
self.cfg.model_path, self.cfg.rollout
)
# Initialize the state of training, taking into account checkpoints and requeing.
def _init_training_device_and_state(self):
if not torch.cuda.is_available():
logging.warning("No CUDA found!")
self.device = "cpu"
else:
if self.cfg.use_distributed_data_parallel:
# For now, ddp rank equals gpu index
self.device = f"cuda:{self.ectx.training_ddp_rank}" # Training device.
else:
self.device = "cuda" # Training device.
if self.cfg.value_model_path is not None:
net, net_args = load_or_create_base_strategy_model_model(
self.cfg.model_path,
reset_weights=self.cfg.reset_agent_weights,
map_location=self.device,
eval=True,
override_has_policy=True,
override_has_value=False,
)
logging.info("Policy model args:\n%s", pformat(net_args, indent=2))
optim, lr_scheduler = build_optimizer(net, self.cfg.optimizer)
value_net, value_net_args = load_or_create_base_strategy_model_model(
self.cfg.value_model_path,
reset_weights=self.cfg.reset_agent_weights,
map_location=self.device,
eval=True,
override_has_policy=False,
override_has_value=True,
)
logging.info("Value model args:\n%s", pformat(value_net_args, indent=2))
value_optim, value_lr_scheduler = build_optimizer(
value_net, self.cfg.value_optimizer or self.cfg.optimizer
)
value_state = NetTrainingState(
args=value_net_args,
model=value_net,
optimizer=value_optim,
scheduler=value_lr_scheduler,
)
else:
net, net_args = load_or_create_base_strategy_model_model(
self.cfg.model_path,
reset_weights=self.cfg.reset_agent_weights,
map_location=self.device,
eval=True,
)
logging.info("Policy+value model args:\n%s", pformat(net_args, indent=2))
optim, lr_scheduler = build_optimizer(net, self.cfg.optimizer)
value_state = None
self.state = TrainerState(
net_state=NetTrainingState(
model=net, optimizer=optim, scheduler=lr_scheduler, args=net_args,
),
value_net_state=value_state,
)
force_sync = False
if REQUEUE_CKPT.exists():
logging.info("Found requeue checkpoint: %s", REQUEUE_CKPT.resolve())
self.state = TrainerState.load(REQUEUE_CKPT, self.state, self.device)
elif self.cfg.requeue_ckpt_path:
force_sync = True
logging.info("Using explicit requeue checkpoint: %s", self.cfg.requeue_ckpt_path)
p = pathlib.Path(self.cfg.requeue_ckpt_path)
assert p.exists(), p
self.state = TrainerState.load(p, self.state, self.device)
else:
force_sync = True
if self.state.value_net_state is not None:
if CKPT_VALUE_DIR.exists() and list(CKPT_VALUE_DIR.iterdir()):
last_ckpt = max(CKPT_VALUE_DIR.iterdir(), key=str)
logging.info(
"Found existing VALUE checkpoint folder. Will load last one: %s", last_ckpt
)
self.state.value_net_state = NetTrainingState.load(
last_ckpt, self.state.value_net_state, self.device
)
else:
logging.info("No VALUE checkpoint found")
if self.state.net_state is not None:
if CKPT_MAIN_DIR.exists() and list(CKPT_MAIN_DIR.iterdir()):
last_ckpt = max(CKPT_MAIN_DIR.iterdir(), key=str)
logging.info(
"Found existing MAIN checkpoint folder. Will load last one: %s", last_ckpt
)
self.state.net_state = NetTrainingState.load(
last_ckpt, self.state.net_state, self.device
)
else:
logging.info("No MAIN checkpoint found")
# Training device and model state should be correct now, so now make sure that
# the model is sent to the data generation workers if it's not there yet.
self.maybe_send_model_to_workers(force_sync=force_sync)
if self.cfg.use_distributed_data_parallel:
torch_ddp_init_fname = get_torch_ddp_init_fname()
logging.info(f"Using {torch_ddp_init_fname} to coordinate launch of ddp processes.")
logging.info("Waiting for distributed data parallel helpers to sync up")
torch.distributed.init_process_group(
"nccl",
init_method=f"file://{torch_ddp_init_fname}",
rank=self.ectx.training_ddp_rank,
world_size=self.ectx.ddp_world_size,
)
logging.info("Distributed data parallel helpers synced, proceeding with training")
def _init_metrics_logger(self):
if self.ectx.is_training_master:
self.logger_server = fairdiplomacy.selfplay.remote_metric_logger.MetricLoggingServer(
self.cfg, ("alphadip" if self.research else "rl_pg")
)
self.logger = fairdiplomacy.selfplay.remote_metric_logger.get_remote_logger(
# DDP training helpers don't log anything. Can hack this here if needed for debugging them.
is_dummy=not self.ectx.is_training_master
)
self.logger.log_config(self.cfg)
def terminate(self):
if hasattr(self, "data_loader"):
logging.info("Killing data loader")
self.data_loader.terminate()
if hasattr(self, "logger"):
logging.info("Closing the logger")
self.logger.close()
if hasattr(self, "logger_server"):
logging.info("Closing the logger server")
self.logger_server.terminate()
def on_requeue(self):
logging.info("Pre-termination callback")
self.terminate()
def maybe_send_model_to_workers(self, force_sync=False):
if self.ectx.is_training_helper:
return
sync_main = (
force_sync or self.state.global_step % self.cfg.trainer.save_sync_checkpoint_every == 0
)
sync_value = force_sync or (
self.state.global_step
% (
self.cfg.trainer.save_sync_value_checkpoint_every
or self.cfg.trainer.save_sync_checkpoint_every
)
== 0
)
if self.research:
if sync_main:
self.data_loader.update_model(
self.state.net_state.model,
global_step=self.state.global_step,
args=self.state.net_state.args,
epoch=self.state.epoch_id,
as_policy=True,
as_value=self.state.value_net_state is None,
)
if sync_value and self.state.value_net_state is not None:
self.data_loader.update_model(
self.state.value_net_state.model,
global_step=self.state.global_step,
args=self.state.value_net_state.args,
epoch=self.state.epoch_id,
as_policy=False,
as_value=True,
)
else:
if sync_main:
self.data_loader.update_model(
self.state.net_state.model,
global_step=self.state.net_state.global_step,
args=self.state.net_state.args,
epoch=self.state.epoch_id,
)
assert self.state.value_net_state is None
def __call__(self):
# we call this immediately to send the data workers off to the right places
# After this returns, we're in the master process or training helper processes only.
self._init_data_loader_workers_may_never_return()
self._init_training_device_and_state()
self._init_metrics_logger()
if self.ectx.is_training_master:
CKPT_MAIN_DIR.mkdir(exist_ok=True, parents=True)
if self.state.value_net_state is not None:
CKPT_VALUE_DIR.mkdir(exist_ok=True, parents=True)
self.state.model.train()
if self.state.value_net_state is not None:
self.state.value_net_state.model.train()
if self.cfg.trainer.train_as_eval:
assert self.state.value_net_state is None, "Not supported"
self.state.model.eval()
# Cast cuDNN RNN back to train mode.
self.state.model.apply(_lstm_to_train)
elif self.cfg.trainer.train_encoder_as_eval:
assert self.state.value_net_state is None, "Not supported"
self.state.model.encoder.eval()
elif self.cfg.trainer.train_decoder_as_eval:
assert self.state.value_net_state is None, "Not supported"
self.state.model.policy_decoder.eval()
self.state.model.policy_decoder.apply(_lstm_to_train)
elif self.cfg.trainer.train_as_eval_but_batchnorm:
assert self.state.value_net_state is None, "Not supported"
self.state.model.eval()
self.state.model.apply(_lstm_to_train)
self.state.model.apply(_bn_to_train)
logging.info(
f"Enforcing self.cfg.training_permute_powers = {self.cfg.training_permute_powers}"
)
self.state.net_state.model.set_training_permute_powers(self.cfg.training_permute_powers)
if self.state.value_net_state is not None:
self.state.value_net_state.model.set_training_permute_powers(
self.cfg.training_permute_powers
)
if not self.research:
assert self.cfg.num_train_gpus == 1, "Only one training GPU for policy gradients"
self.state.model.to(self.device)
else:
if self.cfg.num_train_gpus > 1:
assert torch.cuda.device_count() == 8, "Can only go multi-gpu on full machine"
if self.cfg.use_distributed_data_parallel:
self.state.net_state.model = torch.nn.parallel.DistributedDataParallel(
self.state.net_state.model,
device_ids=(self.ectx.training_ddp_rank,),
output_device=self.ectx.training_ddp_rank,
)
if self.state.value_net_state is not None:
self.state.value_net_state.model = torch.nn.parallel.DistributedDataParallel(
self.state.value_net_state.model,
device_ids=(self.ectx.training_ddp_rank,),
output_device=self.ectx.training_ddp_rank,
)
else:
self.state.net_state.model = torch.nn.DataParallel(
self.state.net_state.model,
device_ids=tuple(range(self.cfg.num_train_gpus)),
)
if self.state.value_net_state is not None:
self.state.value_net_state.model = torch.nn.DataParallel(
self.state.value_net_state.model,
device_ids=tuple(range(self.cfg.num_train_gpus)),
)
else:
self.state.net_state.model.to(self.device)
if self.state.value_net_state is not None:
self.state.value_net_state.model.to(self.device)
if torch.cuda.is_available():
pynvml.nvmlInit()
if self.research and self.cfg.search_rollout.benchmark_only:
self.perform_benchmark()
return
# Installing requeue handle at the last moment so that we don't requeue zombie job.
heyhi.maybe_init_requeue_handler(self.on_requeue)
self.run_training_loop()
def run_training_loop(self):
logging.info("Beginning training loop")
max_epochs = self.cfg.trainer.max_epochs or 10 ** 9
for self.state.epoch_id in range(self.state.epoch_id, max_epochs):
# Clone state each epoch in case we'll need to requeue.
if self.ectx.is_training_master:
self.state.save(REQUEUE_CKPT)
if (
self.cfg.trainer.save_checkpoint_every
and self.state.epoch_id % self.cfg.trainer.save_checkpoint_every == 0
):
self.state.net_state.save(CKPT_MAIN_DIR / (CKPT_TPL % self.state.epoch_id))
if self.state.value_net_state is not None:
self.state.value_net_state.save(
CKPT_VALUE_DIR / (CKPT_TPL % self.state.epoch_id)
)
# Counter accumulate different statistic over the epoch. Default
# accumulation strategy is averaging.
counters = collections.defaultdict(fairdiplomacy.selfplay.metrics.FractionCounter)
use_grad_clip = (self.cfg.optimizer.grad_clip or 0) > 1e-10
if use_grad_clip:
counters["optim/grad_max"] = fairdiplomacy.selfplay.metrics.MaxCounter()
if not self.research:
counters["score/num_games"] = fairdiplomacy.selfplay.metrics.SumCounter()
for p in POWERS:
counters[f"score_{p}/num_games"] = fairdiplomacy.selfplay.metrics.SumCounter()
# For LR just record its value at the start of the epoch.
counters["optim/lr"].update(next(iter(self.state.optimizer.param_groups))["lr"])
if self.state.value_net_state is not None:
counters["optim/lr_value"].update(
next(iter(self.state.value_net_state.optimizer.param_groups))["lr"]
)
epoch_start_time = time.time()
for _ in range(self.cfg.trainer.epoch_size):
self.do_step(counters=counters, use_grad_clip=use_grad_clip)
if (
self.state.global_step < 128
or (self.state.global_step & self.state.global_step + 1) == 0
):
logging.info(
"Metrics (global_step=%d): %s",
self.state.global_step,
{k: v.value() for k, v in sorted(counters.items())},
)
self.state.global_step += 1
if self.research:
for key, value in self.data_loader.get_buffer_stats(prefix="buffer/").items():
counters[key].update(value)
epoch_scalars = {k: v.value() for k, v in sorted(counters.items())}
average_batch_size = epoch_scalars["size/batch"]
epoch_scalars["speed/loop_bps"] = self.cfg.trainer.epoch_size / (
time.time() - epoch_start_time + 1e-5
)
epoch_scalars["speed/loop_eps"] = epoch_scalars["speed/loop_bps"] * average_batch_size
# Speed for to_cuda + forward + backward.
torch_time = epoch_scalars["time/net"] + epoch_scalars["time/to_cuda"]
epoch_scalars["speed/train_bps"] = 1.0 / torch_time
epoch_scalars["speed/train_eps"] = average_batch_size / torch_time
if torch.cuda.is_available():
for i in range(pynvml.nvmlDeviceGetCount()):
mem_info = pynvml.nvmlDeviceGetMemoryInfo(pynvml.nvmlDeviceGetHandleByIndex(i))
epoch_scalars[f"gpu_mem_used/{i}"] = mem_info.used / 2 ** 30
epoch_scalars[f"gpu_mem_free/{i}"] = mem_info.free / 2 ** 30
mem_stats = psutil.virtual_memory()
epoch_scalars["memory/used_gb"] = mem_stats.used / 2 ** 30
epoch_scalars["memory/available_gb"] = mem_stats.available / 2 ** 30
epoch_scalars["memory/free_gb"] = mem_stats.free / 2 ** 30
eval_scores = self.data_loader.extract_eval_scores()
if eval_scores is not None:
for k, v in eval_scores.items():
epoch_scalars[f"score_eval/{k}"] = v
logging.info(
"Finished epoch %d. Metrics:\n%s",
self.state.epoch_id,
format_metrics_for_print(epoch_scalars),
)
self.logger.log_metrics(epoch_scalars, self.state.epoch_id)
if self.state.scheduler is not None:
self.state.scheduler.step()
if self.state.value_net_state is not None and self.state.value_net_state.scheduler:
self.state.value_net_state.scheduler.step()
logging.info("End of training")
logging.info("Exiting main funcion")
def perform_benchmark(self):
start = time.time()
counters = collections.defaultdict(fairdiplomacy.selfplay.metrics.FractionCounter)
for self.state.epoch_id in range(1000):
batch: ReSearchRolloutBatch = self.data_loader.get_batch() # type: ignore
elapsed, start = time.time() - start, time.time()
counters["speed/rps"].update(batch.done.sum(), elapsed)
counters["speed/phps"].update(batch.done.numel(), elapsed)
counters["rollout_len"].update(batch.done.numel(), batch.done.sum())
epoch_scalars = {k: v.value() for k, v in sorted(counters.items())}
if torch.cuda.is_available():
for i in range(pynvml.nvmlDeviceGetCount()):
mem_info = pynvml.nvmlDeviceGetMemoryInfo(pynvml.nvmlDeviceGetHandleByIndex(i))
epoch_scalars[f"gpu_mem_used/{i}"] = mem_info.used / 2 ** 30
epoch_scalars[f"gpu_mem_free/{i}"] = mem_info.free / 2 ** 30
logging.info(
"Finished epoch %d. Metrics:\n%s",
self.state.epoch_id,
format_metrics_for_print(epoch_scalars),
)
self.logger.log_metrics(epoch_scalars, self.state.epoch_id)
def do_step(self, **kwargs):
if self.research:
return self.do_step_research(**kwargs)
else:
return self.do_step_policy_gradient(**kwargs)
def do_step_research(self, *, counters: collections.defaultdict, use_grad_clip: bool):
device = self.device
timings = TimingCtx()
with timings("data_gen"):
research_batch: ReSearchRolloutBatch = self.data_loader.get_batch() # type: ignore
assert (
self.cfg.search_policy_update_prob >= 1.0 or self.cfg.value_update_prob >= 0
), "It does not make sense to subsample both"
do_search_policy_loss = (
self.is_policy_being_trained
and self.random.random() <= self.cfg.search_policy_update_prob
)
do_value_loss = {
self.is_value_being_trained and self.random.random() <= self.cfg.value_update_prob
}
main_net_has_grads = value_net_has_grads = False
per_dataloader_batch_size = self.cfg.search_rollout.batch_size // self.ectx.ddp_world_size
with timings("to_cuda"):
rewards = research_batch.rewards.to(device)
if self.cfg.search_rollout.buffer.shuffle:
assert list(rewards.shape) == [
1,
self.cfg.search_rollout.chunk_length * per_dataloader_batch_size,
len(POWERS),
], rewards.shape
else:
assert list(rewards.shape) == [
self.cfg.search_rollout.chunk_length,
per_dataloader_batch_size,
len(POWERS),
], rewards.shape
obs = {k: v.to(device) for k, v in research_batch.observations.items()}
done = research_batch.done.to(device)
is_search_policy_valid = research_batch.is_search_policy_valid.to(device)
is_explore = research_batch.is_explore.to(device)
is_dead = (research_batch.scores < 1e-3).float().to(device)
targets = research_batch.targets.to(device)
is_move_phase = (research_batch.phase_type == ord("M")).to(device)
is_adj_phase = (research_batch.phase_type == ord("A")).to(device)
years = research_batch.years.to(device)
if is_explore.all():
logging.warning("Whole batch of explore!!! Skipping")
return
if do_search_policy_loss:
search_policy_probs, search_policy_orders, blueprint_probs = (
research_batch.search_policy_probs.to(device),
research_batch.search_policy_orders.long().to(device),
research_batch.blueprint_probs.to(device),
)
if self.cfg.search_ev_loss is not None:
search_policy_evs = research_batch.search_policy_evs.to(device)
else:
search_policy_probs = search_policy_orders = None
is_all_powers = (
self.state.model.module if hasattr(self.state.model, "module") else self.state.model
).is_all_powers()
with timings("net"):
loss = torch.tensor(0.0, device=device)
losses = {}
if do_value_loss:
if self.state.value_net_state is not None:
value_net_has_grads = True
else:
main_net_has_grads = True
flat_obs = nest.map(lambda x: x.flatten(end_dim=1), obs)
# Shape: [T, B, 7].
_, _, _, predicted_values = self.state.value_model(
**flat_obs, temperature=1.0, need_policy=False,
)
predicted_values = predicted_values.reshape(rewards.shape)
# Note, if you even change this, you have to propogate discounting
# to search_data_loader akin to data_loader.
assert self.cfg.discounting == 1.0, "Discounting is not supported for ReSearch"
critic_mses = torch.nn.functional.mse_loss(
targets, predicted_values, reduction="none"
)
losses["critic"] = critic_mses.mean()
loss = loss + self.cfg.critic_weight * losses["critic"]
if do_search_policy_loss:
main_net_has_grads = True
if self.cfg.search_ev_loss is not None:
policy_loss_targets = evs_to_policy(
search_policy_evs,
temperature=self.cfg.search_ev_loss.temperature,
use_softmax=self.cfg.search_ev_loss.use_softmax,
)
else:
policy_loss_targets = search_policy_probs
# torch.save(
# (
# (
# self.state.model,
# obs,
# search_policy_orders,
# policy_loss_targets,
# blueprint_probs,
# ),
# dict(
# mask=is_search_policy_valid,
# is_move_phase=is_move_phase,
# is_adj_phase=is_adj_phase,
# using_ddp=self.cfg.use_distributed_data_parallel,
# max_prob_cap=self.cfg.search_policy_max_prob_cap,
# ),
# ),
# "/tmp/shuffle.pt",
# )
# raise 1
(
search_policy_loss,
search_policy_metrics,
) = compute_search_policy_cross_entropy_sampled(
self.state.model,
obs,
search_policy_orders,
policy_loss_targets,
blueprint_probs,
mask=is_search_policy_valid,
is_move_phase=is_move_phase,
is_adj_phase=is_adj_phase,
using_ddp=self.cfg.use_distributed_data_parallel,
max_prob_cap=self.cfg.search_policy_max_prob_cap,
is_all_powers=is_all_powers,
power_conditioning=self.cfg.power_conditioning,
single_power_chances=self.cfg.single_power_chances,
six_power_chances=self.cfg.six_power_chances,
)
for k, v in search_policy_metrics.items():
counters[k].update(v)
losses["search_policy"] = search_policy_loss
loss = loss + search_policy_loss * self.cfg.search_policy_weight
if (
self.cfg.sampled_entropy_weight is not None
and self.cfg.sampled_entropy_weight > 0.0
):
e_loss, e_mean = sample_and_compute_sampled_entropy_loss(
self.state.model, obs,
)
losses["policy_entropy_loss"] = e_loss
losses["policy_entropy"] = e_mean
loss = loss + e_loss * self.cfg.sampled_entropy_weight
else:
search_policy_loss = None
self.state.optimizer.zero_grad()
if self.state.value_net_state is not None:
self.state.value_net_state.optimizer.zero_grad()
loss.backward()
if use_grad_clip:
if main_net_has_grads:
g_norm_tensor = clip_grad_norm_(
self.state.model.parameters(), self.cfg.optimizer.grad_clip
)
else:
g_norm_tensor = None
if value_net_has_grads:
value_grad_clip = (self.cfg.value_optimizer or self.cfg.optimizer).grad_clip
g_norm_value_tensor = clip_grad_norm_(
self.state.value_net_state.model.parameters(), value_grad_clip
)
else:
g_norm_value_tensor = None
if (
not self.cfg.trainer.max_updates
or self.state.global_step < self.cfg.trainer.max_updates
):
if main_net_has_grads:
self.state.net_state.optimizer.step()
if value_net_has_grads:
self.state.value_net_state.optimizer.step()
# Sync to make sure timing is correct.
loss.item()
with timings("metrics"), torch.no_grad():
last_count = done.long().sum()
time_bsz = rewards.shape[0] * rewards.shape[1]
if do_value_loss:
critic_end_mses = critic_mses[done].sum()
if use_grad_clip:
if g_norm_tensor is not None:
g_norm = g_norm_tensor.item()
counters["optim/grad_max"].update(g_norm)
counters["optim/grad_mean"].update(g_norm)
counters["optim/grad_clip_ratio"].update(
int(g_norm >= self.cfg.optimizer.grad_clip - 1e-5)
)
if g_norm_value_tensor is not None:
g_norm_value = g_norm_value_tensor.item()
counters["optim/grad_value_max"].update(g_norm_value)
counters["optim/grad_value_mean"].update(g_norm_value)
counters["optim/grad_value_clip_ratio"].update(
int(g_norm_value >= value_grad_clip - 1e-5)
)
for key, value in losses.items():
counters[f"loss/{key}"].update(value)
counters["loss/total"].update(loss.item())
explored_on_the_right = research_batch.explored_on_the_right
if do_value_loss:
counters["loss/critic_no_explore"].update(
critic_mses[~explored_on_the_right].sum(), explored_on_the_right.long().sum()
)
counters["loss/critic_last"].update(critic_end_mses, last_count)
counters["loss/is_explore"].update(is_explore.long().sum(), (1 - is_dead).sum())
counters["loss/offpolicy_part"].update(
explored_on_the_right.long().sum(), (1 - is_dead).sum()
)
counters["loss/is_search_policy_valid"].update(
is_search_policy_valid.float().sum(), is_search_policy_valid.numel()
)
if do_search_policy_loss and not is_all_powers:
# What's the entopy of the search policy.
counters["loss/entropy_search"].update(
compute_search_policy_entropy(
search_policy_orders, search_policy_probs, mask=is_search_policy_valid
)
)
# is_search_policy_valid is indexed by [T, B, power]
# is_move_phase is indexed [T, B]
counters["loss/entropy_search_moves"].update(
compute_search_policy_entropy(
search_policy_orders,
search_policy_probs,
mask=is_search_policy_valid * is_move_phase.unsqueeze(2),
)
)
first_two_phases_move_mask = (
is_search_policy_valid
* is_move_phase.unsqueeze(2)
* (years == 1901).unsqueeze(2)
)
if first_two_phases_move_mask.any():
counters["loss/entropy_search_moves_1901"].update(
compute_search_policy_entropy(
search_policy_orders,
search_policy_probs,
mask=first_two_phases_move_mask,
)
)
if self.cfg.search_ev_loss is not None:
counters["loss/entropy_search_from_evs"].update(
compute_search_policy_entropy(
search_policy_orders, policy_loss_targets, mask=is_search_policy_valid
)
)
phase_bp_sums = blueprint_probs.flatten(end_dim=2)[
is_search_policy_valid.flatten(end_dim=2)
]
phase_bp_sums = phase_bp_sums.sum(-1).view(-1)
phase_bp_sums = phase_bp_sums[phase_bp_sums > 1e-10]
counters["loss/bp_share"].update(phase_bp_sums.sum(), len(phase_bp_sums))
counters["reward/mean"].update(rewards.sum(), time_bsz)
# Rewards at the end of episodes. We precompute everything
# before adding to counters to pipeline things when
# possible.
last_rewards = rewards[done]
last_sum = last_rewards.sum()
if self.is_value_being_trained:
# Mean predicted value for dead powers.
counters["value/mean_dead"].update(
(predicted_values * is_dead).sum(), is_dead.sum()
)
counters["reward/last"].update(last_sum, last_count)
counters["reward_solo/last"].update((last_rewards > 0.9).float().sum(), last_count)
for i, power in enumerate(POWERS):
power_rewards = last_rewards[..., i]
counters[f"reward/last_{power}"].update(power_rewards.sum(), last_count)
counters[f"reward_solo/last_{power}"].update(
(power_rewards > 0.9).sum(), last_count
)