-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathtrain.py
executable file
·1373 lines (1181 loc) · 69.2 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
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
#!/usr/bin/python3
import sys
import os
import argparse
import traceback
import random
import math
import time
import logging
import contextlib
import json
import datetime
from datetime import timezone
import gc
import shutil
import glob
import numpy as np
import itertools
import copy
import atexit
from collections import defaultdict
from typing import Dict, List
import torch
import torch.nn
import torch.optim
import torch.distributed
import torch.multiprocessing
from torch.nn.parallel import DistributedDataParallel
from torch.optim.swa_utils import AveragedModel
from torch.cuda.amp import GradScaler, autocast
import modelconfigs
from model_pytorch import Model, ExtraOutputs, MetadataEncoder
from metrics_pytorch import Metrics
import load_model
import data_processing_pytorch
from metrics_logging import accumulate_metrics, log_metrics, clear_metric_nonfinite
# HANDLE COMMAND AND ARGS -------------------------------------------------------------------
if __name__ == "__main__":
description = """
Train neural net on Go positions from npz files of batches from selfplay.
"""
parser = argparse.ArgumentParser(description=description,add_help=False)
required_args = parser.add_argument_group('required arguments')
optional_args = parser.add_argument_group('optional arguments')
optional_args.add_argument(
'-h',
'--help',
action='help',
default=argparse.SUPPRESS,
help='show this help message and exit'
)
required_args.add_argument('-traindir', help='Dir to write to for recording training results', required=True)
required_args.add_argument('-datadir', help='Directory with a train and val subdir of npz data, output by shuffle.py', required=True)
optional_args.add_argument('-exportdir', help='Directory to export models periodically', required=False)
optional_args.add_argument('-exportprefix', help='Prefix to append to names of models', required=False)
optional_args.add_argument('-initial-checkpoint', help='If no training checkpoint exists, initialize from this checkpoint', required=False)
required_args.add_argument('-pos-len', help='Spatial edge length of expected training data, e.g. 19 for 19x19 Go', type=int, required=True)
required_args.add_argument('-batch-size', help='Per-GPU batch size to use for training', type=int, required=True)
optional_args.add_argument('-samples-per-epoch', help='Number of data samples to consider as one epoch', type=int, required=False)
optional_args.add_argument('-model-kind', help='String name for what model config to use', required=False)
optional_args.add_argument('-lr-scale', help='LR multiplier on the hardcoded schedule', type=float, required=False)
optional_args.add_argument('-lr-scale-auto', help='LR auto scaling', required=False, action='store_true')
optional_args.add_argument('-gnorm-clip-scale', help='Multiplier on gradient clipping threshold', type=float, required=False)
optional_args.add_argument('-sub-epochs', help='Reload training data up to this many times per epoch', type=int, default=1, required=False)
optional_args.add_argument('-swa-period-samples', help='How frequently to average an SWA sample, in samples', type=float, required=False)
optional_args.add_argument('-swa-scale', help='Number of samples to average in expectation together for SWA', type=float, required=False)
optional_args.add_argument('-lookahead-k', help='Use lookahead optimizer', type=int, default=6, required=False)
optional_args.add_argument('-lookahead-alpha', help='Use lookahead optimizer', type=float, default=0.5, required=False)
optional_args.add_argument('-lookahead-print', help='Only print on lookahead syncs', required=False, action='store_true')
optional_args.add_argument('-multi-gpus', help='Use multiple gpus, comma-separated device ids', required=False)
optional_args.add_argument('-use-fp16', help='Use fp16 training', required=False, action='store_true')
optional_args.add_argument('-epochs-per-export', help='Export model once every this many epochs', type=int, required=False)
optional_args.add_argument('-export-prob', help='Export model with this probablity', type=float, required=False)
optional_args.add_argument('-max-epochs-this-instance', help='Terminate training after this many more epochs', type=int, required=False)
optional_args.add_argument('-max-training-samples', help='Terminate training after about this many training steps in samples', type=int, required=False)
optional_args.add_argument('-sleep-seconds-per-epoch', help='Sleep this long between epochs', type=int, required=False)
optional_args.add_argument('-max-train-bucket-per-new-data', help='When data added, add this many train rows per data row to bucket', type=float, required=False)
optional_args.add_argument('-max-train-bucket-size', help='Approx total number of train rows allowed if data stops', type=float, required=False)
optional_args.add_argument('-max-train-steps-since-last-reload', help='Approx total of training allowed if shuffling stops', type=float, required=False)
optional_args.add_argument('-stop-when-train-bucket-limited', help='Terminate due to train bucket rather than waiting for more', required=False, action='store_true')
optional_args.add_argument('-max-val-samples', help='Approx max of validation samples per epoch', type=int, required=False)
optional_args.add_argument('-randomize-val', help='Randomize order of validation files', required=False, action='store_true')
optional_args.add_argument('-no-export', help='Do not export models', required=False, action='store_true')
optional_args.add_argument('-no-repeat-files', help='Track what shuffled data was used and do not repeat, even when killed and resumed', required=False, action='store_true')
optional_args.add_argument('-quit-if-no-data', help='If no data, quit instead of waiting for data', required=False, action='store_true')
optional_args.add_argument('-gnorm-stats-debug', required=False, action='store_true')
optional_args.add_argument('-brenorm-avg-momentum', type=float, help='Set brenorm running avg rate to this value', required=False)
optional_args.add_argument('-brenorm-target-rmax', type=float, help='Gradually adjust brenorm rmax to this value', required=False)
optional_args.add_argument('-brenorm-target-dmax', type=float, help='Gradually adjust brenorm dmax to this value', required=False)
optional_args.add_argument('-brenorm-adjustment-scale', type=float, help='How many samples to adjust brenorm params all but 1/e of the way to target', required=False)
optional_args.add_argument('-soft-policy-weight-scale', type=float, default=8.0, help='Soft policy loss coeff', required=False)
optional_args.add_argument('-disable-optimistic-policy', help='Disable optimistic policy', required=False, action='store_true')
optional_args.add_argument('-meta-kata-only-soft-policy', help='Mask soft policy on non-kata rows using sgfmeta', required=False, action='store_true')
optional_args.add_argument('-value-loss-scale', type=float, default=0.6, help='Additional value loss coeff', required=False)
optional_args.add_argument('-td-value-loss-scales', type=str, default="0.6,0.6,0.6", help='Additional td value loss coeffs, 3 comma separated values', required=False)
optional_args.add_argument('-seki-loss-scale', type=float, default=1.0, help='Additional seki loss coeff', required=False)
optional_args.add_argument('-variance-time-loss-scale', type=float, default=1.0, help='Additional variance time loss coeff', required=False)
optional_args.add_argument('-main-loss-scale', type=float, help='Loss factor scale for main head', required=False)
optional_args.add_argument('-intermediate-loss-scale', type=float, help='Loss factor scale for intermediate head', required=False)
args = vars(parser.parse_args())
def get_longterm_checkpoints_dir(traindir):
return os.path.join(traindir,"longterm_checkpoints")
def make_dirs(args):
traindir = args["traindir"]
exportdir = args["exportdir"]
if not os.path.exists(traindir):
os.makedirs(traindir)
if exportdir is not None and not os.path.exists(exportdir):
os.makedirs(exportdir)
longterm_checkpoints_dir = get_longterm_checkpoints_dir(traindir)
if not os.path.exists(longterm_checkpoints_dir):
os.makedirs(longterm_checkpoints_dir)
def multiprocessing_setup(rank: int, world_size: int):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '23456'
logging.info("Running torch.distributed.init_process_group")
torch.distributed.init_process_group("nccl", rank=rank, world_size=world_size)
logging.info(f"Returned from torch.distributed.init_process_group, my rank = {rank}, world_size={world_size}")
def multiprocessing_cleanup():
torch.distributed.destroy_process_group()
def main(rank: int, world_size: int, args, multi_gpu_device_ids, readpipes, writepipes, barrier):
traindir = args["traindir"]
datadir = args["datadir"]
exportdir = args["exportdir"]
exportprefix = args["exportprefix"]
initial_checkpoint = args["initial_checkpoint"]
pos_len = args["pos_len"]
batch_size = args["batch_size"]
samples_per_epoch = args["samples_per_epoch"]
model_kind = args["model_kind"]
lr_scale = args["lr_scale"]
lr_scale_auto = args["lr_scale_auto"]
gnorm_clip_scale = args["gnorm_clip_scale"]
sub_epochs = args["sub_epochs"]
swa_period_samples = args["swa_period_samples"]
swa_scale = args["swa_scale"]
lookahead_k = args["lookahead_k"]
lookahead_alpha = args["lookahead_alpha"]
lookahead_print = args["lookahead_print"]
use_fp16 = args["use_fp16"]
epochs_per_export = args["epochs_per_export"]
export_prob = args["export_prob"]
max_epochs_this_instance = args["max_epochs_this_instance"]
max_training_samples = args["max_training_samples"]
sleep_seconds_per_epoch = args["sleep_seconds_per_epoch"]
max_train_bucket_per_new_data = args["max_train_bucket_per_new_data"]
max_train_bucket_size = args["max_train_bucket_size"]
max_train_steps_since_last_reload = args["max_train_steps_since_last_reload"]
stop_when_train_bucket_limited = args["stop_when_train_bucket_limited"]
max_val_samples = args["max_val_samples"]
randomize_val = args["randomize_val"]
no_export = args["no_export"]
no_repeat_files = args["no_repeat_files"]
quit_if_no_data = args["quit_if_no_data"]
gnorm_stats_debug = args["gnorm_stats_debug"]
brenorm_target_rmax = args["brenorm_target_rmax"]
brenorm_target_dmax = args["brenorm_target_dmax"]
brenorm_avg_momentum = args["brenorm_avg_momentum"]
brenorm_adjustment_scale = args["brenorm_adjustment_scale"]
soft_policy_weight_scale = args["soft_policy_weight_scale"]
disable_optimistic_policy = args["disable_optimistic_policy"]
meta_kata_only_soft_policy = args["meta_kata_only_soft_policy"]
value_loss_scale = args["value_loss_scale"]
td_value_loss_scales = [float(x) for x in args["td_value_loss_scales"].split(",")]
seki_loss_scale = args["seki_loss_scale"]
variance_time_loss_scale = args["variance_time_loss_scale"]
main_loss_scale = args["main_loss_scale"]
intermediate_loss_scale = args["intermediate_loss_scale"]
if lr_scale is None:
lr_scale = 1.0
if lr_scale_auto:
assert lr_scale == 1.0, "Cannot specify both lr_scale and lr_scale_auto"
if samples_per_epoch is None:
samples_per_epoch = 1000000
if max_train_bucket_size is None:
max_train_bucket_size = 1.0e30
if epochs_per_export is None:
epochs_per_export = 1
if swa_period_samples is None:
swa_period_samples = max(1, samples_per_epoch // 2)
if swa_scale is None:
swa_scale = 8
assert lookahead_alpha > 0.0 and lookahead_alpha <= 1.0
if lookahead_alpha >= 1.0: # 1.0 means to disable lookahead optimizer
lookahead_alpha = None
lookahead_k = None
longterm_checkpoints_dir = get_longterm_checkpoints_dir(traindir)
assert (swa_period_samples is None) == (swa_scale is None)
assert (lookahead_k is None) == (lookahead_alpha is None)
# SET UP LOGGING -------------------------------------------------------------
logging.root.handlers = []
if rank == 0:
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[
logging.FileHandler(os.path.join(traindir,f"train{rank}.log"), mode="a"),
logging.StreamHandler()
],
)
else:
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[
logging.FileHandler(os.path.join(traindir,f"train{rank}.log"), mode="a"),
],
)
np.set_printoptions(linewidth=150)
logging.info(str(sys.argv))
# FIGURE OUT MULTIGPU ------------------------------------------------------------
if world_size > 1:
multiprocessing_setup(rank, world_size)
atexit.register(multiprocessing_cleanup)
assert torch.cuda.is_available()
if True or torch.cuda.is_available():
my_gpu_id = multi_gpu_device_ids[rank]
torch.cuda.set_device(my_gpu_id)
logging.info("Using GPU device: " + torch.cuda.get_device_name())
device = torch.device("cuda", my_gpu_id)
else:
logging.warning("WARNING: No GPU, using CPU")
device = torch.device("cpu")
seed = int.from_bytes(os.urandom(7), sys.byteorder)
logging.info(f"Seeding torch with {seed}")
torch.manual_seed(seed)
# LOAD MODEL ---------------------------------------------------------------------
def lr_scale_auto_factor(train_state):
if not lr_scale_auto:
return 1.0
if train_state["global_step_samples"] < 200_000_000:
return 8.00
if train_state["global_step_samples"] < 400_000_000:
return 4.00
if train_state["global_step_samples"] < 500_000_000:
return 2.00
if train_state["global_step_samples"] < 550_000_000:
return 1.00
if train_state["global_step_samples"] < 600_000_000:
return 0.50
if train_state["global_step_samples"] < 650_000_000:
return 0.25
return 0.25
def get_checkpoint_path():
return os.path.join(traindir,"checkpoint.ckpt")
def get_checkpoint_prev_path(i):
return os.path.join(traindir,f"checkpoint_prev{i}.ckpt")
NUM_SHORTTERM_CHECKPOINTS_TO_KEEP = 4
def save(ddp_model, swa_model, optimizer, metrics_obj, running_metrics, train_state, last_val_metrics, path=None):
if gnorm_stats_debug:
logging.warning("Skipping save since debugging gnorm stats")
return
if rank == 0:
state_dict = {}
state_dict["model"] = ddp_model.state_dict()
state_dict["optimizer"] = optimizer.state_dict()
state_dict["metrics"] = metrics_obj.state_dict()
state_dict["running_metrics"] = running_metrics
state_dict["train_state"] = train_state
state_dict["last_val_metrics"] = last_val_metrics
state_dict["config"] = model_config
if swa_model is not None:
state_dict["swa_model"] = swa_model.state_dict()
if path is not None:
logging.info("Saving checkpoint: " + path)
torch.save(state_dict, path + ".tmp")
time.sleep(1)
os.replace(path + ".tmp", path)
else:
logging.info("Saving checkpoint: " + get_checkpoint_path())
for i in reversed(range(NUM_SHORTTERM_CHECKPOINTS_TO_KEEP-1)):
if os.path.exists(get_checkpoint_prev_path(i)):
os.replace(get_checkpoint_prev_path(i), get_checkpoint_prev_path(i+1))
if os.path.exists(get_checkpoint_path()):
shutil.copy(get_checkpoint_path(), get_checkpoint_prev_path(0))
torch.save(state_dict, get_checkpoint_path() + ".tmp")
os.replace(get_checkpoint_path() + ".tmp", get_checkpoint_path())
def get_weight_decay(raw_model, lr_scale, warmup_scale, train_state, running_metrics, group_name):
lr_scale_with_auto = lr_scale * lr_scale_auto_factor(train_state)
if raw_model.get_norm_kind() == "fixup" or raw_model.get_norm_kind() == "fixscale":
if group_name == "normal" or group_name == "normal_gamma" or group_name == "output":
return 0.000001 * world_size * batch_size / 256.0
elif group_name == "noreg":
return 0.00000001 * world_size * batch_size / 256.0
elif group_name == "output_noreg":
return 0.00000001 * world_size * batch_size / 256.0
else:
assert False
elif (
raw_model.get_norm_kind() == "bnorm" or
raw_model.get_norm_kind() == "brenorm" or
raw_model.get_norm_kind() == "fixbrenorm" or
raw_model.get_norm_kind() == "fixscaleonenorm"
):
if group_name == "normal" or group_name == "normal_gamma":
adaptive_scale = 1.0
if "sums" in running_metrics and "norm_normal_batch" in running_metrics["sums"]:
norm_normal_batch = running_metrics["sums"]["norm_normal_batch"] / running_metrics["weights"]["norm_normal_batch"]
baseline = train_state["modelnorm_normal_baseline"]
ratio = norm_normal_batch / (baseline + 1e-30)
# Adaptive weight decay keeping model norm around the baseline level so that batchnorm effective lr is held constant
# throughout training, covering a range of 16x from bottom to top.
adaptive_scale = math.pow(2.0, 2.0 * math.tanh(math.log(ratio+1e-30) * 1.5))
# Batch norm gammas can be regularized a bit less, doing them just as much empirically seemed to be a bit more unstable
gamma_scale = 0.125 if group_name == "normal_gamma" else 1.0
# The theoretical scaling for keeping us confined to a surface of equal model norm should go proportionally with lr_scale.
# because the strength of drift away from that surface goes as lr^2 and weight decay itself is scaled by lr, so we need
# one more factor of lr to make weight decay strength equal drift strength.
# However, at low lr it tends to be the case that gradient norm increases slightly
# while at high lr it tends to be the case that gradient norm decreases, which means drift strength scales a bit slower
# than expected.
# So we scale sublinearly with lr_scale so as to slightly preadjust to this effect.
# Adaptive scale should then help keep us there thereafter.
return 0.00125 * world_size * batch_size / 256.0 * math.pow(lr_scale_with_auto * warmup_scale,0.75) * adaptive_scale * gamma_scale
elif group_name == "output":
return 0.000001 * world_size * batch_size / 256.0
elif group_name == "noreg":
return 0.000001 * world_size * batch_size / 256.0 * math.pow(lr_scale_with_auto * warmup_scale,0.75)
elif group_name == "output_noreg":
return 0.00000001 * world_size * batch_size / 256.0
else:
assert False
else:
assert False
def get_param_groups(raw_model,train_state,running_metrics):
reg_dict : Dict[str,List] = {}
raw_model.add_reg_dict(reg_dict)
param_groups = []
param_groups.append({
"params": reg_dict["normal"],
"weight_decay": get_weight_decay(raw_model, lr_scale, warmup_scale=1.0, train_state=train_state, running_metrics=running_metrics, group_name="normal"),
"group_name": "normal",
})
if len(reg_dict["normal_gamma"]) > 0:
param_groups.append({
"params": reg_dict["normal_gamma"],
"weight_decay": get_weight_decay(raw_model, lr_scale, warmup_scale=1.0, train_state=train_state, running_metrics=running_metrics, group_name="normal_gamma"),
"group_name": "normal_gamma",
})
param_groups.append({
"params": reg_dict["output"],
"weight_decay": get_weight_decay(raw_model, lr_scale, warmup_scale=1.0, train_state=train_state, running_metrics=running_metrics, group_name="output"),
"group_name": "output",
})
param_groups.append({
"params": reg_dict["noreg"],
"weight_decay": get_weight_decay(raw_model, lr_scale, warmup_scale=1.0, train_state=train_state, running_metrics=running_metrics, group_name="noreg"),
"group_name": "noreg",
})
param_groups.append({
"params": reg_dict["output_noreg"],
"weight_decay": get_weight_decay(raw_model, lr_scale, warmup_scale=1.0, train_state=train_state, running_metrics=running_metrics, group_name="output_noreg"),
"group_name": "output_noreg",
})
num_params = len(list(raw_model.parameters()))
num_reg_dict_params = len(reg_dict["normal"]) + len(reg_dict["normal_gamma"]) + len(reg_dict["output"]) + len(reg_dict["noreg"]) + len(reg_dict["output_noreg"])
assert num_params == num_reg_dict_params, "Reg dict does not have entries for all params in model"
return param_groups
def load():
if not os.path.exists(get_checkpoint_path()):
logging.info("No preexisting checkpoint found at: " + get_checkpoint_path())
for i in range(NUM_SHORTTERM_CHECKPOINTS_TO_KEEP):
if os.path.exists(get_checkpoint_prev_path(i)):
raise Exception(f"No preexisting checkpoint found, but {get_checkpoint_prev_path(i)} exists, something is wrong with the training dir")
if initial_checkpoint is not None:
if os.path.exists(initial_checkpoint):
logging.info("Using initial checkpoint: {initial_checkpoint}")
path_to_load_from = initial_checkpoint
else:
raise Exception("No preexisting checkpoint found, initial checkpoint provided is invalid: {initial_checkpoint}")
else:
path_to_load_from = None
else:
path_to_load_from = get_checkpoint_path()
if path_to_load_from is None:
logging.info("Initializing new model!")
assert model_kind is not None, "Model kind is none or unspecified but the model is being created fresh"
model_config = modelconfigs.config_of_name[model_kind]
logging.info(str(model_config))
raw_model = Model(model_config,pos_len)
raw_model.initialize()
raw_model.to(device)
if world_size > 1:
ddp_model = torch.nn.parallel.DistributedDataParallel(raw_model, device_ids=[device])
else:
ddp_model = raw_model
swa_model = None
if rank == 0 and swa_scale is not None:
new_factor = 1.0 / swa_scale
ema_avg = lambda avg_param, cur_param, num_averaged: avg_param + new_factor * (cur_param - avg_param)
swa_model = AveragedModel(raw_model, avg_fn=ema_avg)
metrics_obj = Metrics(batch_size,world_size,raw_model)
running_metrics = {}
train_state = {}
last_val_metrics = {}
train_state["global_step_samples"] = 0
with torch.no_grad():
(modelnorm_normal, modelnorm_normal_gamma, modelnorm_output, modelnorm_noreg, modelnorm_output_noreg) = Metrics.get_model_norms(raw_model)
modelnorm_normal_baseline = modelnorm_normal.detach().cpu().item()
train_state["modelnorm_normal_baseline"] = modelnorm_normal_baseline
logging.info(f"Model norm normal baseline computed: {modelnorm_normal_baseline}")
optimizer = torch.optim.SGD(get_param_groups(raw_model,train_state,running_metrics), lr=1.0, momentum=0.9)
return (model_config, ddp_model, raw_model, swa_model, optimizer, metrics_obj, running_metrics, train_state, last_val_metrics)
else:
state_dict = torch.load(path_to_load_from, map_location=device)
model_config = state_dict["config"] if "config" in state_dict else modelconfigs.config_of_name[model_kind]
logging.info(str(model_config))
raw_model = Model(model_config,pos_len)
raw_model.initialize()
train_state = {}
if "train_state" in state_dict:
train_state = state_dict["train_state"]
else:
logging.info("WARNING: Train state not found in state dict, using fresh train state")
# Do this before loading the state dict, while the model is initialized to fresh values, to get a good baseline
if "modelnorm_normal_baseline" not in train_state:
logging.info("Computing modelnorm_normal_baseline since not in train state")
with torch.no_grad():
(modelnorm_normal, modelnorm_normal_gamma, modelnorm_output, modelnorm_noreg, modelnorm_output_noreg) = Metrics.get_model_norms(raw_model)
modelnorm_normal_baseline = modelnorm_normal.detach().cpu().item()
train_state["modelnorm_normal_baseline"] = modelnorm_normal_baseline
logging.info(f"Model norm normal baseline computed: {modelnorm_normal_baseline}")
# Strip off any "module." from when the model was saved with DDP or other things
model_state_dict = load_model.load_model_state_dict(state_dict)
raw_model.load_state_dict(model_state_dict)
raw_model.to(device)
if world_size > 1:
ddp_model = torch.nn.parallel.DistributedDataParallel(raw_model, device_ids=[device])
else:
ddp_model = raw_model
swa_model = None
if rank == 0 and swa_scale is not None:
new_factor = 1.0 / swa_scale
ema_avg = lambda avg_param, cur_param, num_averaged: avg_param + new_factor * (cur_param - avg_param)
swa_model = AveragedModel(raw_model, avg_fn=ema_avg)
swa_model_state_dict = load_model.load_swa_model_state_dict(state_dict)
if swa_model_state_dict is not None:
swa_model.load_state_dict(swa_model_state_dict)
metrics_obj = Metrics(batch_size,world_size,raw_model)
if "metrics" in state_dict:
metrics_obj.load_state_dict(state_dict["metrics"])
else:
logging.info("WARNING: Metrics not found in state dict, using fresh metrics")
running_metrics = {}
if "running_metrics" in state_dict:
running_metrics = state_dict["running_metrics"]
else:
logging.info("WARNING: Running metrics not found in state dict, using fresh running metrics")
last_val_metrics = {}
if "last_val_metrics" in state_dict:
last_val_metrics = state_dict["last_val_metrics"]
else:
logging.info("WARNING: Running metrics not found in state dict, using fresh last val metrics")
optimizer = torch.optim.SGD(get_param_groups(raw_model,train_state,running_metrics), lr=1.0, momentum=0.9)
if "optimizer" in state_dict:
optimizer.load_state_dict(state_dict["optimizer"])
else:
logging.info("WARNING: Optimizer not found in state dict, using fresh optimizer")
return (model_config, ddp_model, raw_model, swa_model, optimizer, metrics_obj, running_metrics, train_state, last_val_metrics)
(model_config, ddp_model, raw_model, swa_model, optimizer, metrics_obj, running_metrics, train_state, last_val_metrics) = load()
if "global_step_samples" not in train_state:
train_state["global_step_samples"] = 0
if max_train_bucket_per_new_data is not None and "train_bucket_level" not in train_state:
train_state["train_bucket_level"] = samples_per_epoch
if "train_steps_since_last_reload" not in train_state:
train_state["train_steps_since_last_reload"] = 0
if "export_cycle_counter" not in train_state:
train_state["export_cycle_counter"] = 0
if "window_start_data_row_idx" not in train_state:
train_state["window_start_data_row_idx"] = 0
if "total_num_data_rows" not in train_state:
train_state["total_num_data_rows"] = 0
if "old_train_data_dirs" not in train_state:
train_state["old_train_data_dirs"] = []
if "data_files_used" not in train_state:
train_state["data_files_used"] = set()
if "swa_sample_accum" not in train_state:
train_state["swa_sample_accum"] = 0.0
if intermediate_loss_scale is not None:
assert raw_model.get_has_intermediate_head(), "Model must have intermediate head to use intermediate loss"
# If the user specified an intermediate head but no loss scale, pick something reasonable by default
if raw_model.get_has_intermediate_head():
if intermediate_loss_scale is None and main_loss_scale is None:
if model_config["trunk_normless"]:
# fson-bnh default
assert model_config["intermediate_head_blocks"] == len(model_config["block_kind"]), "If these are unequal, don't know what you intend, please specify intermediate_loss_scale"
intermediate_loss_scale = 0.8
main_loss_scale = 0.2
else:
# Intermediate head in the middle of the trunk
intermediate_loss_scale = 0.5
main_loss_scale = 0.5
elif intermediate_loss_scale is None:
assert False, "Please specify both of main_loss_scale and intermediate_loss_scale or neither when using an architecture with an intermediate head."
logging.info(f"swa_period_samples {swa_period_samples}")
logging.info(f"swa_scale {swa_scale}")
logging.info(f"lookahead_alpha {lookahead_alpha}")
logging.info(f"lookahead_k {lookahead_k}")
logging.info(f"soft_policy_weight_scale {soft_policy_weight_scale}")
logging.info(f"disable_optimistic_policy {disable_optimistic_policy}")
logging.info(f"meta_kata_only_soft_policy {meta_kata_only_soft_policy}")
logging.info(f"value_loss_scale {value_loss_scale}")
logging.info(f"td_value_loss_scales {td_value_loss_scales}")
logging.info(f"seki_loss_scale {seki_loss_scale}")
logging.info(f"variance_time_loss_scale {variance_time_loss_scale}")
logging.info(f"main_loss_scale {main_loss_scale}")
logging.info(f"intermediate_loss_scale {intermediate_loss_scale}")
# Print all model parameters just to get a summary
total_num_params = 0
total_trainable_params = 0
logging.info("Parameters in model:")
for name, param in raw_model.named_parameters():
product = 1
for dim in param.shape:
product *= int(dim)
if param.requires_grad:
total_trainable_params += product
total_num_params += product
logging.info(f"{name}, {list(param.shape)}, {product} params")
logging.info(f"Total num params: {total_num_params}")
logging.info(f"Total trainable params: {total_trainable_params}")
lookahead_cache = {}
if lookahead_k is not None:
for param_group in optimizer.param_groups:
for param in param_group["params"]:
lookahead_cache[param] = torch.zeros_like(param.data)
lookahead_cache[param] = lookahead_cache[param].copy_(param.data)
logging.info(f"Using lookahead optimizer {lookahead_alpha} {lookahead_k}")
# EPOCHS AND LR ---------------------------------------------------------------------
def update_and_return_lr_and_wd():
per_sample_lr = 0.00003 * lr_scale * lr_scale_auto_factor(train_state)
# Warmup for initial training
warmup_scale = 1.0
if model_config["norm_kind"] == "fixup" or model_config["norm_kind"] == "fixscale" or model_config["norm_kind"] == "fixscaleonenorm":
if train_state["global_step_samples"] < 1000000:
warmup_scale = 1.0 / 5.0
elif train_state["global_step_samples"] < 2000000:
warmup_scale = 1.0 / 3.0
elif train_state["global_step_samples"] < 4000000:
warmup_scale = 1.0 / 2.0
elif train_state["global_step_samples"] < 6000000:
warmup_scale = 1.0 / 1.4
elif model_config["norm_kind"] == "bnorm" or model_config["norm_kind"] == "brenorm" or model_config["norm_kind"] == "fixbrenorm":
if train_state["global_step_samples"] < 250000:
warmup_scale = 1.0 / 20.0
elif train_state["global_step_samples"] < 500000:
warmup_scale = 1.0 / 14.0
elif train_state["global_step_samples"] < 750000:
warmup_scale = 1.0 / 10.0
elif train_state["global_step_samples"] < 1000000:
warmup_scale = 1.0 / 7.0
elif train_state["global_step_samples"] < 1250000:
warmup_scale = 1.0 / 5.0
elif train_state["global_step_samples"] < 1500000:
warmup_scale = 1.0 / 3.0
elif train_state["global_step_samples"] < 1750000:
warmup_scale = 1.0 / 2.0
elif train_state["global_step_samples"] < 2000000:
warmup_scale = 1.0 / 1.4
else:
warmup_scale = 1.0 / 1.0
else:
assert False
normal_weight_decay = 0.0
for param_group in optimizer.param_groups:
group_name = param_group["group_name"]
if group_name == "normal":
group_scale = 1.0
elif group_name == "normal_gamma":
group_scale = 1.0
elif group_name == "output":
group_scale = 0.5
elif group_name == "noreg":
group_scale = 1.0
elif group_name == "output_noreg":
group_scale = 0.5
else:
assert False
changed = False
# For lookahead optimizer, use weight decay appropriate for lr scale,
# but tell optimizer to take larger steps so as to maintain the same
# effective learning rate after lookahead averaging.
if lookahead_alpha is not None:
new_lr_this_group = per_sample_lr * warmup_scale * group_scale / lookahead_alpha
else:
new_lr_this_group = per_sample_lr * warmup_scale * group_scale
if param_group["lr"] != new_lr_this_group:
param_group["lr"] = new_lr_this_group
changed = True
new_weight_decay_this_group = get_weight_decay(
raw_model,
lr_scale,
warmup_scale=warmup_scale,
train_state=train_state,
running_metrics=running_metrics,
group_name=group_name,
)
if param_group["weight_decay"] != new_weight_decay_this_group:
param_group["weight_decay"] = new_weight_decay_this_group
changed = True
if group_name == "normal":
normal_weight_decay = param_group["weight_decay"]
if changed:
logging.info(f"Param group {param_group['group_name']} lr {param_group['lr']} weight_decay {param_group['weight_decay']}")
return per_sample_lr * warmup_scale, normal_weight_decay
last_brenorm_update_samples_this_instance = train_state["global_step_samples"]
def maybe_update_brenorm_params():
nonlocal last_brenorm_update_samples_this_instance
if model_config["norm_kind"] == "brenorm" or model_config["norm_kind"] == "fixbrenorm":
if "brenorm_rmax" not in train_state:
train_state["brenorm_rmax"] = 1.0
if "brenorm_dmax" not in train_state:
train_state["brenorm_dmax"] = 0.0
num_samples_elapsed = train_state["global_step_samples"] - last_brenorm_update_samples_this_instance
factor = math.exp(-num_samples_elapsed / brenorm_adjustment_scale)
train_state["brenorm_rmax"] = train_state["brenorm_rmax"] + (1.0 - factor) * (brenorm_target_rmax - train_state["brenorm_rmax"])
train_state["brenorm_dmax"] = train_state["brenorm_dmax"] + (1.0 - factor) * (brenorm_target_dmax - train_state["brenorm_dmax"])
raw_model.set_brenorm_params(brenorm_avg_momentum, train_state["brenorm_rmax"], train_state["brenorm_dmax"])
last_brenorm_update_samples_this_instance = train_state["global_step_samples"]
# DATA RELOADING GENERATOR ------------------------------------------------------------
# Some globals
last_curdatadir = None
trainfilegenerator = None
vdatadir = None
def maybe_reload_training_data():
nonlocal last_curdatadir
nonlocal trainfilegenerator
nonlocal vdatadir
assert rank == 0, "Helper ddp training processes should not call maybe_reload_training_data"
while True:
curdatadir = os.path.realpath(datadir)
# Different directory - new shuffle
if curdatadir != last_curdatadir:
if not os.path.exists(curdatadir):
if quit_if_no_data:
logging.info("Shuffled data path does not exist, there seems to be no data or not enough data yet, qutting: %s" % curdatadir)
sys.exit(0)
logging.info("Shuffled data path does not exist, there seems to be no shuffled data yet, waiting and trying again later: %s" % curdatadir)
time.sleep(30)
continue
trainjsonpath = os.path.join(curdatadir,"train.json")
if not os.path.exists(trainjsonpath):
if quit_if_no_data:
logging.info("Shuffled data train.json file does not exist, there seems to be no data or not enough data yet, qutting: %s" % trainjsonpath)
sys.exit(0)
logging.info("Shuffled data train.json file does not exist, there seems to be no shuffled data yet, waiting and trying again later: %s" % trainjsonpath)
time.sleep(30)
continue
logging.info("Updated training data: " + curdatadir)
last_curdatadir = curdatadir
with open(trainjsonpath) as f:
datainfo = json.load(f)
train_state["window_start_data_row_idx"] = datainfo["range"][0]
train_state["total_num_data_rows"] = datainfo["range"][1]
# Fill the buckets
if max_train_bucket_per_new_data is not None:
if "train_bucket_level_at_row" not in train_state:
train_state["train_bucket_level_at_row"] = train_state["total_num_data_rows"]
if train_state["total_num_data_rows"] > train_state["train_bucket_level_at_row"]:
new_row_count = train_state["total_num_data_rows"] - train_state["train_bucket_level_at_row"]
logging.info("Advancing trainbucket row %.0f to %.0f, %.0f new rows" % (
train_state["train_bucket_level_at_row"], train_state["total_num_data_rows"], new_row_count
))
train_state["train_bucket_level_at_row"] = train_state["total_num_data_rows"]
logging.info("Fill per data %.3f, Max bucket size %.0f" % (max_train_bucket_per_new_data, max_train_bucket_size))
logging.info("Old rows in bucket: %.0f" % train_state["train_bucket_level"])
train_state["train_bucket_level"] += new_row_count * max_train_bucket_per_new_data
cap = max(max_train_bucket_size, samples_per_epoch)
if train_state["train_bucket_level"] > cap:
train_state["train_bucket_level"] = cap
logging.info("New rows in bucket: %.0f" % train_state["train_bucket_level"])
if train_state["total_num_data_rows"] < train_state["train_bucket_level_at_row"]:
# Bucket went backward! This must be a network imported from a different run, reset the train bucket level
logging.warning("Train bucket last filled at %d rows but now there are only %d rows!" % (
train_state["train_bucket_level_at_row"], train_state["total_num_data_rows"]
))
logging.warning("Data was deleted or this network was transplanted into a new run, resetting the train bucket fill rows")
train_state["train_bucket_level_at_row"] = train_state["total_num_data_rows"]
logging.info("Train steps since last reload: %.0f -> 0" % train_state["train_steps_since_last_reload"])
train_state["train_steps_since_last_reload"] = 0
# Load training data files
tdatadir = os.path.join(curdatadir,"train")
train_files = [os.path.join(tdatadir,fname) for fname in os.listdir(tdatadir) if fname.endswith(".npz")]
epoch0_train_files = [path for path in train_files if path not in train_state["data_files_used"]]
if no_repeat_files:
logging.info(f"Dropping {len(train_files)-len(epoch0_train_files)}/{len(train_files)} files in: {tdatadir} as already used")
else:
logging.info(f"Skipping {len(train_files)-len(epoch0_train_files)}/{len(train_files)} files in: {tdatadir} as already used first pass")
if len(train_files) <= 0 or (no_repeat_files and len(epoch0_train_files) <= 0):
if quit_if_no_data:
logging.info(f"No new training files found in: {tdatadir}, quitting")
sys.exit(0)
logging.info(f"No new training files found in: {tdatadir}, waiting 30s and trying again")
time.sleep(30)
continue
# Update history of what training data we used
if tdatadir not in train_state["old_train_data_dirs"]:
train_state["old_train_data_dirs"].append(tdatadir)
# Clear out tracking of sufficiently old files
while len(train_state["old_train_data_dirs"]) > 20:
old_dir = train_state["old_train_data_dirs"][0]
train_state["old_train_data_dirs"] = train_state["old_train_data_dirs"][1:]
for filename in list(train_state["data_files_used"]):
if filename.startswith(old_dir):
train_state["data_files_used"].remove(filename)
def train_files_gen():
train_files_shuffled = epoch0_train_files.copy()
while True:
random.shuffle(train_files_shuffled)
for filename in train_files_shuffled:
logging.info("Yielding training file for dataset: " + filename)
train_state["data_files_used"].add(filename)
yield filename
if no_repeat_files:
break
else:
train_files_shuffled = train_files.copy()
train_state["data_files_used"] = set()
trainfilegenerator = train_files_gen()
vdatadir = os.path.join(curdatadir,"val")
# Same directory as before, no new shuffle
else:
if max_train_steps_since_last_reload is not None:
if train_state["train_steps_since_last_reload"] + 0.99 * samples_per_epoch/sub_epochs > max_train_steps_since_last_reload:
logging.info(
"Too many train steps since last reload, waiting 5m and retrying (current %f)" %
train_state["train_steps_since_last_reload"]
)
time.sleep(300)
continue
break
# Load all the files we should train on during a subepoch
def get_files_for_subepoch():
nonlocal trainfilegenerator
assert rank == 0, "Helper ddp training processes should not call get_files_for_subepoch"
num_batches_per_epoch = int(round(samples_per_epoch / batch_size))
num_batches_per_subepoch = num_batches_per_epoch / sub_epochs
# Pick enough files to get the number of batches we want
train_files_to_use = []
batches_to_use_so_far = 0
found_enough = False
for filename in trainfilegenerator:
jsonfilename = os.path.splitext(filename)[0] + ".json"
with open(jsonfilename) as f:
trainfileinfo = json.load(f)
num_batches_this_file = trainfileinfo["num_rows"] // batch_size
if num_batches_this_file <= 0:
continue
if batches_to_use_so_far + num_batches_this_file > num_batches_per_subepoch:
# If we're going over the desired amount, randomly skip the file with probability equal to the
# proportion of batches over - this makes it so that in expectation, we have the desired number of batches
if batches_to_use_so_far > 0 and random.random() >= (batches_to_use_so_far + num_batches_this_file - num_batches_per_subepoch) / num_batches_this_file:
found_enough = True
break
train_files_to_use.append(filename)
batches_to_use_so_far += num_batches_this_file
#Sanity check - load a max of 100000 files.
if batches_to_use_so_far >= num_batches_per_subepoch or len(train_files_to_use) > 100000:
found_enough = True
break
if found_enough:
return train_files_to_use
return None
# METRICS -----------------------------------------------------------------------------------
def detensorify_metrics(metrics):
ret = {}
for key in metrics:
if isinstance(metrics[key], torch.Tensor):
ret[key] = metrics[key].detach().cpu().item()
else:
ret[key] = metrics[key]
return ret
if rank == 0:
train_metrics_out = open(os.path.join(traindir,"metrics_train.json"),"a")
val_metrics_out = open(os.path.join(traindir,"metrics_val.json"),"a")
else:
train_metrics_out = open(os.path.join(traindir,f"metrics_train_rank{rank}.json"),"a")
val_metrics_out = open(os.path.join(traindir,f"metrics_val_rank{rank}.json"),"a")
# TRAIN! -----------------------------------------------------------------------------------
last_longterm_checkpoint_save_time = datetime.datetime.now()
num_epochs_this_instance = 0
print_train_loss_every_batches = 100 if not gnorm_stats_debug else 1000
if "sums" not in running_metrics:
running_metrics["sums"] = defaultdict(float)
else:
running_metrics["sums"] = defaultdict(float,running_metrics["sums"])
if "weights" not in running_metrics:
running_metrics["weights"] = defaultdict(float)
else:
running_metrics["weights"] = defaultdict(float,running_metrics["weights"])
torch.backends.cudnn.benchmark = True
if use_fp16:
logging.info("Training in FP16! Creating scaler")
scaler = GradScaler()
else:
logging.info("Training in FP32.")
# All ddp threads should be lined up at this point before continuing
if barrier is not None:
barrier.wait()
while True:
if max_epochs_this_instance is not None and max_epochs_this_instance >= 0 and num_epochs_this_instance >= max_epochs_this_instance:
logging.info("Hit max epochs this instance, done")
break
if max_training_samples is not None and train_state["global_step_samples"] >= max_training_samples:
logging.info("Hit max training samples, done")
break
if rank == 0:
maybe_reload_training_data()
if max_train_bucket_per_new_data is not None:
if train_state["train_bucket_level"] > 0.99 * samples_per_epoch:
logging.info("Consuming %.0f rows from train bucket (%.0f -> %.0f)" % (
samples_per_epoch, train_state["train_bucket_level"], train_state["train_bucket_level"]-samples_per_epoch
))
train_state["train_bucket_level"] -= samples_per_epoch
else:
if stop_when_train_bucket_limited:
logging.info(
"Exceeding train bucket, not enough new data rows, terminating (current level %f)" %
train_state["train_bucket_level"]
)
break
else:
logging.info(
"Exceeding train bucket, not enough new data rows, waiting 5m and retrying (current level %f)" %
train_state["train_bucket_level"]
)
time.sleep(300)
continue
# DDP need to wait on the main process after reloading data and/or training bucket waiting
if barrier is not None:
barrier.wait()
logging.info("GC collect")
gc.collect()
clear_metric_nonfinite(running_metrics["sums"], running_metrics["weights"])
logging.info("=========================================================================")
logging.info("BEGINNING NEXT EPOCH " + str(num_epochs_this_instance))
logging.info("=========================================================================")
logging.info("Current time: " + str(datetime.datetime.now()))
logging.info("Global step: %d samples" % (train_state["global_step_samples"]))
logging.info("Currently up to data row " + str(train_state["total_num_data_rows"]))
logging.info(f"Training dir: {traindir}")
logging.info(f"Export dir: {exportdir}")
if use_fp16:
logging.info(f"Current grad scale: {scaler.get_scale()}")
lr_right_now, normal_weight_decay_right_now = update_and_return_lr_and_wd()
maybe_update_brenorm_params()
# SUB EPOCH LOOP -----------
batch_count_this_epoch = 0
last_train_stats_time = time.perf_counter()
for i in range(sub_epochs):
if rank == 0:
if i != 0:
maybe_reload_training_data()
train_files_to_use = get_files_for_subepoch()
while train_files_to_use is None or len(train_files_to_use) <= 0:
if quit_if_no_data:
logging.info("Not enough data files to fill a subepoch! Quitting.")