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
91 lines
1.9 KiB
Python
91 lines
1.9 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
|
|
from datasets import load_sift1M, evaluate
|
|
|
|
|
|
print("load data")
|
|
|
|
xb, xq, xt, gt = load_sift1M()
|
|
nq, d = xq.shape
|
|
|
|
# we need only a StandardGpuResources per GPU
|
|
res = faiss.StandardGpuResources()
|
|
|
|
|
|
#################################################################
|
|
# Exact search experiment
|
|
#################################################################
|
|
|
|
print("============ Exact search")
|
|
|
|
flat_config = faiss.GpuIndexFlatConfig()
|
|
flat_config.device = 0
|
|
|
|
index = faiss.GpuIndexFlatL2(res, d, flat_config)
|
|
|
|
print("add vectors to index")
|
|
|
|
index.add(xb)
|
|
|
|
print("warmup")
|
|
|
|
index.search(xq, 123)
|
|
|
|
print("benchmark")
|
|
|
|
for lk in range(11):
|
|
k = 1 << lk
|
|
t, r = evaluate(index, xq, gt, k)
|
|
|
|
# the recall should be 1 at all times
|
|
print("k=%d %.3f ms, R@1 %.4f" % (k, t, r[1]))
|
|
|
|
|
|
#################################################################
|
|
# Approximate search experiment
|
|
#################################################################
|
|
|
|
print("============ Approximate search")
|
|
|
|
index = faiss.index_factory(d, "IVF4096,PQ64")
|
|
|
|
# faster, uses more memory
|
|
# index = faiss.index_factory(d, "IVF16384,Flat")
|
|
|
|
co = faiss.GpuClonerOptions()
|
|
|
|
# here we are using a 64-byte PQ, so we must set the lookup tables to
|
|
# 16 bit float (this is due to the limited temporary memory).
|
|
co.useFloat16 = True
|
|
|
|
index = faiss.index_cpu_to_gpu(res, 0, index, co)
|
|
|
|
print("train")
|
|
|
|
index.train(xt)
|
|
|
|
print("add vectors to index")
|
|
|
|
index.add(xb)
|
|
|
|
print("warmup")
|
|
|
|
index.search(xq, 123)
|
|
|
|
print("benchmark")
|
|
|
|
for lnprobe in range(10):
|
|
nprobe = 1 << lnprobe
|
|
index.nprobe
|
|
index.nprobe = nprobe
|
|
t, r = evaluate(index, xq, gt, 100)
|
|
|
|
print(
|
|
"nprobe=%4d %.3f ms recalls= %.4f %.4f %.4f"
|
|
% (nprobe, t, r[1], r[10], r[100])
|
|
)
|