Files
faiss/benchs/bench_fwht.py
T
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

91 lines
2.6 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.
"""Benchmark HadamardRotation (HD3 FWHT) vs RandomRotationMatrix (BLAS sgemm).
Measures:
1. Speed: wall-clock time for transform.apply(x)
2. Recall@1: nearest-neighbor recall after pre-transform + Flat index
"""
import faiss
import numpy as np
import time
n = 10000
nq = 200
n_trials = 15
print(f"n={n} vectors, nq={nq} queries, {n_trials} speed trials")
# --- Speed benchmark ---
print("\n=== Speed ===")
print(f'{"dim":>6s} {"HR (ms)":>10s} {"RRM (ms)":>10s} {"speedup":>8s}')
print("-" * 42)
dims = [64, 128, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192]
for d in dims:
np.random.seed(42)
x = np.random.randn(n, d).astype("float32")
hr = faiss.HadamardRotation(d, 42)
hr.apply(x[:100]) # warmup
times_hr = []
for _ in range(n_trials):
t0 = time.perf_counter()
hr.apply(x)
t1 = time.perf_counter()
times_hr.append((t1 - t0) * 1000)
hr_ms = sorted(times_hr)[1]
rr = faiss.RandomRotationMatrix(d, d)
rr.train(x[:100])
rr.apply(x[:100]) # warmup
times_rr = []
for _ in range(n_trials):
t0 = time.perf_counter()
rr.apply(x)
t1 = time.perf_counter()
times_rr.append((t1 - t0) * 1000)
rr_ms = sorted(times_rr)[1]
speedup = rr_ms / hr_ms
print(f"{d:6d} {hr_ms:10.2f} {rr_ms:10.2f} {speedup:7.1f}x")
# --- Recall benchmark with IVF (where rotation actually matters) ---
print(f"\n=== Recall@1 with IVF (L2, n={n}, nq={nq}, k=1) ===")
print(f'{"dim":>6s} {"HR R@1":>8s} {"RRM R@1":>8s} {"none R@1":>8s}')
print("-" * 40)
for d in [64, 128, 256, 768, 1024, 2048, 4096]:
np.random.seed(42)
xb = np.random.randn(n, d).astype("float32")
xq = np.random.randn(nq, d).astype("float32")
# Ground truth (brute force, no transform)
gt_index = faiss.IndexFlatL2(d)
gt_index.add(xb)
_, gt_I = gt_index.search(xq, 1)
nlist = 64
recalls = {}
for label, factory_str in [
("HR", f"HR,IVF{nlist},Flat"),
("RRM", f"RR,IVF{nlist},Flat"),
("none", f"IVF{nlist},Flat"),
]:
index = faiss.index_factory(d, factory_str)
index.train(xb)
index.add(xb)
faiss.extract_index_ivf(index).nprobe = 8
_, I = index.search(xq, 1)
recall = (I[:, 0] == gt_I[:, 0]).mean()
recalls[label] = recall
print(
f'{d:6d} {recalls["HR"]:8.4f}'
f' {recalls["RRM"]:8.4f} {recalls["none"]:8.4f}'
)