-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_reranker.py
160 lines (131 loc) · 5.36 KB
/
run_reranker.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
# Copyright 2021 Reranker Author. All rights reserved.
# Code structure inspired by HuggingFace run_glue.py in the transformers library.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import logging
import os
from modeling import Reranker, RerankerDC
from trainer import RerankerTrainer, RerankerDCTrainer
from data import GroupedTrainDataset, PredictionDataset, GroupCollator
from arguments import ModelArguments, DataTrainingArguments, \
RerankerTrainingArguments as TrainingArguments
from transformers import AutoConfig, AutoTokenizer
from transformers import (
HfArgumentParser,
set_seed,
)
logger = logging.getLogger(__name__)
def main():
parser = HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
model_args, data_args, training_args = parser.parse_args_into_dataclasses()
model_args: ModelArguments
data_args: DataTrainingArguments
training_args: TrainingArguments
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 training_args.local_rank in [-1, 0] else logging.WARN,
)
logger.warning(
"Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s",
training_args.local_rank,
training_args.device,
training_args.n_gpu,
bool(training_args.local_rank != -1),
training_args.fp16,
)
logger.info("Training/evaluation parameters %s", training_args)
logger.info("Model parameters %s", model_args)
logger.info("Data parameters %s", data_args)
# Set seed
set_seed(training_args.seed)
num_labels = 1
config = AutoConfig.from_pretrained(
model_args.config_name if model_args.config_name else model_args.model_name_or_path,
num_labels=num_labels,
cache_dir=model_args.cache_dir,
)
tokenizer = AutoTokenizer.from_pretrained(
model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path,
cache_dir=model_args.cache_dir,
use_fast=False,
)
_model_class = RerankerDC if training_args.distance_cache else Reranker
model = _model_class.from_pretrained(
model_args, data_args, training_args,
model_args.model_name_or_path,
from_tf=bool(".ckpt" in model_args.model_name_or_path),
config=config,
cache_dir=model_args.cache_dir,
)
# Get datasets
if training_args.do_train:
train_dataset = GroupedTrainDataset(
data_args, data_args.train_path, tokenizer=tokenizer, train_args=training_args, cache_dir=model_args.cache_dir
)
else:
train_dataset = None
# Initialize our Trainer
_trainer_class = RerankerDCTrainer if training_args.distance_cache else RerankerTrainer
trainer = _trainer_class(
model=model,
args=training_args,
train_dataset=train_dataset,
data_collator=GroupCollator(tokenizer),
)
# Training
if training_args.do_train:
trainer.train(
model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None
)
trainer.save_model(os.path.join(training_args.output_dir, f"checkpoint-final"))
if trainer.is_world_process_zero():
tokenizer.save_pretrained(os.path.join(training_args.output_dir, f"checkpoint-final"))
if training_args.do_eval:
trainer.evaluate()
if training_args.do_predict:
logging.info("*** Prediction ***")
if os.path.exists(data_args.rank_score_path):
if os.path.isfile(data_args.rank_score_path):
raise FileExistsError(f'score file {data_args.rank_score_path} already exists')
else:
raise ValueError(f'Should specify a file name')
else:
score_dir = os.path.split(data_args.rank_score_path)[0]
if not os.path.exists(score_dir):
logger.info(f'Creating score directory {score_dir}')
os.makedirs(score_dir)
test_dataset = PredictionDataset(
data_args.pred_path, tokenizer=tokenizer,
max_len=data_args.max_len,
)
assert data_args.pred_id_file is not None
pred_qids = []
pred_pids = []
with open(data_args.pred_id_file) as f:
for l in f:
q, p = l.split()
pred_qids.append(q)
pred_pids.append(p)
pred_scores = trainer.predict(test_dataset=test_dataset).predictions
if trainer.is_world_process_zero():
assert len(pred_qids) == len(pred_scores)
with open(data_args.rank_score_path, "w") as writer:
for qid, pid, score in zip(pred_qids, pred_pids, pred_scores):
writer.write(f'{qid}\t{pid}\t{score[0]}\n')
def _mp_fn(index):
# For xla_spawn (TPUs)
main()
if __name__ == "__main__":
main()