2019-03-24 09:07:38 +00:00
|
|
|
import faiss
|
|
|
|
from enum import Enum, unique
|
|
|
|
|
|
|
|
|
|
|
|
@unique
|
2019-03-26 06:52:38 +00:00
|
|
|
class INDEXDEVICES(Enum):
|
2019-03-24 09:07:38 +00:00
|
|
|
CPU = 0
|
|
|
|
GPU = 1
|
|
|
|
MULTI_GPU = 2
|
|
|
|
|
|
|
|
|
|
|
|
def FactoryIndex(index_name="DefaultIndex"):
|
|
|
|
cls = globals()[index_name]
|
2019-03-26 06:52:38 +00:00
|
|
|
return cls # invoke __init__() by user
|
2019-03-24 09:07:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Index():
|
2019-03-27 02:56:15 +00:00
|
|
|
def build(self, d, vectors, vector_ids, DEVICE=INDEXDEVICES.CPU):
|
2019-03-24 09:07:38 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def increase(trained_index, vectors):
|
2019-03-27 03:34:32 +00:00
|
|
|
trained_index.add_with_ids(vectors. vector_ids)
|
2019-03-24 09:07:38 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def serialize(index):
|
|
|
|
writer = faiss.VectorIOWriter()
|
|
|
|
faiss.write_index(index, writer)
|
|
|
|
array_data = faiss.vector_to_array(writer.data)
|
|
|
|
return array_data
|
|
|
|
|
|
|
|
|
|
|
|
class DefaultIndex(Index):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# maybe need to specif parameters
|
|
|
|
pass
|
|
|
|
|
2019-03-27 02:56:15 +00:00
|
|
|
def build(self, d, vectors, vector_ids, DEVICE=INDEXDEVICES.CPU):
|
2019-03-27 03:11:45 +00:00
|
|
|
index = faiss.IndexFlatL2(d)
|
|
|
|
index2 = faiss.IndexIDMap(index)
|
|
|
|
index2.add_with_ids(vectors, vector_ids)
|
|
|
|
return index2
|
2019-03-24 09:07:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LowMemoryIndex(Index):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.__nlist = 100
|
|
|
|
self.__bytes_per_vector = 8
|
|
|
|
self.__bits_per_sub_vector = 8
|
|
|
|
|
2019-03-27 02:56:15 +00:00
|
|
|
def build(d, vectors, vector_ids, DEVICE=INDEXDEVICES.CPU):
|
2019-03-24 09:07:38 +00:00
|
|
|
# quantizer = faiss.IndexFlatL2(d)
|
|
|
|
# index = faiss.IndexIVFPQ(quantizer, d, self.nlist,
|
|
|
|
# self.__bytes_per_vector, self.__bits_per_sub_vector)
|
|
|
|
# return index
|
2019-03-26 06:52:38 +00:00
|
|
|
pass
|