|
| 1 | +import faiss |
| 2 | +import time |
| 3 | +import random |
| 4 | + |
| 5 | +import faiss.contrib.datasets |
| 6 | + |
| 7 | + |
| 8 | +# copied from benchs/bench_all_ivf/bench_all_ivf.py |
| 9 | +def unwind_index_ivf(index): |
| 10 | + if isinstance(index, faiss.IndexPreTransform): |
| 11 | + assert index.chain.size() == 1 |
| 12 | + vt = index.chain.at(0) |
| 13 | + index_ivf, vt2 = unwind_index_ivf(faiss.downcast_index(index.index)) |
| 14 | + assert vt2 is None |
| 15 | + return index_ivf, vt |
| 16 | + if hasattr(faiss, "IndexRefine") and isinstance(index, faiss.IndexRefine): |
| 17 | + return unwind_index_ivf(faiss.downcast_index(index.base_index)) |
| 18 | + if isinstance(index, faiss.IndexIVF): |
| 19 | + return index, None |
| 20 | + else: |
| 21 | + return None, None |
| 22 | + |
| 23 | + |
| 24 | +def test_bigann10m(index_file, index_parameters): |
| 25 | + ds = faiss.contrib.datasets.DatasetBigANN(nb_M=10) |
| 26 | + |
| 27 | + xq = ds.get_queries() |
| 28 | + xb = ds.get_database() |
| 29 | + gt = ds.get_groundtruth() |
| 30 | + |
| 31 | + nb, d = xb.shape |
| 32 | + nq, d = xq.shape |
| 33 | + |
| 34 | + print("Reading index {}".format(index_file)) |
| 35 | + index = faiss.read_index(index_file) |
| 36 | + |
| 37 | + ps = faiss.ParameterSpace() |
| 38 | + ps.initialize(index) |
| 39 | + |
| 40 | + index_ivf, vec_transform = unwind_index_ivf(index) |
| 41 | + |
| 42 | + print('params regular transp_centroids regular R@1 R@10 R@100') |
| 43 | + for index_parameter in index_parameters: |
| 44 | + ps.set_index_parameters(index, index_parameter) |
| 45 | + |
| 46 | + print(index_parameter.ljust(70), end=' ') |
| 47 | + |
| 48 | + k = 100 |
| 49 | + |
| 50 | + # warmup |
| 51 | + D, I = index.search(xq, k) |
| 52 | + |
| 53 | + # warmup |
| 54 | + D, I = index.search(xq, k) |
| 55 | + |
| 56 | + # eval |
| 57 | + t2_0 = time.time() |
| 58 | + D, I = index.search(xq, k) |
| 59 | + t2_1 = time.time() |
| 60 | + |
| 61 | + # eval |
| 62 | + index_ivf.pq.sync_transposed_centroids() |
| 63 | + t3_0 = time.time() |
| 64 | + D, I = index.search(xq, k) |
| 65 | + t3_1 = time.time() |
| 66 | + |
| 67 | + # eval |
| 68 | + index_ivf.pq.clear_transposed_centroids() |
| 69 | + t4_0 = time.time() |
| 70 | + D, I = index.search(xq, k) |
| 71 | + t4_1 = time.time() |
| 72 | + |
| 73 | + print(" %9.5f " % (t2_1 - t2_0), end=' ') |
| 74 | + print(" %9.5f " % (t3_1 - t3_0), end=' ') |
| 75 | + print(" %9.5f " % (t4_1 - t4_0), end=' ') |
| 76 | + |
| 77 | + for rank in 1, 10, 100: |
| 78 | + n_ok = (I[:, :rank] == gt[:, :1]).sum() |
| 79 | + print("%.4f" % (n_ok / float(nq)), end=' ') |
| 80 | + print() |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + faiss.contrib.datasets.dataset_basedir = '/home/aguzhva/ANN_SIFT1B/' |
| 85 | + |
| 86 | + # represents OPQ32_128,IVF65536_HNSW32,PQ32 index |
| 87 | + index_file_1 = "/home/aguzhva/ANN_SIFT1B/run_tests/bench_ivf/indexes/hnsw32/.faissindex" |
| 88 | + |
| 89 | + nprobe_values = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024] |
| 90 | + quantizer_efsearch_values = [4, 8, 16, 32, 64, 128, 256, 512] |
| 91 | + ht_values = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 256] |
| 92 | + |
| 93 | + # represents OPQ32_128,IVF65536(IVF256,PQHDx4fs,RFlat),PQ32 index |
| 94 | + index_file_2 = "/home/aguzhva/ANN_SIFT1B/run_tests/bench_ivf/indexes/pq4/.faissindex" |
| 95 | + |
| 96 | + quantizer_k_factor_rf_values = [1, 2, 4, 8, 16, 32, 64] |
| 97 | + quantizer_nprobe_values = [1, 2, 4, 8, 16, 32, 64, 128] |
| 98 | + |
| 99 | + # test the first index |
| 100 | + index_parameters_1 = [] |
| 101 | + for _ in range(0, 20): |
| 102 | + nprobe = random.choice(nprobe_values) |
| 103 | + quantizer_efsearch = random.choice(quantizer_efsearch_values) |
| 104 | + ht = random.choice(ht_values) |
| 105 | + index_parameters_1.append( |
| 106 | + "nprobe={},quantizer_efSearch={},ht={}".format( |
| 107 | + nprobe, |
| 108 | + quantizer_efsearch, |
| 109 | + ht) |
| 110 | + ) |
| 111 | + |
| 112 | + test_bigann10m(index_file_1, index_parameters_1) |
| 113 | + |
| 114 | + # test the second index |
| 115 | + index_parameters_2 = [] |
| 116 | + for _ in range(0, 20): |
| 117 | + nprobe = random.choice(nprobe_values) |
| 118 | + quantizer_k_factor_rf = random.choice(quantizer_k_factor_rf_values) |
| 119 | + quantizer_nprobe = random.choice(quantizer_nprobe_values) |
| 120 | + ht = random.choice(ht_values) |
| 121 | + index_parameters_2.append( |
| 122 | + "nprobe={},quantizer_k_factor_rf={},quantizer_nprobe={},ht={}".format( |
| 123 | + nprobe, |
| 124 | + quantizer_k_factor_rf, |
| 125 | + quantizer_nprobe, |
| 126 | + ht) |
| 127 | + ) |
| 128 | + |
| 129 | + test_bigann10m(index_file_2, index_parameters_2) |
0 commit comments