forked from ReaLLMASIC/nanoGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
864 lines (720 loc) · 47.4 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
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
import argparse
from contextlib import nullcontext
import csv
import json
import math
import os
import pickle
import shutil
import sys
import time
from torchinfo import summary
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objects as go
from rich import print
import torch
from torch.distributed import destroy_process_group, init_process_group
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.tensorboard import SummaryWriter
from statistics_util.statistic_plots import initialize_statistics, plot_statistics, create_statistics
from variations.model_variations import model_variation_dictionary
from model import GPT, GPTConfig
def parse_args():
parser = argparse.ArgumentParser()
# argparse groups
model_group = parser.add_argument_group('model_group')
training_group = parser.add_argument_group('training_group')
logging_group = parser.add_argument_group('logging_group')
# I/O args
training_group.add_argument('--out_dir', default='out', type=str)
training_group.add_argument('--eval_interval', default=250, type=int)
training_group.add_argument('--log_interval', default=10, type=int)
training_group.add_argument('--eval_iters', default=200, type=int)
training_group.add_argument('--eval_only', default=False, action=argparse.BooleanOptionalAction)
# Checkpoint args
training_group.add_argument('--only_save_checkpoint_at_end', default=False, action=argparse.BooleanOptionalAction)
training_group.add_argument('--always_save_checkpoint', default=False, action=argparse.BooleanOptionalAction)
training_group.add_argument('--patience', default=None, type=int, help="if set, will stop training if the number of evaluations since val loss was seen to decrease exceeds 'patience' setting.")
training_group.add_argument('--init_from', default='scratch', choices=['scratch', 'prev_run', 'resume', 'gpt2*'], type=str)
training_group.add_argument('--gpt2_type', default='gpt2', type=str)
training_group.add_argument('--prev_run_ckpt', default='', type=str)
training_group.add_argument('--csv_ckpt_dir', default='', type=str)
# Data args
training_group.add_argument('--dataset', default='shakespeare_char', type=str)
training_group.add_argument('--batch_size', default=64, type=int)
training_group.add_argument("--seed", default=1337, type=int)
# Model args
model_group.add_argument('--block_size', default=256, type=int)
model_group.add_argument('--n_layer', default=6, type=int)
model_group.add_argument('--n_head', default=6, type=int)
model_group.add_argument('--n_kv_group', default=None, type=int)
model_group.add_argument('--n_embd', default=384, type=int)
model_group.add_argument('--dropout', default=0.2, type=float)
model_group.add_argument('--use_post_ln', default=False, action=argparse.BooleanOptionalAction)
model_group.add_argument('--window_size', default=None, type=int, help="Sliding window size, note this cannot be greater than block size")
model_group.add_argument('--gate', default=False, action=argparse.BooleanOptionalAction, help="option for gated attention see https://arxiv.org/abs/2306.12929")
model_group.add_argument('--use_moe', default=False, action=argparse.BooleanOptionalAction, help="option for Mixture of Experts (MoE) architecture")
model_group.add_argument('--moe_layer_freq', default=2, type=int, help="set frequency for replacing FFNs with MoE layers")
model_group.add_argument('--n_experts', default=8, type=int, help="set number of experts per MoE layer")
model_group.add_argument('--moe_top_k', default=2, type=int)
model_group.add_argument('--moe_router_scheme', default="softmax", type=str, help="option to set routing scheme for MoE layer, defaults to softmax")
## MLP Options
model_group.add_argument('--use_parallel_mlp', default=False, action=argparse.BooleanOptionalAction)
model_group.add_argument("--mlp_variant", type=str, default="mlp", choices=["mlp", "kan", "swiglu"], help="MLP variation type")
## KAN Options
model_group.add_argument("--kan_poly_order", type=int, default=3, help="Order of KAN non-linearity")
model_group.add_argument("--kan_base_activation", type=str, default="silu", help="initial KAN activation")
model_group.add_argument("--kan_middle_layers", type=int, nargs='+', help="List of integers", default=[])
# Shared Parameter Settings
model_group.add_argument('--shared_mlp_size', default=1, type=int, help="every 'k' contiguous blocks of mlp are shared")
model_group.add_argument('--shared_mlp_sym', default=False, action=argparse.BooleanOptionalAction)
model_group.add_argument('--shared_attn_size', default=1, type=int, help="every 'k' contiguous blocks of attn are shared")
model_group.add_argument('--shared_attn_sym', default=False, action=argparse.BooleanOptionalAction, help="symmetrical attention sharing")
# NORM VARIATIONS
model_group.add_argument("--norm_variant_attn", type=str, default="rmsnorm", choices=["krmsnorm", "prmsnorm", "rmsnorm", "layernorm"])
model_group.add_argument("--norm_variant_output", type=str, default="rmsnorm", choices=["krmsnorm", "prmsnorm", "rmsnorm", "layernorm"])
model_group.add_argument('--bias', default=False, action=argparse.BooleanOptionalAction, help="only used for layernorm variation option")
model_group.add_argument("--prmsnorm_pct", default=0.0625, type=float, help="percentage (1 being 100 percent) of first entries used for partial rms" )
model_group.add_argument("--krmsnorm_num", default=10, type=int, help="max number of first entries for partial rms" )
model_group.add_argument("--krmsnorm_quantize_type", type=str, default="none", choices=["int8", "int16", "none"])
model_group.add_argument('--krmsnorm_enable_gain', default=True, action=argparse.BooleanOptionalAction, help="include gain in kRMSNorm")
model_group.add_argument("--krmsnorm_selection_type", type=str, default="last", choices=["first", "last", "random"])
model_group.add_argument("--krmsnorm_recompute_percentage", type=float, default=None, help="percentage needed within the total RMS to not trigger recompute")
activation_variations = [
"celu",
"elu",
"gelu",
"glu",
"leaky_relu",
"mish",
"prelu",
"relu6",
"rrelu",
"selu",
"sigmoid",
"silu",
"softplus",
"softsign",
"squared_relu",
"tanh",
]
# ACTIVATION VARIATIONS
model_group.add_argument( "--activation_variant", type=str, default="gelu", choices=activation_variations,)
# LINEAR VARIATIONS
linear_variants = ["linear", "bitlinear", "bitlinear_1p58", "bitlinear_optimized", "kan","quantized_linear"]
model_group.add_argument("--linear_variant_attn", type=str, default="linear", choices=linear_variants)
model_group.add_argument("--linear_variant_q", type=str, default=None, choices=linear_variants, help="sets the linear variant for c_attn_q in attention (takes precedence over linear_variant_attn)")
model_group.add_argument("--linear_variant_k", type=str, default=None, choices=linear_variants, help="sets the linear variant for c_attn_k in attention (takes precedence over linear_variant_attn)")
model_group.add_argument("--linear_variant_v", type=str, default=None, choices=linear_variants, help="sets the linear variant for c_attn_v in attention (takes precedence over linear_variant_attn)")
model_group.add_argument("--linear_variant_attn_proj", type=str, default=None, choices=linear_variants, help="sets the linear variant for c_proj in attention (takes precedence over linear_variant_attn)")
model_group.add_argument("--linear_variant_mlp", type=str, default="linear", choices=linear_variants)
model_group.add_argument("--linear_variant_mlp_up", type=str, default=None, choices=linear_variants, help="sets the linear variant for c_fc in mlp (takes precedence over linear_variant_mlp)")
model_group.add_argument("--linear_variant_mlp_down", type=str, default=None, choices=linear_variants, help="sets the linear variant for c_proj in mlp (takes precedence over linear_variant_mlp)")
## Linear Weight Initialization Options
model_group.add_argument( "--linear_mean_init", type=float, default=0.0)
model_group.add_argument( "--linear_std_init", type=float, default=0.02)
# Quatization
## Quantization Method Options
quant_methods = ["symmetric_quant", "affine_quant", "stochastic_quant"]
## WTE
model_group.add_argument("--quantize_wte", default=None, action=argparse.BooleanOptionalAction, help="Whether the word embedding is quantized")
model_group.add_argument("--quantize_wte_method", type=str, default="affine_quant", choices=quant_methods, help="function used for word embedding quantization")
model_group.add_argument("--quantize_wte_bits", type=int, default=8, help="number of bits for word embedding quantization")
## WPE
model_group.add_argument("--quantize_wpe", default=None, action=argparse.BooleanOptionalAction, help="Whether the word position embedding is quantized")
model_group.add_argument("--quantize_wpe_method", type=str, default="affine_quant", choices=quant_methods, help="function used for position embedding quantization")
model_group.add_argument("--quantize_wpe_bits", type=int, default=8, help="number of bits for position embedding quantization")
## Activations
model_group.add_argument("--activations_quant_method", type=str, default="affine_quant", choices=quant_methods, help="function used for quantization of activations")
### Attention Activations
model_group.add_argument("--quantize_attn_act", action=argparse.BooleanOptionalAction, default=False, help="quantize all input/output activations in attn")
#### Whether to do Attention Activation quantization at the Arrow
model_group.add_argument("--quantize_attn_act_input", action=argparse.BooleanOptionalAction, default=False, help="quantize input activation to attention")
model_group.add_argument("--quantize_attn_act_qk_mult_q_input", action=argparse.BooleanOptionalAction, default=False, help="quantize query input activation to qk mult")
model_group.add_argument("--quantize_attn_act_qk_mult_k_input", action=argparse.BooleanOptionalAction, default=False, help="quantize key input activation to qk mult")
model_group.add_argument("--quantize_attn_act_softmax_input", action=argparse.BooleanOptionalAction, default=False, help="quantize input activation to softmax")
model_group.add_argument("--quantize_attn_act_pv_mult_p_input", action=argparse.BooleanOptionalAction, default=False, help="quantize softmax input activation to pv mult")
model_group.add_argument("--quantize_attn_act_pv_mult_v_input", action=argparse.BooleanOptionalAction, default=False, help="quantize value input activation to pv mult")
model_group.add_argument("--quantize_attn_act_pv_mult_output", action=argparse.BooleanOptionalAction, default=False, help="quantize output activation of pv_mult")
model_group.add_argument("--quantize_attn_act_output", action=argparse.BooleanOptionalAction, default=False, help="quantize output activation of attention")
### Default Precisions for Attention Activations
model_group.add_argument("--quantize_attn_act_bits", type=int, default=8, help="number of bits for attn quantization")
### Overrides for granular Attention Activatinos
model_group.add_argument("--quantize_attn_act_input_bits", type=int, default=None, help="number of bits for attention input quantization")
model_group.add_argument("--quantize_attn_act_qk_mult_q_input_bits", type=int, default=None, help="number of bits for qk mult query input quantization")
model_group.add_argument("--quantize_attn_act_qk_mult_k_input_bits", type=int, default=None, help="number of bits for qk mult key input quantization")
model_group.add_argument("--quantize_attn_act_softmax_input_bits", type=int, default=None, help="number of bits for softmax input quantization")
model_group.add_argument("--quantize_attn_act_pv_mult_p_input_bits", type=int, default=None, help="number of bits for pv mult softmax input quantization")
model_group.add_argument("--quantize_attn_act_pv_mult_v_input_bits", type=int, default=None, help="number of bits for pv mult value input quantization")
model_group.add_argument("--quantize_attn_act_pv_mult_output_bits", type=int, default=None, help="number of bits for pv mult output quantization")
model_group.add_argument("--quantize_attn_act_output_bits", type=int, default=None, help="number of bits for attention output quantization")
### Whether to use MLP Activations
model_group.add_argument("--quantize_mlp_act", action=argparse.BooleanOptionalAction, default=False, help="quantize all input/output activations in mlp")
model_group.add_argument("--quantize_mlp_act_input", action=argparse.BooleanOptionalAction, default=False, help="quantize input activation to mlp")
model_group.add_argument("--quantize_mlp_act_activation_input", action=argparse.BooleanOptionalAction, default=False, help="quantize input activation to activation function")
model_group.add_argument("--quantize_mlp_act_activation_output", action=argparse.BooleanOptionalAction, default=False, help="quantize output activation of activation function")
model_group.add_argument("--quantize_mlp_act_output", action=argparse.BooleanOptionalAction, default=False, help="quantize output activation of mlp")
### Default Precisions for MLP Activations
model_group.add_argument("--quantize_mlp_act_bits", type=int, default=8, help="number of bits for mlp quantization")
### Overrides for granular MLP Activatinos
model_group.add_argument("--quantize_mlp_act_input_bits", type=int, default=None, help="number of bits for mlp input quantization")
model_group.add_argument("--quantize_mlp_act_activation_input_bits", type=int, default=None, help="number of bits for activation function input quantization")
model_group.add_argument("--quantize_mlp_act_activation_output_bits", type=int, default=None, help="number of bits for activation function output quantization")
model_group.add_argument("--quantize_mlp_act_output_bits", type=int, default=None, help="number of bits for mlp output quantization")
### Whether activations should be saved
model_group.add_argument("--store_activations", action=argparse.BooleanOptionalAction, default=False, help="whether the activations should be saved as a buffer and updated through training")
## Linear Attn Weight Quantization Precision and Method
### Default methods and precisions
model_group.add_argument("--quantize_linear_method", type=str, default="affine_quant", choices=quant_methods, help="function used for linear quantization")
model_group.add_argument("--quantize_linear_bits", type=int, default=8, help="number of bits for linear quantization")
#### Overrides for granular Methods and Precisions
model_group.add_argument("--quantize_linear_attn_q_method", type=str, default=None, choices=quant_methods, help="function used for c_attn_q quantization")
model_group.add_argument("--quantize_linear_attn_q_bits", type=int, default=None, help="number of bits for c_attn_q quantization")
model_group.add_argument("--quantize_linear_attn_k_method", type=str, default=None, choices=quant_methods, help="function used for c_attn_k quantization")
model_group.add_argument("--quantize_linear_attn_k_bits", type=int, default=None, help="number of bits for c_attn_k quantization")
model_group.add_argument("--quantize_linear_attn_v_method", type=str, default=None, choices=quant_methods, help="function used for c_attn_v quantization")
model_group.add_argument("--quantize_linear_attn_v_bits", type=int, default=None, help="number of bits for c_attn_v quantization")
model_group.add_argument("--quantize_linear_attn_proj_method", type=str, default=None, choices=quant_methods, help="function used for c_proj in attention quantization")
model_group.add_argument("--quantize_linear_attn_proj_bits", type=int, default=None, help="number of bits for c_proj in attention quantization")
#### Overrides for Linear MLP Weight Quantization Precision and Method
model_group.add_argument("--quantize_linear_mlp_up_method", type=str, default=None, choices=quant_methods, help="function used for mlp_up quantization")
model_group.add_argument("--quantize_linear_mlp_up_bits", type=int, default=None, help="number of bits for mlp_up quantization")
model_group.add_argument("--quantize_linear_mlp_down_method", type=str, default=None, choices=quant_methods, help="function used for mlp_down quantization")
model_group.add_argument("--quantize_linear_mlp_down_bits", type=int, default=None, help="number of bits for mlp_down quantization")
## Quantized Linear Warmup Iterations -- how many to first use regular linear, before switching to quantized
model_group.add_argument("--quantization_warmup_iters", type=int, default=100)
# POSITIONAL EMBEDDING VARIATIONS
model_group.add_argument('--use_rotary_embeddings', default=False, action=argparse.BooleanOptionalAction)
model_group.add_argument('--sym_rot_num_angles', type=int, default=512, help="number of angles to use for symmetric rope variant")
model_group.add_argument("--rope_variant", type=str, default="rope", choices=["rope", "soap"])
model_group.add_argument("--rope_length", type=int, default=None, help="Defaults to all embeddings (if set to None), else must be even.")
model_group.add_argument('--use_abs_pos_embeddings', default=True, action=argparse.BooleanOptionalAction)
model_group.add_argument('--use_fire_embeddings', default=False, action=argparse.BooleanOptionalAction)
model_group.add_argument('--shared_fire_embeddings', default=False, action=argparse.BooleanOptionalAction)
## Positional Embedding Weight Initialization Options
model_group.add_argument( "--embedding_mean_init", type=float, default=0.0)
model_group.add_argument( "--embedding_std_init", type=float, default=0.02)
# SOFTMAX VARIATIONS
softmax_variations = [
"saturatingconsmax",
"consmax",
"consmax_quan",
"polymax",
"relumax",
"vpolymax",
"exppolymax",
"strongermax",
"softermax",
"sigsoftmax",
"softmax",
"softplus",
"squareplus",
"exppolymax",
]
## Selection of softmax variation for attention and output layers
model_group.add_argument("--softmax_variant_attn", type=str, default="softmax", choices=softmax_variations)
model_group.add_argument("--softmax_variant_output", type=str, default="softmax", choices=softmax_variations)
## Custom Softmax Variation Options
### ConSmax and SaturatingConSmax Options
model_group.add_argument("--consmax_initial_beta", type=float, default=2.5)
model_group.add_argument("--consmax_initial_gamma", type=float, default=100.0)
model_group.add_argument('--consmax_use_euler_base', default=True, action=argparse.BooleanOptionalAction)
model_group.add_argument("--consmax_base", type=float, default=2.0)
### Special Options for SaturatingConSmax
model_group.add_argument("--consmax_saturation", type=float, default=11.0, help="point where we transition from consmax to linear saturatingconsmax, defaults to 11 to approximate e^x sat for fp16")
model_group.add_argument('--consmax_learnable_beta', default=True, action=argparse.BooleanOptionalAction)
model_group.add_argument('--consmax_learnable_gamma', default=True, action=argparse.BooleanOptionalAction)
### Polymax Options
model_group.add_argument("--polymax_x_intercept", type=float, default=-100.0)
model_group.add_argument("--polymax_y_intercept", type=float, default=1.0)
model_group.add_argument("--polymax_power", type=float, default=2.0)
model_group.add_argument("--polymax_divisor", type=float, default=1000.0)
### ReLUMax Options
model_group.add_argument("--relumax_divisor", type=float, default=256.0)
### SigSoftmax Options
model_group.add_argument('--sigsoftmax_use_euler_base', default=True, action=argparse.BooleanOptionalAction)
model_group.add_argument("--sigsoftmax_base", type=float, default=2.0)
### Strongermax Options - Testing Incremental Adjustments to Regular Softmax
model_group.add_argument("--strongermax_strength", type=float, default=4.0)
model_group.add_argument('--strongermax_sum_to_1', default=True, action=argparse.BooleanOptionalAction)
model_group.add_argument("--strongermax_divisor", type=float, default=1.0)
model_group.add_argument('--strongermax_use_xmax', default=True, action=argparse.BooleanOptionalAction)
### ExpPolymax Options
model_group.add_argument('--exppolymax_use_euler_base', default=True, action=argparse.BooleanOptionalAction)
model_group.add_argument("--exppolymax_base", type=float, default=4.0)
model_group.add_argument("--exppolymax_y_intercept", type=float, default=1.0)
model_group.add_argument("--exppolymax_power", type=float, default=2.0)
model_group.add_argument("--exppolymax_divisor", type=float, default=1000.0)
### Softermax Specific Options
model_group.add_argument('--softermax_use_xmax', default=True, action=argparse.BooleanOptionalAction)
### SoftPlus Options
model_group.add_argument('--softplus_divisor', type=float,default=100.0)
### SquarePlus Options
model_group.add_argument('--squareplus_divisor', type=float,default=100.0)
### Sequence Length Division https://arxiv.org/abs/2309.
model_group.add_argument('--div_by_seq_len', default=False, action=argparse.BooleanOptionalAction)
# Gradient Checkpointing
training_group.add_argument('--use_gradient_checkpointing', default=False, action=argparse.BooleanOptionalAction, help="Memory efficient training, but takes longer time to train due to trading compute time for memory efficiency. For best memory tradeoff omit the --compile flag. For medium memory tradeoff add --compile.")
# Optimizer args
training_group.add_argument('--max_iters', default=3500, type=int)
training_group.add_argument('--weight_decay', default=1e-1, type=float)
training_group.add_argument('--beta1', default=0.9, type=float)
training_group.add_argument('--beta2', default=0.99, type=float)
training_group.add_argument('--grad_clip', default=1.0, type=float)
# LR schedule args
training_group.add_argument('--learning_rate', default=1e-3, type=float)
training_group.add_argument('--min_lr', default=1e-4, type=float)
training_group.add_argument('--decay_lr', default=False, action=argparse.BooleanOptionalAction)
training_group.add_argument('--lr_decay_iters', default=3500, type=int)
training_group.add_argument('--lr_decay_match_max_iters', default=True, action=argparse.BooleanOptionalAction)
training_group.add_argument('--warmup_iters', default=100, type=int)
# DDP args
training_group.add_argument('--backend', default='nccl', type=str)
training_group.add_argument('--gradient_accumulation_steps', default=1, type=int)
# System args
training_group.add_argument('--device', default='cuda', type=str)
training_group.add_argument("--dtype", type=str, default="float16", choices=["bfloat16", "float16", "float32"], help="torch data type for inference, e.g. 'int8'")
training_group.add_argument('--compile', default=False, action=argparse.BooleanOptionalAction)
# Logging args
logging_group.add_argument('--log_project', default='out-test', type=str)
logging_group.add_argument('--log_run_name', default='logs-test', type=str)
logging_group.add_argument('--timestamp', default='', type=str)
logging_group.add_argument('--save_nan_checkpoint', default=False, action=argparse.BooleanOptionalAction)
# CSV logging
logging_group.add_argument('--csv_log', default=True, action=argparse.BooleanOptionalAction)
logging_group.add_argument('--csv_dir', default='csv_logs', type=str)
logging_group.add_argument('--csv_name', default='output', type=str, help="Output csv basename. Note, the .csv will be automatically appended.")
# Tensorboard args
logging_group.add_argument('--tensorboard_log', default=True, action=argparse.BooleanOptionalAction)
logging_group.add_argument('--tensorboard_log_dir', type=str, default='logs')
logging_group.add_argument('--tensorboard_run_name', type=str, default='logs-test')
# Wandb args
logging_group.add_argument('--wandb_log', default=False, action=argparse.BooleanOptionalAction)
logging_group.add_argument('--wandb_project', type=str, default='out-test')
logging_group.add_argument('--wandb_run_name', type=str, default='logs-test')
### Create model from json config file & save config file to json
logging_group.add_argument('--load_config_json', type=str, help="Option to load model parameters from existing json file")
logging_group.add_argument('--save_config_json', type=str, help="Option to save model parameters as new config json file")
# Visualization args
logging_group.add_argument('--statistic', choices=[
'input_mean', 'input_median', 'input_stdev', 'input_max', 'input_min',
'output_mean', 'output_median', 'output_stdev', 'output_max', 'output_min', 'all_stats', 'input_all','output_all'
], default='input_mean', help='Select one or all statistics to display, e.g., --statistic input_min, or --statistic all_stats')
logging_group.add_argument('--graph_type', choices=[
"heatmap", "plot", "boxplot", "all"
], default='no_graph', help='Select one of the graph types to display, e.g., --graph_type heatmap, or --graph_type plot')
logging_group.add_argument('--box_plot_interval', default=1000, type=int, help='Instead of using mean/median/stdev statistics, create box plot of all input/output values at certain intervals of iteration')
logging_group.add_argument('--box_plot_statistic', choices=['input', 'output', 'all'],
default='', help='Select input or output statistic to display in boxplot')
# Model Parameter Distribution
logging_group.add_argument('--print_block_summary', default=False, action=argparse.BooleanOptionalAction)
args = parser.parse_args()
if args.load_config_json is not None:
with open(args.load_config_json, 'r') as config_file:
config = json.load(config_file)
# Update the args namespace with values from the JSON file
for key, value in config.items():
setattr(args, key, value)
# Save all params to provided json if flag is present
if args.save_config_json is not None:
with open(args.save_config_json, 'w') as json_file:
json.dump(vars(args), json_file)
return args, model_group, training_group, logging_group
class Trainer:
def __init__(self, args, model_group, training_group, logging_group):
self.args = args
self.model_group = model_group
self.training_group = training_group
self.logging_group = logging_group
# typically make the decay iters equal to max_iters
if self.args.lr_decay_match_max_iters:
self.args.lr_decay_iters = self.args.max_iters
self.setup()
self.stats = initialize_statistics(self.args.n_layer, self.args.n_head)
def setup(self):
# Setup DDP
self.ddp = int(os.environ.get('RANK', -1)) != -1
if self.ddp:
init_process_group(backend=self.args.backend)
self.ddp_rank = int(os.environ['RANK'])
self.ddp_local_rank = int(os.environ['LOCAL_RANK'])
self.ddp_world_size = int(os.environ['WORLD_SIZE'])
self.device = f'cuda:{self.ddp_local_rank}'
print("this is my device", self.device)
torch.cuda.set_device(self.device)
self.master_process = self.ddp_rank == 0
self.seed_offset = self.ddp_rank
self.args.gradient_accumulation_steps //= self.ddp_world_size
else:
self.device = self.args.device
self.master_process = True
self.seed_offset = 0
self.ddp_world_size = 1
self.tokens_per_iter = self.args.gradient_accumulation_steps * self.ddp_world_size * self.args.batch_size * self.args.block_size
if self.master_process:
os.makedirs(self.args.out_dir, exist_ok=True)
print("seed: ", self.args.seed)
print("seed offset: ", self.seed_offset)
torch.manual_seed(self.args.seed + self.seed_offset)
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
self.device_type = 'cuda' if 'cuda' in self.args.device else 'cpu'
self.ptdtype = {"bfloat16" : torch.bfloat16, "float16" : torch.float16, "float32" : torch.float32}[self.args.dtype]
self.ctx = nullcontext() if self.device_type == 'cpu' else torch.amp.autocast(device_type=self.device_type, dtype=self.ptdtype)
# Model settings
# TODO only add if they are defined from the argparse
self.model_args = {action.dest: getattr(self.args, action.dest) for action in self.model_group._group_actions}
self.model_args['vocab_size'] = None
self.model_args['use_gradient_checkpointing'] = self.args.use_gradient_checkpointing
# Training settings
self.training_args = {action.dest: getattr(self.args, action.dest) for action in self.training_group._group_actions}
if self.args.init_from == 'scratch':
self.model_args['vocab_size'] = self.get_vocab_size_from_meta()
# Save full configuration used for training
config_json = {**self.model_args, **self.training_args}
with open(self.args.out_dir + "/full_config.json", "w") as configuration_file:
json.dump(config_json, configuration_file, indent=4)
with open(self.args.out_dir + "/best_val_loss_and_iter.txt", 'w') as file:
print("resetting best val loss file")
self.load_data()
gptconf = GPTConfig(**self.model_args)
self.model = GPT(gptconf)
self.iter_num = 0 # for starting from scratch
self.best_val_loss = 1e9 # really big number
elif self.args.init_from == 'resume' or self.args.init_from == 'prev_run':
if self.args.init_from == 'resume':
ckpt_path = os.path.join(self.args.out_dir, 'ckpt.pt')
checkpoint = torch.load(ckpt_path, map_location=self.device)
self.iter_num = checkpoint['iter_num']
else:
ckpt_path = os.path.join(self.args.prev_run_ckpt, 'ckpt.pt')
checkpoint = torch.load(ckpt_path, map_location=self.device)
self.iter_num = 0
# we should enforce that during resume training, the identical model args are used
checkpoint_model_args = checkpoint['model_args']
self.model_args = checkpoint_model_args
# support for changing select params from resume (eg. for finetuning) based on cmd-line args entered (checks if diff than defaults)
altered_model_args = {action.dest: getattr(self.args, action.dest) for action in self.model_group._group_actions if action.default != getattr(self.args, action.dest)}
for k in altered_model_args:
self.model_args[k] = altered_model_args[k]
self.load_data()
gptconf = GPTConfig(**self.model_args)
self.model = GPT(gptconf)
## TODO: Add ability here to swap WTE factors.
state_dict = checkpoint['model']
for k,v in list(state_dict.items()):
if k.startswith('_orig_mod.'):
state_dict[k[len('_orig_mod.'):]] = state_dict.pop(k)
self.model.load_state_dict(state_dict)
self.best_val_loss = checkpoint['best_val_loss']
elif self.args.init_from.startswith('gpt2'):
assert self.args.gpt2_type in model_variation_dictionary
self.iter_num = 0 # for starting from scratch
self.best_val_loss = 1e9 # really big number
variation_dict = model_variation_dictionary[self.args.gpt2_type]
# NOTE: the hierarchy of parameters goes: 1)variation_dict >> 2)cmd-line args >> 3)GPTConfig defaults
for k in variation_dict:
self.model_args[k] = variation_dict[k]
gptconf = GPTConfig(**self.model_args)
self.model = GPT.from_pretrained(gptconf, model_type=self.args.gpt2_type)
self.load_data()
if self.args.block_size < self.model.config.block_size:
self.model.crop_block_size(self.args.block_size)
self.model_args['block_size'] = self.args.block_size
self.model.to(self.device)
# Print the model summary
summary(self.model)
if self.args.print_block_summary:
for idx, block in enumerate(self.model.transformer.h):
print(f"Summary for Block {idx + 1}:")
summary(block)
# Optimizer
self.scaler = torch.cuda.amp.GradScaler(enabled=(self.args.dtype == 'float16'))
self.optimizer = self.model.configure_optimizers(self.args.weight_decay, self.args.learning_rate,
(self.args.beta1, self.args.beta2), self.device_type)
if self.args.compile:
print("compiling the model... (takes a ~minute)")
self.unoptimized_model = self.model
self.model = torch.compile(self.model)
if self.ddp:
self.model = DDP(self.model, device_ids=[self.ddp_local_rank])
self.raw_model = self.model.module if self.ddp else self.model
timestamp_prefix = time.strftime("%Y%m%d-%H%M%S")
if self.args.timestamp:
timestamp_prefix = self.args.timestamp
# Tensorboard
if self.args.tensorboard_log:
timestamped_run_name = timestamp_prefix + "_" + self.args.tensorboard_run_name
if self.args.csv_log:
self.args.csv_name = timestamped_run_name
log_subpath = os.path.join(self.args.tensorboard_log_dir, timestamped_run_name)
self.writer = SummaryWriter(log_subpath)
# Wandb
if self.args.wandb_log and self.master_process:
import wandb
self.args.csv_name = wandb_run_name
wandb.init(project=self.args.wandb_project, name=self.args.wandb_run_name, config=self.args)
def get_vocab_size_from_meta(self):
# Data loader
meta_path = os.path.join('data', self.args.dataset, 'meta.pkl')
# Save a copy of meta.pkl tokenization into the output folder
self.copy_file_to_directory(meta_path, self.args.out_dir)
if os.path.exists(meta_path):
with open(meta_path, 'rb') as f:
meta = pickle.load(f)
if 'vocab_size' in meta:
return meta['vocab_size']
else:
sys.exit(f"Error: 'vocab_size' key not found in {meta_path}")
else:
sys.exit(f"Error: File not found - {meta_path}")
def copy_file_to_directory(self, src_file, dest_dir):
try:
# Ensure the destination directory exists
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# Copy the file
shutil.copy(src_file, dest_dir)
print(f"File {src_file} copied to {dest_dir}")
except Exception as e:
print(f"Error copying file: {e}")
def load_data(self):
if self.model_args['vocab_size'] is None:
sys.exit("Error: no vocab size specified")
elif self.model_args['vocab_size'] == 100277:
# cl100k_base, vocab size 100277, requires np.uint32
self.train_data = np.memmap(os.path.join('data', self.args.dataset, 'train.bin'), dtype=np.uint32, mode='r')
self.val_data = np.memmap(os.path.join('data', self.args.dataset, 'val.bin'), dtype=np.uint32, mode='r')
else:
# all other tokenations so far require only np.uint16
self.train_data = np.memmap(os.path.join('data', self.args.dataset, 'train.bin'), dtype=np.uint16, mode='r')
self.val_data = np.memmap(os.path.join('data', self.args.dataset, 'val.bin'), dtype=np.uint16, mode='r')
def get_batch(self, split):
data = self.train_data if split == 'train' else self.val_data
ix = torch.randint(len(data) - self.args.block_size, (self.args.batch_size,))
x = torch.stack([torch.from_numpy((data[i:i+self.args.block_size]).astype(np.int64)) for i in ix])
y = torch.stack([torch.from_numpy((data[i+1:i+1+self.args.block_size]).astype(np.int64)) for i in ix])
if self.device_type == 'cuda':
x, y = x.pin_memory().to(self.device, non_blocking=True), y.pin_memory().to(self.device, non_blocking=True)
else:
x, y = x.to(self.device), y.to(self.device)
return x, y
@torch.no_grad()
def estimate_loss(self):
out = {}
self.model.eval()
for split in ['train', 'val']:
losses = torch.zeros(self.args.eval_iters)
for k in range(self.args.eval_iters):
X, Y = self.get_batch(split)
with self.ctx:
logits, loss = self.model(X, Y)
losses[k] = loss.item()
out[split] = losses.mean()
self.model.train()
return out
def get_lr(self, it):
if it < self.args.warmup_iters:
return self.args.learning_rate * it / self.args.warmup_iters
if it > self.args.lr_decay_iters:
return self.args.min_lr
decay_ratio = (it - self.args.warmup_iters) / (self.args.lr_decay_iters - self.args.warmup_iters)
assert 0 <= decay_ratio <= 1
coeff = 0.5 * (1.0 + math.cos(math.pi * decay_ratio))
return self.args.min_lr + coeff * (self.args.learning_rate - self.args.min_lr)
def log_metrics(self, losses, lr, running_mfu, iter_num):
if self.args.tensorboard_log:
self.writer.add_scalars(
"loss", { "train": losses['train'], "val": losses['val'] }, iter_num
)
self.writer.add_scalar("mfu_pct", running_mfu * 100, iter_num)
self.writer.add_scalar("lr", lr, iter_num)
if self.args.wandb_log and self.master_process:
import wandb
wandb.log({
"iter": iter_num,
"train/loss": losses['train'],
"val/loss": losses['val'],
"lr": lr,
"mfu": running_mfu*100,
})
if self.args.csv_log:
self.write_to_csv(losses['train'].item(), losses['val'].item())
def write_to_csv(self, *args, prefix=""):
csv_full_dir = self.args.csv_dir
if self.args.csv_ckpt_dir:
csv_full_dir = f"{self.args.csv_dir}/{self.args.csv_ckpt_dir}"
else:
if self.args.tensorboard_log:
csv_full_dir = f"{self.args.csv_dir}/{self.args.tensorboard_run_name.split('-')[0]}-{self.args.dataset}"
os.makedirs(csv_full_dir, exist_ok=True)
csv_path = os.path.join(csv_full_dir, prefix + self.args.csv_name + ".csv")
with open(csv_path, 'a', newline='') as file:
writer = csv.writer(file)
# Write arguments as a new row in the CSV
writer.writerow(args)
def log_gamma_beta(self, gamma, beta, iter_num, layer_num):
if self.args.tensorboard_log:
self.writer.add_scalar( "gamma_" + str(layer_num), gamma, iter_num)
self.writer.add_scalar( "beta_" + str(layer_num), beta, iter_num)
if self.args.wandb_log and self.master_process:
import wandb
wandb.log({
"iter": iter_num,
"train/loss": losses['train'],
"val/loss": losses['val'],
"lr": lr,
"mfu": running_mfu*100,
})
def log_metrics_non_validation(self, loss_training, running_mfu, iter_num):
if self.args.tensorboard_log:
self.writer.add_scalars(
"loss", { "train": loss_training }, iter_num
)
self.writer.add_scalar("mfu_pct", running_mfu * 100, iter_num)
if self.args.wandb_log and self.master_process:
import wandb
wandb.log({
"iter": iter_num,
"train/loss": loss_training,
"mfu": running_mfu*100,
})
def train(self):
self.X, self.Y = self.get_batch('train')
t0 = time.time()
local_iter_num = 0
running_mfu = -1.0
num_steps_with_worse_loss = 0
graph_y_labels = []
for layer in range(self.args.n_layer):
for head in range(self.args.n_head):
graph_y_labels.append(f"Layer {layer} Head {head}")
while True:
lr = self.get_lr(self.iter_num) if self.args.decay_lr else self.args.learning_rate
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
if self.iter_num % self.args.eval_interval == 0 and self.master_process:
losses = self.estimate_loss()
print(f"step {self.iter_num}: train loss {losses['train']:.4f}, val loss {losses['val']:.4f}")
self.log_metrics(losses, lr, running_mfu, self.iter_num)
if math.isnan(losses["val"]):
checkpoint = {
'model': self.raw_model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'model_args': self.model_args,
'iter_num': self.iter_num,
'best_val_loss': self.best_val_loss,
'nan_iter_num' : 0,
'nan' : True,
'config': vars(self.args),
}
torch.save(checkpoint, os.path.join(self.args.out_dir, 'ckpt.pt'))
if losses['val'] < self.best_val_loss or self.args.always_save_checkpoint:
if losses['val'] < self.best_val_loss:
self.iter_num_best_val_loss = self.iter_num
self.best_val_loss = losses['val']
# Save best validation loss
with open(os.path.join(self.args.out_dir, 'best_val_loss_and_iter.txt'), "w") as best_loss_file:
best_loss_file.write(str(self.best_val_loss.item())+","+str(self.iter_num))
# Reset early exit counter
num_steps_with_worse_loss = 0
if self.iter_num > 0:
checkpoint = {
'model': self.raw_model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'model_args': self.model_args,
'iter_num': self.iter_num,
'best_val_loss': self.best_val_loss,
'nan_iter_num' : None,
'nan' : None,
'config': vars(self.args),
}
print(f"saving checkpoint to {self.args.out_dir}")
# Save checkpoint
torch.save(checkpoint, os.path.join(self.args.out_dir, 'ckpt.pt'))
if self.args.patience is not None and num_steps_with_worse_loss >= self.args.patience:
print(f"Early Stopping: loss has not decreased in {self.args.patience + 1} steps")
plot_statistics(self.args, self.stats, graph_y_labels)
break
if losses['val'] > self.best_val_loss:
num_steps_with_worse_loss += 1
if self.iter_num == 0 and self.args.eval_only:
break
for micro_step in range(self.args.gradient_accumulation_steps):
if self.ddp:
self.model.require_backward_grad_sync = (micro_step == self.args.gradient_accumulation_steps - 1)
with self.ctx:
logits, loss = self.model(self.X, self.Y)
loss = loss / self.args.gradient_accumulation_steps
self.X, self.Y = self.get_batch('train')
self.scaler.scale(loss).backward()
if self.args.grad_clip != 0.0:
self.scaler.unscale_(self.optimizer)
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.args.grad_clip)
self.scaler.step(self.optimizer)
self.scaler.update()
self.optimizer.zero_grad(set_to_none=True)
t1 = time.time()
dt = t1 - t0
t0 = t1
if self.iter_num % self.args.log_interval == 0 and self.master_process:
lossf = loss.item() * self.args.gradient_accumulation_steps
if local_iter_num >= 5:
mfu = self.raw_model.estimate_mfu(self.args.batch_size * self.args.gradient_accumulation_steps, dt)
running_mfu = mfu if running_mfu == -1.0 else 0.9*running_mfu + 0.1*mfu
print(f"iter {self.iter_num}: loss {lossf:.4f}, time {dt*1000:.2f} ms, mfu {running_mfu*100:.2f}%")
if math.isnan(lossf):
if self.args.save_nan_checkpoint:
checkpoint = {
'model': self.raw_model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'model_args': self.model_args,
'iter_num': self.iter_num_best_val_loss,
'best_val_loss': self.best_val_loss,
'nan_iter_num' : self.iter_num,
'nan' : True,
'config': vars(self.args),
}
print(f"saving checkpoint to {self.args.out_dir}")
torch.save(checkpoint, os.path.join(self.args.out_dir, 'ckpt.pt'))
sys.exit("Exiting training loss is NaN")
self.log_metrics_non_validation(lossf, running_mfu, self.iter_num)
create_statistics(self, graph_y_labels)
self.iter_num += 1
local_iter_num += 1
# End of training actions
if self.iter_num > self.args.max_iters:
plot_statistics(self.args, self.stats, graph_y_labels)
if self.args.only_save_checkpoint_at_end:
checkpoint = {
'model': self.raw_model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'model_args': self.model_args,
'iter_num': self.iter_num,
'best_val_loss': self.best_val_loss,
'nan_iter_num' : None,
'nan' : None,
'config': vars(self.args),
}
print(f"saving checkpoint to {self.args.out_dir}")
torch.save(checkpoint, os.path.join(self.args.out_dir, 'ckpt.pt'))
break
if self.args.tensorboard_log:
self.writer.flush()
self.writer.close()
if self.args.wandb_log and self.master_process:
import wandb
wandb.log({"finished": True})
wandb.finish()
def main():
args, model_group, training_group, logging_group = parse_args()
trainer = Trainer(args, model_group, training_group, logging_group)
trainer.train()
if trainer.ddp:
destroy_process_group()
if args.tensorboard_log:
trainer.writer.flush()
trainer.writer.close()
if __name__ == '__main__':
main()