Skip to content

Commit 94e24ba

Browse files
ahuber21ibhatidian-lun-lin
authored
feat: Add scalar quantization dataset (#97)
This PR adds a new class `SQDataset` which implements global scalar quantization. --------- Co-authored-by: ibhati <ishwar.s.bhati@intel.com> Co-authored-by: Dian-Lun (Aaron) Lin <aaron.dl.lin@intel.com>
1 parent 2c642d6 commit 94e24ba

File tree

8 files changed

+1319
-3
lines changed

8 files changed

+1319
-3
lines changed

examples/cpp/vamana.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
//! [Includes]
2020
// SVS Dependencies
21-
#include "svs/orchestrators/vamana.h" // bulk of the dependencies required.
22-
#include "svs/core/recall.h" // Convenient k-recall@n computation.
21+
#include "svs/orchestrators/vamana.h" // bulk of the dependencies required.
22+
#include "svs/core/recall.h" // Convenient k-recall@n computation.
23+
#include "svs/extensions/vamana/scalar.h" // SQ vamana extensions.
24+
#include "svs/quantization/scalar/scalar.h" // SQ implementation.
2325

2426
// Alternative main definition
2527
#include "svsmain.h"
@@ -51,7 +53,7 @@ double run_recall(
5153
}
5254

5355
const bool DEBUG = false;
54-
void check(double expected, double got, double eps = 0.001) {
56+
void check(double expected, double got, double eps = 0.005) {
5557
double diff = std::abs(expected - got);
5658
if constexpr (DEBUG) {
5759
fmt::print("Expected {}. Got {}\n", expected, got);
@@ -156,6 +158,36 @@ int svs_main(std::vector<std::string> args) {
156158
index.set_threadpool(svs::threads::DefaultThreadPool(4));
157159
//! [Set a new thread pool with n-threads]
158160

161+
//! [Compressed Loader]
162+
// Quantization
163+
namespace scalar = svs::quantization::scalar;
164+
165+
// Wrap the compressor object in a lazy functor.
166+
// This will defer loading and compression of the SQ dataset until the threadpool
167+
// used in the index has been created.
168+
auto compressor = svs::lib::Lazy([=](svs::threads::ThreadPool auto& threadpool) {
169+
auto data = svs::VectorDataLoader<float, 128>("example_data").load();
170+
return scalar::SQDataset<std::int8_t, 128>::compress(data, threadpool);
171+
});
172+
index = svs::Vamana::assemble<float>(
173+
"example_config",
174+
svs::GraphLoader("example_graph"),
175+
compressor,
176+
svs::DistanceType::L2,
177+
4
178+
);
179+
recall = run_recall(index, queries, groundtruth, 30, 10, "Compressed load");
180+
check(0.8190, recall);
181+
//! [Compressed Loader]
182+
183+
//! [Build Index Compressed]
184+
// Compressed building
185+
index =
186+
svs::Vamana::build<float>(parameters, compressor, svs::DistanceL2(), num_threads);
187+
recall = run_recall(index, queries, groundtruth, 30, 10, "Compressed Build");
188+
check(0.8212, recall);
189+
//! [Build Index Compressed]
190+
159191
return 0;
160192
}
161193

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#include "svs/index/vamana/extensions.h"
18+
#include "svs/quantization/scalar/scalar.h"
19+
20+
namespace svs::quantization::scalar {
21+
22+
template <IsSQData Data>
23+
SVS_FORCE_INLINE data::GetDatumAccessor svs_invoke(
24+
svs::tag_t<svs::index::vamana::extensions::reconstruct_accessor> SVS_UNUSED(cpo),
25+
const Data& SVS_UNUSED(data)
26+
) {
27+
return data::GetDatumAccessor();
28+
}
29+
30+
template <IsSQData Data, typename Distance>
31+
auto svs_invoke(
32+
svs::tag_t<svs::index::vamana::extensions::single_search_setup>,
33+
const Data& data,
34+
const Distance& SVS_UNUSED(distance)
35+
) {
36+
return compressed_distance_t<Distance, typename Data::element_type>(
37+
data.get_scale(), data.get_bias(), data.dimensions()
38+
);
39+
}
40+
41+
} // namespace svs::quantization::scalar

0 commit comments

Comments
 (0)