|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <cassert> |
| 9 | +#include <cstdio> |
| 10 | +#include <cstdlib> |
| 11 | +#include <random> |
| 12 | + |
| 13 | +#include <faiss/IndexPQFastScan.h> |
| 14 | +#include <faiss/IndexRefine.h> |
| 15 | +#include <faiss/index_factory.h> |
| 16 | +using idx_t = faiss::idx_t; |
| 17 | + |
| 18 | +int main() { |
| 19 | + int d = 64; // dimension |
| 20 | + int nb = 100000; // database size |
| 21 | + int nq = 10000; // nb of queries |
| 22 | + |
| 23 | + std::mt19937 rng; |
| 24 | + std::uniform_real_distribution<> distrib; |
| 25 | + |
| 26 | + float* xb = new float[(int)(d * nb)]; |
| 27 | + float* xq = new float[(int)(d * nq)]; |
| 28 | + |
| 29 | + for (int i = 0; i < nb; i++) { |
| 30 | + for (int j = 0; j < d; j++) { |
| 31 | + xb[d * i + j] = distrib(rng); |
| 32 | + } |
| 33 | + xb[d * i] += i / 1000.; |
| 34 | + } |
| 35 | + |
| 36 | + for (int i = 0; i < nq; i++) { |
| 37 | + for (int j = 0; j < d; j++) { |
| 38 | + xq[d * i + j] = distrib(rng); |
| 39 | + } |
| 40 | + xq[d * i] += i / 1000.; |
| 41 | + } |
| 42 | + |
| 43 | + int m = 8; |
| 44 | + int n_bit = 4; |
| 45 | + |
| 46 | + // Constructing the refine PQ index with SQfp16 with index factory |
| 47 | + faiss::Index* index_fp16; |
| 48 | + index_fp16 = faiss::index_factory( |
| 49 | + d, "PQ32x4fs,Refine(SQfp16)", faiss::METRIC_L2); |
| 50 | + index_fp16->train(nb, xb); |
| 51 | + index_fp16->add(nb, xb); |
| 52 | + |
| 53 | + // Constructing the refine PQ index with SQ8 |
| 54 | + faiss::Index* index_sq8; |
| 55 | + index_sq8 = |
| 56 | + faiss::index_factory(d, "PQ32x4fs,Refine(SQ8)", faiss::METRIC_L2); |
| 57 | + index_sq8->train(nb, xb); |
| 58 | + index_sq8->add(nb, xb); |
| 59 | + |
| 60 | + int k = 10; |
| 61 | + { // search xq |
| 62 | + idx_t* I_fp16 = new idx_t[(int)(k * nq)]; |
| 63 | + float* D_fp16 = new float[(int)(k * nq)]; |
| 64 | + idx_t* I_sq8 = new idx_t[(int)(k * nq)]; |
| 65 | + float* D_sq8 = new float[(int)(k * nq)]; |
| 66 | + |
| 67 | + // Parameterization on k factor while doing search for index refinement |
| 68 | + float k_factor = 3; |
| 69 | + faiss::IndexRefineSearchParameters* params = |
| 70 | + new faiss::IndexRefineSearchParameters(); |
| 71 | + params->k_factor = k_factor; |
| 72 | + |
| 73 | + // Perform index search using different index refinement |
| 74 | + index_fp16->search(nq, xq, k, D_fp16, I_fp16, params); |
| 75 | + index_sq8->search(nq, xq, k, D_sq8, I_sq8, params); |
| 76 | + |
| 77 | + printf("I_fp16=\n"); |
| 78 | + for (int i = nq - 5; i < nq; i++) { |
| 79 | + for (int j = 0; j < k; j++) { |
| 80 | + printf("%5zd ", I_fp16[i * k + j]); |
| 81 | + } |
| 82 | + printf("\n"); |
| 83 | + } |
| 84 | + |
| 85 | + printf("I_sq8=\n"); |
| 86 | + for (int i = nq - 5; i < nq; i++) { |
| 87 | + for (int j = 0; j < k; j++) { |
| 88 | + printf("%5zd ", I_sq8[i * k + j]); |
| 89 | + } |
| 90 | + printf("\n"); |
| 91 | + } |
| 92 | + |
| 93 | + delete[] I_fp16; |
| 94 | + delete[] D_fp16; |
| 95 | + delete[] I_sq8; |
| 96 | + delete[] D_sq8; |
| 97 | + delete params; |
| 98 | + |
| 99 | + delete index_fp16; |
| 100 | + delete index_sq8; |
| 101 | + } |
| 102 | + |
| 103 | + delete[] xb; |
| 104 | + delete[] xq; |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
0 commit comments