| 13 | namespace rabitqlib::python_bindings { |
| 14 | |
| 15 | class IvfIndex { |
| 16 | public: |
| 17 | IvfIndex( |
| 18 | size_t dim, |
| 19 | size_t max_elements, |
| 20 | size_t num_clusters, |
| 21 | size_t nbits, |
| 22 | const std::string& metric = "l2" |
| 23 | ) |
| 24 | : dim_(dim) |
| 25 | , max_elements_(max_elements) |
| 26 | , num_clusters_(num_clusters) |
| 27 | , nbits_(nbits) |
| 28 | , metric_(metric_from_string(metric)) |
| 29 | , index_(std::make_unique<rabitqlib::ivf::IVF>( |
| 30 | max_elements, |
| 31 | dim, |
| 32 | num_clusters, |
| 33 | nbits, |
| 34 | metric_, |
| 35 | rabitqlib::RotatorType::FhtKacRotator |
| 36 | )) {} |
| 37 | |
| 38 | void build( |
| 39 | py::handle data, |
| 40 | py::handle centroids, |
| 41 | py::handle cluster_ids, |
| 42 | size_t num_threads = 1, |
| 43 | bool fast_quantization = false |
| 44 | ) { |
| 45 | auto data_array = ensure_2d_array<float>(data, "data"); |
| 46 | auto centroids_array = ensure_2d_array<float>(centroids, "centroids"); |
| 47 | auto cluster_ids_array = ensure_1d_array<rabitqlib::PID>(cluster_ids, "cluster_ids"); |
| 48 | |
| 49 | if (static_cast<size_t>(data_array.shape(1)) != dim_) { |
| 50 | throw std::invalid_argument("data dimension does not match index dim"); |
| 51 | } |
| 52 | if (static_cast<size_t>(centroids_array.shape(1)) != dim_) { |
| 53 | throw std::invalid_argument("centroid dimension does not match index dim"); |
| 54 | } |
| 55 | if (static_cast<size_t>(cluster_ids_array.shape(0)) != static_cast<size_t>(data_array.shape(0))) { |
| 56 | throw std::invalid_argument("cluster_ids length must match number of rows in data"); |
| 57 | } |
| 58 | |
| 59 | index_->construct(data_array.data(), centroids_array.data(), cluster_ids_array.data(), fast_quantization, num_threads); |
| 60 | built_ = true; |
| 61 | } |
| 62 | |
| 63 | py::tuple search( |
| 64 | py::handle queries, |
| 65 | size_t k, |
| 66 | size_t nprobe, |
| 67 | bool high_accuracy = true, |
| 68 | size_t num_threads = 1 |
| 69 | ) { |
| 70 | auto query_array = ensure_2d_array<float>(queries, "queries"); |
| 71 | if (static_cast<size_t>(query_array.shape(1)) != dim_) { |
| 72 | throw std::invalid_argument("query dimension does not match index dim"); |