|
| 1 | +/* |
| 2 | + * Copyright 2025 Intel Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// SVS |
| 18 | +#include "svs/core/recall.h" |
| 19 | +#include "svs/extensions/flat/leanvec.h" |
| 20 | +#include "svs/extensions/flat/lvq.h" |
| 21 | +#include "svs/extensions/vamana/leanvec.h" |
| 22 | +#include "svs/extensions/vamana/lvq.h" |
| 23 | +#include "svs/orchestrators/dynamic_vamana.h" |
| 24 | +#include "svs/orchestrators/exhaustive.h" |
| 25 | +#include "svs/orchestrators/vamana.h" |
| 26 | + |
| 27 | +int main() { |
| 28 | + // STEP 1: Compress Data with LeanVec, reducing dimensionality to leanvec_dim dimensions |
| 29 | + // and using 4 and 8 bits for primary and secondary levels respectively. |
| 30 | + //! [Compress data] |
| 31 | + const size_t num_threads = 4; |
| 32 | + size_t padding = 32; |
| 33 | + size_t leanvec_dim = 64; |
| 34 | + auto threadpool = svs::threads::as_threadpool(num_threads); |
| 35 | + auto loaded = |
| 36 | + svs::VectorDataLoader<float>(std::filesystem::path(SVS_DATA_DIR) / "data_f32.svs") |
| 37 | + .load(); |
| 38 | + auto data = svs::leanvec::LeanDataset< |
| 39 | + svs::leanvec::UsingLVQ<4>, |
| 40 | + svs::leanvec::UsingLVQ<8>, |
| 41 | + svs::Dynamic, |
| 42 | + svs::Dynamic>:: |
| 43 | + reduce( |
| 44 | + loaded, |
| 45 | + std::nullopt, |
| 46 | + threadpool, |
| 47 | + padding, |
| 48 | + svs::lib::MaybeStatic<svs::Dynamic>(leanvec_dim) |
| 49 | + ); |
| 50 | + //! [Compress data] |
| 51 | + |
| 52 | + // STEP 2: Build Vamana Index |
| 53 | + //! [Index Build] |
| 54 | + auto parameters = svs::index::vamana::VamanaBuildParameters{}; |
| 55 | + svs::Vamana index = svs::Vamana::build<float>( |
| 56 | + parameters, data, svs::distance::DistanceL2(), num_threads |
| 57 | + ); |
| 58 | + //! [Index Build] |
| 59 | + |
| 60 | + // STEP 3: Search the Index |
| 61 | + //! [Perform Queries] |
| 62 | + const size_t search_window_size = 50; |
| 63 | + const size_t n_neighbors = 10; |
| 64 | + index.set_search_window_size(search_window_size); |
| 65 | + |
| 66 | + auto queries = |
| 67 | + svs::load_data<float>(std::filesystem::path(SVS_DATA_DIR) / "queries_f32.fvecs"); |
| 68 | + auto results = index.search(queries, n_neighbors); |
| 69 | + //! [Perform Queries] |
| 70 | + |
| 71 | + //! [Recall] |
| 72 | + auto groundtruth = svs::load_data<int>( |
| 73 | + std::filesystem::path(SVS_DATA_DIR) / "groundtruth_euclidean.ivecs" |
| 74 | + ); |
| 75 | + double recall = svs::k_recall_at_n(groundtruth, results, n_neighbors, n_neighbors); |
| 76 | + |
| 77 | + fmt::print("Recall@{} = {:.4f}\n", n_neighbors, recall); |
| 78 | + //! [Recall] |
| 79 | + |
| 80 | + // STEP 4: Saving and reloading the index |
| 81 | + //! [Saving Loading] |
| 82 | + index.save("config", "graph", "data"); |
| 83 | + index = svs::Vamana::assemble<float>( |
| 84 | + "config", |
| 85 | + svs::GraphLoader("graph"), |
| 86 | + svs::lib::load_from_disk<svs::leanvec::LeanDataset< |
| 87 | + svs::leanvec::UsingLVQ<4>, |
| 88 | + svs::leanvec::UsingLVQ<8>, |
| 89 | + svs::Dynamic, |
| 90 | + svs::Dynamic>>("data", padding), |
| 91 | + svs::distance::DistanceL2(), |
| 92 | + num_threads |
| 93 | + ); |
| 94 | + //! [Saving Loading] |
| 95 | + index.set_search_window_size(search_window_size); |
| 96 | + recall = svs::k_recall_at_n(groundtruth, results, n_neighbors, n_neighbors); |
| 97 | + |
| 98 | + fmt::print("Recall@{} after saving and reloading = {:.4f}\n", n_neighbors, recall); |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
0 commit comments