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