-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_train.py
331 lines (284 loc) · 12.8 KB
/
decoder_train.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
from functools import cache
from args_helper import (
DataArguments,
ModelArguments,
TrainingArguments
)
from datasets import load_from_disk, load_metric, set_caching_enabled, DatasetDict
from data_utils import load_dataset
from itertools import chain
from models import ClipCaptionModel
from torch.nn.functional import cross_entropy
from tqdm import tqdm
from transformers import (
default_data_collator,
get_linear_schedule_with_warmup,
set_seed,
AdamW,
DataCollatorForLanguageModeling,
GPT2Config,
AutoTokenizer,
AutoModelForCausalLM,
EarlyStoppingCallback,
HfArgumentParser,
Trainer
)
from transformers.trainer_utils import get_last_checkpoint, is_main_process
import logging
import math
import numpy as np
import os
import sys
import torch
import transformers
set_caching_enabled(True)
logger = logging.getLogger(__name__)
#####
# Main Functions
#####
def run(model_args, data_args, training_args):
###
# Prepare Processor & Model
###
training_args.output_dir="{}/decoder_finetuning/{}".format(training_args.output_dir, model_args.model_name_or_path)
os.makedirs(training_args.output_dir, exist_ok=True)
if data_args.cache_dir_path is None:
data_args.cache_dir_path = "./{}/{}".format(data_args.cache_dir_name, model_args.model_name_or_path)
os.makedirs(data_args.cache_dir_path, exist_ok=True)
###
# Prepare Dataset
###
datasets = DatasetDict()
print('Loading train, validation, test dataset...')
datasets = load_from_disk(data_args.dataset_path)
print('Preprocess dataset...')
# Load model and processor
tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path)
# tokenizer.model_max_length = 2048
model = AutoModelForCausalLM.from_pretrained(model_args.model_name_or_path)
model.config.update({"use_cache": False})
# def _resize_wpe(model, n_positions):
# old_embeddings = model.transformer.wpe
# new_embeddings = model._get_resized_embeddings(old_embeddings, n_positions)
# model.transformer.wpe = new_embeddings
# model.config.update({"n_positions": n_positions})
# return model
# model = _resize_wpe(model, 2048)
model.resize_token_embeddings(len(tokenizer))
# Preprocess chunked data
print('Vectorize dataset...')
def tokenize(batch):
outputs = tokenizer(batch[data_args.text_column_name], truncation=True)
return outputs
with training_args.main_process_first(desc="dataset tokenization"):
tokenized_datasets = datasets.map(
tokenize,
num_proc=data_args.preprocessing_num_workers,
remove_columns=datasets["train"].column_names,
writer_batch_size=data_args.writer_batch_size,
desc="preprocess datasets",
load_from_cache_file=True,
cache_file_names={
"train": "{}/train_tokenized.arrow".format(data_args.cache_dir_path),
"valid": "{}/valid_tokenized.arrow".format(data_args.cache_dir_path),
"test": "{}/test_tokenized.arrow".format(data_args.cache_dir_path),
}
)
if data_args.block_size is None:
block_size = tokenizer.model_max_length
if block_size > 1024:
logger.warning(
f"The tokenizer picked seems to have a very large `model_max_length` ({tokenizer.model_max_length}). "
"Picking 1024 instead. You can change that default value by passing --block_size xxx."
)
block_size = 1024
else:
if data_args.block_size > tokenizer.model_max_length:
logger.warning(
f"The block_size passed ({data_args.block_size}) is larger than the maximum length for the model"
f"({tokenizer.model_max_length}). Using block_size={tokenizer.model_max_length}."
)
block_size = min(data_args.block_size, tokenizer.model_max_length)
# Main data processing function that will concatenate all texts from our dataset and generate chunks of block_size.
def group_texts(examples):
# Concatenate all texts.
concatenated_examples = {k: list(chain(*examples[k])) for k in examples.keys()}
total_length = len(concatenated_examples[list(examples.keys())[0]])
# We drop the small remainder, we could add padding if the model supported it instead of this drop, you can
# customize this part to your needs.
if total_length >= block_size:
total_length = (total_length // block_size) * block_size
# Split by chunks of max_len.
result = {
k: [t[i : i + block_size] for i in range(0, total_length, block_size)]
for k, t in concatenated_examples.items()
}
result["labels"] = result["input_ids"].copy()
return result
# Note that with `batched=True`, this map processes 1,000 texts together, so group_texts throws away a remainder
# for each of those groups of 1,000 texts. You can adjust that batch_size here but a higher value might be slower
# to preprocess.
#
# To speed up this part, we use multiprocessing. See the documentation of the map method for more information:
# https://huggingface.co/docs/datasets/package_reference/main_classes.html#datasets.Dataset.map
with training_args.main_process_first(desc="grouping texts together"):
preprocessed_datasets = tokenized_datasets.map(
group_texts,
batched=True,
num_proc=data_args.preprocessing_num_workers,
load_from_cache_file=True,
cache_file_names={
"train": "{}/train_grouped.arrow".format(data_args.cache_dir_path),
"valid": "{}/valid_grouped.arrow".format(data_args.cache_dir_path),
"test": "{}/test_grouped.arrow".format(data_args.cache_dir_path),
},
desc=f"Grouping texts in chunks of {block_size}",
)
if data_args.preprocessing_only:
logger.info(f"Data preprocessing finished. Files cached at {preprocessed_datasets.cache_files}.")
return
if training_args.do_train:
if "train" not in preprocessed_datasets:
raise ValueError("--do_train requires a train dataset")
if training_args.do_eval:
if "valid" not in preprocessed_datasets:
raise ValueError("--do_eval requires a validation dataset")
###
# Prepare Data Collator and Trainer
###
def preprocess_logits_for_metrics(logits, labels):
if isinstance(logits, tuple):
# Depending on the model and config, logits may contain extra tensors,
# like past_key_values, but logits always come first
logits = logits[0]
return logits.argmax(dim=-1)
metric = load_metric("accuracy")
def compute_metrics(eval_preds):
preds, labels = eval_preds
# preds have the same shape as the labels, after the argmax(-1) has been calculated
# by preprocess_logits_for_metrics but we need to shift the labels
labels = labels[:, 1:].reshape(-1)
preds = preds[:, :-1].reshape(-1)
return metric.compute(predictions=preds, references=labels)
print('Preparing Trainer...')
# Initialize Trainer
trainer = Trainer(
train_dataset=preprocessed_datasets["train"],
eval_dataset=preprocessed_datasets["valid"],
model=model,
data_collator=default_data_collator,
args=training_args,
compute_metrics=compute_metrics if training_args.do_eval else None,
preprocess_logits_for_metrics=preprocess_logits_for_metrics if training_args.do_eval else None,
callbacks=[EarlyStoppingCallback(early_stopping_patience=5)]
)
###
# Training Phase
###
if training_args.do_train:
print('*** Training Phase ***')
checkpoint = None
# Detecting last checkpoint.
last_checkpoint = None
if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(training_args.output_dir)
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None and training_args.resume_from_checkpoint is None:
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
if training_args.resume_from_checkpoint is not None:
checkpoint = training_args.resume_from_checkpoint
elif last_checkpoint is not None:
checkpoint = last_checkpoint
train_result = trainer.train(resume_from_checkpoint=checkpoint)
trainer.save_model() # Saves the tokenizer too for easy upload
metrics = train_result.metrics
metrics["train_samples"] = len(preprocessed_datasets["train"])
trainer.log_metrics("train", metrics)
trainer.save_metrics("train", metrics)
trainer.save_state()
###
# Evaluation Phase
###
if training_args.do_eval:
print("*** Evaluation Phase ***")
metrics = trainer.evaluate()
metrics["eval_samples"] = len(preprocessed_datasets["valid"])
try:
perplexity = math.exp(metrics["eval_loss"])
except OverflowError:
perplexity = float("inf")
metrics["perplexity"] = perplexity
trainer.log_metrics("eval", metrics)
trainer.save_metrics("eval", metrics)
kwargs = {"finetuned_from": model_args.model_name_or_path, "tasks": "text-generation"}
if data_args.dataset_name is not None:
kwargs["dataset_tags"] = data_args.dataset_name
if data_args.dataset_config_name is not None:
kwargs["dataset_args"] = data_args.dataset_config_name
kwargs["dataset"] = f"{data_args.dataset_name} {data_args.dataset_config_name}"
else:
kwargs["dataset"] = data_args.dataset_name
if training_args.push_to_hub:
trainer.push_to_hub(**kwargs)
else:
trainer.create_model_card(**kwargs)
#####
# Entry Point
#####
def main():
###
# Parsing & Initialization
###
# Parse argument
parser = HfArgumentParser((ModelArguments, DataArguments, TrainingArguments))
if len(sys.argv) == 2 and sys.argv[1].endswith(".json"):
model_args, data_args, training_args = parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]))
else:
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
# Set random seed
set_seed(training_args.seed)
# Detect last checkpoint
last_checkpoint = None
if os.path.isdir(training_args.output_dir) and training_args.do_train and not training_args.overwrite_output_dir:
last_checkpoint = get_last_checkpoint(training_args.output_dir)
if last_checkpoint is None and len(os.listdir(training_args.output_dir)) > 0:
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty. "
"Use --overwrite_output_dir to overcome."
)
elif last_checkpoint is not None:
logger.info(
f"Checkpoint detected, resuming training at {last_checkpoint}. To avoid this behavior, change "
"the `--output_dir` or add `--overwrite_output_dir` to train from scratch."
)
###
# Prepare logger
###
# Init logging
os.makedirs("./log", exist_ok=True)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout), logging.FileHandler(
"./log/log__{}".format(model_args.model_name_or_path.replace("/", "_")), mode="w")],
)
logger.setLevel(logging.INFO if is_main_process(training_args.local_rank) else logging.WARN)
# Log on each process the small summary:
logger.warning(
f"Process rank: {training_args.local_rank}, device: {training_args.device}, n_gpu: {training_args.n_gpu}"
f"distributed training: {bool(training_args.local_rank != -1)}, 16-bits training: {training_args.fp16}"
)
# Set the verbosity to warn of the Transformers logger (on main process only):
if is_main_process(training_args.local_rank):
transformers.utils.logging.set_verbosity(transformers.logging.WARNING)
logger.info("Training/evaluation parameters %s", training_args)
run(model_args, data_args, training_args)
if __name__ == '__main__':
main()