/
c4.py
1039 lines (881 loc) · 30.1 KB
/
c4.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
# coding=utf-8
# Copyright 2022 The Pax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Language Model configurations on the T5/C4 dataset."""
import functools
import math
from absl import logging
from etils import epath
import fiddle as fdl
import jax
from jax import numpy as jnp
from paxml import base_experiment
from paxml import experiment_registry
from paxml import seqio_input
from paxml import tasks_lib
from paxml import trainer_lib
from paxml.tasks.lm import model_params
from paxml.tasks.lm.params import lm_cloud
from praxis import base_hyperparams
from praxis import base_input
from praxis import base_layer
from praxis import layers
from praxis import optimizers
from praxis import pax_fiddle
from praxis import schedules
from praxis.layers import transformers
import seqio
import t5.data
from t5.data import preprocessors as t5_preprocessors
WeightInit = base_layer.WeightInit
GPT_SPM_PATH = (
'gs://mlperf-llm-public2/vocab/c4_en_301_5Mexp2_spm.model'
)
GPT_EOS_ID = 1
GPT_VOCABULARY = t5.data.SentencePieceVocabulary(GPT_SPM_PATH)
PASS_THROUGH_VOCABULARY = t5.data.PassThroughVocabulary(size=50257)
C4_GPT_TRAIN_FEATURES_LM = {
'targets': t5.data.Feature(vocabulary=GPT_VOCABULARY, add_eos=False)
}
C4_GPT_EVAL_FEATURES_LM = {
'targets': t5.data.Feature(
vocabulary=PASS_THROUGH_VOCABULARY, add_eos=False
)
}
C4_TRAIN_DATADIR = 'gs://mlperf-llm-public2'
C4_EVAL_DATADIR = 'gs://mlperf-llm-public2'
class TaskRegistry(t5.data.TaskRegistry):
"""Task registry with extra tracking."""
TASK_NAMES = []
@classmethod
def add_versioned_tfds_task(
cls,
name: str,
*,
versions: list[str],
pinned_version: str | None = None,
tfds_name: str,
tfds_data_dir: str | None = None,
**kwargs,
) -> list[seqio.Task]:
tasks = []
for version in versions:
tasks.append(
cls.add(
f'{name}_{version}',
seqio.Task,
source=seqio.TfdsDataSource(
tfds_name=f'{tfds_name}:{version}',
tfds_data_dir=tfds_data_dir,
),
**kwargs,
))
if pinned_version is not None:
tasks.append(
cls.add(
name,
seqio.Task,
source=seqio.TfdsDataSource(
tfds_name=f'{tfds_name}:{pinned_version}',
tfds_data_dir=tfds_data_dir,
),
**kwargs,
))
return tasks
# C4 corpus for language model pretraining
TaskRegistry.add_versioned_tfds_task(
'c4_lm_v301_gpt',
versions=['3.0.4'],
pinned_version='3.0.4',
tfds_name='c4/en',
tfds_data_dir=C4_TRAIN_DATADIR,
preprocessors=[
functools.partial(
t5_preprocessors.rekey,
key_map={
'inputs': None,
'targets': 'text',
},
),
seqio.preprocessors.tokenize,
functools.partial(
t5_preprocessors.reduce_concat_tokens,
batch_size=4096,
),
t5_preprocessors.split_tokens_to_targets_length,
],
output_features=C4_GPT_TRAIN_FEATURES_LM,
metric_fns=[],
shuffle_buffer_size=10000,
)
TaskRegistry.add_versioned_tfds_task(
'c4_lm_v301_gpt_eval_tokenized',
versions=['3.0.5'],
pinned_version='3.0.5',
tfds_name='c4/en',
tfds_data_dir=C4_EVAL_DATADIR,
preprocessors=[
functools.partial(
t5_preprocessors.rekey,
key_map={
'inputs': None,
'targets': 'ids',
},
),
seqio.preprocessors.tokenize,
],
output_features=C4_GPT_EVAL_FEATURES_LM,
metric_fns=[],
shuffle_buffer_size=None,
)
class C4UnsupervisedDataset(base_experiment.BaseExperiment):
"""Used for training Baseline ULM."""
PERCORE_BATCH_SIZE = 1
PERCORE_EVAL_BATCH_SIZE = None
MAX_SEQ_LEN = 1024
TRAINING_SEED = 9876
TRAINING_NUM_BATCHES_TO_SKIP = None
TRAIN_MIXTURE = 'c4_lm_v301_gpt'
EVAL_MIXTURE = 'c4_lm_v301_gpt_eval_tokenized'
def _dataset_common(
self, is_training
) -> pax_fiddle.Config[base_input.BaseInput]:
if is_training:
percore_batch_size = self.PERCORE_BATCH_SIZE
else:
if self.PERCORE_EVAL_BATCH_SIZE is not None:
percore_batch_size = self.PERCORE_EVAL_BATCH_SIZE
else:
percore_batch_size = self.PERCORE_BATCH_SIZE
num_local_devices = jax.local_device_count()
global_batch_size = int(
percore_batch_size * num_local_devices * jax.process_count() + 1e-6
)
if percore_batch_size >= 1:
assert global_batch_size % num_local_devices == 0
batch_size_per_process = int(
math.ceil(percore_batch_size) * num_local_devices + 1e-6
)
num_infeed_hosts = global_batch_size // batch_size_per_process
else:
if jax.process_count() > 1:
assert global_batch_size % num_local_devices == 0
batch_size_per_process = num_local_devices
num_infeed_hosts = global_batch_size // batch_size_per_process
else:
batch_size_per_process = int(
percore_batch_size * num_local_devices + 1e-6
)
num_infeed_hosts = 1
seed = None
if is_training:
seed = self.TRAINING_SEED
# TODO(sgpyc): enable sync of seeds across hosts, currently the
# following failed because of "sync_global_devices name mismatch"
# seed = jnp.int32(multihost_utils.broadcast_one_to_all(seed))
logging.info('Train input seed: %s',
'None' if seed is None else seed)
p = pax_fiddle.Config(
seqio_input.SeqIOInput,
name='C4Train' if is_training else 'C4Validation',
mixture_name=self.TRAIN_MIXTURE if is_training else self.EVAL_MIXTURE,
split_name='train2' if is_training else 'validation_tokenized_5662seqs',
task_feature_lengths={'targets': self.MAX_SEQ_LEN},
use_cached=False,
repeat=True if is_training else False,
feature_converter=seqio_input.LanguageModelFeatures(
pack=True if is_training else False,
use_custom_packing_ops=False,
bos_id=0,
reverse_bos_padding=True,
eos_id=GPT_EOS_ID,
),
is_training=is_training,
input_random_seed=(seed if is_training else 4321),
batch_size=batch_size_per_process,
drop_remainder=True if is_training else False,
num_batches_to_skip=self.TRAINING_NUM_BATCHES_TO_SKIP,
num_infeed_hosts=num_infeed_hosts,
reset_for_eval=False if is_training else True,
annotate_padding_fields=True,
)
return p
def datasets(self) -> list[pax_fiddle.Config[base_input.BaseInput]]:
"""Returns a list of dataset parameters."""
return [
self._dataset_common(is_training=True),
self._dataset_common(is_training=False)
]
def set_adam_and_learning_rate_schedule(
cls,
task_p: pax_fiddle.Config[tasks_lib.SingleTask],
) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Sets the Adam optimizer and the learning rate schedule."""
lp = task_p.train.learner
lp.loss_name = 'total_loss'
lp.optimizer = pax_fiddle.Config(
optimizers.Adam,
beta1=cls.ADAM_BETA1 if cls.ADAM_BETA1 else 0.9,
beta2=cls.ADAM_BETA2 if cls.ADAM_BETA2 else 0.999,
weight_decay=cls.WEIGHT_DECAY if cls.WEIGHT_DECAY else 0.0,
epsilon=cls.ADAM_EPSILON if cls.ADAM_EPSILON else 1e-6,
epsilon_root=cls.ADAM_EPSILON_ROOT if cls.ADAM_EPSILON_ROOT else 0.0,
clip_gradient_norm_to_value=cls.CLIP_GRADIENT_NORM_TO_VALUE
if cls.CLIP_GRADIENT_NORM_TO_VALUE
else 5.0,
clip_threshold=cls.ADAM_CLIP_THRESHOLD
if cls.ADAM_CLIP_THRESHOLD
else 1.0,
)
if hasattr(cls, 'PERCORE_BATCH_SIZE'):
global_batch_size = int(cls.PERCORE_BATCH_SIZE * jax.device_count() + 1e-6)
if global_batch_size == 0:
logging.warning(
(
'Found global_batch_size = 0: cls.PERCORE_BATCH_SIZE=%s,'
' jax.device_count()=%s'
),
cls.PERCORE_BATCH_SIZE,
jax.device_count(),
)
assert global_batch_size <= 8192
else:
global_batch_size = None
if cls.LEARNING_RATE is not None:
lp.optimizer.learning_rate = cls.LEARNING_RATE
else:
assert global_batch_size is not None
if global_batch_size <= 3584:
lp.optimizer.learning_rate = 2e-5
else:
lp.optimizer.learning_rate = 3e-5
if cls.LR_SCHEDULE == 'linear_rampup_exponential_decay':
lp.optimizer.lr_schedule = pax_fiddle.Config(
schedules.LinearRampupExponentialDecay,
warmup_steps=cls.LR_LRED_WARMUP,
decay_start=cls.LR_LRED_DECAY_START,
decay_end=cls.LR_LRED_DECAY_END,
min_ratio=cls.LR_LRED_MIN_RATIO,
max=cls.LR_LRED_MAX,
)
elif cls.LR_SCHEDULE == 'linear_rampup_cosine_decay':
if cls.LR_COS_WARMUP is not None:
warmup_steps = cls.LR_COS_WARMUP
else:
assert global_batch_size is not None
warmup_steps = math.ceil(265.0 * 1536 / global_batch_size - 1e-6)
assert warmup_steps > 0
if cls.LR_COS_DECAY_START is not None:
decay_start_step = cls.LR_COS_DECAY_START
else:
decay_start_step = warmup_steps + 1
if cls.LR_COS_DECAY_END is not None:
decay_end_step = cls.LR_COS_DECAY_END
else:
assert global_batch_size is not None
decay_end_step = math.ceil(108600.0 * 1536 / global_batch_size - 1e-6)
assert decay_end_step > 0
lp.optimizer.lr_schedule = pax_fiddle.Config(
schedules.LinearRampupCosineDecay,
warmup_steps=warmup_steps,
decay_start=decay_start_step,
decay_end=decay_end_step,
min_ratio=cls.LR_COS_MIN_RATIO,
max=cls.LR_COS_MAX,
)
elif cls.LR_SCHEDULE == 'constant':
lp.optimizer.lr_schedule = pax_fiddle.Config(
schedules.Constant,
)
else:
raise NotImplementedError(
f'Learning rate schedule {cls.LR_SCHEDULE} is not supported.'
)
return task_p
class TransformerLmSpmdAdam(model_params.TransformerLmSpmdAdafactor):
"""Base SPMD Transformer LM configuration using Adam.
Only things different from TransformerLmSpmdAdafactor are listed.
"""
# architecture related
NUM_LAYERS = 32
NUM_HEADS = 16
MODEL_DIMS = 1024
HIDDEN_DIMS = MODEL_DIMS * 4
FPROP_DTYPE = jnp.float32
PACKED_INPUT = True
USE_BIAS = False
EMBEDDING_LOOKUP_STYLE = 'matmul'
# optimizer related
LEARNING_RATE = 1e-3
ADAM_BETA1 = 0.9
ADAM_BETA2 = 0.99
ADAM_CLIP_THRESHOLD = 1.0
ADAM_EPSILON = 1e-6
ADAM_EPSILON_ROOT = 0.0
# Learning rate schedule
LR_SCHEDULE = 'linear_rampup_exponential_decay'
LR_LRED_WARMUP = 4000
LR_LRED_DECAY_START = 4001
LR_LRED_DECAY_END = 300000
LR_LRED_MIN_RATIO = 0.1
LR_LRED_MAX = 1.0
LR_COS_MIN_RATIO = 0.1
LR_COS_MAX = 1.0
LR_COS_WARMUP = 4000
LR_COS_DECAY_START = 4001
LR_COS_DECAY_END = 300000
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = super().task()
model_p = task_p.model
model_p.lm_tpl.packed_input = self.PACKED_INPUT # pytype: disable=attribute-error # enable-nested-classes
stacked_p = model_p.lm_tpl.stacked_transformer_tpl # pytype: disable=attribute-error # enable-nested-classes
if fdl.get_callable(stacked_p) == transformers.PipelinedTransformer:
stacked_p = stacked_p.pipeline_stage
if self.USE_REPEATED_LAYER:
stacked_p = stacked_p.block
transformer_layer_p = stacked_p.transformer_layer_params_tpl
transformer_layer_p.tr_atten_tpl.use_bias = self.USE_BIAS
task_p = set_adam_and_learning_rate_schedule(cls=self, task_p=task_p)
return task_p
class TransformerLmSpmdPipelineAdam(
model_params.TransformerLmSpmdPipelineAdafactor
):
"""Base pipelined SPMD Transformer LM configuration using Adam.
Only things different from TransformerLmSpmdPipelineAdafactor are listed.
"""
# architecture related
NUM_LAYERS = 32
NUM_HEADS = 16
MODEL_DIMS = 1024
HIDDEN_DIMS = MODEL_DIMS * 4
FPROP_DTYPE = jnp.float32
PACKED_INPUT = True
USE_BIAS = False
EMBEDDING_LOOKUP_STYLE = 'matmul'
# optimizer related
LEARNING_RATE = 1e-3
ADAM_BETA1 = 0.9
ADAM_BETA2 = 0.99
ADAM_CLIP_THRESHOLD = 1.0
ADAM_EPSILON = 1e-6
ADAM_EPSILON_ROOT = 0.0
# Learning rate schedule
LR_SCHEDULE = 'linear_rampup_exponential_decay'
LR_LRED_WARMUP = 4000
LR_LRED_DECAY_START = 4001
LR_LRED_DECAY_END = 300000
LR_LRED_MIN_RATIO = 0.1
LR_LRED_MAX = 1.0
LR_COS_MIN_RATIO = 0.1
LR_COS_MAX = 1.0
LR_COS_WARMUP = 4000
LR_COS_DECAY_START = 4001
LR_COS_DECAY_END = 300000
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = super().task()
model_p = task_p.model
model_p.lm_tpl.packed_input = self.PACKED_INPUT # pytype: disable=attribute-error # enable-nested-classes
stacked_p = model_p.lm_tpl.stacked_transformer_tpl # pytype: disable=attribute-error # enable-nested-classes
if fdl.get_callable(stacked_p) == transformers.PipelinedTransformer:
stacked_p = stacked_p.pipeline_stage
if self.USE_REPEATED_LAYER:
stacked_p = stacked_p.block
transformer_layer_p = stacked_p.transformer_layer_params_tpl
transformer_layer_p.tr_atten_tpl.use_bias = self.USE_BIAS
task_p = set_adam_and_learning_rate_schedule(cls=self, task_p=task_p)
return task_p
@experiment_registry.register
class LmCloudSpmdAdam(TransformerLmSpmdAdam, lm_cloud.SyntheticDataset):
"""Base config for an SPMD model."""
NUM_LAYERS = 2
MODEL_DIMS = 2048
HIDDEN_DIMS = MODEL_DIMS * 4
ACTIVATION_CLS = layers.GELU
USE_GATED_ACTIVATION = False
# Autodiff remat.
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_NOTHING
# Sub-class has to specify a mesh.
ICI_MESH_SHAPE = [1, 4, 2]
@experiment_registry.register
class LmCloudSpmdAdamLimitSteps(LmCloudSpmdAdam):
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
task_p = super().task()
task_p.train.num_train_steps = 4000
return task_p
class EarlyStoppingFn(base_hyperparams.FiddleBaseParameterizable):
r"""Early stopping function to log eval log_pplx and stop when reaching target.
Attributes:
target_log_pplx: target log pplx value to stop training when eval log pplx
reaches this value.
"""
target_log_pplx: float | None = None
def __call__(
self,
metrics: dict[str, float],
running_mode: trainer_lib.RunningMode,
global_step: int,
is_last_ckpt: bool,
checkpoint_path: epath.Path,
) -> bool:
"""Returns True if run should be stopped early."""
if 'eval_test_C4Validation/metrics/log_pplx' not in metrics.keys():
return False
log_pplx = metrics['eval_test_C4Validation/metrics/log_pplx']
if log_pplx <= self.target_log_pplx:
return True
return False
def configure_gpt3_task(
cls,
task_p: pax_fiddle.Config[tasks_lib.SingleTask],
) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns task with gpt3 related configs."""
model_p = task_p.model # pytype: disable=attribute-error # enable-nested-classes
model_p.decoder_tpl.eos_id = (
GPT_EOS_ID # pytype: disable=attribute-error # enable-nested-classes
)
model_p.decoder_tpl.seqlen = cls.MAX_SEQ_LEN # pytype: disable=attribute-error # enable-nested-classes
model_p.params_init = WeightInit.Gaussian(0.006)
softmax_init = WeightInit.Gaussian(0.006)
model_p.lm_tpl.softmax_tpl.params_init = softmax_init
model_p.lm_tpl.softmax_tpl.feed_forward_tpl.has_bias = False
model_p.lm_tpl.softmax_tpl.soft_cap_logits = None
if cls.SEPARATE_EMBEDDING:
model_p.lm_tpl.separate_embedding_tpl.scale_sqrt_depth = False
model_p.lm_tpl.separate_embedding_tpl.lookup_style = (
cls.EMBEDDING_LOOKUP_STYLE
)
else:
model_p.lm_tpl.softmax_tpl.scale_sqrt_depth = False
model_p.lm_tpl.softmax_tpl.lookup_style = cls.EMBEDDING_LOOKUP_STYLE
if cls.TRAINABLE_POSITION_EMB:
model_p.lm_tpl.position_emb_tpl.lookup_style = cls.EMBEDDING_LOOKUP_STYLE
stacked_p = model_p.lm_tpl.stacked_transformer_tpl
if fdl.get_callable(stacked_p) == transformers.PipelinedTransformer:
stacked_p = stacked_p.pipeline_stage
if issubclass(
fdl.get_callable(stacked_p), transformers.StackedTransformerRepeated
):
stacked_p = stacked_p.block
transformer_layer_p = stacked_p.transformer_layer_params_tpl
transformer_layer_p.ln_tpl.epsilon = cls.LAYERNORM_EPSILON
transformer_layer_p.tr_fflayer_tpl.ln_tpl.epsilon = cls.LAYERNORM_EPSILON
model_p.lm_tpl.final_ln_tpl.epsilon = cls.LAYERNORM_EPSILON
transformer_layer_p.tr_atten_tpl.internal_enable_per_dim_scale = False
transformer_layer_p.tr_atten_tpl.use_bias = True
transformer_layer_p.tr_fflayer_tpl.activation_tpl.approximate = True
for atten_p in (
transformer_layer_p.tr_atten_tpl,
transformer_layer_p.cross_atten_tpl,
):
if atten_p is None:
continue
atten_wp = atten_p.weight_split_dims_mapping
atten_wp.proj = ['data', 'mdl', None]
if task_p.early_stopping_fn is None:
task_p.early_stopping_fn = pax_fiddle.Config(EarlyStoppingFn)
task_p.early_stopping_fn.target_log_pplx = cls.TARGET_LOG_PPLX
return task_p
@experiment_registry.register
class C4SpmdAdam(TransformerLmSpmdAdam,
C4UnsupervisedDataset):
r"""Base config for a decoder only transformer."""
NUM_LAYERS = 24
NUM_HEADS = 32
MODEL_DIMS = 2048
# Known as MLP_DIM in t5x
HIDDEN_DIMS = MODEL_DIMS * 4
# Defaults to MODEL_DIMS // NUM_HEADS.
DIMS_PER_HEAD = None
# Known as NUM_EMBEDDINGS in t5x
VOCAB_SIZE = 32128
ACTIVATION_CLS = layers.GELU
USE_GATED_ACTIVATION = False
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_DOT_FOR_MLPERF_200B
CHECKPOINT_EVERY_N_STEPS = 1000
# Sub-class has to specify a mesh.
ICI_MESH_SHAPE = [1, 4, 2]
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = super().task()
model_p = task_p.model # pytype: disable=attribute-error # enable-nested-classes
model_p.decoder_tpl.eos_id = GPT_EOS_ID # pytype: disable=attribute-error # enable-nested-classes
model_p.decoder_tpl.seqlen = self.MAX_SEQ_LEN # pytype: disable=attribute-error # enable-nested-classes
task_p = set_adam_and_learning_rate_schedule(cls=self, task_p=task_p)
return task_p
class C4SpmdGpt3AdamOrgHP(C4SpmdAdam):
r"""GPT-3 config with original HPs.
From the paper & after convergence matching with
NVIDIA's Megatron-LM framework.
"""
MAX_SEQ_LEN = 2048
NUM_LAYERS = 96
NUM_HEADS = 96
MODEL_DIMS = 12288
# Known as MLP_DIM in t5x
HIDDEN_DIMS = MODEL_DIMS * 4
# Defaults to MODEL_DIMS // NUM_HEADS.
DIMS_PER_HEAD = None
# Known as NUM_EMBEDDINGS in t5x
VOCAB_SIZE = 50257
USE_REPEATED_LAYER = True
# Model configs
ACTIVATION_CLS = layers.GELU
USE_GATED_ACTIVATION = False
SEPARATE_EMBEDDING = False
TRAINABLE_POSITION_EMB = True
TRAINABLE_PE_MAX_SEQ_LEN = 16384
ATTEN_LOGIT_CAP = -1.0 # Disable logits cap in atten
# HPs
LEARNING_RATE = 6e-5
WEIGHT_DECAY = 0.1
ADAM_BETA1 = 0.9
ADAM_BETA2 = 0.95
ADAM_EPSILON = 1e-8
ADAM_CLIP_THRESHOLD = -1.0 # Disable Adam clip_threshold
CLIP_GRADIENT_NORM_TO_VALUE = 1.0
LAYERNORM_EPSILON = 1e-5
# In units of steps for BS1.5k
LR_SCHEDULE = 'linear_rampup_cosine_decay'
LR_COS_WARMUP = 265
LR_COS_DECAY_START = LR_COS_WARMUP + 1
LR_COS_DECAY_END = 108600
LR_COS_MAX = 1.0
LR_COS_MIN_RATIO = 0.1
# Training target
TARGET_LOG_PPLX = 2.69
# Autodiff remat.
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_NOTHING
# Checkpoint
EVAL_INTERVAL_STEPS = 100
SUMMARY_INTERVAL_STEPS = 10
CHECKPOINT_EVERY_N_STEPS = 100
CHECKPOINT_MAX_TO_KEEP = 10
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = super().task()
task_p = configure_gpt3_task(self, task_p)
return task_p
@experiment_registry.register
class C4SpmdGpt3AdamOrgHPBS1p5k1536Replicas(C4SpmdGpt3AdamOrgHP):
r"""GPT-3 config in fp32 for 1536 replicas with 1536 global batch size."""
# Padded to TPU friendly size
VOCAB_SIZE = 51200
PERCORE_BATCH_SIZE = 1
ICI_MESH_SHAPE = [1, 64, 24]
FPROP_DTYPE = jnp.float32
CHECKPOINT_MAX_TO_KEEP = 100
EVAL_INTERVAL_STEPS = 25
SUMMARY_INTERVAL_STEPS = 1
@experiment_registry.register
class C4SpmdPipelineAdam(TransformerLmSpmdPipelineAdam, C4UnsupervisedDataset):
r"""Base config for a decoder only transformer with pipeline."""
NUM_LAYERS = 24
NUM_HEADS = 32
MODEL_DIMS = 2048
# Known as MLP_DIM in t5x
HIDDEN_DIMS = MODEL_DIMS * 4
# Defaults to MODEL_DIMS // NUM_HEADS.
DIMS_PER_HEAD = None
# Known as NUM_EMBEDDINGS in t5x
VOCAB_SIZE = 32128
ACTIVATION_CLS = layers.GELU
USE_GATED_ACTIVATION = False
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_DOT_FOR_MLPERF_200B
CHECKPOINT_EVERY_N_STEPS = 1000
# Sub-class has to specify a mesh.
MICROBATCH_SIZE = 2
ICI_MESH_SHAPE = [2, 1, 2, 2]
NUM_STAGES = 2
EMB_W_DATA_DIMS = ('replica', 'data')
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = super().task()
model_p = task_p.model # pytype: disable=attribute-error # enable-nested-classes
model_p.decoder_tpl.eos_id = (
GPT_EOS_ID # pytype: disable=attribute-error # enable-nested-classes
)
model_p.decoder_tpl.seqlen = self.MAX_SEQ_LEN # pytype: disable=attribute-error # enable-nested-classes
task_p = set_adam_and_learning_rate_schedule(cls=self, task_p=task_p)
return task_p
class C4SpmdPipelineGpt3AdamOrgHP(C4SpmdPipelineAdam):
r"""GPT-3 config with original HPs.
From the paper & after convergence matching with
NVIDIA's Megatron-LM framework.
"""
MAX_SEQ_LEN = 2048
NUM_LAYERS = 96
NUM_HEADS = 96
MODEL_DIMS = 12288
# Known as MLP_DIM in t5x
HIDDEN_DIMS = MODEL_DIMS * 4
# Defaults to MODEL_DIMS // NUM_HEADS.
DIMS_PER_HEAD = None
# Known as NUM_EMBEDDINGS in t5x
VOCAB_SIZE = 50257
USE_REPEATED_LAYER = False
# Model configs
ACTIVATION_CLS = layers.GELU
USE_GATED_ACTIVATION = False
SEPARATE_EMBEDDING = False
TRAINABLE_POSITION_EMB = True
TRAINABLE_PE_MAX_SEQ_LEN = 16384
ATTEN_LOGIT_CAP = -1.0 # Disable logits cap in atten
# HPs
LEARNING_RATE = 6e-5
WEIGHT_DECAY = 0.1
ADAM_BETA1 = 0.9
ADAM_BETA2 = 0.95
ADAM_EPSILON = 1e-8
ADAM_CLIP_THRESHOLD = -1.0 # Disable Adam clip_threshold
CLIP_GRADIENT_NORM_TO_VALUE = 1.0
LAYERNORM_EPSILON = 1e-5
# In units of steps for BS1.5k
LR_SCHEDULE = 'linear_rampup_cosine_decay'
LR_COS_WARMUP = 265
LR_COS_DECAY_START = LR_COS_WARMUP + 1
LR_COS_DECAY_END = 108600
LR_COS_MAX = 1.0
LR_COS_MIN_RATIO = 0.1
# Training target
TARGET_LOG_PPLX = 2.69
# Autodiff remat.
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_NOTHING
# Checkpoint
EVAL_INTERVAL_STEPS = 100
SUMMARY_INTERVAL_STEPS = 10
CHECKPOINT_EVERY_N_STEPS = 100
CHECKPOINT_MAX_TO_KEEP = 10
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
"""Returns the task parameters."""
task_p = super().task()
task_p = configure_gpt3_task(self, task_p)
return task_p
class C4SpmdPipelineGpt3AdamMLPerfHP(C4SpmdPipelineGpt3AdamOrgHP):
r"""GPT-3 config for MLPerf reference."""
# Padded to TPU friendly size
VOCAB_SIZE = 51200
FPROP_DTYPE = jnp.float32
SUMMARY_INTERVAL_STEPS = 1
# subclass must set the eval and the checkpoint intervals
EVAL_INTERVAL_STEPS = None
CHECKPOINT_EVERY_N_STEPS = None
CHECKPOINT_MAX_TO_KEEP = 100
# Let set_adam_and_learning_rate_schedule calculate the following HPs
# based on global batch size
LEARNING_RATE = None
LR_COS_WARMUP = None
LR_COS_DECAY_START = None
LR_COS_DECAY_END = None
@experiment_registry.register
class C4SpmdPipelineGpt3AdamOrgHPBS1p5k768Replicas(C4SpmdPipelineGpt3AdamOrgHP):
r"""GPT-3 config in fp32 for 768 replicas with 1536 global batch size.
Using the orininal HP set.
"""
PERCORE_BATCH_SIZE = 2
VOCAB_SIZE = 51200
NUM_STAGES = 8
ICI_MESH_SHAPE = [8, 1, 8, 12]
# NUM_MICROBATCHS = 192
MICROBATCH_SIAZE = 8
FPROP_DTYPE = jnp.float32
CHECKPOINT_MAX_TO_KEEP = 100
EVAL_INTERVAL_STEPS = 25
SUMMARY_INTERVAL_STEPS = 1
CHECKPOINT_EVERY_N_STEPS = 50
STREAM_IO = False
@experiment_registry.register
class C4SpmdPipelineGpt3AdamMLPerfHPBS1p5k768Replicas(
C4SpmdPipelineGpt3AdamMLPerfHP
):
r"""GPT-3 config in fp32 for 768 replicas with 1536 global batch size.
Following MLPerf training benchmarking HP requirements.
"""
PERCORE_BATCH_SIZE = 2
NUM_STAGES = 8
ICI_MESH_SHAPE = [8, 1, 8, 12]
# NUM_MICROBATCHS = 192
MICROBATCH_SIZE = 8
EVAL_INTERVAL_STEPS = 16
CHECKPOINT_EVERY_N_STEPS = EVAL_INTERVAL_STEPS * 2
STREAM_IO = False
@experiment_registry.register
class C4SpmdPipelineGpt3AdamMLPerfHPBS2k512Replicas(
C4SpmdPipelineGpt3AdamMLPerfHP
):
r"""GPT-3 config in fp32 for 512 replicas with 2k global batch size.
Following MLPerf training benchmarking HP requirements.
"""
PERCORE_BATCH_SIZE = 4
NUM_STAGES = 8
ICI_MESH_SHAPE = [8, 1, 8, 8]
# NUM_MICROBATCHS = 256
MICROBATCH_SIZE = 8
EVAL_INTERVAL_STEPS = 12
CHECKPOINT_EVERY_N_STEPS = EVAL_INTERVAL_STEPS * 2
STREAM_IO = True
@experiment_registry.register
class C4SpmdPipelineGpt3AdamMLPerfHPBS3k768Replicas(
C4SpmdPipelineGpt3AdamMLPerfHP
):
r"""GPT-3 config in fp32 for 768 replicas with 3072 global batch size.
Following MLPerf benchmarking HP requirements.
"""
PERCORE_BATCH_SIZE = 4
NUM_STAGES = 4
ICI_MESH_SHAPE = [4, 1, 16, 12]
# NUM_MICROBATCHS = 192
MICROBATCH_SIZE = 16
EVAL_INTERVAL_STEPS = 8
CHECKPOINT_EVERY_N_STEPS = EVAL_INTERVAL_STEPS * 2
STREAM_IO = True
@experiment_registry.register
class C4SpmdPipelineGpt3AdamMLPerfHPBS4k1024Replicas(
C4SpmdPipelineGpt3AdamMLPerfHP
):
r"""GPT-3 config in fp32 for 1024 replicas with 4096 global batch size.
Following MLPerf benchmarking HP requirements.
"""
PERCORE_BATCH_SIZE = 4
NUM_STAGES = 8
ICI_MESH_SHAPE = [8, 1, 8, 16]
# NUM_MICROBATCHS = 512
MICROBATCH_SIZE = 8
EVAL_INTERVAL_STEPS = 6
CHECKPOINT_EVERY_N_STEPS = EVAL_INTERVAL_STEPS * 2
STREAM_IO = True
@experiment_registry.register
class C4SpmdPipelineGpt3AdamMLPerfHPBS8k1024Replicas(
C4SpmdPipelineGpt3AdamMLPerfHP
):
r"""GPT-3 config in fp32 for 1024 replicas with 8192 global batch size.
Following MLPerf benchmarking HP requirements.
"""
PERCORE_BATCH_SIZE = 8
NUM_STAGES = 4
ICI_MESH_SHAPE = [4, 1, 16, 16]
# NUM_MICROBATCHS = 512
MICROBATCH_SIZE = 16
EVAL_INTERVAL_STEPS = 3
CHECKPOINT_EVERY_N_STEPS = EVAL_INTERVAL_STEPS * 2
STREAM_IO = True
@experiment_registry.register
class C4Spmd1BAdam4Replicas(C4SpmdAdam):
r"""GPT-3 config with 1B params.
Model Parameters: Global batch size = 1 * 4 * 1 * 32 = 128
"""
NUM_LAYERS = 13
MODEL_DIMS = 2560
HIDDEN_DIMS = MODEL_DIMS * 4
NUM_HEADS = 20
DIMS_PER_HEAD = 128
PERCORE_BATCH_SIZE = 32
MAX_SEQ_LEN = 1024
VOCAB_SIZE = 32000
FPROP_DTYPE = jnp.bfloat16
USE_REPEATED_LAYER = True
SUMMARY_INTERVAL_STEPS = 10
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_NOTHING
ICI_MESH_SHAPE = [1, 4, 1]
@experiment_registry.register
class C4Spmd1BAdam4ReplicasLimitSteps(C4Spmd1BAdam4Replicas):
def task(self) -> pax_fiddle.Config[tasks_lib.SingleTask]:
task_p = super().task()
task_p.train.num_train_steps = 15000
return task_p
@experiment_registry.register
class C4Spmd2BAdam4Replicas(C4SpmdAdam):
r"""GPT-3 config with 2B params.
Model Parameters: Global batch size = 1 * 4 * 1 * 32 = 128.
"""
NUM_LAYERS = 18
MODEL_DIMS = 3072
HIDDEN_DIMS = MODEL_DIMS * 4
NUM_HEADS = 24
DIMS_PER_HEAD = 128
PERCORE_BATCH_SIZE = 32
MAX_SEQ_LEN = 1024
VOCAB_SIZE = 32000
FPROP_DTYPE = jnp.bfloat16
USE_REPEATED_LAYER = True
SUMMARY_INTERVAL_STEPS = 10
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_NOTHING
ICI_MESH_SHAPE = [1, 4, 1]
@experiment_registry.register
class C4Spmd16BAdam32Replicas(C4SpmdAdam):
r"""GPT-3 config with 16B params.
Model Parameters: Global batch size = 1 * 2 * 16 * 16 = 512.
"""
NUM_LAYERS = 36
MODEL_DIMS = 6144
HIDDEN_DIMS = MODEL_DIMS * 4
NUM_HEADS = 48
DIMS_PER_HEAD = 128
PERCORE_BATCH_SIZE = 16
MAX_SEQ_LEN = 1024
VOCAB_SIZE = 32000
FPROP_DTYPE = jnp.bfloat16
USE_REPEATED_LAYER = True
SUMMARY_INTERVAL_STEPS = 10
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_NOTHING
ICI_MESH_SHAPE = [1, 16, 2]
@experiment_registry.register
class C4Spmd32BAdam64Replicas(C4SpmdAdam):
r"""GPT-3 config with 32B params.
Model Parameters: Global batch size = 1 * 16 * 4 * 8 = 512.
"""
NUM_LAYERS = 40
MODEL_DIMS = 8192
HIDDEN_DIMS = MODEL_DIMS * 4
NUM_HEADS = 64
DIMS_PER_HEAD = 128
PERCORE_BATCH_SIZE = 8
MAX_SEQ_LEN = 1024
VOCAB_SIZE = 32000
FPROP_DTYPE = jnp.bfloat16
USE_REPEATED_LAYER = True
SUMMARY_INTERVAL_STEPS = 10
CHECKPOINT_POLICY = layers.AutodiffCheckpointType.SAVE_NOTHING