forked from intel/ScalableVectorSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_based.cpp
More file actions
150 lines (133 loc) · 5.56 KB
/
Copy pathmemory_based.cpp
File metadata and controls
150 lines (133 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright 2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "svs/index/inverted/memory_based.h"
#include "catch2/catch_test_macros.hpp"
#include "spdlog/sinks/callback_sink.h"
#include "svs-benchmark/datasets.h"
#include "svs/lib/timing.h"
#include "svs/orchestrators/inverted.h"
#include "tests/utils/inverted_reference.h"
#include "tests/utils/test_dataset.h"
#include <filesystem>
#include <sstream>
CATCH_TEST_CASE("InvertedIndex Logging Test", "[long][logging]") {
// Vector to store captured log messages
std::vector<std::string> captured_logs;
std::vector<std::string> global_captured_logs;
// Create a callback sink to capture log messages
auto callback_sink = std::make_shared<spdlog::sinks::callback_sink_mt>(
[&captured_logs](const spdlog::details::log_msg& msg) {
captured_logs.emplace_back(msg.payload.data(), msg.payload.size());
}
);
callback_sink->set_level(spdlog::level::trace);
// Create a logger with the callback sink
auto test_logger = std::make_shared<spdlog::logger>("test_logger", callback_sink);
test_logger->set_level(spdlog::level::trace);
auto global_callback_sink = std::make_shared<spdlog::sinks::callback_sink_mt>(
[&global_captured_logs](const spdlog::details::log_msg& msg) {
global_captured_logs.emplace_back(msg.payload.data(), msg.payload.size());
}
);
global_callback_sink->set_level(spdlog::level::trace);
auto original_logger = svs::logging::get();
original_logger->sinks().push_back(global_callback_sink);
// Setup index
auto distance = svs::DistanceL2();
constexpr auto distance_type = svs::distance_type_v<svs::DistanceL2>;
auto expected_results = test_dataset::inverted::expected_build_results(
distance_type, svsbenchmark::Uncompressed(svs::DataType::float32)
);
auto data = svs::data::SimpleData<float>::load(test_dataset::data_svs_file());
auto threadpool = svs::threads::DefaultThreadPool(1);
auto invertedIndex = svs::index::inverted::auto_build(
expected_results.build_parameters_.value(),
data,
distance,
std::move(threadpool),
{},
{},
{},
test_logger
);
// Verify the internal log messages
CATCH_REQUIRE(global_captured_logs.empty());
CATCH_REQUIRE(captured_logs[0].find("Vamana Build Parameters:") != std::string::npos);
CATCH_REQUIRE(captured_logs[1].find("Number of syncs") != std::string::npos);
}
namespace {
constexpr size_t NUM_NEIGHBORS = 10;
template <typename Strategy> void test_stream_save_load(Strategy strategy) {
auto distance = svs::DistanceL2();
constexpr auto distance_type = svs::distance_type_v<svs::DistanceL2>;
auto expected_results = test_dataset::inverted::expected_build_results(
distance_type, svsbenchmark::Uncompressed(svs::DataType::float32)
);
auto build_parameters = expected_results.build_parameters_.value();
// Capture the clustering during build.
svs::index::inverted::Clustering<uint32_t> clustering;
auto clustering_op = [&](const auto& c) { clustering = c; };
svs::Inverted index = svs::Inverted::build<float>(
build_parameters,
svs::data::SimpleData<float>::load(test_dataset::data_svs_file()),
distance,
2,
strategy,
svs::index::inverted::PickRandomly{},
clustering_op
);
auto queries = svs::data::SimpleData<float>::load(test_dataset::query_file());
auto parameters = index.get_search_parameters();
auto results = index.search(queries, NUM_NEIGHBORS);
// Serialize to stream.
std::stringstream ss;
svs::lib::save_to_stream(clustering, ss);
index.save_primary_index(ss);
// Load from stream.
svs::Inverted loaded = svs::Inverted::assemble_from_clustering<svs::lib::Types<float>>(
ss,
svs::data::SimpleData<float>::load(test_dataset::data_svs_file()),
distance,
2,
strategy
);
loaded.set_search_parameters(parameters);
// Compare basic properties.
CATCH_REQUIRE(loaded.size() == index.size());
CATCH_REQUIRE(loaded.dimensions() == index.dimensions());
// Compare search results element-wise.
auto loaded_results = loaded.search(queries, NUM_NEIGHBORS);
CATCH_REQUIRE(loaded_results.n_queries() == results.n_queries());
CATCH_REQUIRE(loaded_results.n_neighbors() == results.n_neighbors());
for (size_t q = 0; q < results.n_queries(); ++q) {
for (size_t i = 0; i < NUM_NEIGHBORS; ++i) {
CATCH_REQUIRE(loaded_results.index(q, i) == results.index(q, i));
CATCH_REQUIRE(
loaded_results.distance(q, i) ==
Catch::Approx(results.distance(q, i)).epsilon(1e-5)
);
}
}
}
} // namespace
CATCH_TEST_CASE("InvertedIndex Save and Load", "[saveload][inverted][index]") {
CATCH_SECTION("SparseStrategy") {
test_stream_save_load(svs::index::inverted::SparseStrategy());
}
CATCH_SECTION("DenseStrategy") {
test_stream_save_load(svs::index::inverted::DenseStrategy());
}
}