Debian Codegraph @

tutorial/cpp/10-SVS-Vamana-LVQ.cpp

raw
1 /*
2 * Portions Copyright (c) Meta Platforms, Inc. and 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 /*
9 * Portions Copyright 2025 Intel Corporation
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24 #include <cassert>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <random>
28
29 #include <faiss/svs/IndexSVSVamanaLVQ.h>
30
31 using idx_t = faiss::idx_t;
32
33 int main() {
34 int d = 64; // dimension
35 int nb = 100000; // database size
36 int nq = 10000; // nb of queries
37
38 std::mt19937 rng;
39 std::uniform_real_distribution<> distrib;
40
41 float* xb = new float[d * nb];
42 float* xq = new float[d * nq];
43
44 for (int i = 0; i < nb; i++) {
45 for (int j = 0; j < d; j++)
46 xb[d * i + j] = distrib(rng);
47 xb[d * i] += i / 1000.;
48 }
49
50 for (int i = 0; i < nq; i++) {
51 for (int j = 0; j < d; j++)
52 xq[d * i + j] = distrib(rng);
53 xq[d * i] += i / 1000.;
54 }
55
56 int k = 4;
57
58 faiss::IndexSVSVamanaLVQ index(d, 64);
59 index.add(nb, xb);
60
61 { // search xq
62 idx_t* I = new idx_t[k * nq];
63 float* D = new float[k * nq];
64
65 index.search(nq, xq, k, D, I);
66
67 printf("I=\n");
68 for (int i = nq - 5; i < nq; i++) {
69 for (int j = 0; j < k; j++)
70 printf("%5zd ", I[i * k + j]);
71 printf("\n");
72 }
73
74 printf("D=\n");
75 for (int i = nq - 5; i < nq; i++) {
76 for (int j = 0; j < k; j++)
77 printf("%5f ", D[i * k + j]);
78 printf("\n");
79 }
80
81 delete[] I;
82 delete[] D;
83 }
84
85 delete[] xb;
86 delete[] xq;
87
88 return 0;
89 }