From f46050eb56c6458e368496ff74281630fd462e4c Mon Sep 17 00:00:00 2001 From: Weixiang Hong Date: Fri, 9 Oct 2026 16:25:26 -0700 Subject: [PATCH] Deflake test_io TestIVFPQRead.test_reader (#5718) Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/5718 `test_reader` searches one IVFPQ index loaded twice, with and without the precomputed table, and required the neighbor ids to be identical. The two paths add the distance terms in a different order, so near-tied neighbors can swap. With unseeded data it failed in 7 of 400 random draws, and once in seven runs of the Cogwheel faiss suite on T1_BGM (2 of 10000 ids). Compare with `check_ref_knn_with_draws`, which allows swaps only among tied distances, and seed the data so a failure reproduces. `test_io` moves to `py_tests_with_contrib` for the helper. Reviewed By: mnorris11 Differential Revision: D124383290 fbshipit-source-id: ceb81262ffaba81ac23e5a592e1658741192060d --- tests/test_io.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/test_io.py b/tests/test_io.py index df66d5f94..e0be06969 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -15,6 +15,7 @@ from multiprocessing.pool import ThreadPool import faiss import numpy as np from common_faiss_tests import get_dataset_2 +from faiss.contrib.evaluation import check_ref_knn_with_draws d = 32 @@ -502,8 +503,9 @@ class Test_IO_IndexIVFSpectralHash(unittest.TestCase): class TestIVFPQRead(unittest.TestCase): def test_reader(self): d, n = 32, 1000 - xq = np.random.uniform(size=(n, d)).astype("float32") - xb = np.random.uniform(size=(n, d)).astype("float32") + rs = np.random.RandomState(123) + xq = rs.uniform(size=(n, d)).astype("float32") + xb = rs.uniform(size=(n, d)).astype("float32") index = faiss.index_factory(32, "IVF32,PQ16np", faiss.METRIC_L2) index.train(xb) @@ -521,8 +523,9 @@ class TestIVFPQRead(unittest.TestCase): Da, Ia = index_a.search(xq, 10) Db, Ib = index_b.search(xq, 10) - np.testing.assert_array_equal(Ia, Ib) - np.testing.assert_almost_equal(Da, Db, decimal=5) + # The precomputed table changes the order of float operations, so + # near-tied neighbors can swap. + check_ref_knn_with_draws(Da, Ia, Db, Ib) codes_a = index_a.sa_encode(xq) codes_b = index_b.sa_encode(xq)