From 59923532aa1cb8a5def6a0f213bbd1f2e7c41b0a Mon Sep 17 00:00:00 2001 From: gouyt13clear Date: Sat, 21 Nov 2026 23:47:39 +0800 Subject: [PATCH 1/2] fix(utils)!: remove the evaluation helpers from tools.hpp distance_ratio and horizontal_avg in the installed header rabitqlib/utils/tools.hpp had undefined behavior. distance_ratio formed data + id * dim for every result ID, so a missing result (kNoId or kPidMax, which the index searches use for padding) read out of bounds. horizontal_avg read data[0] on empty input, read past shorter rows, and divided signed sums by a size_t, so horizontal_avg of three rows of -1 gave 1431655764. Neither is index functionality: distance_ratio has no callers, and horizontal_avg's only callers are the C++ querying samples. Remove both, and the , and includes only they used. The samples average their measurements with querying_example::column_averages in sample/cpp/querying_example.hpp. The release notes record the removal. --- docs/docs/releases/unreleased.md | 1 + include/rabitqlib/utils/tools.hpp | 59 ----------------------------- sample/cpp/hnsw_rabitq_querying.cpp | 6 +-- sample/cpp/ivf_rabitq_querying.cpp | 6 +-- sample/cpp/querying_example.hpp | 24 ++++++++++++ sample/cpp/symqg_querying.cpp | 6 +-- 6 files changed, 34 insertions(+), 68 deletions(-) create mode 100644 sample/cpp/querying_example.hpp diff --git a/docs/docs/releases/unreleased.md b/docs/docs/releases/unreleased.md index 6d289541..ef022a0a 100644 --- a/docs/docs/releases/unreleased.md +++ b/docs/docs/releases/unreleased.md @@ -127,6 +127,7 @@ Four differences are not renames: `HnswIndex::add` uses every CPU unless `num_th - The vendored hnswlib adds `saveIndex(std::ostream&)` and `loadIndex(std::istream&, size_t bytes, ...)`; the path overloads are unchanged. - `rabitqlib::ceil_log2` in `rabitqlib/utils/tools.hpp` shifts a `size_t`, so an input of 2^32 or more is no longer undefined on 64-bit Windows, where `unsigned long` has 32 bits (#247). The library does not call it. +- `rabitqlib::distance_ratio` and `rabitqlib::horizontal_avg` are removed from `rabitqlib/utils/tools.hpp`. They were evaluation helpers for the C++ samples, and the library does not call them. `distance_ratio` read out of bounds for a missing result ID (`kNoId` or `kPidMax`), and `horizontal_avg` read out of bounds for empty or ragged input and averaged negative integer columns wrongly, for example `horizontal_avg` gave 1431655764 for three rows of -1. Code that uses them needs its own version that handles these inputs; the samples now average their measurements with a local helper. `tools.hpp` no longer includes ``, so code that used `std::priority_queue` through RaBitQ headers must include `` itself. - `MatrixRotator`, which `choose_rotator` returns for `RotatorType::MatrixRotator` and `IVF` and `QuantizedGraph` use when given it, draws a Haar-distributed rotation, as described for `quant.Rotator` above. A given seed now produces a different matrix; saved rotators and indexes keep theirs. diff --git a/include/rabitqlib/utils/tools.hpp b/include/rabitqlib/utils/tools.hpp index b661f94f..8b50bee5 100644 --- a/include/rabitqlib/utils/tools.hpp +++ b/include/rabitqlib/utils/tools.hpp @@ -1,16 +1,13 @@ #pragma once #include -#include #include #include #include #include -#include #include #include #include -#include namespace rabitqlib { template @@ -114,60 +111,4 @@ inline int openmp_team_size(size_t requested, size_t work_items) { } } // namespace detail -template -float distance_ratio( - const T* data, - const T* query, - const TP* gt, - const TP* ann_results, - size_t k, - size_t dim, - T (*dist_func)(const T*, const T*, size_t) -) { - std::priority_queue gt_distances; - std::priority_queue ann_distances; - - for (size_t i = 0; i < k; ++i) { - TP gt_id = gt[i]; - TP ann_id = ann_results[i]; - gt_distances.emplace(dist_func(query, data + (gt_id * dim), dim)); - ann_distances.emplace(dist_func(query, data + (ann_id * dim), dim)); - } - - float ret = 0; - size_t valid_k = 0; - - while (!gt_distances.empty()) { - if (gt_distances.top() > 1e-5) { - ret += std::sqrt(ann_distances.top() / gt_distances.top()); - ++valid_k; - } - gt_distances.pop(); - ann_distances.pop(); - } - - if (valid_k == 0) { - return static_cast(k); - } - return ret * static_cast(k) / static_cast(valid_k); -} - -template -std::vector horizontal_avg(const std::vector>& data) { - size_t rows = data.size(); - size_t cols = data[0].size(); - - std::vector avg(cols, 0); - for (auto& row : data) { - for (size_t j = 0; j < cols; ++j) { - avg[j] += row[j]; - } - } - - for (size_t j = 0; j < cols; ++j) { - avg[j] /= rows; - } - - return avg; -} } // namespace rabitqlib diff --git a/sample/cpp/hnsw_rabitq_querying.cpp b/sample/cpp/hnsw_rabitq_querying.cpp index 797661c1..4820c00d 100644 --- a/sample/cpp/hnsw_rabitq_querying.cpp +++ b/sample/cpp/hnsw_rabitq_querying.cpp @@ -6,12 +6,12 @@ #include #include +#include "querying_example.hpp" #include "rabitqlib/core/detail/eigen.hpp" #include "rabitqlib/index/hnsw_index.hpp" #include "rabitqlib/index/types.hpp" #include "rabitqlib/utils/io.hpp" #include "rabitqlib/utils/stopw.hpp" -#include "rabitqlib/utils/tools.hpp" std::vector efs = {10, 20, 40, 50, 60, 80, 100, 150, 170, 190, 200, 250, 300, 400, 500, 600, 700, 800, 1000, 1500, 2000}; @@ -100,8 +100,8 @@ int run(int argc, char* argv[]) { } } - auto avg_qps = rabitqlib::horizontal_avg(all_qps); - auto avg_recall = rabitqlib::horizontal_avg(all_recall); + auto avg_qps = querying_example::column_averages(all_qps); + auto avg_recall = querying_example::column_averages(all_recall); std::cout << "EF\tQPS\tRecall\t" diff --git a/sample/cpp/ivf_rabitq_querying.cpp b/sample/cpp/ivf_rabitq_querying.cpp index 5d633546..40249b9e 100644 --- a/sample/cpp/ivf_rabitq_querying.cpp +++ b/sample/cpp/ivf_rabitq_querying.cpp @@ -6,12 +6,12 @@ #include #include +#include "querying_example.hpp" #include "rabitqlib/core/detail/eigen.hpp" #include "rabitqlib/index/ivf_index.hpp" #include "rabitqlib/index/types.hpp" #include "rabitqlib/utils/io.hpp" #include "rabitqlib/utils/stopw.hpp" -#include "rabitqlib/utils/tools.hpp" using index_type = rabitqlib::IvfIndex; using data_type = rabitqlib::RowMajorArray; @@ -116,8 +116,8 @@ int run(int argc, char** argv) { } } - auto avg_qps = rabitqlib::horizontal_avg(all_qps); - auto avg_recall = rabitqlib::horizontal_avg(all_recall); + auto avg_qps = querying_example::column_averages(all_qps); + auto avg_recall = querying_example::column_averages(all_recall); std::cout << "nprobe\tQPS\trecall" << '\n'; diff --git a/sample/cpp/querying_example.hpp b/sample/cpp/querying_example.hpp new file mode 100644 index 00000000..0e17c668 --- /dev/null +++ b/sample/cpp/querying_example.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace querying_example { + +// The average of each column over the rounds. The querying samples store one row per +// round, at least one, with one value per search setting, so every row has the same +// length. +inline std::vector column_averages(const std::vector>& rows) { + std::vector averages(rows.front().size(), 0.0F); + for (const auto& row : rows) { + for (size_t column = 0; column < averages.size(); ++column) { + averages[column] += row[column]; + } + } + for (auto& average : averages) { + average /= static_cast(rows.size()); + } + return averages; +} + +} // namespace querying_example diff --git a/sample/cpp/symqg_querying.cpp b/sample/cpp/symqg_querying.cpp index 92fefcdf..51ea1bd3 100644 --- a/sample/cpp/symqg_querying.cpp +++ b/sample/cpp/symqg_querying.cpp @@ -4,12 +4,12 @@ #include #include +#include "querying_example.hpp" #include "rabitqlib/core/detail/eigen.hpp" #include "rabitqlib/index/symqg_index.hpp" #include "rabitqlib/index/types.hpp" #include "rabitqlib/utils/io.hpp" #include "rabitqlib/utils/stopw.hpp" -#include "rabitqlib/utils/tools.hpp" using index_type = rabitqlib::SymqgIndex; using data_type = rabitqlib::RowMajorArray; @@ -98,8 +98,8 @@ int run(int argc, char** argv) { } } - auto avg_qps = rabitqlib::horizontal_avg(all_qps); - auto avg_recall = rabitqlib::horizontal_avg(all_recall); + auto avg_qps = querying_example::column_averages(all_qps); + auto avg_recall = querying_example::column_averages(all_recall); std::cout << "EF\tQPS\tRecall\n"; for (size_t i = 0; i < avg_qps.size(); ++i) { From 0aac0d80f7b12f325c230a748b5ee04c85cadfcf Mon Sep 17 00:00:00 2001 From: gouyt13clear Date: Sat, 21 Nov 2026 23:47:58 +0800 Subject: [PATCH 2/2] docs(releases): link the removal of the evaluation helpers to #262 --- docs/docs/releases/unreleased.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/releases/unreleased.md b/docs/docs/releases/unreleased.md index ef022a0a..54fbf483 100644 --- a/docs/docs/releases/unreleased.md +++ b/docs/docs/releases/unreleased.md @@ -127,7 +127,7 @@ Four differences are not renames: `HnswIndex::add` uses every CPU unless `num_th - The vendored hnswlib adds `saveIndex(std::ostream&)` and `loadIndex(std::istream&, size_t bytes, ...)`; the path overloads are unchanged. - `rabitqlib::ceil_log2` in `rabitqlib/utils/tools.hpp` shifts a `size_t`, so an input of 2^32 or more is no longer undefined on 64-bit Windows, where `unsigned long` has 32 bits (#247). The library does not call it. -- `rabitqlib::distance_ratio` and `rabitqlib::horizontal_avg` are removed from `rabitqlib/utils/tools.hpp`. They were evaluation helpers for the C++ samples, and the library does not call them. `distance_ratio` read out of bounds for a missing result ID (`kNoId` or `kPidMax`), and `horizontal_avg` read out of bounds for empty or ragged input and averaged negative integer columns wrongly, for example `horizontal_avg` gave 1431655764 for three rows of -1. Code that uses them needs its own version that handles these inputs; the samples now average their measurements with a local helper. `tools.hpp` no longer includes ``, so code that used `std::priority_queue` through RaBitQ headers must include `` itself. +- `rabitqlib::distance_ratio` and `rabitqlib::horizontal_avg` are removed from `rabitqlib/utils/tools.hpp` (#262). They were evaluation helpers for the C++ samples, and the library does not call them. `distance_ratio` read out of bounds for a missing result ID (`kNoId` or `kPidMax`), and `horizontal_avg` read out of bounds for empty or ragged input and averaged negative integer columns wrongly, for example `horizontal_avg` gave 1431655764 for three rows of -1. Code that uses them needs its own version that handles these inputs; the samples now average their measurements with a local helper. `tools.hpp` no longer includes ``, so code that used `std::priority_queue` through RaBitQ headers must include `` itself. - `MatrixRotator`, which `choose_rotator` returns for `RotatorType::MatrixRotator` and `IVF` and `QuantizedGraph` use when given it, draws a Haar-distributed rotation, as described for `quant.Rotator` above. A given seed now produces a different matrix; saved rotators and indexes keep theirs.