Skip to content

Commit

Permalink
Merge pull request #24877 from vespa-engine/toregge/move-inner-class-…
Browse files Browse the repository at this point in the history
…hnswindex-config-to-hnswindexconfig

Move inner class HnswIndex::Config to HnswIndexConfig.
  • Loading branch information
geirst authored Nov 15, 2022
2 parents 9a35cc0 + 7310a44 commit 1581058
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion searchlib/src/tests/tensor/hnsw_index/hnsw_index_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class HnswIndexTest : public ::testing::Test {
level_generator = generator.get();
index = std::make_unique<HnswIndex>(vectors, std::make_unique<SquaredEuclideanDistance>(vespalib::eval::CellType::FLOAT),
std::move(generator),
HnswIndex::Config(5, 2, 10, 0, heuristic_select_neighbors));
HnswIndexConfig(5, 2, 10, 0, heuristic_select_neighbors));
}
void add_document(uint32_t docid, uint32_t max_level = 0) {
level_generator->level = max_level;
Expand Down
2 changes: 1 addition & 1 deletion searchlib/src/tests/tensor/hnsw_index/stress_hnsw_mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class Stressor : public ::testing::Test {
uint32_t m = 16;
index = std::make_unique<HnswIndex>(vectors, std::make_unique<FloatSqEuclideanDistance>(),
std::make_unique<InvLogLevelGenerator>(m),
HnswIndex::Config(2*m, m, 200, 10, true));
HnswIndexConfig(2*m, m, 200, 10, true));
}
size_t get_rnd(size_t size) {
return rng.nextUniform() * size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ DefaultNearestNeighborIndexFactory::make(const DocVectorAccess& vectors,
{
(void) vector_size;
uint32_t m = params.max_links_per_node();
HnswIndex::Config cfg(m * 2,
m,
params.neighbors_to_explore_at_insert(),
10000,
true);
HnswIndexConfig cfg(m * 2,
m,
params.neighbors_to_explore_at_insert(),
10000,
true);
return std::make_unique<HnswIndex>(vectors,
make_distance_function(params.distance_metric(), cell_type),
make_random_level_generator(m),
Expand Down
2 changes: 1 addition & 1 deletion searchlib/src/vespa/searchlib/tensor/hnsw_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ HnswIndex::search_layer(const TypedCells& input, uint32_t neighbors_to_find,
}

HnswIndex::HnswIndex(const DocVectorAccess& vectors, DistanceFunction::UP distance_func,
RandomLevelGenerator::UP level_generator, const Config& cfg)
RandomLevelGenerator::UP level_generator, const HnswIndexConfig& cfg)
: _graph(),
_vectors(vectors),
_distance_func(std::move(distance_func)),
Expand Down
34 changes: 4 additions & 30 deletions searchlib/src/vespa/searchlib/tensor/hnsw_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "hnsw_index_config.h"
#include "distance_function.h"
#include "doc_vector_access.h"
#include "hnsw_identity_mapping.h"
Expand Down Expand Up @@ -36,33 +37,6 @@ namespace search::tensor {
*/
class HnswIndex : public NearestNeighborIndex {
public:
class Config {
private:
uint32_t _max_links_at_level_0;
uint32_t _max_links_on_inserts;
uint32_t _neighbors_to_explore_at_construction;
uint32_t _min_size_before_two_phase;
bool _heuristic_select_neighbors;

public:
Config(uint32_t max_links_at_level_0_in,
uint32_t max_links_on_inserts_in,
uint32_t neighbors_to_explore_at_construction_in,
uint32_t min_size_before_two_phase_in,
bool heuristic_select_neighbors_in)
: _max_links_at_level_0(max_links_at_level_0_in),
_max_links_on_inserts(max_links_on_inserts_in),
_neighbors_to_explore_at_construction(neighbors_to_explore_at_construction_in),
_min_size_before_two_phase(min_size_before_two_phase_in),
_heuristic_select_neighbors(heuristic_select_neighbors_in)
{}
uint32_t max_links_at_level_0() const { return _max_links_at_level_0; }
uint32_t max_links_on_inserts() const { return _max_links_on_inserts; }
uint32_t neighbors_to_explore_at_construction() const { return _neighbors_to_explore_at_construction; }
uint32_t min_size_before_two_phase() const { return _min_size_before_two_phase; }
bool heuristic_select_neighbors() const { return _heuristic_select_neighbors; }
};

class HnswIndexCompactionSpec {
CompactionSpec _level_arrays;
CompactionSpec _link_arrays;
Expand Down Expand Up @@ -109,7 +83,7 @@ class HnswIndex : public NearestNeighborIndex {
DistanceFunction::UP _distance_func;
RandomLevelGenerator::UP _level_generator;
IdMapping _id_mapping; // mapping from docid to nodeid vector
Config _cfg;
HnswIndexConfig _cfg;
HnswIndexCompactionSpec _compaction_spec;

uint32_t max_links_for_level(uint32_t level) const;
Expand Down Expand Up @@ -212,10 +186,10 @@ class HnswIndex : public NearestNeighborIndex {
void internal_complete_add_node(uint32_t nodeid, uint32_t docid, uint32_t subspace, PreparedAddNode &prepared_node);
public:
HnswIndex(const DocVectorAccess& vectors, DistanceFunction::UP distance_func,
RandomLevelGenerator::UP level_generator, const Config& cfg);
RandomLevelGenerator::UP level_generator, const HnswIndexConfig& cfg);
~HnswIndex() override;

const Config& config() const { return _cfg; }
const HnswIndexConfig& config() const { return _cfg; }

// Implements NearestNeighborIndex
void add_document(uint32_t docid) override;
Expand Down
39 changes: 39 additions & 0 deletions searchlib/src/vespa/searchlib/tensor/hnsw_index_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <cstdint>

namespace search::tensor {

/*
* Class containing config for HnswIndex.
*/
class HnswIndexConfig {
private:
uint32_t _max_links_at_level_0;
uint32_t _max_links_on_inserts;
uint32_t _neighbors_to_explore_at_construction;
uint32_t _min_size_before_two_phase;
bool _heuristic_select_neighbors;

public:
HnswIndexConfig(uint32_t max_links_at_level_0_in,
uint32_t max_links_on_inserts_in,
uint32_t neighbors_to_explore_at_construction_in,
uint32_t min_size_before_two_phase_in,
bool heuristic_select_neighbors_in)
: _max_links_at_level_0(max_links_at_level_0_in),
_max_links_on_inserts(max_links_on_inserts_in),
_neighbors_to_explore_at_construction(neighbors_to_explore_at_construction_in),
_min_size_before_two_phase(min_size_before_two_phase_in),
_heuristic_select_neighbors(heuristic_select_neighbors_in)
{}
uint32_t max_links_at_level_0() const { return _max_links_at_level_0; }
uint32_t max_links_on_inserts() const { return _max_links_on_inserts; }
uint32_t neighbors_to_explore_at_construction() const { return _neighbors_to_explore_at_construction; }
uint32_t min_size_before_two_phase() const { return _min_size_before_two_phase; }
bool heuristic_select_neighbors() const { return _heuristic_select_neighbors; }
};

}

0 comments on commit 1581058

Please sign in to comment.