This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training_loop.py
289 lines (230 loc) · 7.99 KB
/
training_loop.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
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
import argparse
from dataclasses import dataclass
from pprint import pprint
import evaluate
import numpy as np
import pandas as pd
import torch
from datasets import load_dataset
from peft import LoraConfig, TaskType, get_peft_model
from transformers import (
AutoConfig,
AutoTokenizer,
DataCollatorWithPadding,
EvalPrediction,
OPTForSequenceClassification,
Trainer,
TrainingArguments,
)
import wandb
MODEL = "facebook/opt-350m"
MAX_POSITION_EMBEDDINGS = 2048
@dataclass
class Data:
dataset: any
classes: list
class2id: dict
id2class: dict
def train(args: argparse.Namespace):
global tokenizer
global classes
global class2id
global id2class
tokenizer = AutoTokenizer.from_pretrained(MODEL, use_fast=True)
if args.biotech:
data = load_biotech(tokenizer)
else:
data = load_mimic(tokenizer, args)
dataset = data.dataset
classes, class2id, id2class = data.classes, data.class2id, data.id2class
create_metrics(args)
# note - save stratedy and evaluation strategy need to match
training_args = TrainingArguments(
disable_tqdm=args.disable_tqdm,
output_dir=args.checkpoint_dir,
dataloader_num_workers=3,
evaluation_strategy="epoch",
eval_steps=args.save_interval,
save_strategy="epoch",
save_steps=args.save_interval,
learning_rate=0.00007895,
num_train_epochs=args.epochs,
weight_decay=0.05537,
load_best_model_at_end=True,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
eval_accumulation_steps=250,
logging_steps=100,
adam_epsilon=1e-8,
save_total_limit=3,
tf32=True,
)
if args.gradient_checkpointing:
training_args.gradient_checkpointing = True
training_args.gradient_checkpointing_kwargs = {"use_reentrant": False}
if args.tiny:
# Use tiny subset of dataset
dataset["train"] = dataset["train"].shard(index=1, num_shards=args.shard)
dataset["test"] = dataset["test"].shard(index=1, num_shards=args.shard)
training_args.evaluation_strategy = "epoch"
training_args.eval_steps = 1
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
if args.wandb:
pprint("Using wandb with already logged in user")
training_args.report_to = ["wandb"]
elif args.wandb_key:
pprint("Using wandb with specified key")
wandb.login(key=args.wandb_key)
training_args.report_to = ["wandb"]
if args.fresh_start:
pprint("Fresh Start")
trainer = Trainer(
model_init=model_init,
args=training_args,
train_dataset=dataset["train"],
eval_dataset=dataset["test"],
tokenizer=tokenizer,
compute_metrics=compute_metrics,
data_collator=data_collator,
)
if args.search:
print("Doing hyperparameter search")
best_run = trainer.hyperparameter_search(
n_trials=args.n_trials,
backend="optuna",
hp_space=optuna_hp_space,
direction="maximize",
)
print(best_run)
else:
trainer.train()
else:
# Load model from local checkpoint
pprint("Attempting to load local model checkpoint")
model = OPTForSequenceClassification.from_pretrained(
args.checkpoint_dir,
torch_dtype=torch.float16,
attn_implementation="flash_attention_2",
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset["train"],
eval_dataset=dataset["validation"],
tokenizer=tokenizer,
data_collator=data_collator,
)
trainer.train(resume_from_checkpoint=True)
trainer.save_model(args.checkpoint_dir)
trainer.save_state()
def optuna_hp_space(trial):
return {
"learning_rate": trial.suggest_float("learning_rate", 1e-6, 1e-4, log=True),
"adam_epsilon": trial.suggest_float("adam_epsilon", 1e-8, 0.1, log=True),
"weight_decay": trial.suggest_float("weight_decay", 0, 0.1),
}
def create_metrics(args):
global clf_metrics
print("Creating evaluation metrics")
f1 = evaluate.load(
"f1",
config_name="multilabel",
)
precision = evaluate.load(
"precision",
config_name="multilabel",
)
recall = evaluate.load(
"recall",
config_name="multilabel",
)
clf_metrics = evaluate.combine([f1, precision, recall])
def model_init():
"""Model init for use for hyperparameter_search"""
lora_config = LoraConfig(
r=16,
target_modules=["q_proj", "v_proj"],
task_type=TaskType.SEQ_CLS,
lora_alpha=32,
lora_dropout=0.05,
)
config, unused_kwargs = AutoConfig.from_pretrained(
MODEL,
num_labels=len(classes),
id2label=id2class,
label2id=class2id,
problem_type="multi_label_classification",
return_unused_kwargs=True,
)
if unused_kwargs:
print(f"Unused kwargs: {unused_kwargs}")
model = OPTForSequenceClassification.from_pretrained(
MODEL,
config=config,
)
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
return model
def compute_metrics(p: EvalPrediction):
"""preds = p.predictions[0] if isinstance(p.predictions, tuple) else p.predictions
return result"""
preds = p.predictions[0] if isinstance(p.predictions, tuple) else p.predictions
preds = np.where(preds > 0, 1, 0)
result = clf_metrics.compute(
predictions=preds, references=p.label_ids, average="micro"
)
return result
def load_biotech(tokenizer):
print("Loading biotech events classification dataset")
dataset = load_dataset(
"knowledgator/events_classification_biotech", trust_remote_code=True
)
classes = [
class_ for class_ in dataset["train"].features["label 1"].names if class_
]
class2id = {class_: id for id, class_ in enumerate(classes)}
id2class = {id: class_ for class_, id in class2id.items()}
def preprocess_function(example):
text = f"{example['title']}.\n{example['content']}"
all_labels = example["all_labels"]
labels = [0.0 for i in range(len(classes))]
for label in all_labels:
label_id = class2id[label]
labels[label_id] = 1.0
example = tokenizer(text, truncation=True, max_length=MAX_POSITION_EMBEDDINGS)
example["labels"] = labels
return example
dataset = dataset.map(preprocess_function)
return Data(dataset, classes, class2id, id2class)
def load_mimic(tokenizer, args):
print("Loading MIMIC-IV dataset")
data_files = {
"train": args.train_path,
"validation": args.val_path,
"test": args.test_path,
}
code_labels = pd.read_csv(args.code_labels)
dataset = load_dataset("csv", data_files=data_files, cache_dir=args.cache_dir)
# Create class dictionaries
classes = [class_ for class_ in code_labels["icd_code"] if class_]
class2id = {class_: id for id, class_ in enumerate(classes)}
id2class = {id: class_ for class_, id in class2id.items()}
def multi_labels_to_ids(labels: list[str]) -> list[float]:
ids = [0.0] * len(class2id) # BCELoss requires float as target type
for label in labels:
ids[class2id[label]] = 1.0
return ids
def preprocess_function(example):
result = tokenizer(
example["text"], truncation=True, max_length=MAX_POSITION_EMBEDDINGS
)
result["labels"] = [
multi_labels_to_ids(eval(label)) for label in example["labels"]
]
return result
dataset = dataset.map(
preprocess_function, load_from_cache_file=True, batched=True, num_proc=8
)
return Data(dataset, classes, class2id, id2class)
if __name__ == "__main__":
print("Please use manager.py")