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
83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
#! /usr/bin/env python3
|
|
# 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.
|
|
|
|
from __future__ import print_function
|
|
import numpy as np
|
|
import faiss
|
|
import time
|
|
|
|
swig_ptr = faiss.swig_ptr
|
|
|
|
if False:
|
|
a = np.arange(10, 14).astype("float32")
|
|
b = np.arange(20, 24).astype("float32")
|
|
|
|
faiss.fvec_inner_product(swig_ptr(a), swig_ptr(b), 4)
|
|
|
|
1 / 0
|
|
|
|
xd = 100
|
|
yd = 1000000
|
|
|
|
np.random.seed(1234)
|
|
|
|
faiss.omp_set_num_threads(1)
|
|
|
|
print("xd=%d yd=%d" % (xd, yd))
|
|
|
|
print("Running inner products test..")
|
|
for d in 3, 4, 12, 36, 64:
|
|
|
|
x = faiss.rand(xd * d).reshape(xd, d)
|
|
y = faiss.rand(yd * d).reshape(yd, d)
|
|
|
|
distances = np.empty((xd, yd), dtype="float32")
|
|
|
|
t0 = time.time()
|
|
for i in range(xd):
|
|
faiss.fvec_inner_products_ny(
|
|
swig_ptr(distances[i]), swig_ptr(x[i]), swig_ptr(y), d, yd
|
|
)
|
|
t1 = time.time()
|
|
|
|
# sparse verification
|
|
ntry = 100
|
|
num, denom = 0, 0
|
|
for t in range(ntry):
|
|
xi = np.random.randint(xd)
|
|
yi = np.random.randint(yd)
|
|
num += abs(distances[xi, yi] - np.dot(x[xi], y[yi]))
|
|
denom += abs(distances[xi, yi])
|
|
|
|
print("d=%d t=%.3f s diff=%g" % (d, t1 - t0, num / denom))
|
|
|
|
|
|
print("Running L2sqr test..")
|
|
for d in 3, 4, 12, 36, 64:
|
|
|
|
x = faiss.rand(xd * d).reshape(xd, d)
|
|
y = faiss.rand(yd * d).reshape(yd, d)
|
|
|
|
distances = np.empty((xd, yd), dtype="float32")
|
|
|
|
t0 = time.time()
|
|
for i in range(xd):
|
|
faiss.fvec_L2sqr_ny(
|
|
swig_ptr(distances[i]), swig_ptr(x[i]), swig_ptr(y), d, yd
|
|
)
|
|
t1 = time.time()
|
|
|
|
# sparse verification
|
|
ntry = 100
|
|
num, denom = 0, 0
|
|
for t in range(ntry):
|
|
xi = np.random.randint(xd)
|
|
yi = np.random.randint(yd)
|
|
num += abs(distances[xi, yi] - np.sum((x[xi] - y[yi]) ** 2))
|
|
denom += abs(distances[xi, yi])
|
|
|
|
print("d=%d t=%.3f s diff=%g" % (d, t1 - t0, num / denom))
|