Spaces:
Runtime error
Runtime error
File size: 18,079 Bytes
2024325 |
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 |
import argparse
import logging
import math
import re
import numpy as np
import torch
from accelerate import Accelerator
from accelerate.utils import set_seed
from torch.utils.data import DataLoader
from tqdm.auto import tqdm
from transformers import get_scheduler, AutoTokenizer, AdamW, SchedulerType, AutoModelForSeq2SeqLM, \
DataCollatorWithPadding
from datasets import load_dataset
logger = logging.getLogger(__name__)
def get_parser():
parser = argparse.ArgumentParser(description="Train ELI5 seq2seq answer generation model")
parser.add_argument(
"--dataset_name",
type=str,
default="vblagoje/lfqa",
help="The name of the dataset to use (via the datasets library).",
)
parser.add_argument(
"--per_device_train_batch_size",
type=int,
default=4,
)
parser.add_argument(
"--per_device_eval_batch_size",
type=int,
default=4,
help="Batch size (per device) for the evaluation dataloader.",
)
parser.add_argument(
"--pretrained_model_name",
type=str,
default="facebook/bart-large",
)
parser.add_argument(
"--model_save_name",
type=str,
default="eli5_bart_model",
)
parser.add_argument(
"--learning_rate",
type=float,
default=2e-4,
)
parser.add_argument(
"--weight_decay",
type=float,
default=0.0,
help="Weight decay to use."
)
parser.add_argument(
"--log_freq",
type=int,
default=100,
help="Log train/validation loss every log_freq update steps"
)
parser.add_argument(
"--ignore_pad_token_for_loss",
type=bool,
default=True,
help="Whether to ignore the tokens corresponding to " "padded labels in the loss computation or not.",
)
parser.add_argument(
"--num_train_epochs",
type=int,
default=3,
)
parser.add_argument(
"--max_train_steps",
type=int,
default=None,
help="Total number of training steps to perform. If provided, overrides num_train_epochs.",
)
parser.add_argument(
"--gradient_accumulation_steps",
type=int,
default=16,
help="Number of updates steps to accumulate before performing a backward/update pass.",
)
parser.add_argument(
"--pad_to_max_length",
action="store_true",
help="If passed, pad all samples to `max_length`. Otherwise, dynamic padding is used.",
)
parser.add_argument(
"--overwrite_cache", type=bool, default=None, help="Overwrite the cached training and evaluation sets"
)
parser.add_argument(
"--max_source_length",
type=int,
default=1024,
help="The maximum total input sequence length after "
"tokenization.Sequences longer than this will be truncated, sequences shorter will be padded.",
)
parser.add_argument(
"--max_target_length",
type=int,
default=360,
help="The maximum total sequence length for target text after "
"tokenization. Sequences longer than this will be truncated, sequences shorter will be padded."
)
parser.add_argument(
"--lr_scheduler_type",
type=SchedulerType,
default="linear", # this is linear with warmup
help="The scheduler type to use.",
choices=["linear", "cosine", "cosine_with_restarts", "polynomial", "constant", "constant_with_warmup"],
)
parser.add_argument(
"--num_warmup_steps",
type=int,
default=None,
help="Number of steps for the warmup in the lr scheduler."
)
parser.add_argument(
"--warmup_percentage",
type=float,
default=0.08,
help="Number of steps for the warmup in the lr scheduler."
)
return parser
def cleanup_references(text):
# URL reference where we need to remove both the link text and URL
# ...and this letter is used by most biographers as the cornerstone of Lee's personal
# views on slavery ([1](_URL_2_ & pg=PA173), [2](_URL_1_), [3](_URL_5_)).
# ...and this letter is used by most biographers as the cornerstone of Lee's personal views on slavery.
result = re.sub(r"[\(\s]*\[\d+\]\([^)]+\)[,)]*", "", text, 0, re.MULTILINE)
# URL reference where we need to preserve link text but remove URL
# At the outbreak of the Civil War, [Leyburn left his church](_URL_19_) and joined the South.
# At the outbreak of the Civil War, Leyburn left his church and joined the South.
result = re.sub(r"\[([^]]+)\]\([^)]+\)", "\\1", result, 0, re.MULTILINE)
# lastly remove just dangling _URL_[0-9]_ URL references
result = re.sub(r"_URL_\d_", "", result, 0, re.MULTILINE)
return result
def clean_answer(text):
result = cleanup_references(text)
result = result.replace("\n", " ")
result = re.sub(r"\s\s+", " ", result)
result = re.sub(r"BULLET::::-", "", result)
return result.strip()
def clean_question(text):
result = cleanup_references(text)
result = result.replace("\n", " ")
result = re.sub(r"\s\s+", " ", result)
result = result.replace("[deleted]", "")
return result.lower().strip()
def prepare_support_docs(example):
provenances = example["output"][-1]["provenance"]
context = "<P> " + " <P> ".join([p["text"] for p in provenances])
return {"context": context}
def preprocess_eli5(examples, **fn_kwargs):
document_cache = fn_kwargs["document_cache"]
training = fn_kwargs.get("training", True)
extra_answer_threshold = fn_kwargs.get("extra_answer_threshold", 3)
include_selftext = fn_kwargs.get("include_selftext", False)
exclude_answer_patterns = fn_kwargs.get("exclude_answer_patterns", [])
questions, contexts, answers = [], [], []
for q_id, question, selftext, answer in zip(examples["q_id"], examples["title"], examples["selftext"],
examples["answers"]):
accepted_answer_idx = []
if training:
accepted_answer_idx = [idx for idx, score in enumerate(answer["score"]) if
score > extra_answer_threshold]
if not training or not accepted_answer_idx:
accepted_answer_idx = [0]
document = document_cache[q_id]
for idx in accepted_answer_idx:
skip_answer = any([p.search(answer["text"][idx]) for p in exclude_answer_patterns])
if skip_answer:
continue
if include_selftext:
questions.append(clean_question(f"{question} {selftext}"))
else:
questions.append(clean_question(question))
contexts.append(document.lower().strip())
answers.append(clean_answer(answer["text"][idx]))
return {"question": questions, "context": contexts, "answer": answers}
def eval_qa_s2s_epoch(model, dataloader, accelerator, args):
model.eval()
num_eval_steps = math.ceil(len(dataloader))
progress_bar = tqdm(range(num_eval_steps), disable=not accelerator.is_local_main_process)
total_loss = 0.
with torch.no_grad():
for step, batch in enumerate(dataloader):
outputs = model(**batch)
loss = outputs.loss
total_loss += loss.item()
progress_bar.update(1)
progress_bar.set_postfix(loss=round((total_loss / (step + 1)), 3))
return total_loss / (step + 1)
def train(config):
set_seed(42)
args = config["args"]
eli5 = load_dataset(args.dataset_name)
support_docs = load_dataset("vblagoje/lfqa_support_docs")
# Initialize the accelerator. We will let the accelerator handle device placement for us in this example.
accelerator = Accelerator()
# Make one log on every process with the configuration for debugging.
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger.setLevel(logging.INFO if accelerator.is_local_main_process else logging.ERROR)
logger.info(accelerator.state)
tokenizer = AutoTokenizer.from_pretrained(args.pretrained_model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(args.pretrained_model_name)
# Optimizer
# Split weights in two groups, one with weight decay and the other not.
no_decay = ["bias", "LayerNorm.weight"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": args.weight_decay,
},
{
"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)],
"weight_decay": 0.0,
},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, weight_decay=args.weight_decay)
processed_datasets = {}
support_docs_prepared = {}
with accelerator.main_process_first():
for split in ["train", "validation"]:
support_docs_prepared[split] = support_docs[split].map(prepare_support_docs,
batched=False,
cache_file_name=f"./support_docs_{split}.arrow",
load_from_cache_file=not args.overwrite_cache,
desc="Preparing support docs",
)
column_names = eli5["train"].column_names
for split in ["train", "validation"]:
d_cache = dict([(e["id"], e["context"]) for e in tqdm(support_docs_prepared[split],
desc=f"Adding support docs to LFQA {split}")])
processed_datasets[split] = eli5[split].map(preprocess_eli5,
batched=True,
remove_columns=column_names,
cache_file_name=f"./processed_datasets_{split}.arrow",
load_from_cache_file=not args.overwrite_cache,
desc="Preparing dataset for tokenization",
fn_kwargs={"document_cache": d_cache,
"training": split == "train",
"exclude_answer_patterns": [re.compile("not sure what you"),
re.compile("\n\n >")]}
)
padding = "max_length" if args.pad_to_max_length else False
# Temporarily set max_target_length for training.
max_target_length = args.max_target_length
label_pad_token_id = -100 if args.ignore_pad_token_for_loss else tokenizer.pad_token_id
def tokenize_dataset(examples):
inputs = ["question: {} context: {}".format(q, c) for q, c in zip(examples["question"], examples["context"])]
targets = examples["answer"]
model_inputs = tokenizer(inputs, max_length=args.max_source_length, padding=padding, truncation=True)
# Setup the tokenizer for targets
with tokenizer.as_target_tokenizer():
labels = tokenizer(targets, max_length=max_target_length, padding=True, truncation=True,
return_tensors="np")
model_inputs["decoder_input_ids"] = labels["input_ids"][:, :-1].tolist()
# replace pad_token_id with label_pad_token_id to avoid loss calculation on those tokens
labels["input_ids"] = np.where(labels["input_ids"] == tokenizer.pad_token_id,
label_pad_token_id, labels["input_ids"])
model_inputs["labels"] = labels["input_ids"][:, 1:].tolist()
return model_inputs
tokenized_datasets = {}
with accelerator.main_process_first():
for split, dataset in processed_datasets.items():
tokenized_datasets[split] = dataset.map(
tokenize_dataset,
batched=True,
cache_file_name=f"./tokenized_dataset_{split}.arrow",
remove_columns=dataset.column_names,
load_from_cache_file=not args.overwrite_cache,
desc="Running tokenizer on dataset"
)
train_dataset = tokenized_datasets["train"]
eval_dataset = tokenized_datasets["validation"]
train_dataset.set_format(type='torch')
eval_dataset.set_format(type='torch')
data_collator = DataCollatorWithPadding(tokenizer, "max_length")
# first epoch we don't shuffle
train_dataloader = DataLoader(train_dataset, shuffle=False, batch_size=args.per_device_train_batch_size,
collate_fn=data_collator)
eval_dataloader = DataLoader(eval_dataset, batch_size=args.per_device_eval_batch_size, collate_fn=data_collator)
# train the model
model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare(model, optimizer, train_dataloader,
eval_dataloader)
# Scheduler and math around the number of training steps.
num_update_steps_per_epoch = math.ceil(len(train_dataloader) / args.gradient_accumulation_steps)
if args.max_train_steps is None:
args.max_train_steps = args.num_train_epochs * num_update_steps_per_epoch
else:
args.num_train_epochs = math.ceil(args.max_train_steps / num_update_steps_per_epoch)
num_warmup_steps = args.num_warmup_steps if args.num_warmup_steps else math.ceil(args.max_train_steps *
args.warmup_percentage)
scheduler = get_scheduler(
name=args.lr_scheduler_type,
optimizer=optimizer,
num_warmup_steps=num_warmup_steps,
num_training_steps=args.max_train_steps,
)
# Train!
total_batch_size = args.per_device_train_batch_size * accelerator.num_processes * args.gradient_accumulation_steps
logger.info("***** Running training *****")
logger.info(f" Num examples = {len(train_dataset)}")
logger.info(f" Num eval examples = {len(eval_dataset)}")
logger.info(f" Num Epochs = {args.num_train_epochs}")
logger.info(f" Instantaneous batch size per device = {args.per_device_train_batch_size}")
logger.info(f" Total train batch size (w. parallel, distributed & accumulation) = {total_batch_size}")
logger.info(f" Gradient Accumulation steps = {args.gradient_accumulation_steps}")
logger.info(f" Total optimization steps = {args.max_train_steps}")
logger.info(f" Warmup steps = {num_warmup_steps}")
logger.info(f" Logging training progress every {args.log_freq} optimization steps")
# Only show the progress bar once on each machine.
progress_bar = tqdm(range(args.max_train_steps), disable=not accelerator.is_local_main_process)
completed_steps = 0
switched_train_dataloader = False
for epoch in range(args.num_train_epochs):
model.train()
if epoch > 0 and not switched_train_dataloader:
train_dataloader = DataLoader(train_dataset, batch_size=args.per_device_train_batch_size,
shuffle=True, collate_fn=data_collator)
train_dataloader = accelerator.prepare(train_dataloader)
switched_train_dataloader = True
for step, batch in enumerate(train_dataloader):
outputs = model(**batch)
loss = torch.mean(outputs.loss)
accelerator.backward(loss)
if ((step + 1) % args.gradient_accumulation_steps == 0) or (step + 1 == len(train_dataloader)):
optimizer.step()
scheduler.step()
optimizer.zero_grad()
progress_bar.update(1)
progress_bar.set_postfix(loss=round(loss.item(), 3))
completed_steps += 1
if completed_steps >= args.max_train_steps:
break
if step % (args.log_freq * args.gradient_accumulation_steps) == 0:
validation_loss = eval_qa_s2s_epoch(model, eval_dataloader, accelerator, args)
model.train()
logger.info(f"Train loss {loss.item()} , validation loss {validation_loss}")
if args.wandb and accelerator.is_local_main_process:
import wandb
wandb.log({"loss": loss.item(),
"lr": scheduler.get_last_lr()[0],
"validation_loss": validation_loss,
"completed_steps": completed_steps})
logger.info("Saving model {}".format(args.model_save_name))
accelerator.wait_for_everyone()
unwrapped_model = accelerator.unwrap_model(model)
accelerator.save(unwrapped_model.state_dict(), "{}_{}.bin".format(args.model_save_name, epoch))
# Calculating the validation loss over epoch
validation_loss = eval_qa_s2s_epoch(model, eval_dataloader, accelerator, args)
logger.info("Epoch: {}".format(epoch))
logger.info("Validation loss: {}".format(validation_loss))
def main():
parser = get_parser()
parser.add_argument(
"--wandb",
action="store_true",
help="If true, use W&B logging",
)
main_args, _ = parser.parse_known_args()
config = {"args": main_args}
if main_args.wandb:
import wandb
wandb.init(project="Bart_ELI5")
train(config=config)
main()
|