2023-05-04 21:52:25 +08:00
|
|
|
import argparse
|
2023-05-07 13:01:02 +08:00
|
|
|
from functools import partial
|
2023-05-04 21:52:25 +08:00
|
|
|
from itertools import chain
|
2023-05-07 13:01:02 +08:00
|
|
|
from typing import Dict, Tuple
|
2023-05-04 21:52:25 +08:00
|
|
|
|
|
|
|
import datasets
|
|
|
|
import pytorch_lightning as pl
|
|
|
|
import torch
|
|
|
|
from torch.utils.data import ConcatDataset, DataLoader
|
|
|
|
from transformers import (
|
|
|
|
BatchEncoding,
|
|
|
|
DefaultDataCollator,
|
|
|
|
PreTrainedTokenizer,
|
|
|
|
set_seed,
|
|
|
|
)
|
|
|
|
|
2023-05-07 13:01:02 +08:00
|
|
|
from lit_module import LitModule
|
2023-05-07 16:51:57 +08:00
|
|
|
from lit_patches import apply_all_patches
|
2023-05-07 13:01:02 +08:00
|
|
|
from utils import load_tokenizer
|
2023-05-04 21:52:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
def split_raw_dataset(
|
|
|
|
raw_dataset: datasets.DatasetDict,
|
|
|
|
) -> Tuple[datasets.Dataset, datasets.Dataset]:
|
2024-02-24 12:06:30 +08:00
|
|
|
if "validation" in raw_dataset:
|
|
|
|
train_dataset, val_dataset = raw_dataset["train"], raw_dataset["validation"]
|
2023-05-04 21:52:25 +08:00
|
|
|
else:
|
2024-02-24 12:06:30 +08:00
|
|
|
raw_dataset = raw_dataset["train"].train_test_split(test_size=0.05, seed=args.seed)
|
|
|
|
train_dataset, val_dataset = raw_dataset["train"], raw_dataset["test"]
|
2023-05-04 21:52:25 +08:00
|
|
|
return train_dataset, val_dataset
|
|
|
|
|
|
|
|
|
2023-05-28 20:02:56 +08:00
|
|
|
def process_dataset(dataset: datasets.Dataset, tokenizer: PreTrainedTokenizer) -> datasets.Dataset:
|
2023-05-04 21:52:25 +08:00
|
|
|
def group_texts(examples: Dict[str, list], block_size: int = 512) -> BatchEncoding:
|
|
|
|
concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
|
|
|
|
total_length = len(concatenated_examples[list(examples.keys())[0]])
|
|
|
|
total_length = (total_length // block_size) * block_size
|
|
|
|
result = {
|
|
|
|
k: [t[i : i + block_size] for i in range(0, total_length, block_size)]
|
|
|
|
for k, t in concatenated_examples.items()
|
|
|
|
}
|
2024-02-24 12:06:30 +08:00
|
|
|
result["labels"] = result["input_ids"].copy()
|
2023-05-04 21:52:25 +08:00
|
|
|
result = BatchEncoding(result)
|
|
|
|
return result
|
|
|
|
|
2024-02-24 12:06:30 +08:00
|
|
|
def format_inputs(examples):
|
|
|
|
p = examples["段落"]
|
|
|
|
mergeLine = ""
|
|
|
|
for line in p:
|
|
|
|
mergeLine += line["内容"] + "\n"
|
|
|
|
return {"text": mergeLine}
|
|
|
|
|
2023-05-04 21:52:25 +08:00
|
|
|
def tokenize_inputs(
|
|
|
|
examples: Dict[str, list],
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
2024-02-24 12:06:30 +08:00
|
|
|
column_name: str = "text",
|
2023-05-04 21:52:25 +08:00
|
|
|
) -> BatchEncoding:
|
|
|
|
return tokenizer(examples[column_name], return_attention_mask=False)
|
|
|
|
|
|
|
|
dataset_column_names = list(dataset.features)
|
|
|
|
dataset = dataset.map(
|
2024-02-24 12:06:30 +08:00
|
|
|
partial(format_inputs),
|
|
|
|
batched=False,
|
|
|
|
num_proc=args.num_proc,
|
|
|
|
remove_columns=dataset_column_names,
|
|
|
|
)
|
|
|
|
dataset_column_names = list(dataset.features)
|
|
|
|
dataset = dataset.map(
|
|
|
|
partial(tokenize_inputs, tokenizer=tokenizer),
|
2023-05-04 21:52:25 +08:00
|
|
|
batched=True,
|
|
|
|
num_proc=args.num_proc,
|
|
|
|
remove_columns=dataset_column_names,
|
2024-02-24 12:06:30 +08:00
|
|
|
)
|
|
|
|
dataset = dataset.map(
|
2023-05-04 21:52:25 +08:00
|
|
|
partial(group_texts, block_size=tokenizer.model_max_length),
|
|
|
|
batched=True,
|
|
|
|
num_proc=args.num_proc,
|
|
|
|
)
|
2024-02-24 12:06:30 +08:00
|
|
|
|
2023-05-04 21:52:25 +08:00
|
|
|
return dataset
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
|
|
|
"--model_name",
|
|
|
|
type=str,
|
|
|
|
help="Name of or path to model",
|
2024-02-24 12:06:30 +08:00
|
|
|
default="gpt2",
|
2023-05-04 21:52:25 +08:00
|
|
|
)
|
2023-05-09 00:02:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--learning_rate",
|
|
|
|
type=float,
|
|
|
|
help="Learning rate",
|
|
|
|
default=0.0001,
|
|
|
|
)
|
2023-05-06 21:06:18 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--use_tril_attention_mask",
|
|
|
|
help="Use tril attention mask during training",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2024-02-24 12:06:30 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--precision",
|
|
|
|
help="precision:bf16-mixed,16-mixed,32-true",
|
|
|
|
action="store_true",
|
|
|
|
default="16-mixed",
|
|
|
|
)
|
2023-05-04 21:52:25 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--tokenizer_name_or_path",
|
|
|
|
type=str,
|
|
|
|
help="Name of or path to tokenizer",
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--dataset_name",
|
2024-02-24 12:06:30 +08:00
|
|
|
nargs="+",
|
2023-05-04 21:52:25 +08:00
|
|
|
type=str,
|
|
|
|
help="Name(s) of dataset. To specify a config, pass a <dataset_name>:<dataset_config_name>",
|
2024-02-24 12:06:30 +08:00
|
|
|
default=["/home/colin/develop/dataset/liwu/MNBVC/wiki"],
|
2023-05-04 21:52:25 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--train_batch_size",
|
|
|
|
type=int,
|
|
|
|
help="Batch size of training",
|
2024-02-22 15:03:32 +08:00
|
|
|
default=2,
|
2023-05-04 21:52:25 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--val_batch_size",
|
|
|
|
type=int,
|
|
|
|
help="Batch size of validating",
|
2024-02-22 15:03:32 +08:00
|
|
|
default=2,
|
2023-05-04 21:52:25 +08:00
|
|
|
)
|
2023-05-06 19:39:24 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--accumulate_grad_batches",
|
|
|
|
type=int,
|
|
|
|
help="Accumulate grad batches",
|
|
|
|
default=32,
|
|
|
|
)
|
2023-05-04 21:52:25 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--num_proc",
|
|
|
|
type=str,
|
|
|
|
help="Number of data processes",
|
2024-02-24 12:06:30 +08:00
|
|
|
default=12,
|
2023-05-04 21:52:25 +08:00
|
|
|
)
|
2023-05-09 00:02:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--max_epochs",
|
|
|
|
type=int,
|
|
|
|
help="Max epochs",
|
|
|
|
default=None,
|
|
|
|
)
|
2023-05-06 19:39:24 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--strategy",
|
|
|
|
type=str,
|
|
|
|
help="Name of pytorch lightning distribution strategy",
|
2024-02-24 12:06:30 +08:00
|
|
|
default="fsdp",
|
2023-05-06 19:39:24 +08:00
|
|
|
)
|
2023-05-04 21:52:25 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--resume_from_ckpt_path",
|
|
|
|
type=str,
|
|
|
|
help="Checkpoint file path to resume from",
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--seed",
|
2023-05-09 23:00:28 +08:00
|
|
|
type=int,
|
2023-05-04 21:52:25 +08:00
|
|
|
help="Random seed",
|
|
|
|
default=42,
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
2024-02-24 12:06:30 +08:00
|
|
|
if __name__ == "__main__":
|
2023-05-04 21:52:25 +08:00
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
if args.tokenizer_name_or_path is None:
|
|
|
|
args.tokenizer_name_or_path = args.model_name
|
|
|
|
|
|
|
|
set_seed(args.seed)
|
|
|
|
|
|
|
|
# lightning module
|
2023-05-28 20:02:56 +08:00
|
|
|
lit_module = LitModule(args.model_name, args.learning_rate, args.use_tril_attention_mask)
|
2023-05-04 21:52:25 +08:00
|
|
|
|
|
|
|
# datasets
|
|
|
|
tokenizer = load_tokenizer(args.tokenizer_name_or_path)
|
|
|
|
train_dataset_list = []
|
|
|
|
val_dataset_list = []
|
|
|
|
for dataset_name in args.dataset_name:
|
2024-02-24 12:06:30 +08:00
|
|
|
dataset_args = dataset_name.split(":")
|
|
|
|
raw_dataset = datasets.load_dataset(
|
|
|
|
"json", data_files="/home/colin/develop/dataset/liwu/MNBVC/wiki/20230197/0.jsonl.gz"
|
|
|
|
)
|
|
|
|
# raw_dataset = datasets.load_dataset(*dataset_args)
|
2023-05-04 21:52:25 +08:00
|
|
|
train_dataset, val_dataset = split_raw_dataset(raw_dataset)
|
|
|
|
train_dataset = process_dataset(train_dataset, tokenizer)
|
|
|
|
val_dataset = process_dataset(val_dataset, tokenizer)
|
|
|
|
train_dataset_list.append(train_dataset)
|
|
|
|
val_dataset_list.append(val_dataset)
|
|
|
|
train_dataset = ConcatDataset(train_dataset_list)
|
|
|
|
val_dataset = ConcatDataset(val_dataset_list)
|
|
|
|
|
|
|
|
# dataloaders
|
|
|
|
train_dataloader = DataLoader(
|
|
|
|
train_dataset,
|
|
|
|
batch_size=args.train_batch_size,
|
|
|
|
num_workers=args.num_proc,
|
|
|
|
collate_fn=DefaultDataCollator(),
|
2023-05-06 19:39:24 +08:00
|
|
|
persistent_workers=True,
|
2023-05-04 21:52:25 +08:00
|
|
|
shuffle=True,
|
|
|
|
)
|
|
|
|
val_dataloader = DataLoader(
|
|
|
|
val_dataset,
|
|
|
|
batch_size=args.val_batch_size,
|
|
|
|
num_workers=args.num_proc,
|
|
|
|
collate_fn=DefaultDataCollator(),
|
2023-05-06 19:39:24 +08:00
|
|
|
persistent_workers=True,
|
2023-05-04 21:52:25 +08:00
|
|
|
)
|
|
|
|
|
2024-02-22 15:03:32 +08:00
|
|
|
ne = next(train_dataloader._get_iterator())
|
|
|
|
|
2023-05-04 21:52:25 +08:00
|
|
|
# trainer
|
2024-02-22 15:03:32 +08:00
|
|
|
# apply_all_patches()
|
2024-02-24 12:06:30 +08:00
|
|
|
torch.set_float32_matmul_precision("medium")
|
|
|
|
precision = args.precision
|
2023-05-04 21:52:25 +08:00
|
|
|
lit_trainer = pl.Trainer(
|
2024-02-24 12:06:30 +08:00
|
|
|
accelerator="gpu",
|
2023-05-04 21:52:25 +08:00
|
|
|
precision=precision,
|
|
|
|
log_every_n_steps=5,
|
2023-05-06 19:39:24 +08:00
|
|
|
accumulate_grad_batches=args.accumulate_grad_batches,
|
|
|
|
strategy=args.strategy,
|
2023-05-09 00:02:29 +08:00
|
|
|
max_epochs=args.max_epochs,
|
2023-05-04 21:52:25 +08:00
|
|
|
)
|
|
|
|
lit_trainer.fit(
|
|
|
|
lit_module,
|
|
|
|
train_dataloaders=train_dataloader,
|
|
|
|
val_dataloaders=val_dataloader,
|
|
|
|
ckpt_path=args.resume_from_ckpt_path,
|
|
|
|
)
|