mirror of
https://github.com/facebookresearch/faiss.git
synced 2026-10-12 02:13:36 +00:00
Summary: ## Changes This PR unifies the default Panorama batch size to 1024 going forward. This was shown to offer better performance than the current values ([paper](https://arxiv.org/abs/2510.00566), figure 11). ## Performance ### IVFFlatPanorama SIFT1M | nprobe | qps_before | qps_after | speedup | |---|---|---|---| | 1 | 5980.4 | 6343.7 | 1.06x | | 2 | 3537.7 | 3933.2 | 1.11x | | 4 | 2105.4 | 2419.9 | 1.15x | | 8 | 1245.2 | 1466.4 | 1.18x | | 16 | 729.2 | 887.4 | 1.22x | | 32 | 429.6 | 538.0 | 1.25x | | 64 | 249.0 | 319.3 | 1.28x | GIST1M | nprobe | qps_before | qps_after | speedup | |---|---|---|---| | 1 | 1826.0 | 1494.7 | 0.82x | | 2 | 1091.0 | 1001.7 | 0.92x | | 4 | 642.2 | 639.0 | 0.99x | | 8 | 368.7 | 392.7 | 1.07x | | 16 | 212.2 | 233.9 | 1.10x | | 32 | 122.2 | 138.6 | 1.13x | | 64 | 71.3 | 82.6 | 1.16x | ## FlatL2Panorama SIFT1M | qps_before | qps_after | speedup | |---|---|---| | 124.8 | 133.4 | 1.07x | GIST1M | qps_before | qps_after | speedup | |---|---|---| | 44.1 | 46.2 | 1.05x | Pull Request resolved: https://github.com/facebookresearch/faiss/pull/5441 Reviewed By: alibeklfc Differential Revision: D114387063 Pulled By: mnorris11 fbshipit-source-id: b71ba6ced074c4998aee3183df236b692626cd34
108 lines
2.4 KiB
Python
108 lines
2.4 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 argparse
|
|
import multiprocessing as mp
|
|
import time
|
|
|
|
import faiss
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
try:
|
|
from faiss.contrib.datasets_fb import DatasetSIFT1M, DatasetGIST1M
|
|
except ImportError:
|
|
from faiss.contrib.datasets import DatasetSIFT1M, DatasetGIST1M
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--dataset", default="gist1m", choices=["sift1m", "gist1m"])
|
|
args = parser.parse_args()
|
|
|
|
if args.dataset == "sift1m":
|
|
ds = DatasetSIFT1M()
|
|
else:
|
|
ds = DatasetGIST1M()
|
|
|
|
nq = 10
|
|
xq = ds.get_queries()[:nq]
|
|
xb = ds.get_database()
|
|
gt = ds.get_groundtruth()[:nq]
|
|
|
|
xt = ds.get_train()
|
|
|
|
nb, d = xb.shape
|
|
nt, d = xt.shape
|
|
|
|
k = 10
|
|
gt = gt[:, :k]
|
|
|
|
|
|
def eval_qps(index):
|
|
faiss.cvar.indexPanorama_stats.reset()
|
|
t0 = time.time()
|
|
_, I = index.search(xq, k=k)
|
|
t = time.time() - t0
|
|
speed = t * 1000 / nq # ms/query
|
|
qps = 1000 / speed
|
|
|
|
corrects = (gt == I).sum()
|
|
recall = corrects / (nq * k)
|
|
ratio_dims_scanned = faiss.cvar.indexPanorama_stats.ratio_dims_scanned
|
|
print(
|
|
f"\tRecall@{k}: {recall:.6f}, speed: {speed:.6f} ms/query, "
|
|
f"dims scanned: {ratio_dims_scanned * 100:.2f}%"
|
|
)
|
|
return recall, qps
|
|
|
|
|
|
def build_index(name):
|
|
index = faiss.index_factory(d, name)
|
|
|
|
faiss.omp_set_num_threads(mp.cpu_count())
|
|
index.train(xt)
|
|
index.add(xb)
|
|
|
|
faiss.omp_set_num_threads(1)
|
|
return index
|
|
|
|
|
|
nlevels = 16 if args.dataset == "gist1m" else 8
|
|
|
|
plt.figure(figsize=(8, 6), dpi=80)
|
|
|
|
names = [
|
|
"Flat",
|
|
f"PCA{d},FlatL2Panorama{nlevels}",
|
|
]
|
|
|
|
labels = []
|
|
qps_values = []
|
|
|
|
for name in names:
|
|
print(f"======{name}")
|
|
index = build_index(name)
|
|
recall, qps = eval_qps(index)
|
|
labels.append(f"{name}\n(r@{recall:.3f})")
|
|
qps_values.append(qps)
|
|
|
|
x = np.arange(len(names))
|
|
plt.bar(x, qps_values, color=["#1f77b4", "#ff7f0e"])
|
|
speedup = qps_values[1] / qps_values[0]
|
|
ax = plt.gca()
|
|
ax.text(
|
|
x[1],
|
|
qps_values[1] * 1.01,
|
|
f"{speedup:.2f}x",
|
|
ha="center",
|
|
va="bottom",
|
|
)
|
|
plt.xticks(x, labels, rotation=0)
|
|
plt.ylabel("QPS")
|
|
dataset_label = args.dataset.upper()
|
|
plt.title(f"Flat Indexes on {dataset_label}")
|
|
|
|
plt.tight_layout()
|
|
plt.savefig(f"bench_flat_l2_panorama_{args.dataset}.png", bbox_inches="tight")
|