2019-05-28 22:17:22 +08:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
2018-12-20 00:48:35 +08:00
|
|
|
#
|
2019-05-28 22:17:22 +08:00
|
|
|
# This source code is licensed under the MIT license found in the
|
2018-12-20 00:48:35 +08:00
|
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
|
2021-12-08 05:11:15 +08:00
|
|
|
import os
|
2018-12-20 00:48:35 +08:00
|
|
|
import numpy as np
|
|
|
|
import faiss
|
|
|
|
import unittest
|
|
|
|
|
2021-05-10 13:29:28 +08:00
|
|
|
from common_faiss_tests import Randu10k
|
2018-12-20 00:48:35 +08:00
|
|
|
|
|
|
|
ru = Randu10k()
|
|
|
|
|
|
|
|
xb = ru.xb
|
|
|
|
xt = ru.xt
|
|
|
|
xq = ru.xq
|
|
|
|
nb, d = xb.shape
|
|
|
|
nq, d = xq.shape
|
|
|
|
|
|
|
|
|
|
|
|
class IDRemap(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_id_remap_idmap(self):
|
|
|
|
# reference: index without remapping
|
|
|
|
|
|
|
|
index = faiss.IndexPQ(d, 8, 8)
|
|
|
|
k = 10
|
|
|
|
index.train(xt)
|
|
|
|
index.add(xb)
|
|
|
|
_Dref, Iref = index.search(xq, k)
|
|
|
|
|
|
|
|
# try a remapping
|
2020-08-28 03:39:41 +08:00
|
|
|
ids = np.arange(nb)[::-1].copy().astype('int64')
|
2018-12-20 00:48:35 +08:00
|
|
|
|
|
|
|
sub_index = faiss.IndexPQ(d, 8, 8)
|
|
|
|
index2 = faiss.IndexIDMap(sub_index)
|
|
|
|
|
|
|
|
index2.train(xt)
|
|
|
|
index2.add_with_ids(xb, ids)
|
|
|
|
|
|
|
|
_D, I = index2.search(xq, k)
|
|
|
|
|
|
|
|
assert np.all(I == nb - 1 - Iref)
|
|
|
|
|
|
|
|
def test_id_remap_ivf(self):
|
|
|
|
# coarse quantizer in common
|
|
|
|
coarse_quantizer = faiss.IndexFlatIP(d)
|
|
|
|
ncentroids = 25
|
|
|
|
|
|
|
|
# reference: index without remapping
|
|
|
|
|
|
|
|
index = faiss.IndexIVFPQ(coarse_quantizer, d,
|
|
|
|
ncentroids, 8, 8)
|
|
|
|
index.nprobe = 5
|
|
|
|
k = 10
|
|
|
|
index.train(xt)
|
|
|
|
index.add(xb)
|
|
|
|
_Dref, Iref = index.search(xq, k)
|
|
|
|
|
|
|
|
# try a remapping
|
2020-08-28 03:39:41 +08:00
|
|
|
ids = np.arange(nb)[::-1].copy().astype('int64')
|
2018-12-20 00:48:35 +08:00
|
|
|
|
|
|
|
index2 = faiss.IndexIVFPQ(coarse_quantizer, d,
|
|
|
|
ncentroids, 8, 8)
|
|
|
|
index2.nprobe = 5
|
|
|
|
|
|
|
|
index2.train(xt)
|
|
|
|
index2.add_with_ids(xb, ids)
|
|
|
|
|
|
|
|
_D, I = index2.search(xq, k)
|
|
|
|
assert np.all(I == nb - 1 - Iref)
|
|
|
|
|
|
|
|
|
|
|
|
class Shards(unittest.TestCase):
|
|
|
|
|
2021-12-08 05:11:15 +08:00
|
|
|
@unittest.skipIf(os.name == "posix" and os.uname().sysname == "Darwin",
|
|
|
|
"There is a bug in the OpenMP implementation on OSX.")
|
2018-12-20 00:48:35 +08:00
|
|
|
def test_shards(self):
|
|
|
|
k = 32
|
|
|
|
ref_index = faiss.IndexFlatL2(d)
|
|
|
|
|
|
|
|
print('ref search')
|
|
|
|
ref_index.add(xb)
|
|
|
|
_Dref, Iref = ref_index.search(xq, k)
|
|
|
|
print(Iref[:5, :6])
|
|
|
|
|
|
|
|
shard_index = faiss.IndexShards(d)
|
|
|
|
shard_index_2 = faiss.IndexShards(d, True, False)
|
|
|
|
|
|
|
|
ni = 3
|
|
|
|
for i in range(ni):
|
|
|
|
i0 = int(i * nb / ni)
|
|
|
|
i1 = int((i + 1) * nb / ni)
|
|
|
|
index = faiss.IndexFlatL2(d)
|
|
|
|
index.add(xb[i0:i1])
|
|
|
|
shard_index.add_shard(index)
|
|
|
|
|
|
|
|
index_2 = faiss.IndexFlatL2(d)
|
|
|
|
irm = faiss.IndexIDMap(index_2)
|
|
|
|
shard_index_2.add_shard(irm)
|
|
|
|
|
|
|
|
# test parallel add
|
|
|
|
shard_index_2.verbose = True
|
|
|
|
shard_index_2.add(xb)
|
|
|
|
|
|
|
|
for test_no in range(3):
|
|
|
|
with_threads = test_no == 1
|
|
|
|
|
|
|
|
print('shard search test_no = %d' % test_no)
|
|
|
|
if with_threads:
|
|
|
|
remember_nt = faiss.omp_get_max_threads()
|
|
|
|
faiss.omp_set_num_threads(1)
|
|
|
|
shard_index.threaded = True
|
|
|
|
else:
|
|
|
|
shard_index.threaded = False
|
|
|
|
|
|
|
|
if test_no != 2:
|
|
|
|
_D, I = shard_index.search(xq, k)
|
|
|
|
else:
|
|
|
|
_D, I = shard_index_2.search(xq, k)
|
|
|
|
|
|
|
|
print(I[:5, :6])
|
|
|
|
|
|
|
|
if with_threads:
|
|
|
|
faiss.omp_set_num_threads(remember_nt)
|
|
|
|
|
|
|
|
ndiff = (I != Iref).sum()
|
|
|
|
|
|
|
|
print('%d / %d differences' % (ndiff, nq * k))
|
2022-09-23 22:19:21 +08:00
|
|
|
assert (ndiff < nq * k / 1000.)
|
2018-12-20 00:48:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|