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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import absolute_import, division, print_function
import unittest
import faiss
import numpy as np
import os
import random
class TestIVFlib(unittest.TestCase):
def test_methods_exported(self):
methods = ['check_compatible_for_merge', 'extract_index_ivf',
'merge_into', 'search_centroid',
'search_and_return_centroids', 'get_invlist_range',
'set_invlist_range', 'search_with_parameters']
for method in methods:
assert callable(getattr(faiss, method, None))
def search_single_scan(index, xq, k, bs=128):
"""performs a search so that the inverted lists are accessed
sequentially by blocks of size bs"""
# handle pretransform
if isinstance(index, faiss.IndexPreTransform):
xq = index.apply_py(xq)
index = faiss.downcast_index(index.index)
# coarse assignment
nprobe = min(index.nprobe, index.nlist)
coarse_dis, assign = index.quantizer.search(xq, nprobe)
nlist = index.nlist
assign_buckets = assign // bs
nq = len(xq)
rh = faiss.ResultHeap(nq, k)
index.parallel_mode |= index.PARALLEL_MODE_NO_HEAP_INIT
for l0 in range(0, nlist, bs):
bucket_no = l0 // bs
skip_rows, skip_cols = np.where(assign_buckets != bucket_no)
sub_assign = assign.copy()
sub_assign[skip_rows, skip_cols] = -1
index.search_preassigned(
xq, k, sub_assign, coarse_dis,
D=rh.D, I=rh.I
)
rh.finalize()
return rh.D, rh.I
class TestSequentialScan(unittest.TestCase):
def test_sequential_scan(self):
d = 20
index = faiss.index_factory(d, 'IVF100,SQ8')
rs = np.random.RandomState(123)
xt = rs.rand(5000, d).astype('float32')
xb = rs.rand(10000, d).astype('float32')
index.train(xt)
index.add(xb)
k = 15
xq = rs.rand(200, d).astype('float32')
ref_D, ref_I = index.search(xq, k)
D, I = search_single_scan(index, xq, k, bs=10)
assert np.all(D == ref_D)
assert np.all(I == ref_I)
class TestSearchWithParameters(unittest.TestCase):
def test_search_with_parameters(self):
d = 20
index = faiss.index_factory(d, 'IVF100,SQ8')
rs = np.random.RandomState(123)
xt = rs.rand(5000, d).astype('float32')
xb = rs.rand(10000, d).astype('float32')
index.train(xt)
index.nprobe = 3
index.add(xb)
k = 15
xq = rs.rand(200, d).astype('float32')
stats = faiss.cvar.indexIVF_stats
stats.reset()
Dref, Iref = index.search(xq, k)
ref_ndis = stats.ndis
# make sure the nprobe used is the one from params not the one
# set in the index
index.nprobe = 1
params = faiss.IVFSearchParameters()
params.nprobe = 3
Dnew, Inew, stats2 = faiss.search_with_parameters(
index, xq, k, params, output_stats=True)
np.testing.assert_array_equal(Inew, Iref)
np.testing.assert_array_equal(Dnew, Dref)
self.assertEqual(stats2["ndis"], ref_ndis)
def test_range_search_with_parameters(self):
d = 20
index = faiss.index_factory(d, 'IVF100,SQ8')
rs = np.random.RandomState(123)
xt = rs.rand(5000, d).astype('float32')
xb = rs.rand(10000, d).astype('float32')
index.train(xt)
index.nprobe = 3
index.add(xb)
xq = rs.rand(200, d).astype('float32')
Dpre, _ = index.search(xq, 15)
radius = float(np.median(Dpre[:, -1]))
stats = faiss.cvar.indexIVF_stats
stats.reset()
Lref, Dref, Iref = index.range_search(xq, radius)
ref_ndis = stats.ndis
# make sure the nprobe used is the one from params not the one
# set in the index
index.nprobe = 1
params = faiss.IVFSearchParameters()
params.nprobe = 3
Lnew, Dnew, Inew, stats2 = faiss.range_search_with_parameters(
index, xq, radius, params, output_stats=True)
np.testing.assert_array_equal(Lnew, Lref)
np.testing.assert_array_equal(Inew, Iref)
np.testing.assert_array_equal(Dnew, Dref)
self.assertEqual(stats2["ndis"], ref_ndis)
class TestSmallData(unittest.TestCase):
"""Test in case of nprobe > nlist."""
def test_small_data(self):
d = 20
# nlist = (2^4)^2 = 256
index = faiss.index_factory(d, 'IMI2x4,Flat')
# When nprobe >= nlist, it is equivalent to an IndexFlat.
rs = np.random.RandomState(123)
xt = rs.rand(100, d).astype('float32')
xb = rs.rand(1000, d).astype('float32')
index.train(xt)
index.add(xb)
index.nprobe = 2048
k = 5
xq = rs.rand(10, d).astype('float32')
# test kNN search
D, I = index.search(xq, k)
ref_D, ref_I = faiss.knn(xq, xb, k)
assert np.all(D == ref_D)
assert np.all(I == ref_I)
# test range search
thresh = 0.1 # *squared* distance
lims, D, I = index.range_search(xq, thresh)
ref_index = faiss.IndexFlat(d)
ref_index.add(xb)
ref_lims, ref_D, ref_I = ref_index.range_search(xq, thresh)
assert np.all(lims == ref_lims)
assert np.all(D == ref_D)
assert np.all(I == ref_I)
class TestIvfSharding(unittest.TestCase):
d = 32
nlist = 100
nb = 1000
def custom_sharding_function(self, i, _):
return 1 if i % 2 == 0 else 7
# Mimics the default in DefaultShardingFunction.
# This impl is just used for verification.
def default_sharding_function(self, i, shard_count):
return i % shard_count
def verify_sharded_ivf_indexes(
self, template, xb, shard_count, sharding_function, generate_ids=True):
sharded_indexes_counters = [0] * shard_count
sharded_indexes = []
for i in range(shard_count):
if xb[0].dtype.name == 'uint8':
index = faiss.read_index_binary(template % i)
else:
index = faiss.read_index(template % i)
sharded_indexes.append(index)
# Reconstruct and verify each centroid
if generate_ids:
for i in range(len(xb)):
shard_id = sharding_function(i, shard_count)
reconstructed = sharded_indexes[shard_id].quantizer.reconstruct(i)
np.testing.assert_array_equal(reconstructed, xb[i])
else:
for i in range(len(xb)):
shard_id = sharding_function(i, shard_count)
reconstructed = sharded_indexes[shard_id].quantizer.reconstruct(
sharded_indexes_counters[shard_id])
sharded_indexes_counters[shard_id] += 1
np.testing.assert_array_equal(reconstructed, xb[i])
# Clean up
for i in range(shard_count):
os.remove(template % i)
def test_save_index_shards_by_centroids_no_op(self):
quantizer = faiss.IndexFlatL2(self.d)
index = faiss.IndexIVFFlat(quantizer, self.d, self.nlist)
with self.assertRaises(RuntimeError):
faiss.shard_ivf_index_centroids(
index,
10,
"shard.%d.index",
None
)
def test_save_index_shards_by_centroids_flat_quantizer_default_sharding(
self):
xb = np.random.rand(self.nb, self.d).astype('float32')
quantizer = faiss.IndexFlatL2(self.d)
index = faiss.IndexIVFFlat(quantizer, self.d, self.nlist)
shard_count = 3
index.quantizer.add(xb)
template = str(random.randint(0, 100000)) + "shard.%d.index"
faiss.shard_ivf_index_centroids(
index,
shard_count,
template,
None,
True
)
self.verify_sharded_ivf_indexes(
template, xb, shard_count, self.default_sharding_function)
def test_save_index_shards_by_centroids_flat_quantizer_custom_sharding(
self):
xb = np.random.rand(self.nb, self.d).astype('float32')
quantizer = faiss.IndexFlatL2(self.d)
index = faiss.IndexIVFFlat(quantizer, self.d, self.nlist)
shard_count = 20
index.quantizer.add(xb)
template = str(random.randint(0, 100000)) + "shard.%d.index"
faiss.shard_ivf_index_centroids(
index,
shard_count,
template,
self.custom_sharding_function,
True
)
self.verify_sharded_ivf_indexes(
template, xb, shard_count, self.custom_sharding_function)
def test_save_index_shards_by_centroids_hnsw_quantizer(self):
xb = np.random.rand(self.nb, self.d).astype('float32')
quantizer = faiss.IndexHNSWFlat(self.d, 32)
index = faiss.IndexIVFFlat(quantizer, self.d, self.nlist)
shard_count = 17
index.quantizer.add(xb)
template = str(random.randint(0, 100000)) + "shard.%d.index"
faiss.shard_ivf_index_centroids(
index,
shard_count,
template,
None,
True
)
self.verify_sharded_ivf_indexes(
template, xb, shard_count, self.default_sharding_function)
def test_save_index_shards_by_centroids_binary_flat_quantizer(self):
xb = np.random.randint(256, size=(self.nb, int(self.d / 8))).astype('uint8')
quantizer = faiss.IndexBinaryFlat(self.d)
index = faiss.IndexBinaryIVF(quantizer, self.d, self.nlist)
shard_count = 11
index.quantizer.add(xb)
template = str(random.randint(0, 100000)) + "shard.%d.index"
faiss.shard_binary_ivf_index_centroids(
index,
shard_count,
template,
None,
True
)
self.verify_sharded_ivf_indexes(
template, xb, shard_count, self.default_sharding_function)
def test_save_index_shards_by_centroids_binary_hnsw_quantizer(self):
xb = np.random.randint(256, size=(self.nb, int(self.d / 8))).astype('uint8')
quantizer = faiss.IndexBinaryHNSW(self.d, 32)
index = faiss.IndexBinaryIVF(quantizer, self.d, self.nlist)
shard_count = 13
index.quantizer.add(xb)
template = str(random.randint(0, 100000)) + "shard.%d.index"
faiss.shard_binary_ivf_index_centroids(
index,
shard_count,
template,
None,
True
)
self.verify_sharded_ivf_indexes(
template, xb, shard_count, self.default_sharding_function)
def test_save_index_shards_without_id_generation(self):
xb = np.random.randint(256, size=(self.nb, int(self.d / 8))).astype('uint8')
quantizer = faiss.IndexBinaryHNSW(self.d, 32)
index = faiss.IndexBinaryIVF(quantizer, self.d, self.nlist)
shard_count = 5
index.quantizer.add(xb)
template = str(random.randint(0, 100000)) + "shard.%d.index"
faiss.shard_binary_ivf_index_centroids(
index,
shard_count,
template,
None,
False
)
self.verify_sharded_ivf_indexes(
template, xb, shard_count, self.default_sharding_function, False)
xb = np.random.rand(self.nb, self.d).astype('float32')
quantizer = faiss.IndexHNSWFlat(self.d, 32)
index = faiss.IndexIVFFlat(quantizer, self.d, self.nlist)
shard_count = 23
index.quantizer.add(xb)
template = str(random.randint(0, 100000)) + "shard.%d.index"
faiss.shard_ivf_index_centroids(
index,
shard_count,
template,
None,
False
)
self.verify_sharded_ivf_indexes(
template, xb, shard_count, self.default_sharding_function, False)
|