|
import transformers |
|
import numpy as np |
|
from datasets import load_dataset, DatasetDict |
|
from transformers import AutoModelForSeq2SeqLM |
|
from transformers import AutoTokenizer |
|
from transformers import DataCollatorForSeq2Seq |
|
import evaluate |
|
import numpy as np |
|
from transformers import Seq2SeqTrainingArguments |
|
from transformers import Seq2SeqTrainer |
|
from torch.utils.data import DataLoader |
|
from transformers import pipeline |
|
from transformers import AdamW |
|
from accelerate import Accelerator |
|
from transformers import get_scheduler |
|
from huggingface_hub import Repository, get_full_repo_name |
|
from tqdm.auto import tqdm |
|
import torch |
|
from torch import Tensor |
|
import os |
|
|
|
|
|
|
|
def addperiod(entry): |
|
entry['en'] += '.' |
|
entry['fr'] += '.' |
|
return entry |
|
|
|
raw_datasets = load_dataset("aatherton2024/eng-nah-svo") |
|
train_ds = raw_datasets['train'].map(addperiod) |
|
validation_ds = raw_datasets['validation'].map(addperiod) |
|
test_ds = raw_datasets['test'].map(addperiod) |
|
|
|
raw_datasets = DatasetDict({"train" : train_ds, "validation" : validation_ds, "test" : test_ds}) |
|
model_checkpoint = "eng-nah-svo-cpt" |
|
|
|
if False: |
|
def get_training_corpus(raw_datasets): |
|
return ( |
|
raw_datasets["train"][i : i + 1000] |
|
for i in range(0, len(raw_datasets["train"]), 1000) |
|
) |
|
|
|
training_corpus = get_training_corpus(raw_datasets) |
|
old_tokenizer = AutoTokenizer.from_pretrained("gpt2") |
|
tokenizer = old_tokenizer.train_new_from_iterator(training_corpus, 52000) |
|
|
|
tokenizer.save_pretrained("eng-nah-svo-cpt") |
|
tokenizer.push_to_hub("eng-nah-svo-cpt") |
|
|
|
max_length = 128 |
|
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) |
|
tokenizer.add_special_tokens({'pad_token': '[PAD]'}) |
|
|
|
|
|
def preprocess_function(examples): |
|
inputs = examples["en"] |
|
targets = examples["fr"] |
|
model_inputs = tokenizer( |
|
inputs, text_target=targets, max_length=max_length, truncation=True |
|
) |
|
return model_inputs |
|
|
|
|
|
tokenized_datasets = raw_datasets.map( |
|
preprocess_function, |
|
batched=True, |
|
remove_columns=raw_datasets["train"].column_names |
|
) |
|
|
|
|
|
if False: |
|
model = AutoModelForSeq2SeqLM.from_pretrained("eng-nah-svo-translation") |
|
|
|
else: |
|
from transformers import BertConfig, BertLMHeadModel |
|
from transformers import AutoModel |
|
|
|
model = AutoModelForSeq2SeqLM.from_pretrained("eng-nah-svo-translation") |
|
|
|
|
|
|
|
|
|
data_collator = DataCollatorForSeq2Seq(tokenizer, model=model) |
|
|
|
|
|
metric_bleu = evaluate.load("sacrebleu") |
|
metric_chrf = evaluate.load("chrf") |
|
|
|
|
|
def compute_metrics(eval_preds): |
|
preds, labels = eval_preds |
|
|
|
if isinstance(preds, tuple): |
|
preds = preds[0] |
|
|
|
decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True) |
|
|
|
|
|
labels = np.where(labels != -100, labels, tokenizer.pad_token_id) |
|
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) |
|
|
|
|
|
decoded_preds = [pred.strip() for pred in decoded_preds] |
|
decoded_labels = [[label.strip()] for label in decoded_labels] |
|
|
|
result_bleu = metric_bleu.compute(predictions=decoded_preds, references=decoded_labels) |
|
result_chrf = metric_chrf.compute(predictions=decoded_preds, references=decoded_labels) |
|
return {"bleu": result_bleu["score"], "chrf": result_chrf["score"]} |
|
|
|
|
|
|
|
|
|
|
|
args = Seq2SeqTrainingArguments( |
|
f"eng-nah-svo-translation", |
|
evaluation_strategy="no", |
|
save_strategy="epoch", |
|
learning_rate=2e-5, |
|
per_device_train_batch_size=32, |
|
per_device_eval_batch_size=64, |
|
weight_decay=0.01, |
|
save_total_limit=3, |
|
num_train_epochs=3, |
|
predict_with_generate=True, |
|
fp16=False, |
|
push_to_hub=True, |
|
) |
|
|
|
|
|
trainer = Seq2SeqTrainer( |
|
model, |
|
args, |
|
train_dataset=tokenized_datasets["train"], |
|
eval_dataset=tokenized_datasets["test"], |
|
data_collator=data_collator, |
|
tokenizer=tokenizer, |
|
compute_metrics=compute_metrics, |
|
) |
|
|
|
print("evaluate1") |
|
print(trainer.evaluate(max_length=max_length)) |
|
print("trainer train 1") |
|
trainer.train() |
|
print("evaluate 2") |
|
print(trainer.evaluate(max_length=max_length)) |
|
trainer.push_to_hub(tags="translation", commit_message="Training complete") |
|
print("training model now") |
|
model.train() |
|
|
|
|
|
tokenized_datasets.set_format("torch") |
|
train_dataloader = DataLoader( |
|
tokenized_datasets["train"], |
|
shuffle=True, |
|
collate_fn=data_collator, |
|
batch_size=8, |
|
) |
|
eval_dataloader = DataLoader( |
|
tokenized_datasets["test"], collate_fn=data_collator, batch_size=8, drop_last=True |
|
) |
|
|
|
model = AutoModelForSeq2SeqLM.from_pretrained("eng-nah-svo-translation") |
|
|
|
|
|
optimizer = AdamW(model.parameters(), lr=2e-5) |
|
|
|
|
|
|
|
accelerator = Accelerator() |
|
model, optimizer, train_dataloader, eval_dataloader = accelerator.prepare( |
|
model, optimizer, train_dataloader, eval_dataloader |
|
) |
|
|
|
|
|
|
|
num_train_epochs = 3 |
|
num_update_steps_per_epoch = len(train_dataloader) |
|
num_training_steps = num_train_epochs * num_update_steps_per_epoch |
|
|
|
lr_scheduler = get_scheduler( |
|
"linear", |
|
optimizer=optimizer, |
|
num_warmup_steps=0, |
|
num_training_steps=num_training_steps, |
|
) |
|
|
|
|
|
|
|
model_name = "model" |
|
|
|
output_dir = "./output" |
|
repo = Repository("/mnt/storage/aatherton/hf_eng_fra_trans", clone_from="aatherton2024/hf_eng_fra_trans") |
|
|
|
|
|
def postprocess(predictions, labels): |
|
predictions = predictions.cpu().numpy() |
|
labels = labels.cpu().numpy() |
|
|
|
decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True) |
|
|
|
|
|
labels = np.where(labels != -100, labels, tokenizer.pad_token_id) |
|
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True) |
|
|
|
|
|
decoded_preds = [pred.strip() for pred in decoded_preds] |
|
decoded_labels = [[label.strip()] for label in decoded_labels] |
|
return decoded_preds, decoded_labels |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_checkpoint = "aatherton2024/eng-nah-svo-translation" |
|
translator = pipeline("translation", model=model_checkpoint) |
|
translator("Default to expanded threads") |
|
print(translator( |
|
"you did not frichopize him" |
|
)) |