Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): use fixed seed to improve reproducibility. #2557

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
5 changes: 4 additions & 1 deletion src/search/hnsw_indexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ 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);
HnswIndex(const SearchKey& search_key, HnswVectorFieldMetadata* vector, engine::Storage* storage)
: HnswIndex(search_key, vector, storage, std::random_device()()) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use a single constructor with a default argument?

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

Copy link
Contributor Author

@LindaSummer LindaSummer Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Beihao-Zhou ,

Thanks for your review suggestions!
I have updated related constructors. 😊

Best Regards,
Edward


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
Loading