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
110 lines
2.7 KiB
Python
110 lines
2.7 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()
|
|
|
|
xq = ds.get_queries()
|
|
xb = ds.get_database()
|
|
gt = ds.get_groundtruth()
|
|
|
|
xt = ds.get_train()
|
|
|
|
nb, d = xb.shape
|
|
nq, d = xq.shape
|
|
nt, d = xt.shape
|
|
|
|
k = 10
|
|
gt = gt[:, :k]
|
|
nlevels = 16 if args.dataset == "gist1m" else 8
|
|
|
|
|
|
def get_ivf_index(index):
|
|
if isinstance(index, faiss.IndexPreTransform):
|
|
return faiss.downcast_index(index.index)
|
|
return index
|
|
|
|
|
|
def eval_recall(index, nprobe_val):
|
|
faiss.cvar.indexPanorama_stats.reset()
|
|
t0 = time.time()
|
|
_, I = index.search(xq, k=k)
|
|
t = time.time() - t0
|
|
speed = t * 1000 / nq
|
|
qps = 1000 / speed
|
|
|
|
corrects = (gt == I).sum()
|
|
recall = corrects / (nq * k)
|
|
ratio_dims_scanned = faiss.cvar.indexPanorama_stats.ratio_dims_scanned
|
|
print(
|
|
f"\tnprobe {nprobe_val:3d}, Recall@{k}: "
|
|
f"{recall:.6f}, speed: {speed:.6f} ms/query, "
|
|
f"dims scanned: {ratio_dims_scanned * 100:.2f}%"
|
|
)
|
|
|
|
return recall, qps
|
|
|
|
|
|
def eval_and_plot(name, plot=True):
|
|
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)
|
|
|
|
# Get the underlying IVF index for setting nprobe
|
|
ivf_index = get_ivf_index(index)
|
|
|
|
data = []
|
|
print(f"======{name}")
|
|
for nprobe in 1, 2, 4, 8, 16, 32, 64:
|
|
ivf_index.nprobe = nprobe
|
|
recall, qps = eval_recall(index, nprobe)
|
|
data.append((recall, qps))
|
|
|
|
if plot:
|
|
data = np.array(data)
|
|
plt.plot(data[:, 0], data[:, 1], label=name) # x - recall, y - qps
|
|
|
|
|
|
nlist = 128
|
|
|
|
plt.figure(figsize=(8, 6), dpi=80)
|
|
|
|
# IVFFlat
|
|
eval_and_plot(f"IVF{nlist},Flat")
|
|
|
|
# IVFFlatPanorama (with PCA transform to concentrate energy in early dimensions)
|
|
eval_and_plot(f"PCA{d},IVF{nlist},FlatPanorama{nlevels}")
|
|
|
|
dataset_label = args.dataset.upper()
|
|
plt.title(f"IVF Flat Indexes on {dataset_label}")
|
|
plt.xlabel(f"Recall@{k}")
|
|
plt.ylabel("QPS")
|
|
plt.yscale("log")
|
|
plt.legend(bbox_to_anchor=(1.02, 0.1), loc="upper left", borderaxespad=0)
|
|
plt.savefig(f"bench_ivf_flat_panorama_{args.dataset}.png", bbox_inches="tight")
|