-
Notifications
You must be signed in to change notification settings - Fork 29
feature!: verify and set default VamanaBuildParameters #96
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
Changes from all commits
9702506
97a0128
e5f0cf4
4080533
5d8cf0f
ffa6bd0
eb7be5f
c3ec8ba
749bce2
8bd4161
4e774c4
cc4da27
100cfef
ca56187
1afaee3
f65ca7a
ed83622
585cc2f
b9a0ba3
0af8c60
2489f73
44c0cc1
4ade6f6
e242f1c
f4ca13a
adb379b
d9abbf3
9b68be4
ef1d863
76f61d3
1d35ac4
382d252
3c8085c
0793c85
47c3297
63ebadf
becd2cd
839d3a3
e146904
a9b6471
5d7d2e0
47d967f
dde61e8
d88d970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,19 +404,22 @@ class VamanaIndex { | |
if (graph_.n_nodes() != data_.size()) { | ||
throw ANNEXCEPTION("Wrong sizes!"); | ||
} | ||
|
||
build_parameters_ = parameters; | ||
// verify the parameters before set local var | ||
verify_and_set_default_index_parameters(build_parameters_, distance_function); | ||
auto builder = VamanaBuilder( | ||
graph_, | ||
data_, | ||
distance_, | ||
parameters, | ||
build_parameters_, | ||
threadpool_, | ||
extensions::estimate_prefetch_parameters(data_) | ||
); | ||
|
||
builder.construct(1.0F, entry_point_[0], logging::Level::Info, logger); | ||
builder.construct(parameters.alpha, entry_point_[0], logging::Level::Info, logger); | ||
builder.construct( | ||
build_parameters_.alpha, entry_point_[0], logging::Level::Info, logger | ||
); | ||
} | ||
|
||
/// @brief Getter method for logger | ||
|
@@ -896,10 +899,13 @@ auto auto_build( | |
auto entry_point = extensions::compute_entry_point(data, threadpool); | ||
|
||
// Default graph. | ||
auto graph = default_graph(data.size(), parameters.graph_max_degree, graph_allocator); | ||
auto verified_parameters = parameters; | ||
verify_and_set_default_index_parameters(verified_parameters, distance); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I verified parameters here because we are building the graph using parameters.graph_max_degree, but if it is okay to not verify for the graph, I can remove it |
||
auto graph = | ||
default_graph(data.size(), verified_parameters.graph_max_degree, graph_allocator); | ||
using I = typename decltype(graph)::index_type; | ||
return VamanaIndex{ | ||
parameters, | ||
verified_parameters, | ||
std::move(graph), | ||
std::move(data), | ||
lib::narrow<I>(entry_point), | ||
|
@@ -959,4 +965,57 @@ auto auto_assemble( | |
index.apply(config); | ||
return index; | ||
} | ||
|
||
/// @brief Verify parameters and set defaults if needed | ||
template <typename Dist> | ||
void verify_and_set_default_index_parameters( | ||
VamanaBuildParameters& parameters, Dist distance_function | ||
) { | ||
// Set default values | ||
if (parameters.max_candidate_pool_size == svs::UNSIGNED_INTEGER_PLACEHOLDER) { | ||
parameters.max_candidate_pool_size = 2 * parameters.graph_max_degree; | ||
} | ||
|
||
if (parameters.prune_to == svs::UNSIGNED_INTEGER_PLACEHOLDER) { | ||
if (parameters.graph_max_degree >= 16) { | ||
parameters.prune_to = parameters.graph_max_degree - 4; | ||
} else { | ||
parameters.prune_to = parameters.graph_max_degree; | ||
} | ||
} | ||
|
||
// Check supported distance type using std::is_same type trait | ||
using dist_type = std::decay_t<decltype(distance_function)>; | ||
// Create type flags for each distance type | ||
constexpr bool is_L2 = std::is_same_v<dist_type, svs::distance::DistanceL2>; | ||
constexpr bool is_IP = std::is_same_v<dist_type, svs::distance::DistanceIP>; | ||
constexpr bool is_Cosine = | ||
std::is_same_v<dist_type, svs::distance::DistanceCosineSimilarity>; | ||
|
||
// Handle alpha based on distance type | ||
if constexpr (is_L2) { | ||
if (parameters.alpha == svs::FLOAT_PLACEHOLDER) { | ||
parameters.alpha = svs::VAMANA_ALPHA_MINIMIZE_DEFAULT; | ||
} else if (parameters.alpha < 1.0f) { | ||
// Check User set values | ||
throw std::invalid_argument("For L2 distance, alpha must be >= 1.0"); | ||
} | ||
} else if constexpr (is_IP || is_Cosine) { | ||
if (parameters.alpha == svs::FLOAT_PLACEHOLDER) { | ||
parameters.alpha = svs::VAMANA_ALPHA_MAXIMIZE_DEFAULT; | ||
} else if (parameters.alpha > 1.0f) { | ||
// Check User set values | ||
throw std::invalid_argument("For MIP/Cosine distance, alpha must be <= 1.0"); | ||
} else if (parameters.alpha <= 0.0f) { | ||
throw std::invalid_argument("alpha must be > 0"); | ||
} | ||
} else { | ||
throw std::invalid_argument("Unsupported distance type"); | ||
} | ||
|
||
// Check prune_to <= graph_max_degree | ||
if (parameters.prune_to > parameters.graph_max_degree) { | ||
throw std::invalid_argument("prune_to must be <= graph_max_degree"); | ||
} | ||
} | ||
} // namespace svs::index::vamana |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
|
||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <limits> | ||
|
||
namespace svs::preprocessor::detail { | ||
|
||
// consteval functions for working with preprocessor defines. | ||
|
@@ -159,3 +162,14 @@ inline constexpr bool have_avx512_avx2 = true; | |
#endif | ||
|
||
} // namespace svs::arch | ||
|
||
namespace svs { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the good work. Since some of these parameters are vamana-specific, could we move these parameters to svs::index::vamana namespace? Or change the name to Vamana specific. For example, GRAPH_MAX_DEGREE_DEFAULT -> VAMANA_GRAPH_MAX_DEGREE_DEFAULT. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx for suggestion, fixed! |
||
// Maximum values used as default initializers | ||
inline constexpr size_t UNSIGNED_INTEGER_PLACEHOLDER = std::numeric_limits<size_t>::max(); | ||
inline constexpr float FLOAT_PLACEHOLDER = std::numeric_limits<float>::max(); | ||
inline constexpr float VAMANA_GRAPH_MAX_DEGREE_DEFAULT = 32; | ||
inline constexpr float VAMANA_WINDOW_SIZE_DEFAULT = 64; | ||
inline constexpr bool VAMANA_USE_FULL_SEARCH_HISTORY_DEFAULT = true; | ||
inline constexpr float VAMANA_ALPHA_MINIMIZE_DEFAULT = 1.2; | ||
inline constexpr float VAMANA_ALPHA_MAXIMIZE_DEFAULT = 0.95; | ||
} // namespace svs |
Uh oh!
There was an error while loading. Please reload this page.