forked from PaddlePaddle/PaddleNLP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfaiss_indexer.py
216 lines (176 loc) Β· 8.06 KB
/
faiss_indexer.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
#!/usr/bin/env python3
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""
FAISS-based index components for dense retriever
"""
import logging
import os
import pickle
import time
from typing import Iterator, List, Tuple
import faiss
import numpy as np
logger = logging.getLogger()
class DenseIndexer(object):
"""
Class for building, saving, and finding indexes
"""
def __init__(self, buffer_size: int = 50000):
self.buffer_size = buffer_size
self.index_id_to_db_id = []
self.index = None
def index_data(self, vector_files: List[str]):
start_time = time.time()
buffer = []
for i, item in enumerate(iterate_encoded_files(vector_files)):
db_id, doc_vector = item
buffer.append((db_id, doc_vector))
if 0 < self.buffer_size == len(buffer):
# indexing in batches is beneficial for many faiss index types
self._index_batch(buffer)
logger.info(
"data indexed %d, used_time: %f sec.", len(self.index_id_to_db_id), time.time() - start_time
)
buffer = []
self._index_batch(buffer)
indexed_cnt = len(self.index_id_to_db_id)
logger.info("Total data indexed %d", indexed_cnt)
logger.info("Data indexing completed.")
def _index_batch(self, data: List[Tuple[object, np.array]]):
raise NotImplementedError
def search_knn(self, query_vectors: np.array, top_docs: int) -> List[Tuple[List[object], List[float]]]:
raise NotImplementedError
def serialize(self, file: str):
logger.info("Serializing index to %s", file)
if os.path.isdir(file):
index_file = os.path.join(file, "index.dpr")
meta_file = os.path.join(file, "index_meta.dpr")
else:
index_file = file + ".index.dpr"
meta_file = file + ".index_meta.dpr"
faiss.write_index(self.index, index_file)
with open(meta_file, mode="wb") as f:
pickle.dump(self.index_id_to_db_id, f)
def deserialize_from(self, file: str):
logger.info("Loading index from %s", file)
if os.path.isdir(file):
index_file = os.path.join(file, "index.dpr")
meta_file = os.path.join(file, "index_meta.dpr")
else:
index_file = file + ".index.dpr"
meta_file = file + ".index_meta.dpr"
self.index = faiss.read_index(index_file)
logger.info("Loaded index of type %s and size %d", type(self.index), self.index.ntotal)
with open(meta_file, "rb") as reader:
self.index_id_to_db_id = pickle.load(reader)
assert (
len(self.index_id_to_db_id) == self.index.ntotal
), "Deserialized index_id_to_db_id should match faiss index size"
def _update_id_mapping(self, db_ids: List):
self.index_id_to_db_id.extend(db_ids)
class DenseFlatIndexer(DenseIndexer):
def __init__(self, vector_sz: int, buffer_size: int = 50000):
super(DenseFlatIndexer, self).__init__(buffer_size=buffer_size)
self.index = faiss.IndexFlatIP(vector_sz)
def _index_batch(self, data: List[Tuple[object, np.array]]):
db_ids = [t[0] for t in data]
vectors = [np.reshape(t[1], (1, -1)) for t in data]
vectors = np.concatenate(vectors, axis=0)
self._update_id_mapping(db_ids)
self.index.add(vectors)
def search_knn(self, query_vectors: np.array, top_docs: int) -> List[Tuple[List[object], List[float]]]:
scores, indexes = self.index.search(query_vectors, top_docs)
# convert to external ids
db_ids = [[self.index_id_to_db_id[i] for i in query_top_idxs] for query_top_idxs in indexes]
result = [(db_ids[i], scores[i]) for i in range(len(db_ids))]
return result
class DenseHNSWFlatIndexer(DenseIndexer):
"""
Efficient index for retrieval. Note: default settings are for hugh accuracy but also high RAM usage
"""
def __init__(
self,
vector_sz: int,
buffer_size: int = 50000,
store_n: int = 512,
ef_search: int = 128,
ef_construction: int = 200,
):
super(DenseHNSWFlatIndexer, self).__init__(buffer_size=buffer_size)
# IndexHNSWFlat supports L2 similarity only
# so we have to apply DOT -> L2 similarity space conversion with the help of an extra dimension
index = faiss.IndexHNSWFlat(vector_sz + 1, store_n)
index.hnsw.efSearch = ef_search
index.hnsw.efConstruction = ef_construction
self.index = index
self.phi = None
def index_data(self, vector_files: List[str]):
self._set_phi(vector_files)
super(DenseHNSWFlatIndexer, self).index_data(vector_files)
def _set_phi(self, vector_files: List[str]):
"""
Calculates the max norm from the whole data and assign it to self.phi: necessary to transform IP -> L2 space
:param vector_files: file names to get passages vectors from
:return:
"""
phi = 0
for i, item in enumerate(iterate_encoded_files(vector_files)):
id, doc_vector = item
norms = (doc_vector**2).sum()
phi = max(phi, norms)
logger.info("HNSWF DotProduct -> L2 space phi={}".format(phi))
self.phi = phi
def _index_batch(self, data: List[Tuple[object, np.array]]):
# max norm is required before putting all vectors in the index to convert inner product similarity to L2
if self.phi is None:
raise RuntimeError(
"Max norm needs to be calculated from all data at once,"
"results will be unpredictable otherwise."
"Run `_set_phi()` before calling this method."
)
db_ids = [t[0] for t in data]
vectors = [np.reshape(t[1], (1, -1)) for t in data]
norms = [(doc_vector**2).sum() for doc_vector in vectors]
aux_dims = [np.sqrt(self.phi - norm) for norm in norms]
hnsw_vectors = [np.hstack((doc_vector, aux_dims[i].reshape(-1, 1))) for i, doc_vector in enumerate(vectors)]
hnsw_vectors = np.concatenate(hnsw_vectors, axis=0)
self._update_id_mapping(db_ids)
self.index.add(hnsw_vectors)
def search_knn(self, query_vectors: np.array, top_docs: int) -> List[Tuple[List[object], List[float]]]:
aux_dim = np.zeros(len(query_vectors), dtype="float32")
query_nhsw_vectors = np.hstack((query_vectors, aux_dim.reshape(-1, 1)))
logger.info("query_hnsw_vectors %s", query_nhsw_vectors.shape)
scores, indexes = self.index.search(query_nhsw_vectors, top_docs)
# convert to external ids
db_ids = [[self.index_id_to_db_id[i] for i in query_top_idxs] for query_top_idxs in indexes]
result = [(db_ids[i], scores[i]) for i in range(len(db_ids))]
return result
def deserialize_from(self, file: str):
super(DenseHNSWFlatIndexer, self).deserialize_from(file)
# to trigger warning on subsequent indexing
self.phi = None
def iterate_encoded_files(vector_files: list) -> Iterator[Tuple[object, np.array]]:
for i, file in enumerate(vector_files):
logger.info("Reading file %s", file)
with open(file, "rb") as reader:
doc_vectors = pickle.load(reader)
for doc in doc_vectors:
db_id, doc_vector = doc
yield db_id, doc_vector