-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask A - Inference.py
308 lines (254 loc) · 10.2 KB
/
Task A - Inference.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# coding: utf-8
# %%
import pandas as pd
import numpy as np
from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification,TrainingArguments,Trainer
import huggingface_hub as hf_hub
from pathlib import Path
import datasets as ds
import evaluate
import re
import torch
import torch.nn.functional as F
from torch.optim import AdamW
from torch.utils.data import Dataset, DataLoader, TensorDataset, RandomSampler, SequentialSampler
from transformers import get_linear_schedule_with_warmup
import random
import time
import GPUtil
import wandb
import os
from tqdm import tqdm
import config as code_config
import json
from pprint import pprint
import copy
from peft import (
get_peft_config,
get_peft_model,
get_peft_model_state_dict,
set_peft_model_state_dict,
LoraConfig,
TaskType,
PeftType,
PeftConfig,
PeftModel,
PrefixTuningConfig,
PromptEncoderConfig,
)
import deepspeed
from deepspeed.runtime.utils import see_memory_usage
from fire import Fire
import shutil
# %%
os.environ["WANDB_API_KEY"] = code_config.WANDB_API
os.environ["TOKENIZERS_PARALLELISM"] = "false"
hf_hub.login(code_config.HF_API, add_to_git_credential=True)
WANDB_PROJECT = code_config.MULTI_CLASS_WANDB_PROJECT
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# %%
train_path = Path.cwd().joinpath("2023_ImageCLEFmed_Mediqa","dataset","TaskB","TaskB-TrainingSet.csv")
validation_path = Path.cwd().joinpath("2023_ImageCLEFmed_Mediqa","dataset","TaskB","TaskB-ValidationSet.csv")
augmented_path = Path.cwd().joinpath("TaskA-augmented_data.csv")
train_df = pd.read_csv(train_path,index_col="ID")
valid_df = pd.read_csv(validation_path,index_col="ID")
valid_index = {idx:idx+train_df.shape[0] for idx in valid_df.index}
valid_df.rename(mapper=valid_index,inplace=True)
augmented_data = pd.read_csv(augmented_path,index_col="ID")
augmented_sections = augmented_data["section_header"].unique().tolist()
merge_df = pd.concat([train_df,valid_df,augmented_data],axis=0,ignore_index=False)
merge_df["dialogue_wo_whitespaces"] = merge_df["dialogue"].apply(lambda x: re.sub(r'[\r\n\s]+',' ',x))
merge_df.reset_index(inplace=True)
merge_df.rename(mapper={'index':'ID'},axis=1,inplace=True)
with open("TaskA_and_B-idx2label.json","r") as f:
idx2label = json.load(f)
with open("TaskA_and_B-label2idx.json","r") as f:
label2idx = json.load(f)
merge_df["label"] = merge_df["section_header"].apply(lambda x: label2idx[x])
if any([True if k in code_config.MULTI_CLASS_MODEL_CHECKPOINT else False for k in ["gpt", "opt", "bloom"]]):
padding_side = "left"
else:
padding_side = "right"
# %%
config = AutoConfig.from_pretrained(code_config.MULTI_CLASS_MODEL_CHECKPOINT)
config.balanced_loss = code_config.MUTLI_CLASS_BALANCE_LOSS
config.num_labels = merge_df["label"].nunique()
tokenizer = AutoTokenizer.from_pretrained(
code_config.MULTI_CLASS_MODEL_CHECKPOINT, do_lower_case=True, force_download=True, padding_side=padding_side
)
if getattr(tokenizer, "pad_token_id") is None:
tokenizer.pad_token_id = tokenizer.eos_token_id
# %%
def seed_everything(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# %%
def my_tokenizer(data, labels, max_length):
complete_input_ids = []
input_ids = []
attention_mask = []
for sentence in data:
non_truncated_sentence = tokenizer.encode(
sentence,
return_tensors="pt",
padding="max_length",
truncation=True,
verbose=False,
max_length=3000,
)
complete_input_ids.append(non_truncated_sentence)
tokenized_sentence = tokenizer.encode_plus(
sentence,
add_special_tokens=True,
padding="max_length",
truncation=True,
max_length=code_config.MULTI_CLASS_MAX_LENGTH,
verbose=False,
return_tensors="pt",
return_attention_mask=True,
)
input_ids.append(tokenized_sentence["input_ids"])
attention_mask.append(tokenized_sentence["attention_mask"])
non_truncated_sentence_tensors = torch.cat(complete_input_ids, dim=0)
input_ids_tensor = torch.cat(input_ids, dim=0)
attention_mask_tensor = torch.cat(attention_mask, dim=0)
labels_tensor = torch.tensor(labels.tolist())
return (
input_ids_tensor,
attention_mask_tensor,
labels_tensor,
non_truncated_sentence_tensors,
)
def preprocess_function(examples):
inputs = examples["dialogue_wo_whitespaces"]
targets = examples["label"]
model_inputs = \
tokenizer(inputs, \
padding="max_length", \
truncation=True, \
max_length=code_config.MULTI_CLASS_MAX_LENGTH, \
return_tensors="pt")
model_inputs["labels"] = targets
return model_inputs
def data_creation(df,train_idx,valid_idx,test_idx):
train_df = df.loc[df["ID"].isin(train_idx),:]
_ = train_df.pop("ID")
valid_df = df.loc[df["ID"].isin(valid_idx),:]
_ = valid_df.pop("ID")
test_df = df.loc[df["ID"].isin(test_idx),:]
_ = test_df.pop("ID")
train_ds = ds.Dataset.from_pandas(train_df)
valid_ds = ds.Dataset.from_pandas(valid_df)
test_ds = ds.Dataset.from_pandas(test_df)
raw_dataset = ds.DatasetDict()
raw_dataset["train"] = train_ds
raw_dataset["valid"] = valid_ds
raw_dataset["test"] = test_ds
columns = raw_dataset["train"].column_names
processed_datasets = \
raw_dataset.map(function=preprocess_function, \
batched=True, \
remove_columns=columns, \
load_from_cache_file=False, \
desc="Running tokenizer on dataset")
train_dataset = processed_datasets["train"]
valid_dataset = processed_datasets["valid"]
test_dataset = processed_datasets["test"]
return train_dataset,valid_dataset,test_dataset
# %%
def flat_accuracy(preds, labels):
pred_flat = np.argmax(preds, axis=-1).flatten()
labels_flat = labels.flatten()
return (pred_flat == labels_flat).sum() / len(labels_flat)
# %%
def log_validation_predictions(full_input_ids, input_ids, labels, logits):
if len(full_input_ids) != len(input_ids):
raise Exception(
"Length of full_input_ids must be equal to length of truncated_input_ids"
)
if len(input_ids) != len(labels):
raise Exception(
"Length of truncated_input_ids must be equal to length of labels"
)
if len(labels) != len(logits):
raise Exception("Length of labels must be equal to length of logits")
columns = ["id", "full_sentence", "truncated_sentence", "label", "prediction"]
for section in label2idx.keys():
columns.append(f"Score_{section}")
valid_table = wandb.Table(columns=columns)
full_input_ids = torch.cat(full_input_ids, dim=0)
input_ids = torch.cat(input_ids, dim=0)
labels = torch.cat(labels, dim=0).float()
logits = torch.cat(logits, dim=0).float()
scores = F.softmax(logits, dim=-1)
predictions = torch.argmax(scores, dim=-1)
log_full_input_ids = full_input_ids
log_truncated_input_ids = input_ids
log_scores = scores.detach().cpu()
log_labels = [idx2label[l.item()] for l in labels]
log_preds = [idx2label[p.item()] for p in predictions]
for idx, (lfs, lts, ll, lp, ls) in enumerate(
zip(
log_full_input_ids,
log_truncated_input_ids,
log_labels,
log_preds,
log_scores,
)
):
log_full_sentences = tokenizer.decode(lfs, skip_special_tokens=True)
log_truncated_sentences = tokenizer.decode(lts, skip_special_tokens=True)
sentence_id = str(idx)
valid_table.add_data(
sentence_id, log_full_sentences, log_truncated_sentences, ll, lp, *ls
)
wandb.log({"validation_table": valid_table})
# %%
def main():
with open("taskA_and_B_train_valid_test_split.json","r") as f:
split_data = json.load(f)
for split, split_w_indices in split_data.items():
train_idx = split_w_indices["train"]
valid_idx = split_w_indices["valid"]
test_idx = split_w_indices["test"]
train_ds,valid_ds,test_ds = \
data_creation(merge_df,train_idx,valid_idx,test_idx)
peft_model_id = f"suryakiran786/LoRA-3-stratified-cv-Bio_ClinicalBERT-lora-{int(split)}"
metric = evaluate.load("accuracy")
config = PeftConfig.from_pretrained(peft_model_id)
inference_model = AutoModelForSequenceClassification.from_pretrained(config.base_model_name_or_path,num_labels=20)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
inference_model = PeftModel.from_pretrained(inference_model, peft_model_id)
inference_model.to(device)
inference_model.eval()
test_dataloader = DataLoader(test_ds, shuffle=False, batch_size=code_config.MULTI_CLASS_MICRO_BATCH_SIZE)
for step, batch in enumerate(tqdm(test_dataloader)):
input_ids = [b[None,:] for b in batch["input_ids"]]
input_ids = torch.cat(input_ids,dim=0).to(device)
batch["input_ids"] = input_ids.T
token_type_ids = [b[None,:] for b in batch["token_type_ids"]]
token_type_ids = torch.cat(token_type_ids,dim=0).to(device)
batch["token_type_ids"] = token_type_ids.T
attention_mask = [b[None,:] for b in batch["attention_mask"]]
attention_mask = torch.cat(attention_mask,dim=0).to(device)
batch["attention_mask"] = attention_mask.T
batch["labels"] = batch["labels"][None,:].to(device)
with torch.no_grad():
outputs = inference_model(**batch)
predictions = outputs.logits.argmax(dim=-1)
predictions, references = predictions, batch["labels"]
predictions = predictions.detach().cpu().numpy()
references = references.detach().cpu().numpy().squeeze(0)
metric.add_batch(
predictions=predictions,
references=references,
)
eval_metric = metric.compute()
print(peft_model_id)
print(eval_metric)
# wandb.finish()
if __name__ == "__main__":
main()