Skip to content

Commit

Permalink
fix(test): use fixed seed to improve reproducibility. (#2557)
Browse files Browse the repository at this point in the history
  • Loading branch information
LindaSummer authored Sep 27, 2024
1 parent f2bc224 commit b8cf118
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/search/hnsw_indexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,13 @@ StatusOr<double> ComputeSimilarity(const VectorItem& left, const VectorItem& rig
}
}

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

uint16_t HnswIndex::RandomizeLayer() {
std::uniform_real_distribution<double> level_dist(0.0, 1.0);
Expand Down
3 changes: 2 additions & 1 deletion src/search/hnsw_indexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ struct HnswIndex {
std::mt19937 generator;
double m_level_normalization_factor;

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

static StatusOr<std::vector<VectorItem>> DecodeNodesToVectorItems(engine::Context& ctx,
const std::vector<NodeKey>& node_key,
Expand Down
3 changes: 2 additions & 1 deletion tests/cppunit/hnsw_index_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ 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());
hnsw_index = std::make_unique<redis::HnswIndex>(search_key, &metadata, storage_.get(), seed);
}

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

0 comments on commit b8cf118

Please sign in to comment.