Debian Codegraph @

tutorial/cpp/11-SVS-Vamana-LeanVec.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/IndexSVSVamanaLeanVec.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::IndexSVSVamanaLeanVec index(d, 32);
59 index.train(nb, xb);
60 index.add(nb, xb);
61
62 { // search xq
63 idx_t* I = new idx_t[k * nq];
64 float* D = new float[k * nq];
65
66 index.search(nq, xq, k, D, I);
67
68 printf("I=\n");
69 for (int i = nq - 5; i < nq; i++) {
70 for (int j = 0; j < k; j++)
71 printf("%5zd ", I[i * k + j]);
72 printf("\n");
73 }
74
75 printf("D=\n");
76 for (int i = nq - 5; i < nq; i++) {
77 for (int j = 0; j < k; j++)
78 printf("%5f ", D[i * k + j]);
79 printf("\n");
80 }
81
82 delete[] I;
83 delete[] D;
84 }
85
86 delete[] xb;
87 delete[] xq;
88
89 return 0;
90 }