-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrun.py
259 lines (231 loc) · 12.7 KB
/
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
import logging
import ir_datasets
from pyserini.search.lucene import LuceneSearcher
from pyserini.search._base import get_topics
from llmrankers.rankers import SearchResult
from llmrankers.pointwise import PointwiseLlmRanker, MonoT5LlmRanker
from llmrankers.setwise import SetwiseLlmRanker, OpenAiSetwiseLlmRanker
from llmrankers.pairwise import PairwiseLlmRanker, DuoT5LlmRanker, OpenAiPairwiseLlmRanker
from llmrankers.listwise import OpenAiListwiseLlmRanker, ListwiseLlmRanker
from tqdm import tqdm
import argparse
import sys
import json
import time
import random
random.seed(929)
logger = logging.getLogger(__name__)
def parse_args(parser, commands):
# Divide argv by commands
split_argv = [[]]
for c in sys.argv[1:]:
if c in commands.choices:
split_argv.append([c])
else:
split_argv[-1].append(c)
# Initialize namespace
args = argparse.Namespace()
for c in commands.choices:
setattr(args, c, None)
# Parse each command
parser.parse_args(split_argv[0], namespace=args) # Without command
for argv in split_argv[1:]: # Commands
n = argparse.Namespace()
setattr(args, argv[0], n)
parser.parse_args(argv, namespace=n)
return args
def write_run_file(path, results, tag):
with open(path, 'w') as f:
for qid, _, ranking in results:
rank = 1
for doc in ranking:
docid = doc.docid
score = doc.score
f.write(f"{qid}\tQ0\t{docid}\t{rank}\t{score}\t{tag}\n")
rank += 1
def main(args):
if args.pointwise:
if 'monot5' in args.run.model_name_or_path:
ranker = MonoT5LlmRanker(model_name_or_path=args.run.model_name_or_path,
tokenizer_name_or_path=args.run.tokenizer_name_or_path,
device=args.run.device,
cache_dir=args.run.cache_dir,
method=args.pointwise.method,
batch_size=args.pointwise.batch_size)
else:
ranker = PointwiseLlmRanker(model_name_or_path=args.run.model_name_or_path,
tokenizer_name_or_path=args.run.tokenizer_name_or_path,
device=args.run.device,
cache_dir=args.run.cache_dir,
method=args.pointwise.method,
batch_size=args.pointwise.batch_size)
elif args.setwise:
if args.run.openai_key:
ranker = OpenAiSetwiseLlmRanker(model_name_or_path=args.run.model_name_or_path,
api_key=args.run.openai_key,
num_child=args.setwise.num_child,
method=args.setwise.method,
k=args.setwise.k)
else:
ranker = SetwiseLlmRanker(model_name_or_path=args.run.model_name_or_path,
tokenizer_name_or_path=args.run.tokenizer_name_or_path,
device=args.run.device,
cache_dir=args.run.cache_dir,
num_child=args.setwise.num_child,
scoring=args.run.scoring,
method=args.setwise.method,
num_permutation=args.setwise.num_permutation,
k=args.setwise.k)
elif args.pairwise:
if args.pairwise.method != 'allpair':
args.pairwise.batch_size = 2
logger.info(f'Setting batch_size to 2.')
if args.run.openai_key:
ranker = OpenAiPairwiseLlmRanker(model_name_or_path=args.run.model_name_or_path,
api_key=args.run.openai_key,
method=args.pairwise.method,
k=args.pairwise.k)
elif 'duot5' in args.run.model_name_or_path:
ranker = DuoT5LlmRanker(model_name_or_path=args.run.model_name_or_path,
tokenizer_name_or_path=args.run.tokenizer_name_or_path,
device=args.run.device,
cache_dir=args.run.cache_dir,
method=args.pairwise.method,
batch_size=args.pairwise.batch_size,
k=args.pairwise.k)
else:
ranker = PairwiseLlmRanker(model_name_or_path=args.run.model_name_or_path,
tokenizer_name_or_path=args.run.tokenizer_name_or_path,
device=args.run.device,
cache_dir=args.run.cache_dir,
method=args.pairwise.method,
batch_size=args.pairwise.batch_size,
k=args.pairwise.k)
elif args.listwise:
if args.run.openai_key:
ranker = OpenAiListwiseLlmRanker(model_name_or_path=args.run.model_name_or_path,
api_key=args.run.openai_key,
window_size=args.listwise.window_size,
step_size=args.listwise.step_size,
num_repeat=args.listwise.num_repeat)
else:
ranker = ListwiseLlmRanker(model_name_or_path=args.run.model_name_or_path,
tokenizer_name_or_path=args.run.tokenizer_name_or_path,
device=args.run.device,
cache_dir=args.run.cache_dir,
window_size=args.listwise.window_size,
step_size=args.listwise.step_size,
scoring=args.run.scoring,
num_repeat=args.listwise.num_repeat)
else:
raise ValueError('Must specify either --pointwise, --setwise, --pairwise or --listwise.')
query_map = {}
if args.run.ir_dataset_name is not None:
dataset = ir_datasets.load(args.run.ir_dataset_name)
for query in dataset.queries_iter():
qid = query.query_id
text = query.text
query_map[qid] = ranker.truncate(text, args.run.query_length)
dataset = ir_datasets.load(args.run.ir_dataset_name)
docstore = dataset.docs_store()
else:
topics = get_topics(args.run.pyserini_index+'-test')
for topic_id in list(topics.keys()):
text = topics[topic_id]['title']
query_map[str(topic_id)] = ranker.truncate(text, args.run.query_length)
docstore = LuceneSearcher.from_prebuilt_index(args.run.pyserini_index+'.flat')
logger.info(f'Loading first stage run from {args.run.run_path}.')
first_stage_rankings = []
with open(args.run.run_path, 'r') as f:
current_qid = None
current_ranking = []
for line in tqdm(f):
qid, _, docid, _, score, _ = line.strip().split()
if qid != current_qid:
if current_qid is not None:
first_stage_rankings.append((current_qid, query_map[current_qid], current_ranking[:args.run.hits]))
current_ranking = []
current_qid = qid
if len(current_ranking) >= args.run.hits:
continue
if args.run.ir_dataset_name is not None:
text = docstore.get(docid).text
if 'title' in dir(docstore.get(docid)):
text = f'{docstore.get(docid).title} {text}'
else:
data = json.loads(docstore.doc(docid).raw())
text = data['text']
if 'title' in data:
text = f'{data["title"]} {text}'
text = ranker.truncate(text, args.run.passage_length)
current_ranking.append(SearchResult(docid=docid, score=float(score), text=text))
first_stage_rankings.append((current_qid, query_map[current_qid], current_ranking[:args.run.hits]))
reranked_results = []
total_comparisons = 0
total_prompt_tokens = 0
total_completion_tokens = 0
tic = time.time()
for qid, query, ranking in tqdm(first_stage_rankings):
if args.run.shuffle_ranking is not None:
if args.run.shuffle_ranking == 'random':
random.shuffle(ranking)
elif args.run.shuffle_ranking == 'inverse':
ranking = ranking[::-1]
else:
raise ValueError(f'Invalid shuffle ranking method: {args.run.shuffle_ranking}.')
reranked_results.append((qid, query, ranker.rerank(query, ranking)))
total_comparisons += ranker.total_compare
total_prompt_tokens += ranker.total_prompt_tokens
total_completion_tokens += ranker.total_completion_tokens
toc = time.time()
print(f'Avg comparisons: {total_comparisons/len(reranked_results)}')
print(f'Avg prompt tokens: {total_prompt_tokens/len(reranked_results)}')
print(f'Avg completion tokens: {total_completion_tokens/len(reranked_results)}')
print(f'Avg time per query: {(toc-tic)/len(reranked_results)}')
write_run_file(args.run.save_path, reranked_results, 'LLMRankers')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
commands = parser.add_subparsers(title='sub-commands')
run_parser = commands.add_parser('run')
run_parser.add_argument('--run_path', type=str, help='Path to the first stage run file (TREC format) to rerank.')
run_parser.add_argument('--save_path', type=str, help='Path to save the reranked run file (TREC format).')
run_parser.add_argument('--model_name_or_path', type=str,
help='Path to the pretrained model or model identifier from huggingface.co/models')
run_parser.add_argument('--tokenizer_name_or_path', type=str, default=None,
help='Path to the pretrained tokenizer or tokenizer identifier from huggingface.co/tokenizers')
run_parser.add_argument('--ir_dataset_name', type=str, default=None)
run_parser.add_argument('--pyserini_index', type=str, default=None)
run_parser.add_argument('--hits', type=int, default=100)
run_parser.add_argument('--query_length', type=int, default=128)
run_parser.add_argument('--passage_length', type=int, default=128)
run_parser.add_argument('--device', type=str, default='cuda')
run_parser.add_argument('--cache_dir', type=str, default=None)
run_parser.add_argument('--openai_key', type=str, default=None)
run_parser.add_argument('--scoring', type=str, default='generation', choices=['generation', 'likelihood'])
run_parser.add_argument('--shuffle_ranking', type=str, default=None, choices=['inverse', 'random'])
pointwise_parser = commands.add_parser('pointwise')
pointwise_parser.add_argument('--method', type=str, default='yes_no',
choices=['qlm', 'yes_no'])
pointwise_parser.add_argument('--batch_size', type=int, default=2)
pairwise_parser = commands.add_parser('pairwise')
pairwise_parser.add_argument('--method', type=str, default='allpair',
choices=['allpair', 'heapsort', 'bubblesort'])
pairwise_parser.add_argument('--batch_size', type=int, default=2)
pairwise_parser.add_argument('--k', type=int, default=10)
setwise_parser = commands.add_parser('setwise')
setwise_parser.add_argument('--num_child', type=int, default=3)
setwise_parser.add_argument('--method', type=str, default='heapsort',
choices=['heapsort', 'bubblesort'])
setwise_parser.add_argument('--k', type=int, default=10)
setwise_parser.add_argument('--num_permutation', type=int, default=1)
listwise_parser = commands.add_parser('listwise')
listwise_parser.add_argument('--window_size', type=int, default=3)
listwise_parser.add_argument('--step_size', type=int, default=1)
listwise_parser.add_argument('--num_repeat', type=int, default=1)
args = parse_args(parser, commands)
if args.run.ir_dataset_name is not None and args.run.pyserini_index is not None:
raise ValueError('Must specify either --ir_dataset_name or --pyserini_index, not both.')
arg_dict = vars(args)
if arg_dict['run'] is None or sum(arg_dict[arg] is not None for arg in arg_dict) != 2:
raise ValueError('Need to set --run and can only set one of --pointwise, --pairwise, --setwise, --listwise')
main(args)