-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfinetune.py
239 lines (191 loc) · 8.22 KB
/
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
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
import argparse
import os
import numpy as np
from datasets import load_dataset, load_metric
from transformers import AutoTokenizer, Trainer, TrainingArguments
from model import SwitchTransformersClassificationModel, SwitchTransformersClassificationModel_Multirc
from utils import config
os.environ["WANDB_DISABLED"] = "true"
BASEDIR = config.BASEDIR
def parse_args(verbose=True):
parser = argparse.ArgumentParser("Finetune MOE for GLUE classification")
parser.add_argument("--local_rank", type=int, default=-1)
parser.add_argument("--benchmark", type=str, required=True, choices=["glue", "super_glue"])
parser.add_argument(
"--task",
type=str,
required=True,
choices=["multirc", "cola", "mnli", "mrpc", "qnli", "qqp", "rte", "sst2", "stsb", "wnli"],
)
parser.add_argument("--model", type=str, required=True, default="switch-base-8")
parser.add_argument("--batch_size", type=int, required=True, default=64)
parser.add_argument("--lr", type=float, default=1e-5)
parser.add_argument("--weight_decay", type=float, default=0.1)
args = parser.parse_args()
if verbose:
for k, v in args.__dict__.items():
print(f"{k}={v}")
return args
def get_num_labels(task):
task_to_num_labels = {
"cola": 2,
"mnli": 3,
"mrpc": 2,
"qnli": 2,
"qqp": 2,
"rte": 2,
"sst2": 2,
"stsb": 1,
"wnli": 2,
"multirc": 2,
}
return task_to_num_labels[task]
def get_tokenize_function(task, model):
tokenizer = AutoTokenizer.from_pretrained(f"google/{model}")
if task == "cola" or task == "sst2":
def tokenize_function(examples):
return tokenizer(examples["sentence"], padding="max_length", truncation=True)
elif task == "mnli":
def tokenize_function(examples):
return tokenizer(examples["premise"], examples["hypothesis"], padding="max_length", truncation=True)
elif task == "mrpc" or task == "stsb" or task == "wnli" or task == "rte":
def tokenize_function(examples):
return tokenizer(
examples["sentence1"], examples["sentence2"], padding="max_length", max_length=128, truncation=True
)
elif task == "qnli":
def tokenize_function(examples):
return tokenizer(examples["question"], examples["sentence"], padding="max_length", truncation=True)
elif task == "qqp":
def tokenize_function(examples):
return tokenizer(examples["question1"], examples["question2"], padding="max_length", truncation=True)
elif task == "multirc":
def tokenize_function(examples):
return tokenizer(
examples["paragraph"],
examples["question"],
examples["answer"],
padding="max_length",
max_length=768,
truncation=True,
)
else:
raise ValueError("Task is not supported!")
return tokenize_function
def get_train_eval_splits(tokenized_datasets, task):
train_key = "train"
if task == "mnli":
eval_key = "validation_matched"
else:
eval_key = "validation"
return tokenized_datasets[train_key], tokenized_datasets[eval_key]
def get_compute_metrics(metric, task):
def compute_metrics(eval_pred):
predictions, labels = eval_pred
if task == 'multirc':
predictions = [{'idx': {'paragraph': q[0], 'question': q[1], 'answer': q[2]},
'prediction': np.argmax(p)}
for p, q in zip(predictions[0], predictions[1])]
elif task != "stsb":
predictions = np.argmax(predictions, axis=1)
else:
predictions = predictions[:, 0]
return metric.compute(predictions=predictions, references=labels)
return compute_metrics
def multirc_data_collator(features):
import torch
from collections.abc import Mapping
if not isinstance(features[0], Mapping):
features = [vars(f) for f in features]
first = features[0]
batch = {}
# Special handling for labels.
# Ensure that tensor is created with the correct type
# (it should be automatically the case, but let's make sure of it.)
if "label" in first and first["label"] is not None:
label = first["label"].item() if isinstance(first["label"], torch.Tensor) else first["label"]
dtype = torch.long if isinstance(label, int) else torch.float
batch["labels"] = torch.tensor([f["label"] for f in features], dtype=dtype)
elif "label_ids" in first and first["label_ids"] is not None:
if isinstance(first["label_ids"], torch.Tensor):
batch["labels"] = torch.stack([f["label_ids"] for f in features])
else:
dtype = torch.long if type(first["label_ids"][0]) is int else torch.float
batch["labels"] = torch.tensor([f["label_ids"] for f in features], dtype=dtype)
# Handling of all other possible keys.
# Again, we will use the first element to figure out which key/values are not None for this model.
for k, v in first.items():
if k == "idx":
batch[k] = torch.tensor(list(map(lambda f: (f[k]['paragraph'], f[k]['question'], f[k]['answer']), features)))
elif k not in ("label", "label_ids") and v is not None and not isinstance(v, str):
if isinstance(v, torch.Tensor):
batch[k] = torch.stack([f[k] for f in features])
elif isinstance(v, np.ndarray):
batch[k] = torch.tensor(np.stack([f[k] for f in features]))
else:
batch[k] = torch.tensor([f[k] for f in features])
return batch
if __name__ == "__main__":
args = parse_args()
print(args)
working_dir = os.path.join(config.BASEDIR, "data", args.task, args.model, "finetuned")
print(f"Save model to {working_dir}")
os.makedirs(working_dir, exist_ok=True)
# Load dataset and metric
dataset = load_dataset(args.benchmark, args.task, cache_dir=f"{config.BASEDIR}/tmp/")
metric = load_metric(args.benchmark, args.task)
# Load model
if args.task != "multirc":
model = SwitchTransformersClassificationModel(
MODEL=args.model, BASEDIR=config.BASEDIR, num_labels=get_num_labels(args.task)
)
else:
model = SwitchTransformersClassificationModel_Multirc(
MODEL=args.model, BASEDIR=config.BASEDIR, num_labels=get_num_labels(args.task)
)
model.train()
# # Freeze all layers except the classification head
# for param in model.switch_transformers.parameters():
# param.requires_grad = False
tokenized_datasets = dataset.map(get_tokenize_function(args.task, args.model), batched=True)
# Remove column for multirc
if args.task == "multirc":
tokenized_datasets = tokenized_datasets.remove_columns("labels")
train_dataset, eval_dataset = get_train_eval_splits(tokenized_datasets, args.task)
train_args = TrainingArguments(
learning_rate=args.lr,
weight_decay=args.weight_decay,
output_dir=working_dir,
per_device_train_batch_size=args.batch_size,
per_device_eval_batch_size=args.batch_size,
max_steps=20000,
logging_steps=200,
save_steps=200,
evaluation_strategy="steps",
report_to=["tensorboard"],
save_strategy = "steps",
save_total_limit=2,
local_rank=args.local_rank,
half_precision_backend="cuda_amp",
ddp_backend="nccl",
# HF automatically use standard optimizer for embedding layer
optim="adamw_bnb_8bit",
ddp_find_unused_parameters=True,
auto_find_batch_size=True,
# Reduce the memory usage but slower
# gradient_checkpointing=True,
load_best_model_at_end=True,
metric_for_best_model="eval_f1_a" if args.task == "multirc" else None,
)
trainer = Trainer(
model=model,
args=train_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
compute_metrics=get_compute_metrics(metric, args.task),
data_collator=multirc_data_collator if args.task == "multirc" else None,
)
trainer.train()
trainer.save_model()
eval_result = trainer.evaluate()
print(">>>RESULT:", eval_result)