@@ -171,9 +171,15 @@ VectorIndexHnsw::VectorIndexHnsw(int64_t id, const pb::common::VectorIndexParame
171
171
hnsw_parameter.efconstruction (), pb::common::MetricType_Name (hnsw_parameter.metric_type ()),
172
172
hnsw_parameter.dimension ());
173
173
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 );
177
183
}
178
184
}
179
185
@@ -623,13 +629,30 @@ butil::Status VectorIndexHnsw::CheckAndSetHnswParameter(pb::common::CreateHnswPa
623
629
hnsw_parameter.set_nlinks (FLAGS_max_hnsw_nlinks_of_region);
624
630
}
625
631
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
+ }
633
656
634
657
return butil::Status::OK ();
635
658
}
0 commit comments