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
168 lines
4.7 KiB
Python
168 lines
4.7 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 numpy as np
|
|
import faiss
|
|
|
|
from common_faiss_tests import for_all_simd_levels
|
|
|
|
|
|
@for_all_simd_levels
|
|
class TestBinarySearchParams(unittest.TestCase):
|
|
|
|
def do_test_with_param(
|
|
self, index, ps_params, params, d=64, nb=500, nq=20, k=5
|
|
):
|
|
"""
|
|
Test equivalence between setting
|
|
1. param_name = value with ParameterSpace
|
|
2. pass in a SearchParameters with param_name = value
|
|
"""
|
|
rs = np.random.RandomState(123)
|
|
xb = rs.randint(256, size=(nb, d // 8), dtype="uint8")
|
|
xq = rs.randint(256, size=(nq, d // 8), dtype="uint8")
|
|
if hasattr(index, "is_trained") and not index.is_trained:
|
|
xt = rs.randint(256, size=(nb, d // 8), dtype="uint8")
|
|
index.train(xt)
|
|
|
|
index.add(xb)
|
|
|
|
I0, D0 = index.search(xq, k)
|
|
|
|
Dnew, Inew = index.search(xq, k, params=params)
|
|
|
|
# make sure the parameter does indeed change the result...
|
|
self.assertFalse(np.all(Inew == I0))
|
|
|
|
for param_name, value in ps_params.items():
|
|
faiss.ParameterSpace().set_index_parameter(index, param_name, value)
|
|
Dref, Iref = index.search(xq, k)
|
|
|
|
np.testing.assert_array_equal(Iref, Inew)
|
|
np.testing.assert_array_equal(Dref, Dnew)
|
|
|
|
def test_nprobe(self):
|
|
d = 64
|
|
quantizer = faiss.IndexBinaryFlat(d)
|
|
index = faiss.IndexBinaryIVF(quantizer, d, 32)
|
|
self.do_test_with_param(
|
|
index,
|
|
{"nprobe": 3},
|
|
faiss.SearchParametersIVF(nprobe=3),
|
|
d=d,
|
|
nb=500,
|
|
nq=20,
|
|
k=5,
|
|
)
|
|
|
|
def test_efSearch(self):
|
|
d = 64
|
|
index = faiss.IndexBinaryHNSW(d, 32)
|
|
self.do_test_with_param(
|
|
index,
|
|
{"efSearch": 4},
|
|
faiss.SearchParametersHNSW(efSearch=4),
|
|
d=d,
|
|
nb=500,
|
|
nq=20,
|
|
k=5,
|
|
)
|
|
|
|
def test_quantizer_hnsw(self):
|
|
d = 64
|
|
quantizer = faiss.IndexBinaryHNSW(d, 32)
|
|
index = faiss.IndexBinaryIVF(quantizer, d, 32)
|
|
self.do_test_with_param(
|
|
index,
|
|
{"quantizer_efSearch": 5, "nprobe": 10},
|
|
faiss.SearchParametersIVF(
|
|
nprobe=10,
|
|
quantizer_params=faiss.SearchParametersHNSW(efSearch=5),
|
|
),
|
|
d=d,
|
|
nb=500,
|
|
nq=20,
|
|
k=5,
|
|
)
|
|
|
|
def test_max_codes(self):
|
|
d = 64
|
|
nb = 1000
|
|
nq = 20
|
|
k = 10
|
|
|
|
rs = np.random.RandomState(123)
|
|
xb = rs.randint(256, size=(nb, d // 8), dtype="uint8")
|
|
xq = rs.randint(256, size=(nq, d // 8), dtype="uint8")
|
|
|
|
quantizer = faiss.IndexBinaryFlat(d)
|
|
index = faiss.IndexBinaryIVF(quantizer, d, 32)
|
|
index.train(xb)
|
|
index.add(xb)
|
|
|
|
stats = faiss.cvar.indexIVF_stats
|
|
stats.reset()
|
|
D0, I0 = index.search(xq, k, params=faiss.SearchParametersIVF(nprobe=8))
|
|
ndis0 = stats.ndis
|
|
target_ndis = ndis0 // nq
|
|
for q in range(nq):
|
|
stats.reset()
|
|
Dq, Iq = index.search(
|
|
xq[q : q + 1],
|
|
k,
|
|
params=faiss.SearchParametersIVF(
|
|
nprobe=8, max_codes=target_ndis
|
|
),
|
|
)
|
|
self.assertLessEqual(stats.ndis, target_ndis)
|
|
if stats.ndis < target_ndis:
|
|
np.testing.assert_equal(I0[q], Iq[0])
|
|
|
|
def test_efSearch(self):
|
|
d = 64
|
|
nb = 500
|
|
nq = 20
|
|
k = 5
|
|
|
|
rs = np.random.RandomState(123)
|
|
xb = rs.randint(256, size=(nb, d // 8), dtype="uint8")
|
|
xq = rs.randint(256, size=(nq, d // 8), dtype="uint8")
|
|
|
|
index = faiss.IndexBinaryHNSW(d, 32)
|
|
index.add(xb)
|
|
|
|
D1, I1 = index.search(
|
|
xq, k, params=faiss.SearchParametersHNSW(efSearch=4)
|
|
)
|
|
|
|
D2, I2 = index.search(
|
|
xq, k, params=faiss.SearchParametersHNSW(efSearch=16)
|
|
)
|
|
|
|
self.assertFalse(np.all(I1 == I2))
|
|
|
|
def test_nprobe(self):
|
|
d = 64
|
|
nb = 500
|
|
nq = 20
|
|
k = 5
|
|
|
|
rs = np.random.RandomState(123)
|
|
xb = rs.randint(256, size=(nb, d // 8), dtype="uint8")
|
|
xq = rs.randint(256, size=(nq, d // 8), dtype="uint8")
|
|
|
|
quantizer = faiss.IndexBinaryFlat(d)
|
|
index = faiss.IndexBinaryIVF(quantizer, d, 32)
|
|
index.train(xb)
|
|
index.add(xb)
|
|
|
|
D1, I1 = index.search(xq, k, params=faiss.SearchParametersIVF(nprobe=1))
|
|
|
|
D2, I2 = index.search(xq, k, params=faiss.SearchParametersIVF(nprobe=8))
|
|
|
|
self.assertFalse(np.all(I1 == I2))
|