forked from PaddlePaddle/PaddleNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_pretrain.py
558 lines (478 loc) · 20.1 KB
/
run_pretrain.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
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
import math
import os
import random
import sys
import time
from dataclasses import dataclass, field
from typing import List, Optional
import numpy as np
import paddle
from paddlenlp.data.causal_dataset import (
build_train_valid_test_datasets,
check_data_split,
print_rank_0,
)
from paddlenlp.trainer import (
PdArgumentParser,
Trainer,
TrainingArguments,
get_last_checkpoint,
speed_metrics,
)
from paddlenlp.transformers import (
AutoConfig,
AutoModelForCausalLM,
AutoModelForCausalLMPipe,
AutoTokenizer,
CosineAnnealingWithWarmupDecay,
LinearAnnealingWithWarmupDecay,
register_sequence_parallel_allreduce_hooks,
)
from paddlenlp.utils.batch_sampler import DistributedBatchSampler
from paddlenlp.utils.log import logger
def add_start_docstrings(*docstr):
def docstring_decorator(fn):
fn.__doc__ = "".join(docstr) + (fn.__doc__ if fn.__doc__ is not None else "")
return fn
return docstring_decorator
@dataclass
@add_start_docstrings(TrainingArguments.__doc__)
class PreTrainingArguments(TrainingArguments):
min_learning_rate: float = field(
default=1e-5,
metadata={"help": "Minimum learning rate deacyed to."},
)
decay_steps: float = field(
default=None,
metadata={
"help": "The steps use to control the learing rate. If the step > decay_steps, will use the min_learning_rate."
},
)
enable_linear_fused_grad_add: bool = field(
default=False,
metadata={
"help": "Enable fused linear grad add strategy, which will reduce elementwise add for grad accumulation in the backward of nn.Linear ."
},
)
@dataclass
class DataArguments:
"""
Arguments pertaining to what data we are going to input our model for training and evaluating.
Using `PdArgumentParser` we can turn this class into argparse arguments to be able to
specify them on the command line.
"""
input_dir: str = field(
default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
)
split: str = field(default="949,50,1", metadata={"help": "Train/valid/test data split."})
max_seq_length: int = field(
default=1024,
metadata={
"help": "The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated, sequences shorter will be padded."
},
)
share_folder: bool = field(
default=False,
metadata={"help": "Use share folder for data dir and output dir on multi machine."},
)
data_impl: str = field(default="mmap", metadata={"help": "The format of the preprocessed data."})
skip_warmup: bool = field(
default=True,
metadata={"help": "Whether to skip the warmup process of mmap files."},
)
data_cache: str = field(default=None, metadata={"help": "The path of the cached dataset."})
@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to pre-train from.
"""
model_name_or_path: str = field(
default="__internal_testing__/tiny-random-llama",
metadata={
"help": "Path to pretrained model or model identifier from https://paddlenlp.readthedocs.io/zh/latest/model_zoo/transformers.html"
},
)
tokenizer_name_or_path: Optional[str] = field(
default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
)
config_name: Optional[str] = field(
default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
)
use_flash_attention: bool = field(
default=False,
metadata={"help": "use_flash_attention"},
)
use_fused_rms_norm: bool = field(
default=False,
metadata={"help": "llama or other model, use_fused_rms_norm"},
)
fuse_attention_qkv: bool = field(
default=False,
metadata={"help": "whether to fuse attention qkv"},
)
fuse_attention_ffn: bool = field(
default=False,
metadata={"help": "whether to fuse first up and gate proj in mlp block"},
)
recompute_granularity: str = field(
default="full",
metadata={"help": "Choose among ['full', 'core_attn', 'full_attn']"},
)
virtual_pp_degree: int = field(
default=1,
metadata={"help": "virtual_pp_degree"},
)
continue_training: bool = field(
default=False,
metadata={
"help": "Pre-training from existing paddlenlp model weights. Default False and model will train from scratch. If set True, the model_name_or_path argument must exist in the paddlenlp models."
},
)
sequence_parallel: bool = field(
default=False,
metadata={"help": "whether to use sequence parallel"},
)
fuse_sequence_parallel_allreduce: bool = field(
default=False,
metadata={"help": "whether to use fuse sequence parallel allreduce"},
)
use_fused_rope: Optional[bool] = field(
default=False,
metadata={"help": "Enable rope fusion or not."},
)
no_recompute_layers: Optional[List[int]] = field(
default=None,
metadata={"help": "Specify the full transformer layers that should not be recomputed."},
)
pp_recompute_interval: int = field(
default=1,
metadata={
"help": "The interval for the number of layers at which recomputation occurs. A value of 0 indicates no recomputation. Default is 0."
},
)
recompute_use_reentrant: bool = field(
default=False,
metadata={"help": "recompute_use_reentrant"},
)
def create_pretrained_dataset(
data_args,
training_args,
data_file,
tokenizer,
need_data=True,
):
check_data_split(data_args.split, training_args.do_train, training_args.do_eval, training_args.do_predict)
train_val_test_num_samples = [
training_args.per_device_train_batch_size
* training_args.dataset_world_size
* training_args.max_steps
* training_args.gradient_accumulation_steps,
training_args.per_device_eval_batch_size
* training_args.dataset_world_size
* training_args.eval_iters
* (training_args.max_steps // training_args.eval_steps + 1),
training_args.per_device_eval_batch_size * training_args.dataset_world_size * training_args.test_iters,
]
print_rank_0(" > datasets target sizes (minimum size):")
if training_args.do_train:
print_rank_0(" train: {}".format(train_val_test_num_samples[0]))
if training_args.do_eval:
print_rank_0(" validation: {}".format(train_val_test_num_samples[1]))
if training_args.do_predict:
print_rank_0(" test: {}".format(train_val_test_num_samples[2]))
# Build the datasets.
train_dataset, valid_dataset, test_dataset = build_train_valid_test_datasets(
data_prefix=data_file,
data_impl=data_args.data_impl,
splits_string=data_args.split,
train_val_test_num_samples=train_val_test_num_samples,
seq_length=data_args.max_seq_length,
seed=training_args.seed,
skip_warmup=data_args.skip_warmup,
share_folder=data_args.share_folder,
data_cache_path=data_args.data_cache,
need_data=need_data,
)
def print_dataset(data, mode="train"):
logger.info(f"Sample data for {mode} mode.")
# input_ids, loss_mask, attention_mask, position_ids, labels = data
input_ids = data["text"]
logger.info(tokenizer._decode(list(input_ids)))
from paddlenlp.data import Stack
def _collate_data(data, stack_fn=Stack()):
tokens_ = stack_fn([x["text"] for x in data])
labels = tokens_[:, 1:]
tokens = tokens_[:, :-1]
return {
"input_ids": tokens,
"labels": labels,
}
if need_data:
if training_args.do_train:
print_dataset(train_dataset[0], "train")
if training_args.do_eval:
print_dataset(valid_dataset[0], "valid")
if training_args.do_predict:
print_dataset(test_dataset[0], "test")
return train_dataset, valid_dataset, test_dataset, _collate_data
def get_train_data_file(args):
if len(args.input_dir.split()) > 1:
# weight-1 data-prefix-1 weight-2 data-prefix-2 ...
return args.input_dir.split()
else:
files = [
os.path.join(args.input_dir, f)
for f in os.listdir(args.input_dir)
if (os.path.isfile(os.path.join(args.input_dir, f)) and ("_idx.npz" in str(f) or ".idx" in str(f)))
]
files = [x.replace("_idx.npz", "") for x in files]
files = [x.replace(".idx", "") for x in files] # add
if len(files) > 1:
ret = []
logger.info("You are using multi-dataset:")
for x in files:
ret.append(1.0)
ret.append(x)
logger.info(" > set weight of %s dataset to 1.0" % x)
return ret
return files
def set_seed(args):
if args.device == "cpu":
idx = 0
else:
idx = paddle.distributed.get_rank()
random.seed(args.seed + idx)
np.random.seed(args.seed + idx)
paddle.seed(args.seed + idx)
class PretrainingTrainer(Trainer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def evaluate(self, eval_dataset=None, ignore_keys=None, metric_key_prefix: str = "eval"):
# keep eval_dataloader
eval_dataloader = getattr(self, "eval_dataloader", None)
if eval_dataloader is None:
eval_dataset = self.eval_dataset if eval_dataset is None else eval_dataset
eval_dataloader = self.get_eval_dataloader(eval_dataset)
# must call data loader, otherwise, it will init many times, cause OOM error.
self.eval_dataloader = eval_dataloader()
start_time = time.time()
# Temporarily disable metric computation, we will do it in the loop here.
compute_metrics = self.compute_metrics
eval_loop = self.evaluation_loop
output = eval_loop(
eval_dataloader,
description="Evaluation",
# No point gathering the predictions if there are no metrics, otherwise we defer to
# self.args.prediction_loss_only
prediction_loss_only=True if compute_metrics is None else None,
ignore_keys=ignore_keys,
# Only evaluate max_eval_iters
max_eval_iters=self.args.eval_iters,
)
total_batch_size = self.args.eval_batch_size * self.args.world_size
output.metrics.update(
speed_metrics(
metric_key_prefix,
start_time,
num_samples=output.num_samples,
num_steps=math.ceil(output.num_samples / total_batch_size),
)
)
self.log(output.metrics)
self.control = self.callback_handler.on_evaluate(self.args, self.state, self.control, output.metrics)
return output.metrics
def _get_eval_sampler(self, eval_dataset) -> Optional[paddle.io.Sampler]:
return DistributedBatchSampler(
eval_dataset,
batch_size=self.args.per_device_eval_batch_size,
shuffle=False,
num_replicas=self.args.dataset_world_size,
rank=self.args.dataset_rank,
drop_last=self.args.dataloader_drop_last,
)
def _get_train_sampler(self) -> Optional[paddle.io.Sampler]:
return DistributedBatchSampler(
self.train_dataset,
batch_size=self.args.per_device_train_batch_size,
shuffle=False,
num_replicas=self.args.dataset_world_size,
rank=self.args.dataset_rank,
drop_last=self.args.dataloader_drop_last,
)
def main():
parser = PdArgumentParser((ModelArguments, DataArguments, PreTrainingArguments))
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
else:
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
if training_args.enable_linear_fused_grad_add:
from fused_layers import mock_layers
mock_layers()
if model_args.tokenizer_name_or_path is None:
model_args.tokenizer_name_or_path = model_args.model_name_or_path
if data_args.data_cache is not None:
os.makedirs(data_args.data_cache, exist_ok=True)
set_seed(training_args)
paddle.set_device(training_args.device)
if paddle.distributed.get_world_size() > 1:
paddle.distributed.init_parallel_env()
training_args.eval_iters = 10
training_args.test_iters = training_args.eval_iters * 10
# Log model and data config
training_args.print_config(model_args, "Model")
training_args.print_config(data_args, "Data")
# Log on each process the small summary:
logger.warning(
f"Process rank: {training_args.local_rank}, device: {training_args.device}, world_size: {training_args.world_size}, "
+ f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16 or training_args.bf16}"
)
# Detecting last checkpoint.
last_checkpoint = None
if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(training_args.output_dir)
# if last_checkpoint is None and len(
# os.listdir(training_args.output_dir)) > 1:
# raise ValueError(
# f"Output directory ({training_args.output_dir}) already exists and is not empty. "
# "Use --overwrite_output_dir to overcome.")
if last_checkpoint is not None and training_args.resume_from_checkpoint is None:
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name_or_path)
config = AutoConfig.from_pretrained(model_args.model_name_or_path)
config.seq_length = data_args.max_seq_length
# There are some technique extend RotaryEmbedding context. so don't change max_position_embeddings
if not model_args.continue_training:
config.max_position_embeddings = max(config.max_position_embeddings, data_args.max_seq_length)
if not model_args.continue_training:
config.vocab_size = max(config.vocab_size, ((tokenizer.vocab_size - 1) // 128 + 1) * 128)
logger.info(f"Reset vocab size to {config.vocab_size} for batter amp peformance.")
if model_args.no_recompute_layers is not None:
model_args.no_recompute_layers.sort()
config.use_flash_attention = model_args.use_flash_attention
config.use_fused_rms_norm = model_args.use_fused_rms_norm
config.fuse_attention_qkv = model_args.fuse_attention_qkv
config.fuse_attention_ffn = model_args.fuse_attention_ffn
config.recompute_granularity = model_args.recompute_granularity
config.virtual_pp_degree = model_args.virtual_pp_degree
config.sequence_parallel = model_args.sequence_parallel
config.fuse_sequence_parallel_allreduce = model_args.fuse_sequence_parallel_allreduce
config.use_fused_rope = model_args.use_fused_rope
config.no_recompute_layers = model_args.no_recompute_layers
config.pp_recompute_interval = model_args.pp_recompute_interval
config.recompute_use_reentrant = model_args.recompute_use_reentrant
config.use_recompute = training_args.recompute
config.tensor_parallel_degree = training_args.tensor_parallel_degree
config.tensor_parallel_rank = training_args.tensor_parallel_rank
print("Final pre-training config:", config)
# Set the dtype for loading model
dtype = "float32"
if training_args.fp16_opt_level == "O2":
if training_args.fp16:
dtype = "float16"
if training_args.bf16:
dtype = "bfloat16"
model_class = AutoModelForCausalLM
if training_args.pipeline_parallel_degree > 1:
model_class = AutoModelForCausalLMPipe
if model_args.continue_training:
model = model_class.from_pretrained(
model_args.model_name_or_path,
config=config,
dtype=dtype,
)
else:
model = model_class.from_config(config, dtype=dtype)
if model_args.sequence_parallel:
register_sequence_parallel_allreduce_hooks(
model, training_args.gradient_accumulation_steps, model_args.fuse_sequence_parallel_allreduce
)
if training_args.recompute:
model.recompute_enable()
# Create the learning_rate sheduler and optimizer
if training_args.decay_steps is None:
training_args.decay_steps = training_args.max_steps
if training_args.warmup_steps > 0:
warmup_steps = training_args.warmup_steps
else:
warmup_steps = training_args.warmup_ratio * training_args.max_steps
lr_scheduler = None
if training_args.lr_scheduler_type.value == "cosine":
lr_scheduler = CosineAnnealingWithWarmupDecay(
max_lr=training_args.learning_rate,
min_lr=training_args.min_learning_rate,
warmup_step=warmup_steps,
decay_step=training_args.decay_steps,
last_epoch=0,
)
elif training_args.lr_scheduler_type.value == "linear":
lr_scheduler = LinearAnnealingWithWarmupDecay(
max_lr=training_args.learning_rate,
min_lr=training_args.min_learning_rate,
warmup_step=warmup_steps,
decay_step=training_args.decay_steps,
last_epoch=0,
)
data_file = get_train_data_file(data_args)
train_dataset, eval_dataset, test_dataset, data_collator = create_pretrained_dataset(
data_args,
training_args,
data_file,
tokenizer,
need_data=training_args.should_load_dataset,
)
total_effective_tokens = (
training_args.per_device_train_batch_size
* training_args.dataset_world_size
* training_args.max_steps
* training_args.gradient_accumulation_steps
* data_args.max_seq_length
)
trainer = PretrainingTrainer(
model=model,
args=training_args,
data_collator=data_collator,
train_dataset=train_dataset if training_args.do_train else None,
eval_dataset=eval_dataset if training_args.do_eval else None,
optimizers=(None, lr_scheduler),
tokenizer=tokenizer,
)
checkpoint = None
if training_args.resume_from_checkpoint is not None:
checkpoint = training_args.resume_from_checkpoint
elif last_checkpoint is not None:
checkpoint = last_checkpoint
# Training
if training_args.do_train:
train_result = trainer.train(resume_from_checkpoint=checkpoint)
metrics = train_result.metrics
if not int(os.getenv("test_ci_no_save_model", 0)):
trainer.save_model()
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
if training_args.do_predict:
test_ret = trainer.predict(test_dataset)
trainer.log_metrics("test", test_ret.metrics)
if training_args.should_load_dataset:
effective_tokens_per_second = total_effective_tokens / train_result.metrics["train_runtime"]
print(f"Effective Tokens per second: {effective_tokens_per_second:.2f}")
print(f"ips: {effective_tokens_per_second:.2f} tokens/s")
if __name__ == "__main__":
main()