-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
187 lines (167 loc) · 7.41 KB
/
inference.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
# coding=utf-8
import argparse
import subprocess
import sys
sys.path.append("./")
import faiss
import logging
import os
import numpy as np
import torch
from transformers import RobertaConfig
from tqdm import tqdm
from torch.utils.data.dataloader import DataLoader
from torch.utils.data.sampler import SequentialSampler
from model import RobertaDot
from dataset import (
TextTokenIdsCache, load_rel, SubsetSeqDataset, SequenceDataset,
single_get_collate_function
)
from retrieve_utils import (
construct_flatindex_from_embeddings,
index_retrieve, convert_index_to_gpu
)
logger = logging.Logger(__name__)
def init_logging():
handlers = [logging.StreamHandler()]
handlers.append(logging.FileHandler("inference.log", mode="w"))
logging.basicConfig(handlers=handlers, format="[%(asctime)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO)
#logging.info("COMMAND: %s" % " ".join(sys.argv))
def prediction(model, data_collator, args, test_dataset, embedding_memmap, ids_memmap, is_query):
os.makedirs(args.output_dir, exist_ok=True)
test_dataloader = DataLoader(
test_dataset,
sampler=SequentialSampler(test_dataset),
batch_size=args.eval_batch_size*args.n_gpu,
collate_fn=data_collator,
drop_last=False,
)
# multi-gpu eval
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)
batch_size = test_dataloader.batch_size
num_examples = len(test_dataloader.dataset)
logging.info("***** Running *****")
logging.info(" Num examples = %d", num_examples)
logging.info(" Batch size = %d", batch_size)
model.eval()
write_index = 0
for step, (inputs, ids) in enumerate(tqdm(test_dataloader)):
for k, v in inputs.items():
if isinstance(v, torch.Tensor):
inputs[k] = v.to(args.device)
with torch.no_grad():
logits = model(is_query=is_query, **inputs).detach().cpu().numpy()
write_size = len(logits)
assert write_size == len(ids)
embedding_memmap[write_index:write_index+write_size] = logits
ids_memmap[write_index:write_index+write_size] = ids
write_index += write_size
assert write_index == len(embedding_memmap) == len(ids_memmap)
def query_inference(model, args, embedding_size):
if os.path.exists(args.query_memmap_path):
print(f"{args.query_memmap_path} exists, skip inference")
return
query_collator = single_get_collate_function(args.max_query_length)
ids_cache=TextTokenIdsCache(data_dir=args.preprocess_dir, prefix=f"{args.mode}-query")
query_dataset = SequenceDataset(
ids_cache=ids_cache,
max_seq_length=args.max_query_length
)
logging.info("query_dataset")
logging.info(len(query_dataset))
logging.info("query_dataset1")
logging.info(query_dataset)
query_memmap = np.memmap(args.query_memmap_path,
dtype=np.float32, mode="w+", shape=(len(query_dataset), embedding_size))
queryids_memmap = np.memmap(args.queryids_memmap_path,
dtype=np.int32, mode="w+", shape=(len(query_dataset), ))
try:
prediction(model, query_collator, args,
query_dataset, query_memmap, queryids_memmap, is_query=True)
except:
subprocess.check_call(["rm", args.query_memmap_path])
subprocess.check_call(["rm", args.queryids_memmap_path])
raise
def doc_inference(model, args, embedding_size):
if os.path.exists(args.doc_memmap_path):
print(f"{args.doc_memmap_path} exists, skip inference")
return
doc_collator = single_get_collate_function(args.max_doc_length)
ids_cache = TextTokenIdsCache(data_dir=args.preprocess_dir, prefix="bio-passages")
subset=list(range(len(ids_cache)))
# logging.info("ids_cache")
# logging.info(ids_cache)
doc_dataset = SubsetSeqDataset(
subset=subset,
ids_cache=ids_cache,
max_seq_length=args.max_doc_length
)
assert not os.path.exists(args.doc_memmap_path)
doc_memmap = np.memmap(args.doc_memmap_path,
dtype=np.float32, mode="w+", shape=(len(doc_dataset), embedding_size))
docid_memmap = np.memmap(args.docid_memmap_path,
dtype=np.int32, mode="w+", shape=(len(doc_dataset), ))
try:
prediction(model, doc_collator, args,
doc_dataset, doc_memmap, docid_memmap, is_query=False
)
except:
subprocess.check_call(["rm", args.doc_memmap_path])
subprocess.check_call(["rm", args.docid_memmap_path])
raise
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--data_type", choices=["passage", 'doc'], type=str, required=True)
parser.add_argument("--max_query_length", type=int, default=32)
parser.add_argument("--max_doc_length", type=int, default=512)
parser.add_argument("--eval_batch_size", type=int, default=32)
parser.add_argument("--mode", type=str, choices=["bio-train", "dev", "test", "lead"], required=True)
parser.add_argument("--topk", type=int, default=100)
parser.add_argument("--no_cuda", action="store_true")
parser.add_argument("--faiss_gpus", type=int, default=None, nargs="+")
args = parser.parse_args()
args.device = torch.device(
"cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
args.n_gpu = torch.cuda.device_count()
args.preprocess_dir = f"./data/{args.data_type}/preprocess"
args.model_path = f"./data/{args.data_type}/trained_models/star"
args.output_dir = f"./data/{args.data_type}/evaluate/star"
args.query_memmap_path = os.path.join(args.output_dir, f"{args.mode}-query.memmap")
args.queryids_memmap_path = os.path.join(args.output_dir, f"{args.mode}-query-id.memmap")
args.output_rank_file = os.path.join(args.output_dir, f"{args.mode}.rank.tsv")
args.doc_memmap_path = os.path.join(args.output_dir, "passages.memmap")
args.docid_memmap_path = os.path.join(args.output_dir, "passages-id.memmap")
logger.info(args)
os.makedirs(args.output_dir, exist_ok=True)
init_logging()
config = RobertaConfig.from_pretrained(args.model_path, gradient_checkpointing=False)
model = RobertaDot.from_pretrained(args.model_path, config=config)
output_embedding_size = model.output_embedding_size
model = model.to(args.device)
query_inference(model, args, output_embedding_size)
doc_inference(model, args, output_embedding_size)
model = None
torch.cuda.empty_cache()
doc_embeddings = np.memmap(args.doc_memmap_path,
dtype=np.float32, mode="r")
doc_ids = np.memmap(args.docid_memmap_path,
dtype=np.int32, mode="r")
doc_embeddings = doc_embeddings.reshape(-1, output_embedding_size)
query_embeddings = np.memmap(args.query_memmap_path,
dtype=np.float32, mode="r")
query_embeddings = query_embeddings.reshape(-1, output_embedding_size)
query_ids = np.memmap(args.queryids_memmap_path,
dtype=np.int32, mode="r")
index = construct_flatindex_from_embeddings(doc_embeddings, doc_ids)
if args.faiss_gpus:
index = convert_index_to_gpu(index, args.faiss_gpus, False)
else:
faiss.omp_set_num_threads(32)
nearest_neighbors = index_retrieve(index, query_embeddings, args.topk, batch=32)
with open(args.output_rank_file, 'w') as outputfile:
for qid, neighbors in zip(query_ids, nearest_neighbors):
for idx, pid in enumerate(neighbors):
outputfile.write(f"{qid}\t{pid}\t{idx+1}\n")
if __name__ == "__main__":
main()