-
Notifications
You must be signed in to change notification settings - Fork 0
/
paraphrasze_trainer_finetune.py
144 lines (109 loc) · 4.65 KB
/
paraphrasze_trainer_finetune.py
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
import torch
import os
from torch.utils.data import DataLoader
from transformers import (AutoTokenizer,
AutoModelForSequenceClassification,
AutoModelForSeq2SeqLM,
TrainingArguments, Trainer,
PegasusForConditionalGeneration, PegasusTokenizer,
DataCollatorForTokenClassification)
from datasets import Dataset, DatasetDict, load_metric, load_dataset
from datasets.utils import disable_progress_bar
disable_progress_bar()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("================================= VM INFO =================================")
print(f"device name : {device}")
print(f"Number of GPU: {torch.cuda.device_count()}")
print(f"Number of CPUs: {os.cpu_count()}")
print(f"GPU type: {torch.cuda.get_device_name(0)}")
print("===========================================================================")
class Config:
output_dir="./out"
model_id="sshleifer/distill-pegasus-cnn-16-4"
num_train_epochs=1
per_device_train_batch_size =1
per_device_eval_batch_size=1
save_strategy="no"
fp16=True
report_to ='tensorboard'
push_to_hub=False
organization=None
hub_auth_token=None
model = AutoModelForSeq2SeqLM.from_pretrained(Config.model_id).to(device)
tokenizer = AutoTokenizer.from_pretrained(Config.model_id)
######################## Dataset class and tokenizer function #####################################
class PegasusDataset(torch.utils.data.Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels['input_ids'][idx]) # torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels['input_ids'])
def tokenize_data(texts, labels):
encodings = tokenizer(texts, truncation=True, padding=True)
decodings = tokenizer(labels, truncation=True, padding=True)
dataset_tokenized = PegasusDataset(encodings, decodings)
return dataset_tokenized
################################################################################################
paws_data = load_dataset('paws', 'labeled_final')
# paraphrasing input
train_texts = paws_data['train']['sentence1'][:10]
train_labels= paws_data['train']['sentence2'][:10]
val_texts = paws_data['validation']['sentence1'][:10]
val_labels= paws_data['validation']['sentence2'][:10]
train_dataset = tokenize_data(train_texts, train_labels)
val_dataset = tokenize_data(val_texts, val_labels)
if Config.push_to_hub:
training_args = TrainingArguments(
output_dir=Config.output_dir,
num_train_epochs=Config.num_train_epochs,
per_device_train_batch_size=Config.per_device_train_batch_size,
per_device_eval_batch_size=Config.per_device_eval_batch_size,
report_to = Config.report_to,
save_strategy=Config.save_strategy,
overwrite_output_dir=True,
evaluation_strategy="epoch",
do_eval=True,
fp16=Config.fp16,
dataloader_drop_last=True,
push_to_hub=Config.push_to_hub,
hub_model_id=f"{Config.organization}/finetuned_{Config.model_id.split('/')[-1]}",
hub_token=Config.hub_auth_token
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
tokenizer=tokenizer
)
trainer.train()
trainer.push_to_hub()
tokenizer.push_to_hub(f"finetuned_summarization_{Config.model_id.split('/')[-1]}",
organization=Config.organization,
use_auth_token=Config.hub_auth_token)
else:
training_args = TrainingArguments(
output_dir=Config.output_dir,
num_train_epochs=Config.num_train_epochs,
per_device_train_batch_size=Config.per_device_train_batch_size,
per_device_eval_batch_size=Config.per_device_eval_batch_size,
report_to = Config.report_to,
save_strategy=Config.save_strategy,
overwrite_output_dir=True,
evaluation_strategy="epoch",
do_eval=True,
fp16=Config.fp16,
dataloader_drop_last=True
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
tokenizer=tokenizer
)
trainer.train()