Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/releases/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` (#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<int>` 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 `<queue>`, so code that used `std::priority_queue` through RaBitQ headers must include `<queue>` 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.

Expand Down
59 changes: 0 additions & 59 deletions include/rabitqlib/utils/tools.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#pragma once

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <queue>
#include <random>
#include <thread>
#include <type_traits>
#include <vector>

namespace rabitqlib {
template <typename T>
Expand Down Expand Up @@ -114,60 +111,4 @@ inline int openmp_team_size(size_t requested, size_t work_items) {
}
} // namespace detail

template <typename T, typename TP>
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<float> gt_distances;
std::priority_queue<float> 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<float>(k);
}
return ret * static_cast<float>(k) / static_cast<float>(valid_k);
}

template <typename T>
std::vector<T> horizontal_avg(const std::vector<std::vector<T>>& data) {
size_t rows = data.size();
size_t cols = data[0].size();

std::vector<T> 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
6 changes: 3 additions & 3 deletions sample/cpp/hnsw_rabitq_querying.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#include <ratio>
#include <vector>

#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<size_t> efs = {10, 20, 40, 50, 60, 80, 100, 150, 170, 190, 200,
250, 300, 400, 500, 600, 700, 800, 1000, 1500, 2000};
Expand Down Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions sample/cpp/ivf_rabitq_querying.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#include <string>
#include <vector>

#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<float>;
Expand Down Expand Up @@ -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';

Expand Down
24 changes: 24 additions & 0 deletions sample/cpp/querying_example.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <cstddef>
#include <vector>

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<float> column_averages(const std::vector<std::vector<float>>& rows) {
std::vector<float> 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<float>(rows.size());
}
return averages;
}

} // namespace querying_example
6 changes: 3 additions & 3 deletions sample/cpp/symqg_querying.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include <iostream>
#include <vector>

#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<float>;
Expand Down Expand Up @@ -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) {
Expand Down
Loading