MCPcopy Create free account
hub / github.com/VectorDB-NTU/RaBitQ-Library / HnswIndex

Class HnswIndex

python_bindings/hnsw_bindings.cpp:16–163  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14namespace rabitqlib::python_bindings {
15
16class HnswIndex {
17 public:
18 HnswIndex(
19 size_t dim,
20 size_t max_elements,
21 size_t M,
22 size_t ef_construction,
23 size_t nbits,
24 const std::string& metric = "l2",
25 size_t random_seed = 100
26 )
27 : dim_(dim)
28 , max_elements_(max_elements)
29 , M_(M)
30 , ef_construction_(ef_construction)
31 , nbits_(nbits)
32 , metric_(metric_from_string(metric))
33 , random_seed_(random_seed)
34 , index_(std::make_unique<rabitqlib::hnsw::HierarchicalNSW>(
35 max_elements,
36 dim,
37 nbits,
38 M,
39 ef_construction,
40 random_seed,
41 metric_
42 )) {}
43
44 void build(
45 py::handle data,
46 py::handle centroids,
47 py::handle cluster_ids,
48 size_t num_threads = 1,
49 bool fast_quantization = false
50 ) {
51 auto data_array = ensure_2d_array<float>(data, "data");
52 auto centroids_array = ensure_2d_array<float>(centroids, "centroids");
53 auto cluster_ids_array = ensure_1d_array<rabitqlib::PID>(cluster_ids, "cluster_ids");
54
55 if (static_cast<size_t>(data_array.shape(1)) != dim_) {
56 throw std::invalid_argument("data dimension does not match index dim");
57 }
58 if (static_cast<size_t>(centroids_array.shape(1)) != dim_) {
59 throw std::invalid_argument("centroid dimension does not match index dim");
60 }
61 if (static_cast<size_t>(cluster_ids_array.shape(0)) != static_cast<size_t>(data_array.shape(0))) {
62 throw std::invalid_argument("cluster_ids length must match number of rows in data");
63 }
64
65 const size_t num_clusters = static_cast<size_t>(centroids_array.shape(0));
66 num_clusters_ = num_clusters;
67
68 // Ensure cluster_ids are writable for the C++ API by making a copy
69 std::vector<rabitqlib::PID> cluster_ids_vec(static_cast<size_t>(cluster_ids_array.shape(0)));
70 std::memcpy(cluster_ids_vec.data(), cluster_ids_array.data(), cluster_ids_vec.size() * sizeof(rabitqlib::PID));
71
72 index_->construct(
73 num_clusters,

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected