summaryrefslogtreecommitdiffstats
path: root/searchlib
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-06-12 07:36:20 +0000
committerArne Juul <arnej@verizonmedia.com>2020-06-12 07:36:20 +0000
commit4faeb6fb379e812ff639ec13aaeb217e77578621 (patch)
treeca14ceb5e1d3ac8487f62ce377a61a242e2a1092 /searchlib
parent73edac0b7a44a0c3881ca34eea5e525a0a253097 (diff)
revert back to getting random number in the [0,1) range
* for whatever reason, std::uniform_real_distribution doesn't allow a>b when specifying the [a,b) half-open range. So we need to get a number from [0,1) and then do (1.0-unif) to convert to the (0,1] range that we want. * extend the unit test with more initial numbers, and write it in a more compact way.
Diffstat (limited to 'searchlib')
-rw-r--r--searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp34
-rw-r--r--searchlib/src/vespa/searchlib/tensor/inv_log_level_generator.h4
2 files changed, 27 insertions, 11 deletions
diff --git a/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp b/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp
index 04f2076121f..b49292463bd 100644
--- a/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp
+++ b/searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp
@@ -521,15 +521,31 @@ TEST_F(HnswIndexTest, shrink_called_heuristic)
TEST(LevelGeneratorTest, gives_various_levels)
{
InvLogLevelGenerator generator(4);
- EXPECT_EQ(2u, generator.max_level());
- EXPECT_EQ(1u, generator.max_level());
- EXPECT_EQ(0u, generator.max_level());
- EXPECT_EQ(1u, generator.max_level());
- EXPECT_EQ(0u, generator.max_level());
- EXPECT_EQ(1u, generator.max_level());
- EXPECT_EQ(0u, generator.max_level());
- EXPECT_EQ(0u, generator.max_level());
- EXPECT_EQ(0u, generator.max_level());
+ std::vector<uint32_t> got_levels(16);
+ for (auto & v : got_levels) { v = generator.max_level(); }
+ EXPECT_EQ(got_levels, std::vector<uint32_t>({
+ 2, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0
+ }));
+ for (auto & v : got_levels) { v = generator.max_level(); }
+ EXPECT_EQ(got_levels, std::vector<uint32_t>({
+ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ }));
+ for (auto & v : got_levels) { v = generator.max_level(); }
+ EXPECT_EQ(got_levels, std::vector<uint32_t>({
+ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0
+ }));
+ for (auto & v : got_levels) { v = generator.max_level(); }
+ EXPECT_EQ(got_levels, std::vector<uint32_t>({
+ 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1
+ }));
+ for (auto & v : got_levels) { v = generator.max_level(); }
+ EXPECT_EQ(got_levels, std::vector<uint32_t>({
+ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 2
+ }));
+ for (auto & v : got_levels) { v = generator.max_level(); }
+ EXPECT_EQ(got_levels, std::vector<uint32_t>({
+ 0, 1, 1, 0, 3, 1, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0
+ }));
uint32_t left = 1000000;
std::vector<uint32_t> hist;
diff --git a/searchlib/src/vespa/searchlib/tensor/inv_log_level_generator.h b/searchlib/src/vespa/searchlib/tensor/inv_log_level_generator.h
index 4a96a21534b..e70830a78f8 100644
--- a/searchlib/src/vespa/searchlib/tensor/inv_log_level_generator.h
+++ b/searchlib/src/vespa/searchlib/tensor/inv_log_level_generator.h
@@ -29,13 +29,13 @@ public:
InvLogLevelGenerator(uint32_t m)
: _rng(0x1234deadbeef5678uLL),
_mutex(),
- _uniform(1.0, 0.0),
+ _uniform(0.0, 1.0),
_levelMultiplier(1.0 / log(1.0 * m))
{}
uint32_t max_level() override {
double unif = get_uniform();
- double r = -log(unif) * _levelMultiplier;
+ double r = -log(1.0-unif) * _levelMultiplier;
return (uint32_t) r;
}
};