Compare commits

..

No commits in common. "0ae63298b2191e0a3e1a1990998b6d12f000177a" and "8330cbb036c78462287f171cfeff40cf9d98c36f" have entirely different histories.

13 changed files with 39 additions and 38 deletions

2
.gitignore vendored
View File

@ -3,6 +3,6 @@ __pycache__
*.txt *.txt
*.npy *.npy
temp temp
lightning_logs # lightning_logs
checkpoints checkpoints

View File

@ -4,12 +4,12 @@
# LICENSE file in the root directory of this source tree. # LICENSE file in the root directory of this source tree.
class ModelConfig: class QWenConfig:
def __init__(self): def __init__(self):
self.vocab_size = 4096 self.vocab_size = 4096
self.hidden_size = 1024 self.hidden_size = 128 # 128 1024 2048 32
self.num_hidden_layers = 24 self.num_hidden_layers = 6 # 6 12 24 3
self.num_attention_heads = 16 self.num_attention_heads = 8 # 8 8 16
self.emb_dropout_prob = 0.0 self.emb_dropout_prob = 0.0
self.attn_dropout_prob = 0.0 self.attn_dropout_prob = 0.0
self.layer_norm_epsilon = 1e-6 self.layer_norm_epsilon = 1e-6

View File

@ -4,7 +4,7 @@ from modelscope import snapshot_download
from modeling_wit import QWenLMHeadModel from modeling_wit import QWenLMHeadModel
from modeling_wit import QwenRunner from modeling_wit import QwenRunner
from wit.configuration import ModelConfig from configuration_qwen import QWenConfig
from tokenization_qwen import QWenTokenizer from tokenization_qwen import QWenTokenizer
@ -20,7 +20,7 @@ torch.cuda.manual_seed_all(seed)
model_dir = snapshot_download("qwen/Qwen-1_8B-Chat") model_dir = snapshot_download("qwen/Qwen-1_8B-Chat")
# model_dir = "/home/colin/.cache/modelscope/hub/qwen/Qwen-1_8B-Chat" # model_dir = "/home/colin/.cache/modelscope/hub/qwen/Qwen-1_8B-Chat"
config = ModelConfig() config = QWenConfig()
model = QWenLMHeadModel(config) model = QWenLMHeadModel(config)
print(model) print(model)

View File

@ -0,0 +1,3 @@
model_dir: /home/colin/.cache/modelscope/hub/qwen/Qwen-1_8B-Chat
learning_rate: 0.0001
use_tril_attention_mask: null

View File

@ -0,0 +1,3 @@
model_dir: /home/colin/.cache/modelscope/hub/qwen/Qwen-1_8B-Chat
learning_rate: 0.0001
use_tril_attention_mask: null

View File

@ -0,0 +1,3 @@
model_dir: /home/colin/.cache/modelscope/hub/qwen/Qwen-1_8B-Chat
learning_rate: 0.0001
use_tril_attention_mask: null

View File

@ -6,7 +6,7 @@ import torch
import torchmetrics import torchmetrics
from modeling_wit import QWenLMHeadModel from modeling_wit import QWenLMHeadModel
from wit.configuration import ModelConfig from configuration_qwen import QWenConfig
from transformers import AutoConfig from transformers import AutoConfig
@ -14,20 +14,15 @@ from transformers import AutoConfig
class LitModule(pl.LightningModule): class LitModule(pl.LightningModule):
def __init__( def __init__(
self, self,
pretrained_model_dir: str = None, model_dir: str,
learning_rate: float = 0.0001, learning_rate: float = 0.0001,
config: ModelConfig = None,
use_tril_attention_mask: str = False, use_tril_attention_mask: str = False,
): ):
super().__init__() super().__init__()
self.save_hyperparameters() self.save_hyperparameters()
if config == None: config = QWenConfig()
config = ModelConfig()
model = QWenLMHeadModel(config) model = QWenLMHeadModel(config)
if pretrained_model_dir != None: model = model.from_pretrained(model_dir)
from modelscope import snapshot_download
model = model.from_pretrained(snapshot_download(pretrained_model_dir))
self.llm = self.register_core_module(model) self.llm = self.register_core_module(model)
self.learning_rate = learning_rate self.learning_rate = learning_rate
self.use_tril_attention_mask = use_tril_attention_mask self.use_tril_attention_mask = use_tril_attention_mask

View File

@ -115,7 +115,7 @@ class MeaningDataset(Dataset):
def __getitem__(self, idx): def __getitem__(self, idx):
output = {} output = {}
data = torch.tensor(self.data[idx]).long() data = torch.tensor(self.data[idx])
output["input_ids"] = data output["input_ids"] = data
output["labels"] = data.clone() output["labels"] = data.clone()
output["token_type_ids"] = torch.zeros(data.shape) output["token_type_ids"] = torch.zeros(data.shape)

View File

@ -10,7 +10,7 @@ from torch.utils.data import ConcatDataset, DataLoader, Dataset, random_split, S
class SpecialDataset(Dataset): class SpecialDataset(Dataset):
def __init__(self, start=1, end=128, size=32768): # 1048576 32768 def __init__(self, start=1, end=320, size=32768): # 1048576 32768
self.size = size self.size = size
self.features = [] self.features = []
a = torch.randint(start, end, [size]) a = torch.randint(start, end, [size])
@ -20,8 +20,8 @@ class SpecialDataset(Dataset):
z = torch.zeros([size]).long() z = torch.zeros([size]).long()
# self.data = torch.stack([a, b, a + b, a + b, a + b * 2]).permute(1, 0) # self.data = torch.stack([a, b, a + b, a + b, a + b * 2]).permute(1, 0)
# self.data = torch.stack([a, b, a, a + b / 4]).permute(1, 0).long() # self.data = torch.stack([a, b, a, a + b / 4]).permute(1, 0).long()
self.data = torch.stack([a, a + a, a + a]).permute(1, 0).long() # self.data = torch.stack([a, a + 1, a + 2]).permute(1, 0).long()
# self.data = torch.stack([a, b, a]).permute(1, 0).long() self.data = torch.stack([a, b, a]).permute(1, 0).long()
# self.data = torch.stack([a, b, a, a + a / 8, a + a / 4, a + a / 2, a + a]).permute(1, 0).long() # self.data = torch.stack([a, b, a, a + a / 8, a + a / 4, a + a / 2, a + a]).permute(1, 0).long()
# input a b c # input a b c

View File

@ -9,53 +9,50 @@ import torch
from torch.utils.data import ConcatDataset, DataLoader, Dataset, random_split, Subset from torch.utils.data import ConcatDataset, DataLoader, Dataset, random_split, Subset
from transformers import ( from transformers import (
BatchEncoding,
DefaultDataCollator, DefaultDataCollator,
PreTrainedTokenizer,
set_seed, set_seed,
) )
from modelscope import snapshot_download
from lit_module import LitModule from lit_module import LitModule
from tokenization_qwen import QWenTokenizer from tokenization_qwen import QWenTokenizer
from logger import TBLogger from logger import TBLogger
from special_dataset import SpecialDataset from special_dataset import SpecialDataset
from meaning_dataset import MeaningDataset from meaning_dataset import MeaningDataset
from wit.configuration import ModelConfig
pretrain_model_name = None # "qwen/Qwen-1_8B-Chat" model_name = "qwen/Qwen-1_8B-Chat"
learning_rate = 0.0001 learning_rate = 0.0001
use_tril_attention_mask = None use_tril_attention_mask = None
precision = "32-true" # "precision:bf16-mixed,16-mixed,32-true" precision = "32-true" # "precision:bf16-mixed,16-mixed,32-true"
train_batch_size = 256 tokenizer_name_or_path = None
val_batch_size = 1 train_batch_size = 16
val_batch_size = 16
num_proc = 8 num_proc = 8
max_epochs = 1000 max_epochs = 1000
strategy = "auto" strategy = "auto"
resume_from_ckpt_path = None resume_from_ckpt_path = None
seed = 42 seed = 42
vocab_size = 256 vocab_size = 4096
if __name__ == "__main__": if __name__ == "__main__":
if tokenizer_name_or_path is None:
tokenizer_name_or_path = model_name
set_seed(seed) set_seed(seed)
config = ModelConfig() model_dir = snapshot_download(model_name)
config.vocab_size = vocab_size lit_module = LitModule(model_dir, learning_rate, use_tril_attention_mask)
config.hidden_size = 128 # 128 1024 2048 32
config.num_hidden_layers = 6 # 6 12 24 3
config.num_attention_heads = 8 # 8 8 16
lit_module = LitModule(pretrain_model_name, learning_rate, config, use_tril_attention_mask)
tokenizer = QWenTokenizer("./wit_b64.tiktoken", "./wit_char.tiktoken") tokenizer = QWenTokenizer("./wit_b64.tiktoken", "./wit_char.tiktoken")
raw_dataset = SpecialDataset() # raw_dataset = SpecialDataset()
# raw_dataset = MeaningDataset(start=65536, end=262133, size=32768, max_subitem=4, vocab_size=vocab_size) raw_dataset = MeaningDataset(start=131072, end=1048576, size=32768)
train_dataset, val_dataset = random_split(raw_dataset, [0.95, 0.05]) train_dataset, val_dataset = random_split(raw_dataset, [0.95, 0.05])
it = iter(train_dataset) # daf = next(iter(train_dataset))["input_ids"].numpy().tolist()
print("data samples:")
for i in range(10):
print(next(it)["input_ids"].numpy().tolist())
train_dataloader = DataLoader( train_dataloader = DataLoader(
train_dataset, train_dataset,