-
Notifications
You must be signed in to change notification settings - Fork 3
/
pretrain.py
362 lines (313 loc) · 13.6 KB
/
pretrain.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import logging
import os
from dataclasses import dataclass, field
from typing import Optional
import torch
import collections
import random
import utils
from datasets import Dataset
from multiprocessing import Pool
from pathlib import Path
from tqdm import tqdm
from itertools import combinations
import transformers
from transformers import (
CONFIG_MAPPING,
MODEL_FOR_MASKED_LM_MAPPING,
AutoConfig,
AutoTokenizer,
HfArgumentParser,
TrainingArguments,
set_seed
)
from transformers.trainer_utils import is_main_process
from uctopic.models import UCTopicModel
from trainers import CLTrainer
from collator import OurDataCollatorWithPadding
logger = logging.getLogger(__name__)
MODEL_CONFIG_CLASSES = list(MODEL_FOR_MASKED_LM_MAPPING.keys())
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)
@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.
"""
# Huggingface's original arguments
model_name_or_path: Optional[str] = field(
default=None,
metadata={
"help": "The model checkpoint for weights initialization."
"Don't set if you want to train a model from scratch."
},
)
model_type: Optional[str] = field(
default=None,
metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(MODEL_TYPES)},
)
config_name: Optional[str] = field(
default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
)
tokenizer_name: Optional[str] = field(
default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
)
cache_dir: Optional[str] = field(
default=None,
metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},
)
use_fast_tokenizer: bool = field(
default=True,
metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."},
)
model_revision: str = field(
default="main",
metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},
)
use_auth_token: bool = field(
default=False,
metadata={
"help": "Will use the token generated when running `transformers-cli login` (necessary to use this script "
"with private models)."
},
)
# UCTopic's arguments
temp: float = field(
default=0.05,
metadata={
"help": "Temperature for softmax."
}
)
do_mlm: bool = field(
default=False,
metadata={
"help": "Whether to use MLM auxiliary objective."
}
)
mlm_weight: float = field(
default=0.5,
metadata={
"help": "Weight for MLM auxiliary objective (only effective if --do_mlm)."
}
)
mlp_only_train: bool = field(
default=False,
metadata={
"help": "Use MLP only during training"
}
)
@dataclass
class DataTrainingArguments:
"""
Arguments pertaining to what data we are going to input our model for training and eval.
"""
# Huggingface's original arguments.
dataset_name: Optional[str] = field(
default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
)
dataset_config_name: Optional[str] = field(
default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}
)
overwrite_cache: bool = field(
default=False, metadata={"help": "Overwrite the cached training and evaluation sets"}
)
validation_split_percentage: Optional[int] = field(
default=5,
metadata={
"help": "The percentage of the train set used as validation set in case there's no validation split"
},
)
preprocessing_num_workers: Optional[int] = field(
default=None,
metadata={"help": "The number of processes to use for the preprocessing."},
)
# UCTopic's arguments
train_file: Optional[str] = field(
default=None,
metadata={"help": "The training data file (.txt or .csv)."}
)
max_seq_length: Optional[int] = field(
default=128,
metadata={
"help": "The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated."
},
)
pad_to_max_length: bool = field(
default=False,
metadata={
"help": "Whether to pad all samples to `max_seq_length`. "
"If False, will pad the samples dynamically when batching to the maximum length in the batch."
},
)
mlm_probability: float = field(
default=0.15,
metadata={"help": "Ratio of tokens to mask for MLM (only effective if --do_mlm)"}
)
def __post_init__(self):
if self.dataset_name is None and self.train_file is None and self.validation_file is None:
raise ValueError("Need either a dataset name or a training/validation file.")
else:
if self.train_file is not None:
extension = self.train_file.split(".")[-1]
assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file."
tokenizer_glb = ''
def _par_tokenize_doc(doc):
sentence = doc['text']
entities = doc['selected_entities']
entity_spans = [(entity[2], entity[3]) for entity in entities]
entity_ids = [entity[1] for entity in entities]
tokenized_sent = tokenizer_glb(sentence,
entity_spans=entity_spans,
add_prefix_space=True)
return dict(tokenized_sent), entity_ids
def _par_sample_pairs(entity_list):
return list(combinations(entity_list, 2))
def main():
# See all possible arguments in src/transformers/training_args.py
# or by passing the --help flag to this script.
# We now keep distinct sets of args, for a cleaner separation of concerns.
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
if (
os.path.exists(training_args.output_dir)
and os.listdir(training_args.output_dir)
and training_args.do_train
and not training_args.overwrite_output_dir
):
raise ValueError(
f"Output directory ({training_args.output_dir}) already exists and is not empty."
"Use --overwrite_output_dir to overcome."
)
# Setup logging
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=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 info of the Transformers logger (on main process only):
if is_main_process(training_args.local_rank):
transformers.utils.logging.set_verbosity_info()
transformers.utils.logging.enable_default_handler()
transformers.utils.logging.enable_explicit_format()
logger.info("Training/evaluation parameters %s", training_args)
# Set seed before initializing model.
set_seed(training_args.seed)
config_kwargs = {
"cache_dir": model_args.cache_dir,
"revision": model_args.model_revision,
"use_auth_token": True if model_args.use_auth_token else None,
}
if model_args.config_name:
config = AutoConfig.from_pretrained(model_args.config_name, **config_kwargs)
elif model_args.model_name_or_path:
config = AutoConfig.from_pretrained(model_args.model_name_or_path, **config_kwargs)
else:
config = CONFIG_MAPPING[model_args.model_type]()
logger.warning("You are instantiating a new config instance from scratch.")
tokenizer_kwargs = {
"cache_dir": model_args.cache_dir,
"use_fast": model_args.use_fast_tokenizer,
"revision": model_args.model_revision,
"use_auth_token": True if model_args.use_auth_token else None,
}
if model_args.tokenizer_name:
tokenizer = AutoTokenizer.from_pretrained(model_args.tokenizer_name, **tokenizer_kwargs)
elif model_args.model_name_or_path:
tokenizer = AutoTokenizer.from_pretrained(model_args.model_name_or_path, **tokenizer_kwargs)
else:
raise ValueError(
"You are instantiating a new tokenizer from scratch. This is not supported by this script."
"You can do it from another script, save it, and load it from here, using --tokenizer_name."
)
global tokenizer_glb
tokenizer_glb = tokenizer
# preprocess corpus
path_corpus = Path(data_args.train_file)
dir_corpus = path_corpus.parent
dir_preprocess = dir_corpus / 'preprocess'
dir_preprocess.mkdir(exist_ok=True)
path_tokenized_corpus = dir_preprocess / f'tokenized_{path_corpus.name}'
path_entity_pairs = dir_preprocess / f'entity_pairs_{path_corpus.name}'
if utils.IO.is_valid_file(path_tokenized_corpus) and utils.IO.is_valid_file(path_entity_pairs):
print(f'[Preprocessor] Use cache: {path_tokenized_corpus} and {path_entity_pairs}')
else:
docs = utils.JsonLine.load(path_corpus)
pool = Pool(processes=data_args.preprocessing_num_workers)
pool_func = pool.imap(func=_par_tokenize_doc, iterable=docs)
doc_tuples = list(tqdm(pool_func, total=len(docs), ncols=100, desc=f'[Tokenize] {path_corpus}'))
tokenized_corpus = [tokenized_sent for tokenized_sent, entity_ids in doc_tuples]
doc_entity_list = [entity_ids for tokenized_sent, entity_ids in doc_tuples]
pool.close()
pool.join()
entity_position_dict = collections.defaultdict(list)
for sent_idx, sent_entity_list in tqdm(enumerate(doc_entity_list), ncols=100, desc='Extract entity positions'):
for entity_idx, entity in enumerate(sent_entity_list):
entity_position_dict[entity].append((sent_idx, entity_idx))
pool = Pool(processes=data_args.preprocessing_num_workers)
pool_func = pool.imap(func=_par_sample_pairs, iterable=entity_position_dict.values())
pair_tuples = list(tqdm(pool_func, total=len(entity_position_dict), ncols=100, desc=f'Pairing....'))
entity_pairs = []
for sent_entity_pairs in pair_tuples:
entity_pairs += sent_entity_pairs
pool.close()
pool.join()
print(f'Toal number of pairs: {len(entity_pairs)}')
random.shuffle(entity_pairs)
utils.JsonLine.dump(tokenized_corpus, path_tokenized_corpus)
utils.JsonLine.dump(entity_pairs, path_entity_pairs)
tokenized_corpus = utils.JsonLine.load(path_tokenized_corpus, use_tqdm=True)
entity_pairs = utils.JsonLine.load(path_entity_pairs, use_tqdm=True)
print(f'Successfully load {len(tokenized_corpus)} sentences, and {len(entity_pairs)} entity_pairs.')
model = UCTopicModel(model_args, config)
train_dict = {'entity_pairs': entity_pairs[:-100000]}
dev_dict = {'entity_pairs': entity_pairs[-100000:]}
train_dataset = Dataset.from_dict(train_dict)
dev_dataset = Dataset.from_dict(dev_dict)
data_collator = OurDataCollatorWithPadding(tokenizer, tokenized_corpus, mlm_probability=data_args.mlm_probability)
trainer = CLTrainer(
model=model,
args=training_args,
train_dataset=train_dataset if training_args.do_train else None,
eval_dataset=dev_dataset if training_args.do_eval else None,
tokenizer=tokenizer,
data_collator=data_collator,
)
trainer.model_args = model_args
# Training
if training_args.do_train:
model_path = model_args.model_name_or_path \
if (model_args.model_name_or_path is not None and os.path.isdir(model_args.model_name_or_path)) else None
train_result = trainer.train(model_path=model_path)
trainer.save_model() # Saves the tokenizer too for easy upload
output_train_file = os.path.join(training_args.output_dir, "train_results.txt")
if trainer.is_world_process_zero():
with open(output_train_file, "w") as writer:
logger.info("***** Train results *****")
for key, value in sorted(train_result.metrics.items()):
logger.info(f" {key} = {value}")
writer.write(f"{key} = {value}\n")
# Need to save the state, since Trainer.save_model saves only the tokenizer with the model
trainer.state.save_to_json(os.path.join(training_args.output_dir, "trainer_state.json"))
# Evaluation
results = {}
if training_args.do_eval:
logger.info("*** Evaluate ***")
results = trainer.evaluate(eval_senteval_transfer=True)
output_eval_file = os.path.join(training_args.output_dir, "eval_results.txt")
if trainer.is_world_process_zero():
with open(output_eval_file, "w") as writer:
logger.info("***** Eval results *****")
for key, value in sorted(results.items()):
logger.info(f" {key} = {value}")
writer.write(f"{key} = {value}\n")
return results
def _mp_fn(index):
# For xla_spawn (TPUs)
main()
if __name__ == "__main__":
main()