forked from boostcampaitech2/klue-level2-nlp-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_run.py
340 lines (285 loc) · 12 KB
/
new_run.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# new_run.py
"""
Execution file for model training, inference, and analysis.
Functions:
main(command_args): Training, inference, and analysis are conducted according to command args.
"""
import os
import sys
import argparse
from functools import partial
from typing import Tuple, List, Any, Dict
from tqdm import tqdm
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from datasets import load_dataset, concatenate_datasets
import transformers
from transformers import (
AutoConfig,
AutoTokenizer,
AutoModelForSequenceClassification,
)
from transformers import TrainingArguments, Trainer
from transformers.configuration_utils import PretrainedConfig
from solution.args import (
HfArgumentParser,
DataArguments,
NewTrainingArguments,
ModelingArguments,
ProjectArguments,
)
from solution.data import (
COLLATOR_MAP,
PREPROCESSING_PIPELINE,
kfold_split,
)
from solution.models import (
MODEL_INIT_FUNC,
)
from solution.trainers import (
TRAINER_MAP,
)
from solution.utils import (
softmax,
set_seeds,
get_confusion_matrix,
TASK_METRIC_MAP,
TASK_INFOS_MAP,
CONFIG_FILE_NAME,
PYTORCH_MODEL_NAME,
INFERENCE_PIPELINE,
)
# debug for cuda
os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
def main(command_args):
parser = HfArgumentParser(
(DataArguments,
NewTrainingArguments,
ModelingArguments,
ProjectArguments,)
)
if command_args.config.endswith(".json"):
# read args from json file
args = parser.parse_json_file(json_file=os.path.abspath(command_args.config))
elif command_args.config.endswith(".yaml"):
args = parser.parse_yaml_file(yaml_file=os.path.abspath(command_args.config))
else:
# read args from shell script or real arguments
args = parser.parse_args_into_dataclasses()
data_args, training_args, model_args, project_args = args
# Set seed
set_seeds(training_args.seed)
checkpoint = project_args.checkpoint
task_infos = TASK_INFOS_MAP[project_args.task]
compute_metrics = TASK_METRIC_MAP[project_args.task]
# Get training data
dataset = load_dataset(
data_args.name,
script_version=data_args.revision,
cache_dir=data_args.data_cache_dir,
)
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path)
tokenizer.add_special_tokens(
{"additional_special_tokens": list(task_infos.markers.values())}
)
collate_cls = COLLATOR_MAP[data_args.collator_name]
prep_pipeline = PREPROCESSING_PIPELINE[data_args.prep_pipeline_name]
data_collator = collate_cls(tokenizer, max_length=data_args.max_length)
# Preprocess and tokenizing
tokenized_datasets = prep_pipeline(dataset,
tokenizer,
task_infos,)
# =============================================================
# UnitTest input
# print(tokenized_datasets["train"][0])
# return None
# =============================================================
# Get model
_model_init = MODEL_INIT_FUNC[model_args.model_init]
model_init = partial(_model_init,
model_args=model_args,
task_infos=task_infos,
tokenizer=tokenizer,)
# =============================================================
# UnitTest RECENT
# model = model_init()
# output = model(torch.LongTensor(2, 10).random_(10000),
# head_ids=torch.LongTensor([0,8]),
# labels=torch.LongTensor(
# [[0,0,4,0,0,2,0,0,0,0,0,0,0,],
# [0,8,0,0,0,0,0,0,0,0,1,0,0,]]))
# return output
# =============================================================
# Set-up WANDB
os.environ["WANDB_PROJECT"] = project_args.wandb_project
call_wandb = True
try:
os.environ["WANDB_PROJECT"]
except KeyError:
call_wandb = False
if call_wandb:
import wandb
wandb.login()
# augmentation dataset 선택 및 결합
if data_args.augment != 'original':
train_dataset = concatenate_datasets([tokenized_datasets['train'], tokenized_datasets[data_args.augment]])
else:
train_dataset = tokenized_datasets["train"]
# TODO datasetdict가 아닌 경우 처리
try:
eval_dataset = tokenized_datasets["valid"]
except KeyError:
eval_dataset = None
if project_args == "tapt":
test_dataset = load_dataset(data_args.name,
script_version=data_args.revision,
cache_dir=data_args.data_cache_dir,
split="test",)
test_dataset = prep_pipeline(test_dataset,
tokenizer,
task_infos,
mode="train",)
train_dataset = concatenate_datasets([train_dataset, test_dataset])
eval_dataset = None
# train/valid split
if command_args.fold > 0:
# kfold
train_dataset, eval_dataset = kfold_split(train_dataset, n_splits=5, fold=command_args.fold, random_state=training_args.seed)
training_args.run_name = training_args.run_name + f"_fold{command_args.fold}"
training_args.output_dir = training_args.output_dir + f"/fold{command_args.fold}"
project_args.save_model_dir = project_args.save_model_dir + f"/fold{command_args.fold}"
# =============================================================
# Smoke test
# train_dataset = train_dataset.select([i for i in range(1000)])
# if eval_dataset is not None:
# eval_dataset = eval_dataset.select([i for i in range(500)])
# =============================================================
trainer_class = TRAINER_MAP[training_args.trainer_class]
trainer = trainer_class(
model_init=model_init,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
# Training
if training_args.do_train:
trainer.train()
checkpoint = project_args.save_model_dir
trainer.model.save_pretrained(checkpoint)
tokenizer.save_pretrained(checkpoint)
if training_args.do_predict and not project_args.task == "tapt":
# Load Checkpoint
ckpt_config_file = os.path.join(checkpoint, CONFIG_FILE_NAME)
ckpt_model_file = os.path.join(checkpoint, PYTORCH_MODEL_NAME)
config = PretrainedConfig.from_json_file(ckpt_config_file)
# We load the model state dict on the CPU to avoid an OOM error.
state_dict = torch.load(ckpt_model_file, map_location="cpu")
# If the model is on the GPU, it still works!
trainer._load_state_dict_in_model(state_dict)
del state_dict
# Inference
test_dataset = load_dataset(
data_args.name,
script_version=data_args.revision,
cache_dir=data_args.data_cache_dir,
split="test",
)
test_id = test_dataset["guid"]
tokenized_test_datasets = prep_pipeline(test_dataset,
tokenizer,
task_infos,
mode="test",)
infer_pipeline = INFERENCE_PIPELINE[project_args.infer_pipeline_name]
probs, pred_answer = infer_pipeline(tokenized_test_datasets,
trainer=trainer,
task_infos=task_infos,
training_args=training_args,)
output = pd.DataFrame(
{
'id':test_id,
'pred_label':pred_answer,
'probs':probs,
}
)
submir_dir = training_args.output_dir
run_name = training_args.run_name
output.to_csv(f'./{submir_dir}/submission_{run_name}.csv', index=False)
if project_args.do_analysis:
del trainer
torch.cuda.empty_cache()
# Load & Preprocess the Dataset(for all samples of train dataset)
train_dataset = load_dataset(
data_args.name,
script_version=data_args.revision,
cache_dir=data_args.data_cache_dir,
split="train",
)
train_dataset = train_dataset
train_id = train_dataset["guid"]
tokenized_train_datasets = pipeline(train_dataset,
tokenizer,
task_infos,)
train_dataloader = torch.utils.data.DataLoader(tokenized_train_datasets,
collate_fn=data_collator,
batch_size=training_args.per_device_train_batch_size, shuffle=False,
num_workers=4, pin_memory=True)
model = AutoModelForSequenceClassification.from_pretrained(checkpoint, output_hidden_states=True)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model.to(device)
# Path Setup : analysis results will be saved in `./analysis`
submir_dir = project_args.submit_dir
run_name = training_args.run_name
analysis_dir = f'./analysis/{run_name}'
if (os.path.isdir(analysis_dir) == False):
os.makedirs(analysis_dir)
# Inference
output_logit = []
output_prob = []
output_pred = []
batch_size=training_args.per_device_train_batch_size
embeddings = np.zeros((len(tokenized_train_datasets), model.config.hidden_size), dtype=np.float32)
for i, data in enumerate(tqdm(train_dataloader)):
with torch.no_grad():
outputs = model(
input_ids=data['input_ids'].to(device),
attention_mask=data['attention_mask'].to(device),
token_type_ids=data['token_type_ids'].to(device) if 'token_type_ids' in data.keys() else None,
)
hidden_state = outputs[1][-1][:, 0, :].detach().cpu().numpy()
if len(hidden_state) == batch_size:
embeddings[i*batch_size:(i+1)*batch_size] = hidden_state
else:
embeddings[i*batch_size:i*batch_size + len(hidden_state)] = hidden_state
logits = outputs[0]
prob = nn.functional.softmax(logits, dim=-1).detach().cpu().numpy()
logits = logits.detach().cpu().numpy()
result = np.argmax(logits, axis=-1)
output_logit.append(logits)
output_pred.append(result)
output_prob.append(prob)
# Save Embeddings
np.save(os.path.join(analysis_dir, f'embeddings.npy'), embeddings)
print('Embedding vectors saved in ' , os.path.join(analysis_dir, f'embeddings.npy'))
# Save Confusion Matrix & Dataframe
pred_answer = np.concatenate(output_pred).tolist()
output_prob = np.concatenate(output_prob, axis=0).tolist()
output = pd.DataFrame({'id':train_id, 'pred_label':pred_answer,'probs':output_prob,})
cm_fig = get_confusion_matrix(output['pred_label'].values,
tokenized_train_datasets['label'],
is_logit=False)
cm_fig.savefig(os.path.join(analysis_dir, f'confusion_mtx.png'), dpi=300)
torch.save(output, os.path.join(analysis_dir, 'data_frame.pt'))
print('Dataframe & Confusion matrix saved in ' , analysis_dir)
print('---- Finish! ----')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--fold', type=int, default=0, help='k-fold fold num: 1~5 & no k-fold: 0 (default)')
parser.add_argument('--config', type=str, default="configs/kfold.yaml", help='config file path (default: configs/kfold.yaml)')
command_args = parser.parse_args()
main(command_args)