mirror of
https://github.com/facebookresearch/faiss.git
synced 2026-10-12 02:13:36 +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
36 lines
1.2 KiB
Python
36 lines
1.2 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 faiss
|
|
import numpy as np
|
|
|
|
d = 64 # dimension
|
|
nb = 100000 # database size
|
|
nq = 10000 # nb of queries
|
|
np.random.seed(1234) # make reproducible
|
|
xb = np.random.random((nb, d)).astype("float32") # 64-dim *nb queries
|
|
xb[:, 0] += np.arange(nb) / 1000.0
|
|
xq = np.random.random((nq, d)).astype("float32")
|
|
xq[:, 0] += np.arange(nq) / 1000.0
|
|
|
|
m = 8 # 8 specifies that the number of sub-vector is 8
|
|
k = 4 # number of dimension in extracted vector
|
|
n_bit = 4 # 4 specifies that each sub-vector is encoded as 4 bits
|
|
bbs = 32 # build block size ( bbs % 32 == 0 ) for PQ
|
|
index = faiss.IndexPQFastScan(d, m, n_bit, faiss.METRIC_L2, bbs)
|
|
# construct FastScan Index
|
|
|
|
assert not index.is_trained
|
|
index.train(xb) # Train vectors data index within mockup database
|
|
assert index.is_trained
|
|
|
|
index.add(xb)
|
|
D, I = index.search(xb[:5], k) # sanity check
|
|
print(I)
|
|
print(D)
|
|
index.nprobe = 10 # make comparable with experiment above
|
|
D, I = index.search(xq, k) # search
|
|
print(I[-5:]) # neighbors of the 5 last queries
|