mirror of
https://github.com/facebookresearch/faiss.git
synced 2025-06-03 21:54:02 +08:00
Changelog: - changed license: BSD+Patents -> MIT - propagates exceptions raised in sub-indexes of IndexShards and IndexReplicas - support for searching several inverted lists in parallel (parallel_mode != 0) - better support for PQ codes where nbit != 8 or 16 - IVFSpectralHash implementation: spectral hash codes inside an IVF - 6-bit per component scalar quantizer (4 and 8 bit were already supported) - combinations of inverted lists: HStackInvertedLists and VStackInvertedLists - configurable number of threads for OnDiskInvertedLists prefetching (including 0=no prefetch) - more test and demo code compatible with Python 3 (print with parentheses) - refactored benchmark code: data loading is now in a single file
108 lines
2.8 KiB
Python
108 lines
2.8 KiB
Python
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
#! /usr/bin/env python2
|
|
|
|
"""make sure that the referenced objects are kept"""
|
|
|
|
import numpy as np
|
|
import unittest
|
|
import faiss
|
|
import sys
|
|
import gc
|
|
|
|
d = 10
|
|
xt = np.random.rand(100, d).astype('float32')
|
|
xb = np.random.rand(20, d).astype('float32')
|
|
|
|
|
|
class TestReferenced(unittest.TestCase):
|
|
|
|
def test_IndexIVF(self):
|
|
quantizer = faiss.IndexFlatL2(d)
|
|
index = faiss.IndexIVFFlat(quantizer, d, 10)
|
|
index.train(xt)
|
|
index.add(xb)
|
|
del quantizer
|
|
gc.collect()
|
|
index.add(xb)
|
|
|
|
def test_count_refs(self):
|
|
quantizer = faiss.IndexFlatL2(d)
|
|
index = faiss.IndexIVFFlat(quantizer, d, 10)
|
|
refc1 = sys.getrefcount(quantizer)
|
|
del index
|
|
gc.collect()
|
|
refc2 = sys.getrefcount(quantizer)
|
|
assert refc2 == refc1 - 1
|
|
|
|
def test_IndexIVF_2(self):
|
|
index = faiss.IndexIVFFlat(faiss.IndexFlatL2(d), d, 10)
|
|
index.train(xt)
|
|
index.add(xb)
|
|
|
|
def test_IndexPreTransform(self):
|
|
ltrans = faiss.NormalizationTransform(d)
|
|
sub_index = faiss.IndexFlatL2(d)
|
|
index = faiss.IndexPreTransform(ltrans, sub_index)
|
|
index.add(xb)
|
|
del ltrans
|
|
gc.collect()
|
|
index.add(xb)
|
|
del sub_index
|
|
gc.collect()
|
|
index.add(xb)
|
|
|
|
def test_IndexPreTransform_2(self):
|
|
sub_index = faiss.IndexFlatL2(d)
|
|
index = faiss.IndexPreTransform(sub_index)
|
|
ltrans = faiss.NormalizationTransform(d)
|
|
index.prepend_transform(ltrans)
|
|
index.add(xb)
|
|
del ltrans
|
|
gc.collect()
|
|
index.add(xb)
|
|
del sub_index
|
|
gc.collect()
|
|
index.add(xb)
|
|
|
|
def test_IDMap(self):
|
|
sub_index = faiss.IndexFlatL2(d)
|
|
index = faiss.IndexIDMap(sub_index)
|
|
index.add_with_ids(xb, np.arange(len(xb)))
|
|
del sub_index
|
|
gc.collect()
|
|
index.add_with_ids(xb, np.arange(len(xb)))
|
|
|
|
def test_shards(self):
|
|
index = faiss.IndexShards(d)
|
|
for i in range(3):
|
|
sub_index = faiss.IndexFlatL2(d)
|
|
sub_index.add(xb)
|
|
index.add_shard(sub_index)
|
|
gc.collect()
|
|
index.search(xb, 10)
|
|
|
|
|
|
dbin = 32
|
|
xtbin = np.random.randint(256, size=(100, int(dbin / 8))).astype('uint8')
|
|
xbbin = np.random.randint(256, size=(20, int(dbin / 8))).astype('uint8')
|
|
|
|
|
|
class TestReferencedBinary(unittest.TestCase):
|
|
|
|
def test_binary_ivf(self):
|
|
index = faiss.IndexBinaryIVF(faiss.IndexBinaryFlat(dbin), dbin, 10)
|
|
gc.collect()
|
|
index.train(xtbin)
|
|
|
|
def test_wrap(self):
|
|
index = faiss.IndexBinaryFromFloat(faiss.IndexFlatL2(dbin))
|
|
gc.collect()
|
|
index.add(xbbin)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|