Skip to content
Open
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
7 changes: 3 additions & 4 deletions src/search/hnsw_indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

#include <algorithm>
#include <cmath>
#include <memory>
#include <queue>
#include <random>
#include <unordered_set>
Expand Down Expand Up @@ -172,12 +171,12 @@ StatusOr<double> ComputeSimilarity(const VectorItem& left, const VectorItem& rig
}
}

HnswIndex::HnswIndex(const SearchKey& search_key, HnswVectorFieldMetadata* vector, engine::Storage* storage,
std::random_device::result_type seed)
thread_local std::mt19937 HnswIndex::generator{std::random_device()()};

HnswIndex::HnswIndex(const SearchKey& search_key, HnswVectorFieldMetadata* vector, engine::Storage* storage)
: search_key(search_key),
metadata(vector),
storage(storage),
generator(std::mt19937(seed)),
m_level_normalization_factor(1.0 / std::log(metadata->m)) {}

uint16_t HnswIndex::RandomizeLayer() {
Expand Down
6 changes: 3 additions & 3 deletions src/search/hnsw_indexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ struct HnswIndex {
HnswVectorFieldMetadata* metadata;
engine::Storage* storage = nullptr;

std::mt19937 generator;
double m_level_normalization_factor;

HnswIndex(const SearchKey& search_key, HnswVectorFieldMetadata* vector, engine::Storage* storage,
std::random_device::result_type seed = std::random_device()());
static thread_local std::mt19937 generator;

HnswIndex(const SearchKey& search_key, HnswVectorFieldMetadata* vector, engine::Storage* storage);

static StatusOr<std::vector<VectorItem>> DecodeNodesToVectorItems(engine::Context& ctx,
const std::vector<NodeKey>& node_key,
Expand Down
3 changes: 1 addition & 2 deletions tests/cppunit/hnsw_index_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ struct HnswIndexTest : TestBase {
std::string idx_name = "hnsw_test_idx";
std::string key = "vector";
std::unique_ptr<redis::HnswIndex> hnsw_index;
const std::random_device::result_type seed = 14863; // fixed seed for reproducibility

HnswIndexTest() {
metadata.vector_type = redis::VectorType::FLOAT64;
metadata.dim = 3;
metadata.m = 3;
metadata.distance_metric = redis::DistanceMetric::L2;
auto search_key = redis::SearchKey(ns, idx_name, key);
hnsw_index = std::make_unique<redis::HnswIndex>(search_key, &metadata, storage_.get(), seed);
hnsw_index = std::make_unique<redis::HnswIndex>(search_key, &metadata, storage_.get());
}

void TearDown() override { hnsw_index.reset(); }
Expand Down
Loading