File size: 10,591 Bytes
41404b8 |
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 |
# basic imports
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "4"
# other external imports
import pandas as pd
# torch imports
import torch
from datasets import load_dataset
from torch.utils.data import DataLoader, Dataset
# transformers imports
from transformers import (BertConfig, BertTokenizer, EncoderDecoderConfig,
EncoderDecoderModel, LayoutLMv3Tokenizer, LiltConfig,
LiltModel, Seq2SeqTrainer, Seq2SeqTrainingArguments,
default_data_collator)
# internal imports
# prepare tokenizer.
def prepare_tokenizer(src_tokenizer_dir, tgt_tokenizer_dir):
src_tokenizer = LayoutLMv3Tokenizer.from_pretrained(src_tokenizer_dir)
tgt_tokenizer = BertTokenizer.from_pretrained(tgt_tokenizer_dir)
return src_tokenizer, tgt_tokenizer
# read data points.
def prepare_dataset_df(data_file):
def filter_fn(exam):
bboxes = exam["layout_src"]
for box in bboxes:
x0, y0, x1, y1 = box
if (x0 > x1) or (y0 > y1):
print("(x0 > x1) or (y0 > y1)")
return False
for cor in box:
if cor < 0 or cor > 1000:
# print("cor < 0 or cor > 1000")
# print(exam['img_path'],box)
return False
return True
dataset = load_dataset("json", data_files=data_file)["train"]
print()
print(f"Number of examples: {len(dataset)}")
print()
dataset = dataset.filter(filter_fn, num_proc=48)
dataset_df = dataset.to_pandas()
# dataset_df = pd.read_json(data_file, lines=True, orient="records")
# filter the nan data points.
dataset_df = dataset_df[~dataset_df["tgt_sen_trans"].isna()]
dataset_df = dataset_df[~dataset_df["text_src"].isna()]
dataset_df = dataset_df[~dataset_df["layout_src"].isna()]
# remove entries where "text_src" length is less than 3
dataset_df = dataset_df[dataset_df["text_src"].str.len() >= 3]
# reconstruct the idx to avoid index_error.
dataset_df = dataset_df.reset_index(drop=True)
print(f"Number of examples after filtered: {len(dataset_df)}")
return dataset_df
class MyDataset(Dataset):
def __init__(
self,
df,
src_tokenizer,
tgt_tokenizer,
max_src_length,
max_target_length,
):
self.df = df
self.src_tokenizer = src_tokenizer
self.tgt_tokenizer = tgt_tokenizer
self.max_src_length = max_src_length
self.max_target_length = max_target_length
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
# get text_src + layout_src + tgt_trans.
text_src = self.df['text_src'][idx]
layout_src = self.df['layout_src'][idx]
tgt_trans = self.df['tgt_sen_trans'][idx]
# read in annotations at word-level (words, word boxes)
words_ = text_src.split(" ")
word_boxes_ = layout_src
# print('words', words_, len(words_), len(word_boxes_))
assert len(words_) == len(word_boxes_)
words = []
word_boxes = []
for word, word_box in zip(words_, word_boxes_):
if (word_box[0] >= word_box[2]) or (word_box[1] >= word_box[3]):
continue
words.append(word)
word_boxes.append(word_box)
assert len(words) == len(word_boxes)
encoding = self.src_tokenizer(
words,
boxes=word_boxes,
padding="max_length",
truncation=True,
max_length=self.max_src_length,
)
# construct labels.
labels = self.tgt_tokenizer(
tgt_trans,
padding="max_length",
truncation=True,
max_length=self.max_target_length)["input_ids"]
# important: make sure that PAD tokens are ignored by the loss function
labels = [
label if label != self.tgt_tokenizer.pad_token_id else -100
for label in labels
]
encoding["labels"] = labels
assert len(encoding['input_ids']) == self.max_src_length
assert len(encoding['attention_mask']) == self.max_src_length
assert len(encoding['bbox']) == self.max_src_length
assert len(encoding['labels']) == self.max_target_length
# finally, convert everything to PyTorch tensors
for k, v in encoding.items():
encoding[k] = torch.as_tensor(encoding[k])
return encoding
def prepare_model(src_tokenizer,
tgt_tokenizer,
max_src_len,
max_tgt_len,
num_encoder_hidden_layers,
num_decoder_hidden_layers,
encoder_ckpt_dir,
model_ckpt_dir=None):
config_encoder = LiltConfig.from_pretrained(
encoder_ckpt_dir,
max_position_embeddings=max_src_len + 2,
num_hidden_layers=num_encoder_hidden_layers)
config_decoder = BertConfig(vocab_size=tgt_tokenizer.vocab_size,
max_position_embeddings=max_tgt_len,
num_hidden_layers=num_decoder_hidden_layers)
model_config = EncoderDecoderConfig.from_encoder_decoder_configs(
encoder_config=config_encoder,
decoder_config=config_decoder,
)
model = EncoderDecoderModel(config=model_config, )
model.config.decoder_start_token_id = tgt_tokenizer.cls_token_id
model.config.pad_token_id = tgt_tokenizer.pad_token_id
model.config.vocab_size = tgt_tokenizer.vocab_size
model.config.eos_token_id = tgt_tokenizer.pad_token_id
from safetensors.torch import load_file
if model_ckpt_dir:
bin_path = f"{model_ckpt_dir}/pytorch_model.bin"
safetensors_path = f"{model_ckpt_dir}/model.safetensors"
if os.path.exists(bin_path):
state_dict = torch.load(bin_path)
elif os.path.exists(safetensors_path):
state_dict = load_file(safetensors_path)
else:
raise FileNotFoundError(
"Neither pytorch_model.bin nor model.safetensors found in the specified directory."
)
model.load_state_dict(state_dict, strict=False)
model.save_pretrained(
f"continued_{model_ckpt_dir}") #save at continued training
else:
# Loading the pre-trained params and then save the model, including its configuration.
tmp_encoder = LiltModel.from_pretrained(
pretrained_model_name_or_path=encoder_ckpt_dir,
config=config_encoder,
)
# tmp_encoder = LiltModel(config=config_encoder)
model.encoder = tmp_encoder
# model.save_pretrained("undertrained_default_safe_true")
model.save_pretrained("undertrained_safe_serialization_False", safe_serialization=False)
# model.load_state_dict(torch.load(f"undertrained/pytorch_model.bin"))
bin_path = "undertrained_safe_serialization_False/pytorch_model.bin"
safetensors_path = "undertrained_default_safe_true/model.safetensors"
if os.path.exists(bin_path):
state_dict = torch.load(bin_path)
elif os.path.exists(safetensors_path):
state_dict = load_file(safetensors_path)
else:
raise FileNotFoundError(
"Neither pytorch_model.bin nor model.safetensors found in the specified directory."
)
model.load_state_dict(state_dict, strict=False)
print(model.config)
print(model)
return model
if __name__ == "__main__":
# hyper-parameters.
## for model.
MAX_TGT_LEN = 512
MAX_SRC_LEN = 512
num_encoder_hidden_layers = 12
num_decoder_hidden_layers = 12
## for training.
num_instances = 500000 #total 620082 ./dataset/merged.jsonl Number of examples after filtered: 547084
learning_rate = 1e-4
batch_size = 28
num_train_steps = 400000 #400000
output_dir = f"./train.lr_{learning_rate}.bsz_{batch_size}.step_{num_train_steps}.layer_{num_encoder_hidden_layers}-{num_decoder_hidden_layers}"
save_total_limit = 100
save_steps = num_train_steps // save_total_limit
dataset_dir = "/home/zychen/hwproject/my_modeling_phase_1/dataset"
data_file = f"{dataset_dir}/merged.jsonl"
# model_ckpt_dir = '/home/zychen/hwproject/my_modeling_phase_1/train.lr_0.0001.bsz_8.step_400000.layer_12-12/checkpoint-32000'
model_ckpt_dir = '/home/zychen/hwproject/my_modeling_phase_1/train.lr_0.0001.bsz_16.step_500000.layer_12-12_36k+20k/checkpoint-20000'
encoder_ckpt_dir = "/home/zychen/hwproject/my_modeling_phase_1/Tokenizer_PretrainedWeights/lilt-roberta-en-base"
tgt_tokenizer_dir = "/home/zychen/hwproject/my_modeling_phase_1/Tokenizer_PretrainedWeights/bert-base-chinese-tokenizer"
src_tokenizer, tgt_tokenizer = prepare_tokenizer(
src_tokenizer_dir=encoder_ckpt_dir,
tgt_tokenizer_dir=tgt_tokenizer_dir,
)
dataset_df = prepare_dataset_df(data_file=data_file)[:num_instances]
print(f"\nnum_instances: {len(dataset_df)}\n")
print(dataset_df)
my_dataset = MyDataset(
df=dataset_df,
src_tokenizer=src_tokenizer,
tgt_tokenizer=tgt_tokenizer,
max_src_length=MAX_SRC_LEN,
max_target_length=MAX_TGT_LEN,
)
model = prepare_model(src_tokenizer=src_tokenizer,
tgt_tokenizer=tgt_tokenizer,
max_src_len=MAX_SRC_LEN,
max_tgt_len=MAX_TGT_LEN,
num_encoder_hidden_layers=num_encoder_hidden_layers,
num_decoder_hidden_layers=num_decoder_hidden_layers,
encoder_ckpt_dir=encoder_ckpt_dir,
model_ckpt_dir=model_ckpt_dir)
training_args = Seq2SeqTrainingArguments(
predict_with_generate=False,
evaluation_strategy="no",
per_device_train_batch_size=batch_size,
fp16=True,
output_dir=output_dir,
logging_steps=1,
# save_strategy="epoch",
learning_rate=learning_rate,
max_steps=num_train_steps,
warmup_ratio=0.05,
save_total_limit=save_total_limit,
save_steps=save_steps,
save_safetensors=False,
)
# print(training_args)
# instantiate trainer
trainer = Seq2SeqTrainer(
model=model,
args=training_args,
compute_metrics=None,
train_dataset=my_dataset,
eval_dataset=None,
data_collator=default_data_collator,
)
trainer.train()
|