Files
faiss/tutorial/python/9-RefineComparison.py
Tia Lim 0f0f728166 Format faiss Python files with black (line length 80) (#5349)
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
2026-06-29 17:29:05 -07:00

43 lines
1.5 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 faiss.contrib.evaluation import knn_intersection_measure
from faiss.contrib import datasets
# 64-dim vectors, 50000 vectors in the training, 100000 in database,
# 10000 in queries, dtype ('float32')
ds = datasets.SyntheticDataset(64, 50000, 100000, 10000)
d = 64 # dimension
# Constructing the refine PQ index with SQfp16 with index factory
index_fp16 = faiss.index_factory(d, "PQ32x4fs,Refine(SQfp16)")
index_fp16.train(ds.get_train())
index_fp16.add(ds.get_database())
# Constructing the refine PQ index with SQ8
index_sq8 = faiss.index_factory(d, "PQ32x4fs,Refine(SQ8)")
index_sq8.train(ds.get_train())
index_sq8.add(ds.get_database())
# Parameterization on k factor while doing search for index refinement
k_factor = 3.0
params = faiss.IndexRefineSearchParameters(k_factor=k_factor)
# Perform index search using different index refinement
D_fp16, I_fp16 = index_fp16.search(ds.get_queries(), 100, params=params)
D_sq8, I_sq8 = index_sq8.search(ds.get_queries(), 100, params=params)
# Calculating knn intersection measure for different index types on refinement
KIM_fp16 = knn_intersection_measure(I_fp16, ds.get_groundtruth())
KIM_sq8 = knn_intersection_measure(I_sq8, ds.get_groundtruth())
# KNN intersection measure accuracy shows that choosing SQ8 impacts accuracy
assert KIM_fp16 > KIM_sq8
print(I_sq8[:5])
print(I_fp16[:5])