|
| 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 | +// Alias for blocked Lean dataset that supports resize/compact |
| 28 | +using BlockedLean = svs::leanvec::LeanDataset< |
| 29 | + svs::leanvec::UsingLVQ<4>, |
| 30 | + svs::leanvec::UsingLVQ<8>, |
| 31 | + svs::Dynamic, |
| 32 | + svs::Dynamic, |
| 33 | + svs::data::Blocked<svs::HugepageAllocator<std::byte>>>; |
| 34 | + |
| 35 | +int main() { |
| 36 | + // STEP 1: Compress Data with LeanVec, reducing dimensionality to leanvec_dim dimensions |
| 37 | + // and using 4 and 8 bits for primary and secondary levels respectively. |
| 38 | + //! [Compress data] |
| 39 | + const size_t num_threads = 4; |
| 40 | + size_t padding = 32; |
| 41 | + size_t leanvec_dim = 64; |
| 42 | + auto threadpool = svs::threads::as_threadpool(num_threads); |
| 43 | + auto loaded = |
| 44 | + svs::VectorDataLoader<float>(std::filesystem::path(SVS_DATA_DIR) / "data_f32.svs") |
| 45 | + .load(); |
| 46 | + auto data = BlockedLean::reduce( |
| 47 | + loaded, |
| 48 | + std::nullopt, |
| 49 | + threadpool, |
| 50 | + padding, |
| 51 | + svs::lib::MaybeStatic<svs::Dynamic>(leanvec_dim) |
| 52 | + ); |
| 53 | + //! [Compress data] |
| 54 | + |
| 55 | + // STEP 2: Build Dynamic Vamana Index with initial set of vectors |
| 56 | + //! [Index Build] |
| 57 | + auto parameters = svs::index::vamana::VamanaBuildParameters{}; |
| 58 | + |
| 59 | + // Create id labels for build set |
| 60 | + std::vector<size_t> ids_build(loaded.size()); |
| 61 | + for (size_t i = 0; i < loaded.size(); ++i) { |
| 62 | + ids_build[i] = i; |
| 63 | + } |
| 64 | + |
| 65 | + svs::DynamicVamana index = svs::DynamicVamana::build<float>( |
| 66 | + parameters, |
| 67 | + data, |
| 68 | + svs::lib::as_span(ids_build), |
| 69 | + svs::distance::DistanceL2(), |
| 70 | + num_threads |
| 71 | + ); |
| 72 | + //! [Index Build] |
| 73 | + |
| 74 | + // STEP 3: Add and delete vectors as needed. |
| 75 | + //! [Delete vectors] |
| 76 | + size_t num_to_delete = 100; |
| 77 | + std::vector<size_t> ids_delete(num_to_delete); |
| 78 | + for (size_t i = 0; i < ids_delete.size(); ++i) { |
| 79 | + ids_delete[i] = i; |
| 80 | + } |
| 81 | + |
| 82 | + fmt::print("Deleting {} vectors.\n", ids_delete.size()); |
| 83 | + |
| 84 | + index.delete_points(ids_delete); |
| 85 | + //! [Delete vectors] |
| 86 | + |
| 87 | + //! [Add vectors] |
| 88 | + // Add the deleted vectors back in. |
| 89 | + auto points = |
| 90 | + svs::data::SimpleData<float, svs::Dynamic>(ids_delete.size(), loaded.dimensions()); |
| 91 | + |
| 92 | + size_t i = 0; |
| 93 | + for (const auto& j : ids_delete) { |
| 94 | + points.set_datum(i, loaded.get_datum(j)); |
| 95 | + ++i; |
| 96 | + } |
| 97 | + auto points_const_view = points.cview(); |
| 98 | + |
| 99 | + fmt::print("Adding {} vectors.\n", ids_delete.size()); |
| 100 | + |
| 101 | + index.add_points(points_const_view, svs::lib::as_span(ids_delete), num_threads); |
| 102 | + //! [Add vectors] |
| 103 | + |
| 104 | + // STEP 4: Search the Index |
| 105 | + //! [Perform Queries] |
| 106 | + const size_t search_window_size = 50; |
| 107 | + const size_t n_neighbors = 10; |
| 108 | + index.set_search_window_size(search_window_size); |
| 109 | + |
| 110 | + auto queries = |
| 111 | + svs::load_data<float>(std::filesystem::path(SVS_DATA_DIR) / "queries_f32.fvecs"); |
| 112 | + auto results = index.search(queries, n_neighbors); |
| 113 | + //! [Perform Queries] |
| 114 | + |
| 115 | + //! [Recall] |
| 116 | + auto groundtruth = svs::load_data<int>( |
| 117 | + std::filesystem::path(SVS_DATA_DIR) / "groundtruth_euclidean.ivecs" |
| 118 | + ); |
| 119 | + double recall = svs::k_recall_at_n(groundtruth, results, n_neighbors, n_neighbors); |
| 120 | + |
| 121 | + fmt::print("Recall@{} = {:.4f}\n", n_neighbors, recall); |
| 122 | + fmt::print( |
| 123 | + "Note that recall is low because this example is using a dummy random dataset.\n" |
| 124 | + ); |
| 125 | + //! [Recall] |
| 126 | + |
| 127 | + // STEP 5: Saving and reloading the index |
| 128 | + //! [Saving Loading] |
| 129 | + index.save("config", "graph", "data"); |
| 130 | + index = svs::DynamicVamana::assemble<float>( |
| 131 | + "config", |
| 132 | + svs::GraphLoader("graph"), |
| 133 | + svs::lib::load_from_disk<BlockedLean>("data", padding), |
| 134 | + svs::distance::DistanceL2(), |
| 135 | + num_threads |
| 136 | + ); |
| 137 | + //! [Saving Loading] |
| 138 | + index.set_search_window_size(search_window_size); |
| 139 | + recall = svs::k_recall_at_n(groundtruth, results, n_neighbors, n_neighbors); |
| 140 | + |
| 141 | + fmt::print("Recall@{} after saving and reloading = {:.4f}\n", n_neighbors, recall); |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
0 commit comments