Skip to content

Commit 7895501

Browse files
LiuRuoyu01yuhaijun999
authored andcommitted
[fix][store]Fixup users could not specify max_elements for hnsw
1 parent 0517e03 commit 7895501

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/vector/vector_index_hnsw.cc

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,15 @@ VectorIndexHnsw::VectorIndexHnsw(int64_t id, const pb::common::VectorIndexParame
171171
hnsw_parameter.efconstruction(), pb::common::MetricType_Name(hnsw_parameter.metric_type()),
172172
hnsw_parameter.dimension());
173173

174-
hnsw_index_ =
175-
new hnswlib::HierarchicalNSW<float>(hnsw_space_, FLAGS_hnsw_max_init_max_elements, hnsw_parameter.nlinks(),
176-
hnsw_parameter.efconstruction(), 100, false);
174+
uint32_t hnsw_init_max_elements = 0;
175+
if (max_element_limit_ < FLAGS_hnsw_max_init_max_elements) {
176+
hnsw_init_max_elements = max_element_limit_;
177+
} else {
178+
hnsw_init_max_elements = FLAGS_hnsw_max_init_max_elements;
179+
}
180+
181+
hnsw_index_ = new hnswlib::HierarchicalNSW<float>(hnsw_space_, hnsw_init_max_elements, hnsw_parameter.nlinks(),
182+
hnsw_parameter.efconstruction(), 100, false);
177183
}
178184
}
179185

@@ -623,13 +629,30 @@ butil::Status VectorIndexHnsw::CheckAndSetHnswParameter(pb::common::CreateHnswPa
623629
hnsw_parameter.set_nlinks(FLAGS_max_hnsw_nlinks_of_region);
624630
}
625631

626-
auto max_element_limit = CalcHnswCountFromMemory(FLAGS_max_hnsw_memory_size_of_region, hnsw_parameter.dimension(),
627-
hnsw_parameter.nlinks());
628-
hnsw_parameter.set_max_elements(max_element_limit);
629-
DINGO_LOG(INFO) << fmt::format(
630-
"[vector_index.hnsw] calc max element limit is {}, paramiter max_hnsw_memory_size_of_region({}) dimension({}) "
631-
"nlinks({}).",
632-
max_element_limit, FLAGS_max_hnsw_memory_size_of_region, hnsw_parameter.dimension(), hnsw_parameter.nlinks());
632+
if (hnsw_parameter.max_elements() == 0) {
633+
auto max_element_limit = CalcHnswCountFromMemory(FLAGS_max_hnsw_memory_size_of_region, hnsw_parameter.dimension(),
634+
hnsw_parameter.nlinks());
635+
if (max_element_limit == 0) {
636+
DINGO_LOG(ERROR) << fmt::format(
637+
"[vector_index.hnsw] calc max element limit is 0, max_hnsw_memory_size_of_region({}) "
638+
"dimension({}) "
639+
"nlinks({}).",
640+
FLAGS_max_hnsw_memory_size_of_region, hnsw_parameter.dimension(), hnsw_parameter.nlinks());
641+
return butil::Status(pb::error::Errno::EILLEGAL_PARAMTETERS, "hnsw max elements is too small");
642+
}
643+
hnsw_parameter.set_max_elements(max_element_limit);
644+
DINGO_LOG(INFO) << fmt::format(
645+
"[vector_index.hnsw] calc max element limit is {}, paramiter max_hnsw_memory_size_of_region({}) "
646+
"dimension({}) "
647+
"nlinks({}).",
648+
max_element_limit, FLAGS_max_hnsw_memory_size_of_region, hnsw_parameter.dimension(), hnsw_parameter.nlinks());
649+
} else {
650+
DINGO_LOG(INFO) << fmt::format(
651+
"[vector_index.hnsw] use user set max element {} "
652+
"dimension({}) "
653+
"nlinks({}).",
654+
hnsw_parameter.max_elements(), hnsw_parameter.dimension(), hnsw_parameter.nlinks());
655+
}
633656

634657
return butil::Status::OK();
635658
}

0 commit comments

Comments
 (0)