-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain_gptj_smp_tensor_parallel_script.py
1332 lines (1182 loc) · 50.7 KB
/
train_gptj_smp_tensor_parallel_script.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
import argparse
import collections
import logging
import math
import os
import re
import time
from concurrent.futures import ProcessPoolExecutor
import numpy as np
import smdistributed.modelparallel
import smdistributed.modelparallel.torch as smp
import torch
import torch.nn as nn
import torch.utils.data
import transformers
from data_pipeline import create_pretraining_dataloader
from fp16 import FP16_Module, FP16_Optimizer, load_fp16_optimizer, save_fp16_optimizer
from fp16.megatron.fp16 import Float16OptimizerWithFloat16Params
from fp16.megatron.fp16 import load_fp16_optimizer as load_fp16_optimizer_megatron
from fp16.megatron.fp16 import save_fp16_optimizer as save_fp16_optimizer_megatron
from fp16.megatron.grad_scaler import DynamicGradScaler
from learning_rates import AnnealingLR
from memory_tracker import memory_status
from smdistributed.modelparallel.torch.nn import FusedLayerNorm as LayerNorm
from smdistributed.modelparallel.torch.nn.huggingface.gptj import (
translate_hf_gptj_state_dict_to_smdistributed,
translate_state_dict_to_hf_gptj,
)
from torch import optim
from torch.nn.parallel.distributed import DistributedDataParallel
from transformers import (
CONFIG_MAPPING,
MODEL_FOR_CAUSAL_LM_MAPPING,
AutoConfig,
AutoModelForCausalLM,
AutoTokenizer,
GPTJConfig,
default_data_collator,
set_seed,
)
from transformers.trainer_utils import is_main_process
logger = logging.getLogger(__name__)
def get_learning_rate_scheduler(optimizer, args):
# Add linear learning rate scheduler.
if args.lr_decay_iters is not None:
num_iters = args.lr_decay_iters
else:
num_iters = args.max_steps
num_iters = max(1, num_iters)
init_step = 0
warmup_iter = args.warmup * num_iters
plateau_iter = warmup_iter + args.plateau * num_iters
lr_scheduler = AnnealingLR(
optimizer,
start_lr=args.lr,
warmup_iter=warmup_iter,
plateau_iter=plateau_iter,
total_iters=num_iters,
decay_style=args.lr_decay_style,
last_iter=init_step,
min_lr=args.min_lr,
use_checkpoint_lr_scheduler=args.load_partial or args.load_full,
override_lr_scheduler=False,
)
return lr_scheduler
def get_param_groups_by_weight_decay(module):
weight_decay_params = {"params": []}
no_weight_decay_params = {"params": [], "weight_decay": 0.0}
param_ids = set()
for module_ in module.modules():
if isinstance(module_, LayerNorm):
for p in list(module_._parameters.values()):
if p is not None and id(p) not in param_ids:
no_weight_decay_params["params"].append(p)
param_ids.add(id(p))
else:
for n, p in list(module_._parameters.items()):
if p is not None and n != "bias" and id(p) not in param_ids:
weight_decay_params["params"].append(p)
param_ids.add(id(p))
for n, p in list(module_._parameters.items()):
if p is not None and n == "bias" and id(p) not in param_ids:
no_weight_decay_params["params"].append(p)
param_ids.add(id(p))
return weight_decay_params, no_weight_decay_params
# smdistributed: Define smp.step. Return any tensors needed outside.
@smp.step
def train_step(model, optimizer, input_ids, attention_mask, args):
if args.logits_output:
output = model(input_ids=input_ids, attention_mask=attention_mask, labels=input_ids)
loss = output["loss"]
else:
loss = model(input_ids=input_ids, attention_mask=attention_mask, labels=input_ids)["loss"]
if args.fp16:
if args.megatron:
scaled_loss = optimizer.scale_loss(loss)
model.backward(scaled_loss)
else:
optimizer.backward(loss, update_master_grads=False)
else:
model.backward(loss)
if args.logits_output:
return output
return loss
# smdistributed: Define smp.step. Return any tensors needed outside.
@smp.step
def test_step(model, input_ids, attention_mask):
loss = model(input_ids=input_ids, attention_mask=attention_mask, labels=input_ids)["loss"]
return loss
def save_ckptsum(args, model, optimizer, filename):
results = collections.defaultdict(dict)
model_result = collections.defaultdict(dict)
if args.fp16:
from fp16.fp16util import register_optimizer_hooks
register_optimizer_hooks(model)
def _get_optimizer_result(optimizer_states):
_optimizer_result = collections.defaultdict(dict)
for param_idx, state in optimizer_states.items():
for key, val in state.items():
if isinstance(val, torch.Tensor):
_optimizer_result["tensors"][f"{param_idx}_{key}"] = torch.sum(val)
else:
_optimizer_result["scalars"][f"{param_idx}_{key}"] = val
return _optimizer_result
if not args.shard_optimizer_state:
optimizer_result = _get_optimizer_result(optimizer.local_state_dict()["state"])
else:
local_state_dict = optimizer.local_state_dict()["state"]
if smp.rdp_rank() == 0:
optimizer_result = []
for partial_local_state_dict in local_state_dict:
optimizer_result.append(_get_optimizer_result(partial_local_state_dict))
for param_name, param in model.local_state_dict().items():
if isinstance(param, torch.Tensor):
model_result["tensors"][param_name] = torch.sum(param)
else:
model_result["scalars"][param_name] = param
if smp.rdp_rank() == 0:
results["optimizer"] = optimizer_result
results["model"] = model_result
smp.save(results, filename)
def load_and_verify_ckptsum(args, model, optimizer, filename):
results = smp.load(filename)
optimizer_result = (
results["optimizer"]
if not args.shard_optimizer_state
else results["optimizer"][smp.rdp_rank()]
)
model_result = results["model"]
def opt_check_fn(mod, opt):
loaded_opt_states = (
opt.orig_state_dict()["state"]
if args.shard_optimizer_state
else opt.local_state_dict()["state"]
)
for param_idx, state in loaded_opt_states.items():
for key, val in state.items():
if isinstance(val, torch.Tensor):
assert torch.isclose(
torch.sum(val), optimizer_result["tensors"][f"{param_idx}_{key}"]
), f"mismatch for param_idx: {param_idx}, key is {key}"
else:
assert (
val == optimizer_result["scalars"][f"{param_idx}_{key}"]
), f"mismatch for param_idx: {param_idx}, key is {key}"
print("Optimizer save/load check passed successfully")
def model_check_fn(mod, opt):
for param_name, param in mod.local_state_dict().items():
if isinstance(param, torch.Tensor):
assert torch.isclose(
torch.sum(param), model_result["tensors"][param_name]
), f"mismatch for param_name: {param_name}"
else:
assert (
param == model_result["scalars"][param_name]
), f"mismatch for param_name: {param_name}"
print("Model save/load check passed successfully")
model.register_post_partition_hook(model_check_fn)
model.register_post_step_hook(opt_check_fn)
def save(
output_save_file,
model,
optimizer,
lr_scheduler,
model_config,
num_params,
total_steps,
curr_train_path_index,
args,
partial=True,
translate_to_hf=False,
seq_length=1024,
batch_idx=0,
):
save_fn = save_fp16_optimizer_megatron if args.megatron else save_fp16_optimizer
save_dict = {
"cli_args": args.__dict__,
"num_params": num_params,
"total_steps": total_steps,
"curr_train_path_index": curr_train_path_index,
"model_config": model_config,
"batch_idx": batch_idx,
}
if lr_scheduler is not None:
save_dict["lr_scheduler"] = lr_scheduler.state_dict()
if partial:
if args.gather_if_shard > 0 or smp.rdp_rank() == 0:
# if not gather the opt checkpoint, only save the model for rdp rank 0
save_dict["model"] = model.local_state_dict()
else:
model_state_dict = model.state_dict(gather_to_rank0=True)
if smp.rank() == 0:
save_dict["model"] = (
translate_state_dict_to_hf_gptj(model_state_dict, seq_length)
if translate_to_hf
else model_state_dict
)
if args.fp16:
if not partial and args.skip_full_optimizer:
print("Skipping saving the final optimizer state")
else:
if args.shard_optimizer_state == 0 or partial:
save_dict["optimizer"] = save_fn(args, model, optimizer, partial=partial)
else:
print(
"Saving the full optimizer state does not work with shard_optimizer_state > 0! Skipping..."
)
else:
# fp32
if partial:
save_dict["optimizer"] = optimizer.local_state_dict()
else:
if not args.skip_full_optimizer:
save_dict["optimizer"] = optimizer.state_dict()
else:
print("Skipping saving of full optimizer state")
if not args.gather_if_shard or (smp.rdp_rank() == 0 and partial) or smp.rank() == 0:
smp.save(save_dict, output_save_file, partial=partial, v3=not args.gather_if_shard)
print(f"Finished checkpointing after {total_steps} steps: {output_save_file}")
def save_for_sm_hf_inference(model, model_config, args, seq_length=1024, translate_to_hf=False):
if smp.rdp_rank() == 0:
model_state_dict = model.state_dict(gather_to_rank0=True)
if smp.rank() == 0:
torch.save(translate_state_dict_to_hf_gptj(model_state_dict, seq_length) if translate_to_hf else model_state_dict,
os.path.join(args.model_dir, 'gptj.pt'))
from transformers import AutoTokenizer
# using tokenizer from transformers
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B")
tokenizer.save_pretrained(args.model_dir)
model_config.save_pretrained(args.model_dir)
import shutil
os.makedirs(os.path.join(args.model_dir, "code"))
dst_inference_script = os.path.join(args.model_dir, "code")
shutil.copy("inference.py", dst_inference_script)
def load_model_and_optimizer(
output_dir,
model,
optimizer,
lr_scheduler,
partial,
args,
translate_from_hf=False,
seq_length=1024,
load_model=True,
load_optimizer=True,
num_params=0,
):
# Find longest-trained checkpoint
re_pattern = f"trained_gpt_nparams-{num_params}_steps-(?P<total_steps>\d+)\.pt"
if partial:
re_pattern += "_(?P<rank>\d+)"
else:
re_pattern += "$"
ckpt_paths = sorted(
[
(int(re.match(re_pattern, p).group("total_steps")), os.path.join(output_dir, p))
for p in os.listdir(output_dir)
if re.match(re_pattern, p)
],
reverse=True,
)
if not ckpt_paths:
raise Exception(
f'No checkpoints could be found in "{output_dir}". Candidates: {os.listdir(output_dir)}'
)
local_ckpt_path = ckpt_paths[0][1]
if partial:
# need to pass prefix without ranks to smp
local_ckpt_path = local_ckpt_path.split(".pt")[0] + ".pt"
if args.gather_if_shard > 0:
# Should expect v2 checkpoint here
checkpoint = smp.load(local_ckpt_path, partial=partial)
else:
# Loading separately for model and opt
checkpoint = torch.load(f"{local_ckpt_path}_{smp.pp_rank()}_{smp.tp_rank()}_0")
if smp.rdp_rank() != 0:
opt_checkpoint = torch.load(
f"{local_ckpt_path}_{smp.pp_rank()}_{smp.tp_rank()}_{smp.rdp_rank()}"
)
if load_model:
checkpointed_model = (
translate_hf_gptj_state_dict_to_smdistributed(checkpoint["model"], seq_length)
if translate_from_hf
else checkpoint["model"]
)
model.load_state_dict(checkpointed_model, same_partition_load=args.same_partition_load > 0)
if lr_scheduler is not None:
lr_scheduler.load_state_dict(checkpoint["lr_scheduler"])
if load_optimizer:
checkpoint = (
checkpoint if args.gather_if_shard > 0 or smp.rdp_rank() == 0 else opt_checkpoint
)
# Loading loss scale eagerly
if not args.megatron:
opt_state_dict = checkpoint["optimizer"]
optimizer.loss_scaler = opt_state_dict["loss_scaler"]
optimizer.loss_scaler.model = model
optimizer.dynamic_loss_scale = opt_state_dict["dynamic_loss_scale"]
optimizer.overflow = opt_state_dict["overflow"]
optimizer.first_closure_call_this_step = opt_state_dict["first_closure_call_this_step"]
def opt_load_hook(mod, opt):
load_fn = load_fp16_optimizer_megatron if args.megatron else load_fp16_optimizer
if args.fp16:
if not partial and args.skip_full_optimizer:
print(
"Skipping loading the final optimizer state, and reloading master_params from model_params"
)
opt.reload_model_params()
else:
load_fn(args, mod, opt, checkpoint, partial=partial)
else:
# fp32
if not partial and args.skip_full_optimizer:
print("Skipping loading the final optimizer state")
else:
opt.load_state_dict(checkpoint["optimizer"])
model.register_post_step_hook(opt_load_hook)
print(f'Loaded model from "{local_ckpt_path}"')
batch_idx = 0
if "batch_idx" in checkpoint:
batch_idx = checkpoint["batch_idx"]
return (
model,
optimizer,
checkpoint["total_steps"],
checkpoint["curr_train_path_index"],
batch_idx,
)
def delete_oldest_ckpt(args, delete_on_rank0_only=False):
to_delete = smp.rank() == 0 if delete_on_rank0_only else smp.local_rank() == 0
if to_delete:
re_pattern = "trained_gpt_nparams-(?P<num_params>\d+)_steps-(?P<total_steps>\d+)\.pt"
# partial
re_pattern += "_(?P<pp_rank>\d+)_(?P<tp_rank>\d+)"
paths_per_step = collections.defaultdict(list)
for p in os.listdir(args.checkpoint_dir):
if re.match(re_pattern, p):
step = int(re.match(re_pattern, p).group("total_steps"))
path = os.path.join(args.checkpoint_dir, p)
paths_per_step[step].append(path)
if paths_per_step:
oldest_step = sorted(paths_per_step.keys())[0]
num_parts = len(paths_per_step[oldest_step])
if len(paths_per_step) >= args.num_kept_checkpoints:
# delete oldest step to save the new one
for p in paths_per_step[oldest_step]:
os.remove(p)
# else We still haven't reached maximum number of checkpoints -- no need to delete older ones
return None
def eval_model(model, dataloader, num_batches, use_bert_data):
model = model.eval()
n_batches = 0
loss = 0.0
with torch.no_grad():
for batch_idx, input_data in enumerate(dataloader):
if use_bert_data:
input_ids, _, attention_mask, _, _ = input_data
else:
input_ids, attention_mask = input_data
if batch_idx >= num_batches:
break
loss += test_step(model, input_ids, attention_mask).reduce_mean()
n_batches += 1
if n_batches > 0:
torch.distributed.all_reduce(loss, group=smp.get_dp_process_group())
loss /= smp.dp_size()
loss /= n_batches
loss = loss.item()
ppl = math.exp(loss)
else:
loss = -1.0
ppl = -1.0
return loss, ppl
def train(
model,
optimizer,
lr_scheduler,
model_config,
start_train_path_index,
start_batch_index,
num_params,
total_steps,
args,
):
model.train()
if args.parallel_proc_data_processing:
pool = ProcessPoolExecutor(1)
dp_rank = smp.dp_rank() if not args.prescaled_batch else smp.rdp_rank()
dp_size = smp.dp_size() if not args.prescaled_batch else smp.rdp_size()
data_type = "BERT" if args.use_bert_data else "GPT"
if args.use_bert_data:
train_paths = sorted(
[
os.path.join(args.training_dir, p)
for p in os.listdir(args.training_dir)
if os.path.isfile(os.path.join(args.training_dir, p)) and "training" in p
]
)
else:
if args.zipped_data > 0:
file_extension = ".json.gz"
else:
file_extension = ".json"
train_paths = sorted(
[
os.path.join(args.training_dir, p)
for p in os.listdir(args.training_dir)
if p.endswith(file_extension)
]
)
train_dataloader = create_pretraining_dataloader(
[train_paths[start_train_path_index]],
args.train_batch_size,
args.max_context_width,
seed=args.seed,
dp_rank=dp_rank,
dp_size=dp_size,
shuffle=args.same_seed < 1,
zipped=args.zipped_data > 0,
use_last_file_only=args.fast_validation > 0,
data_type=data_type,
)
if args.validation_freq is not None:
# load all validation examples
if smp.rank() == 0:
print("Creating val dataloader")
if args.use_bert_data:
val_paths = sorted(
[
os.path.join(args.test_dir, p)
for p in os.listdir(args.test_dir)
if os.path.isfile(os.path.join(args.test_dir, p)) and "testing" in p
]
)
else:
if args.zipped_data > 0:
file_extension = ".json.gz"
else:
file_extension = ".json"
val_paths = sorted(
[
os.path.join(args.test_dir, p)
for p in os.listdir(args.test_dir)
if p.endswith(file_extension)
]
)
val_dataloader = create_pretraining_dataloader(
val_paths,
args.val_batch_size,
args.max_context_width,
seed=args.seed,
dp_rank=dp_rank,
dp_size=dp_size,
shuffle=True,
zipped=args.zipped_data > 0,
use_last_file_only=args.fast_validation > 0,
data_type=data_type,
)
if smp.rank() == 0:
print("Created val dataloader")
start = time.time()
throughput = None
to_save = {"loss": [], "val_loss": []}
loss_metric = 0
def should_record():
# only record the ranks that in the tp group that contains global rank 0
if smp.tp_size() > 1:
tp_group = smp.get_tp_group()
return 0 in tp_group
else:
return smp.rank() == 0
# Set the same seed for computation
set_seed(args.seed)
for index in range(start_train_path_index, args.epochs * len(train_paths)):
next_train_path_index = (index + 1) % len(train_paths)
curr_train_path_index = index % len(train_paths)
if total_steps >= args.max_steps:
break
if args.parallel_proc_data_processing:
dataset_future = pool.submit(
create_pretraining_dataloader,
[train_paths[next_train_path_index]],
args.train_batch_size,
args.max_context_width,
seed=args.seed,
dp_rank=dp_rank,
dp_size=dp_size,
shuffle=args.same_seed < 1,
zipped=args.zipped_data > 0,
use_last_file_only=args.fast_validation > 0,
data_type=data_type,
)
if smp.rank() == 0:
if args.use_bert_data:
print(f"Reading data from training path {train_dataloader.dataset.input_file}")
else:
print(f"Reading data from training path {train_dataloader.dataset.input_paths}")
for batch_idx, input_data in enumerate(train_dataloader):
if batch_idx < start_batch_index:
if smp.rank() == 0:
print(
f"Resuming from saved batch index {start_batch_index}, skipping batch {batch_idx}..."
)
if start_batch_index == len(train_dataloader):
# If saving at the last batch of the file, read from the next file
start_batch_index = 0
break
continue
else:
start_batch_index = 0
if args.use_bert_data:
input_ids, _, attention_mask, _, _ = input_data
else:
input_ids, attention_mask = input_data
if total_steps >= args.max_steps:
break
step_start = time.time()
if args.fp16:
optimizer.zero_grad(set_grads_to_None=True)
else:
optimizer.zero_grad()
if args.logits_output:
train_output = train_step(model, optimizer, input_ids, attention_mask, args)
loss_mb = train_output["loss"]
logits_mb = train_output["logits"]
if smp.tp_size() > 1:
logits = torch.cat(tuple(logits_mb.outputs), dim=1)
else:
logits = torch.cat(tuple(logits_mb.outputs), dim=0)
else:
# Return value, loss_mb is a StepOutput object
loss_mb = train_step(model, optimizer, input_ids, attention_mask, args)
# smdistributed: Average the loss across microbatches.
loss = loss_mb.reduce_mean()
if not args.validation_freq:
loss_metric = loss.item()
if args.enable_memory_profiling > 0:
memory_status(msg="After_train_step")
if args.clean_cache > 0:
# empty the cache to avoid OOM
torch.cuda.empty_cache()
if args.fp16:
if args.megatron:
success, _, _ = optimizer.step()
overflow = not success
else:
optimizer.update_master_grads()
optimizer.clip_master_grads(args.grad_clip)
optimizer.step()
overflow = optimizer.overflow
else:
optimizer.step()
if not (args.fp16 and overflow):
lr_scheduler.step()
if args.enable_memory_profiling > 0:
memory_status(msg="After_opt_step")
total_steps += 1
time_elapsed = time.time() - start
step_time = time.time() - step_start
sample_processed = input_ids.shape[0] * dp_size
throughput = sample_processed / step_time
if smp.rank() == 0 and not total_steps % args.logging_freq:
print(
f"({int(time_elapsed)}s), Batch {total_steps - 1} Loss: {loss.item()}, Speed: {throughput} samples/sec"
)
# evaluate on validation
if args.validation_freq and not (total_steps % args.validation_freq):
cur_state = np.random.get_state()
model = model.eval()
val_loss, val_ppl = eval_model(
model, val_dataloader, args.validation_batches, args.use_bert_data
)
if is_main_process(smp.rank()):
print(
f"({int(time.time()-start)}s) Batch {total_steps - 1} Validation loss: {val_loss}"
)
print(
f"({int(time.time()-start)}s) Batch {total_steps - 1} Validation perplexity: {val_ppl}"
)
loss_metric = val_loss
if args.logits_output:
to_save["val_loss"].append(val_loss)
model = model.train()
if args.preserve_np_state > 0:
np.random.set_state(cur_state)
# checkpoint
if not (total_steps % args.checkpoint_freq):
base_path = f"trained_gpt_nparams-{num_params}_steps-{total_steps}.pt"
out_path = os.path.join(args.checkpoint_dir, base_path)
total_ckpts = total_steps // args.checkpoint_freq
delete_oldest_ckpt(args, delete_on_rank0_only=args.use_fsx > 0)
# save_or_verify_ckptsum if this is the last checkpoint
if (args.save_or_verify_ckptsum and total_steps >= args.max_steps) or (
(total_ckpts + 1) * args.checkpoint_freq
) > args.max_steps:
# Save optimizer and model tensor sums and scalars before saving
save_ckptsum(
args,
model,
optimizer,
filename=os.path.join(args.model_dir, "saved_partial_sum"),
)
save(
out_path,
model,
optimizer,
lr_scheduler,
model_config,
num_params,
total_steps,
curr_train_path_index,
args,
partial=True,
batch_idx=batch_idx + 1,
)
if args.logits_output:
to_save["loss"].append(loss.item())
if total_steps >= args.max_steps:
if should_record() and args.logits_output:
to_save["logits"] = logits.detach().cpu()
output_file = f"rank_{smp.rank()}_" + args.logits_output
torch.save(to_save, os.path.join(args.model_dir, output_file))
print(f"logits and loss saved at {os.path.join(args.model_dir, output_file)}")
break
del train_dataloader
if args.parallel_proc_data_processing:
s = time.time()
train_dataloader = dataset_future.result(timeout=None)
wait_time = time.time() - s
if wait_time > 1:
# TODO if this happens, we should try num_workers>1 in dataloader
print(
f"[{smp.rank()}] Waited {wait_time} for data loader to be ready. Please check if dataloader performance can be improved to avoid these waits."
)
else:
train_dataloader = create_pretraining_dataloader(
[train_paths[next_train_path_index]],
args.train_batch_size,
args.max_context_width,
seed=args.seed,
dp_rank=dp_rank,
dp_size=dp_size,
shuffle=args.same_seed < 1,
zipped=args.zipped_data > 0,
use_last_file_only=args.fast_validation > 0,
data_type=data_type,
)
return total_steps, throughput, loss_metric
def parse_args():
parser = argparse.ArgumentParser()
# hyperparameters sent by the client are passed as command-line arguments to the script.
opt_grp = parser.add_argument_group(
title="optimization", description="arguments for optimization"
)
opt_grp.add_argument(
"--train_batch_size",
type=int,
default=4,
help="batch size per dp rank, for tensor parallelism degree 8 with pipeline parallel degree 1 this means 8*this batch size per node",
)
opt_grp.add_argument("--val_batch_size", type=int, default=4)
opt_grp.add_argument("--max_steps", type=int, default=100)
opt_grp.add_argument("--seed", type=int, default=12345)
opt_grp.add_argument("--same_seed", type=int, default=0)
opt_grp.add_argument("--n_gpus", type=str, default=os.environ["SM_NUM_GPUS"])
opt_grp.add_argument("--fp16", default=0, type=int, help="automatic mixed precision training")
opt_grp.add_argument(
"--fp32_grad_accumulation", default=0, type=int, help="Enable FP32 Grad accumulation"
)
opt_grp.add_argument("--megatron", default=0, type=int, help="use megatron fp16 optimizer")
opt_grp.add_argument("--grad_clip", default=1.0, type=float, help="gradient clipping")
opt_grp.add_argument("--weight_decay", default=0.01, type=float, help="weight decay")
opt_grp.add_argument(
"--beta1", default=0.9, type=float, help="beta1 parameter for Adam optimizer"
)
opt_grp.add_argument(
"--beta2", default=0.95, type=float, help="beta2 parameter for Adam optimizer"
)
opt_grp.add_argument(
"--activation_checkpointing",
type=int,
default=1,
help="enable gradient checkpointing to reduce memory consumption",
)
parser.add_argument(
"--logging_freq", type=int, default=1, help="number of iterations between logging"
)
# I/O
io_grp = parser.add_argument_group(title="io", description="location for input and output")
io_grp.add_argument("--use_bert_data", type=int, default=0, help="use bert data for training")
#change to 0 original 1
io_grp.add_argument("--zipped_data", type=int, default=0, help="input data is zipped files")
io_grp.add_argument(
"--epochs", type=int, default=1, help="times of iterating over the training dataset"
)
io_grp.add_argument("--output-data-dir", type=str, default=os.environ["SM_OUTPUT_DATA_DIR"])
io_grp.add_argument(
"--checkpoint-dir",
type=str,
default="/opt/ml/checkpoints",
help="Saves partial checkpoints (model, optimizer) to this dir, and loads latest checkpoint from this if load_partial is specified.",
)
io_grp.add_argument(
"--model-dir",
type=str,
default=os.environ["SM_MODEL_DIR"],
help="Saves full model for inference to this dir. Also used if load_full is given to load the model. Note the lack of optimizer state here.",
)
io_grp.add_argument("--training-dir", type=str, default=os.environ["SM_CHANNEL_TRAIN"])
io_grp.add_argument("--test-dir", type=str, default=os.environ["SM_CHANNEL_TEST"])
io_grp.add_argument(
"--parallel_proc_data_processing",
type=int,
default=0,
help="Load data in parallel with a different process. At any point a process can have two files in memory. With tensor parallelism, each of the 8 processes on an instance will then have 2 files in memory. Depending on file sizes this may or may not be feasible. With pipeline parallelism this was not a problem as only 1 rank on an instance loaded data.",
)
io_grp.add_argument(
"--save_final_full_model",
type=int,
default=0,
help="Enabling this will save a combined model only at the end",
)
io_grp.add_argument(
"--skip_full_optimizer",
type=int,
default=1,
help="Disabling this will also save the full optimizer state",
)
io_grp.add_argument("--load_partial", type=int, default=0, help="Load from partial checkpoints")
io_grp.add_argument("--load_full", type=int, default=0, help="Load from full checkpoints")
io_grp.add_argument(
"--logits_output", type=str, default="", help="Path to save logits and loss"
)
io_grp.add_argument("--prescaled_batch", type=int, default=1, help="use prescaled batch")
# configure model size
model_grp = parser.add_argument_group(
title="model", description="arguments to describe model configuration"
)
model_grp.add_argument("--max_context_width", type=int, default=1024)
model_grp.add_argument("--use_adamw", type=int, default=0, help="Use adamw optimizer")
model_grp.add_argument("--finetune_6b", type=int, default=0, help="Flag to enable finetune 6B GPTJ model")
model_grp.add_argument("--vocab_size", type=int, default=50400)
model_grp.add_argument("--hidden_width", type=int, default=768)
model_grp.add_argument("--num_layers", type=int, default=12)
model_grp.add_argument("--num_heads", type=int, default=12)
model_grp.add_argument("--resid_pdrop", type=float, default=0.1)
model_grp.add_argument("--embd_pdrop", type=float, default=0.1)
model_grp.add_argument("--attn_pdrop", type=float, default=0.1)
model_grp.add_argument("--summary_first_pdrop", type=float, default=0.1)
smp_grp = parser.add_argument_group(title="smp", description="smp")
smp_grp.add_argument("--tensor_parallel_degree", type=int, default=8)
smp_grp.add_argument("--pipeline_parallel_degree", type=int, default=1)
smp_grp.add_argument("--microbatches", type=int, default=1)
smp_grp.add_argument("--active_microbatches", type=int, default=None)
smp_grp.add_argument("--optimize", type=str, default="speed")
smp_grp.add_argument("--activation_strategy", type=str, default="each")
smp_grp.add_argument("--shard_optimizer_state", type=int, default=0)
smp_grp.add_argument("--offload_activations", type=int, default=0)
smp_grp.add_argument("--fast_mode", type=int, default=0)
smp_grp.add_argument("--static_mode", type=int, default=0)
smp_grp.add_argument("--delayed_param", type=int, default=0)
smp_grp.add_argument("--same_partition_load", type=int, default=0)
smp_grp.add_argument("--attention_in_fp32", type=int, default=0)
smp_grp.add_argument("--placement_strategy", type=str, default="cluster")
smp_grp.add_argument("--activation_loading_horizon", type=int, default=4)
smp_grp.add_argument("--skip_tracing", type=int, default=0)
smp_grp.add_argument("--query_key_layer_scaling", type=int, default=1)
smp_grp.add_argument("--fused_softmax", type=int, default=1)
smp_grp.add_argument("--fused_bias_gelu", type=int, default=1)
parser.add_argument(
"--num_kept_checkpoints",
type=int,
default=5,
help="how many checkpoints to keep before deleting",
)
parser.add_argument(
"--checkpoint_freq",
type=int,
default=10000,
help="number of iterations between checkpointing",
)
parser.add_argument(
"--validation_freq",
type=int,
default=None,
help="number of iterations to print validation loss",
)
parser.add_argument(
"--validation_batches",
type=int,
default=10,
help="number of batches to estimate validation loss",
)
parser.add_argument(
"--manual_partition",
type=int,
default=0,
help="evenly distribute layers across the partitions",
)
parser.add_argument(
"--partition_assignment",
type=str,
default="",
help="number of transformer layers assigned to each partition",
)
parser.add_argument(
"--match_weights", type=int, default=0, help="Get weights from the original model"
)
parser.add_argument(
"--preserve_np_state",
type=int,
default=0,
help="Perserve the numpy random state between validation",
)
parser.add_argument(
"--fast_validation",
type=int,
default=1,
help="Running validation only with the last data file for faster speed",
)
parser.add_argument(
"--gather_if_shard",
type=int,
default=1,
help="When sharding opt states is enabled, gather the opt checkpoint to rdp rank 0 during saving",
)
parser.add_argument(
"--clean_cache",
type=int,
default=0,
help="Clean torch reserved memory at he end of every step",
)
parser.add_argument("--use_fsx", type=int, default=0, help="Using FSx for checkpointing")
parser.add_argument(
"--enable_memory_profiling", type=int, default=0, help="Enable memory profile"
)
# learning rate
lr_grp = parser.add_argument_group(
title="lr", description="arguments for learning rate schedule"
)
lr_grp.add_argument("--lr", type=float, default=None, help="Initial learning rate.")
lr_grp.add_argument(
"--lr_decay_style",
type=str,
default="linear",
choices=["constant", "linear", "cosine", "exponential", "plateau"],
help="Learning rate decay function.",
)
lr_grp.add_argument(
"--lr_decay_iters",
type=int,
default=None,
help="number of iterations to decay learning rate over," " If None defaults to train iters",
)
lr_grp.add_argument(
"--min_lr",
type=float,
default=0.0,
help="Minumum value for learning rate. The scheduler" "clip values below this threshold.",
)
lr_grp.add_argument(
"--warmup",
type=float,
default=0.01,
help="Percentage of total iterations to warmup on "
"(.01 = 1 percent of all training iters).",
)
lr_grp.add_argument(
"--plateau",
type=float,
default=0.4,
help="Percentage of total iterations to keep at max if using plateau lr",
)