|
| 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 | + |
| 28 | +int main() { |
| 29 | + // STEP 1: Compress Data with LeanVec, reducing dimensionality to leanvec_dim dimensions and using |
| 30 | + // 4 and 8 bits for primary and secondary levels respectively. |
| 31 | + //! [Compress data] |
| 32 | + const size_t num_threads = 4; |
| 33 | + size_t padding = 32; |
| 34 | + size_t leanvec_dim = 64; |
| 35 | + auto threadpool = svs::threads::as_threadpool(num_threads); |
| 36 | + auto loaded = svs::VectorDataLoader<float>(std::filesystem::path(SVS_DATA_DIR) / "data_f32.svs").load(); |
| 37 | + auto data = svs::leanvec::LeanDataset<svs::leanvec::UsingLVQ<4>, svs::leanvec::UsingLVQ<8>, svs::Dynamic, svs::Dynamic>::reduce( |
| 38 | + loaded, std::nullopt, threadpool, padding, svs::lib::MaybeStatic<svs::Dynamic>(leanvec_dim) |
| 39 | + ); |
| 40 | + //! [Compress data] |
| 41 | + |
| 42 | + |
| 43 | + // STEP 2: Build Vamana Index |
| 44 | + //! [Index Build] |
| 45 | + auto parameters = svs::index::vamana::VamanaBuildParameters{}; |
| 46 | + svs::Vamana index = svs::Vamana::build<float>(parameters, data, svs::distance::DistanceL2(), num_threads); |
| 47 | + //! [Index Build] |
| 48 | + |
| 49 | + // STEP 3: Search the Index |
| 50 | + //! [Perform Queries] |
| 51 | + const size_t search_window_size = 50; |
| 52 | + const size_t n_neighbors = 10; |
| 53 | + index.set_search_window_size(search_window_size); |
| 54 | + |
| 55 | + auto queries = svs::load_data<float>(std::filesystem::path(SVS_DATA_DIR) / "queries_f32.fvecs"); |
| 56 | + auto results = index.search(queries, n_neighbors); |
| 57 | + //! [Perform Queries] |
| 58 | + |
| 59 | + //! [Recall] |
| 60 | + auto groundtruth = svs::load_data<int>(std::filesystem::path(SVS_DATA_DIR) / "groundtruth_euclidean.ivecs"); |
| 61 | + double recall = svs::k_recall_at_n(groundtruth, results, n_neighbors, n_neighbors); |
| 62 | + |
| 63 | + fmt::print("Recall@{} = {:.4f}\n", n_neighbors, recall); |
| 64 | + //! [Recall] |
| 65 | + |
| 66 | + // STEP 4: Saving and reloading the index |
| 67 | + //! [Saving Loading] |
| 68 | + index.save("config", "graph", "data"); |
| 69 | + index = svs::Vamana::assemble<float>( |
| 70 | + "config", svs::GraphLoader("graph"), svs::lib::load_from_disk<svs::leanvec::LeanDataset<svs::leanvec::UsingLVQ<4>, svs::leanvec::UsingLVQ<8>, svs::Dynamic, svs::Dynamic>>("data", padding), svs::distance::DistanceL2(), num_threads |
| 71 | + ); |
| 72 | + //! [Saving Loading] |
| 73 | + index.set_search_window_size(search_window_size); |
| 74 | + recall = svs::k_recall_at_n(groundtruth, results, n_neighbors, n_neighbors); |
| 75 | + |
| 76 | + fmt::print("Recall@{} after saving and reloading = {:.4f}\n", n_neighbors, recall); |
| 77 | + |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
0 commit comments