mirror of
https://github.com/facebookresearch/faiss.git
synced 2026-10-11 22:50:00 +00:00
Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/5349 # this is a no-op change Ran `black -l 80` over all tracked Python files under `fbcode/faiss/`, using line length 80 to match faiss's documented coding style (`CONTRIBUTING.md`: "80 character line length (both for C++ and Python)"). 172 files were reformatted; 28 were already compliant. This is a pure-formatting change. `black` runs its AST-equivalence safety check on every file, so no identifiers, string/numeric values, operators, or control flow were altered — only whitespace, indentation, line wrapping, blank lines, trailing commas, and string-quote normalization (single to double quotes). `fbcode/faiss/` is intentionally excluded from Meta's standard Python autoformatter (`arc f` / `arc lint -a` report no formatter for these paths) because it mirrors to public GitHub, so the Python had drifted out of any consistent style. The C++ is already clang-format-clean (0 of 807 files change), so this effort is Python-only. This diff brings the Python to canonical `black` formatting. Excluded `demos/index_pq_flat_separate_codes_from_codebook.py`: it contains a Jupyter `!rm` shell-magic line and is not valid Python, so `black` cannot parse it. This is the base of a 3-diff stack: (1) this black reformat; (2) `.flake8` alignment (max-line-length 80 + black-compatible ignores E203/W503); (3) genuine lint fixes (F401/F403/F841/B011). Reviewed By: mnorris11 Differential Revision: D109736099 fbshipit-source-id: 9a054635766ed11aa3760a788395babd3d198abf
109 lines
3.4 KiB
Python
109 lines
3.4 KiB
Python
# 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.
|
|
|
|
import unittest
|
|
|
|
import faiss
|
|
import numpy as np
|
|
|
|
from faiss.contrib import datasets
|
|
|
|
faiss.omp_set_num_threads(4)
|
|
|
|
|
|
class TestClone(unittest.TestCase):
|
|
"""
|
|
Test clone_index for various index combinations.
|
|
"""
|
|
|
|
def do_test_clone(self, factory, with_ids=False):
|
|
"""
|
|
Verify that cloning works for a given index type
|
|
"""
|
|
d = 32
|
|
ds = datasets.SyntheticDataset(d, 1000, 2000, 10)
|
|
index1 = faiss.index_factory(d, factory)
|
|
index1.train(ds.get_train())
|
|
if with_ids:
|
|
index1.add_with_ids(
|
|
ds.get_database(), np.arange(ds.nb).astype("int64")
|
|
)
|
|
else:
|
|
index1.add(ds.get_database())
|
|
k = 5
|
|
Dref1, Iref1 = index1.search(ds.get_queries(), k)
|
|
|
|
index2 = faiss.clone_index(index1)
|
|
self.assertEqual(type(index1), type(index2))
|
|
index1 = None
|
|
|
|
Dref2, Iref2 = index2.search(ds.get_queries(), k)
|
|
np.testing.assert_array_equal(Dref1, Dref2)
|
|
np.testing.assert_array_equal(Iref1, Iref2)
|
|
|
|
def test_RFlat(self):
|
|
self.do_test_clone("SQ4,RFlat")
|
|
|
|
def test_Refine(self):
|
|
self.do_test_clone("SQ4,Refine(SQ8)")
|
|
|
|
def test_IVF(self):
|
|
self.do_test_clone("IVF16,Flat")
|
|
|
|
def test_PCA(self):
|
|
self.do_test_clone("PCA8,Flat")
|
|
|
|
def test_IDMap(self):
|
|
self.do_test_clone("IVF16,Flat,IDMap", with_ids=True)
|
|
|
|
def test_IDMap2(self):
|
|
self.do_test_clone("IVF16,Flat,IDMap2", with_ids=True)
|
|
|
|
def test_NSGPQ(self):
|
|
self.do_test_clone("NSG32,Flat")
|
|
|
|
def test_IVFAdditiveQuantizer(self):
|
|
self.do_test_clone("IVF16,LSQ5x6_Nqint8")
|
|
self.do_test_clone("IVF16,RQ5x6_Nqint8")
|
|
self.do_test_clone("IVF16,PLSQ4x3x5_Nqint8")
|
|
self.do_test_clone("IVF16,PRQ4x3x5_Nqint8")
|
|
|
|
def test_IVFAdditiveQuantizerFastScan(self):
|
|
self.do_test_clone("IVF16,LSQ3x4fs_32_Nlsq2x4")
|
|
self.do_test_clone("IVF16,RQ3x4fs_32_Nlsq2x4")
|
|
self.do_test_clone("IVF16,PLSQ2x3x4fs_Nlsq2x4")
|
|
self.do_test_clone("IVF16,PRQ2x3x4fs_Nrq2x4")
|
|
|
|
def test_AdditiveQuantizer(self):
|
|
self.do_test_clone("LSQ5x6_Nqint8")
|
|
self.do_test_clone("RQ5x6_Nqint8")
|
|
self.do_test_clone("PLSQ4x3x5_Nqint8")
|
|
self.do_test_clone("PRQ4x3x5_Nqint8")
|
|
|
|
def test_AdditiveQuantizerFastScan(self):
|
|
self.do_test_clone("LSQ3x4fs_32_Nlsq2x4")
|
|
self.do_test_clone("RQ3x4fs_32_Nlsq2x4")
|
|
self.do_test_clone("PLSQ2x3x4fs_Nlsq2x4")
|
|
self.do_test_clone("PRQ2x3x4fs_Nrq2x4")
|
|
|
|
def test_RowwiseMinMax(self):
|
|
# clone_Index for IndexRowwiseMinMaxBase was missing return res, so
|
|
# clone_index returned nullptr and leaked the clone allocation plus
|
|
# the deep-copied inner index.
|
|
d = 32
|
|
n = 200
|
|
rng = np.random.default_rng(42)
|
|
x = rng.standard_normal((n, d)).astype(np.float32)
|
|
|
|
for factory in ("MinMax,SQ8", "MinMaxFP16,SQ8"):
|
|
codec = faiss.index_factory(d, factory)
|
|
codec.train(x)
|
|
codes = codec.sa_encode(x)
|
|
|
|
codec2 = faiss.clone_index(codec)
|
|
self.assertEqual(type(codec), type(codec2))
|
|
codes2 = codec2.sa_encode(x)
|
|
np.testing.assert_array_equal(codes, codes2)
|