Witllm/wit/lit_train.py

106 lines
2.9 KiB
Python
Raw Normal View History

2024-02-25 20:20:32 +08:00
import argparse
from functools import partial
from itertools import chain
from typing import Dict, Tuple
import datasets
import pytorch_lightning as pl
import torch
2024-03-04 21:41:46 +08:00
from torch.utils.data import ConcatDataset, DataLoader, Dataset, random_split, Subset
2024-02-25 20:20:32 +08:00
from transformers import (
BatchEncoding,
DefaultDataCollator,
PreTrainedTokenizer,
set_seed,
)
from modelscope import snapshot_download
from lit_module import LitModule
from tokenization_qwen import QWenTokenizer
2024-03-05 22:09:28 +08:00
from logger import TBLogger
2024-02-25 20:20:32 +08:00
model_name = "qwen/Qwen-1_8B-Chat"
learning_rate = 0.0001
use_tril_attention_mask = None
precision = "32-true" # "precision:bf16-mixed,16-mixed,32-true"
2024-02-25 20:20:32 +08:00
tokenizer_name_or_path = None
train_batch_size = 256
val_batch_size = 16
2024-02-25 20:20:32 +08:00
num_proc = 8
max_epochs = 1000
2024-03-05 22:09:28 +08:00
strategy = "auto"
2024-02-25 20:20:32 +08:00
resume_from_ckpt_path = None
seed = 42
2024-03-05 22:09:28 +08:00
vocab_size = 4096
2024-02-25 20:20:32 +08:00
2024-02-26 00:31:47 +08:00
class SpecialDataset(Dataset):
2024-03-05 22:09:28 +08:00
def __init__(self, start=1, end=320, size=32768):
2024-02-26 00:31:47 +08:00
self.size = size
self.features = []
2024-02-26 23:59:00 +08:00
a = torch.randint(start, end, [size])
2024-03-04 21:41:46 +08:00
b = torch.randint(start, end, [size])
c = torch.randint(start, end, [size])
d = torch.randint(start, end, [size])
2024-03-05 22:09:28 +08:00
# self.data = torch.stack([a, b, a + b, a + b]).permute(1, 0)
self.data = torch.stack([a, a + a, a + a, a + a]).permute(1, 0)
2024-02-26 00:31:47 +08:00
def __len__(self):
return self.size
def __getitem__(self, idx):
output = {}
data = self.data[idx]
output["input_ids"] = data
2024-03-04 21:41:46 +08:00
output["labels"] = data.clone()
2024-03-05 22:09:28 +08:00
# output["labels"][:2] = 0
# output["labels"][:2] = vocab_size
output["token_type_ids"] = torch.zeros(data.shape)
2024-02-26 00:31:47 +08:00
return output
2024-02-25 20:20:32 +08:00
if __name__ == "__main__":
if tokenizer_name_or_path is None:
tokenizer_name_or_path = model_name
set_seed(seed)
# lightning module
model_dir = snapshot_download(model_name)
lit_module = LitModule(model_dir, learning_rate, use_tril_attention_mask)
tokenizer = QWenTokenizer("./wit_b64.tiktoken", "./wit_char.tiktoken")
2024-03-05 22:09:28 +08:00
train_dataset, val_dataset = random_split(SpecialDataset(), [0.95, 0.05])
2024-02-26 00:31:47 +08:00
2024-02-25 20:20:32 +08:00
train_dataloader = DataLoader(
train_dataset,
batch_size=train_batch_size,
num_workers=num_proc,
collate_fn=DefaultDataCollator(),
persistent_workers=True,
shuffle=True,
)
val_dataloader = DataLoader(
val_dataset,
batch_size=val_batch_size,
num_workers=num_proc,
collate_fn=DefaultDataCollator(),
persistent_workers=True,
)
torch.set_float32_matmul_precision("medium")
2024-03-05 22:09:28 +08:00
lit_trainer = pl.Trainer(
accelerator="gpu",
precision=precision,
logger=TBLogger("./", default_hp_metric=False),
strategy=strategy,
max_epochs=max_epochs,
)
2024-02-25 20:20:32 +08:00
lit_trainer.fit(
lit_module,
train_dataloaders=train_dataloader,
val_dataloaders=val_dataloader,
ckpt_path=resume_from_ckpt_path,
)