| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
* |
| 4 |
* This source code is licensed under the MIT license found in the |
| 5 |
* LICENSE file in the root directory of this source tree. |
| 6 |
*/ |
| 7 |
|
| 8 |
// -*- c++ -*- |
| 9 |
|
| 10 |
#include "Clustering_c.h" |
| 11 |
#include <faiss/Clustering.h> |
| 12 |
#include <faiss/Index.h> |
| 13 |
#include <vector> |
| 14 |
#include "macros_impl.h" |
| 15 |
|
| 16 |
extern "C" { |
| 17 |
|
| 18 |
using faiss::Clustering; |
| 19 |
using faiss::ClusteringIterationStats; |
| 20 |
using faiss::ClusteringParameters; |
| 21 |
using faiss::Index; |
| 22 |
|
| 23 |
DEFINE_GETTER(Clustering, int, niter) |
| 24 |
DEFINE_GETTER(Clustering, int, nredo) |
| 25 |
DEFINE_GETTER(Clustering, int, verbose) |
| 26 |
DEFINE_GETTER(Clustering, int, spherical) |
| 27 |
DEFINE_GETTER(Clustering, int, int_centroids) |
| 28 |
DEFINE_GETTER(Clustering, int, update_index) |
| 29 |
DEFINE_GETTER(Clustering, int, frozen_centroids) |
| 30 |
|
| 31 |
DEFINE_GETTER(Clustering, int, min_points_per_centroid) |
| 32 |
DEFINE_GETTER(Clustering, int, max_points_per_centroid) |
| 33 |
|
| 34 |
DEFINE_GETTER(Clustering, int, seed) |
| 35 |
DEFINE_GETTER(Clustering, size_t, decode_block_size) |
| 36 |
|
| 37 |
/// getter for d |
| 38 |
DEFINE_GETTER(Clustering, size_t, d) |
| 39 |
|
| 40 |
/// getter for k |
| 41 |
DEFINE_GETTER(Clustering, size_t, k) |
| 42 |
|
| 43 |
DEFINE_GETTER(ClusteringIterationStats, float, obj) |
| 44 |
DEFINE_GETTER(ClusteringIterationStats, double, time) |
| 45 |
DEFINE_GETTER(ClusteringIterationStats, double, time_search) |
| 46 |
DEFINE_GETTER(ClusteringIterationStats, double, imbalance_factor) |
| 47 |
DEFINE_GETTER(ClusteringIterationStats, int, nsplit) |
| 48 |
|
| 49 |
void faiss_ClusteringParameters_init(FaissClusteringParameters* params) { |
| 50 |
ClusteringParameters d; |
| 51 |
params->frozen_centroids = d.frozen_centroids; |
| 52 |
params->max_points_per_centroid = d.max_points_per_centroid; |
| 53 |
params->min_points_per_centroid = d.min_points_per_centroid; |
| 54 |
params->niter = d.niter; |
| 55 |
params->nredo = d.nredo; |
| 56 |
params->seed = d.seed; |
| 57 |
params->spherical = d.spherical; |
| 58 |
params->int_centroids = d.int_centroids; |
| 59 |
params->update_index = d.update_index; |
| 60 |
params->verbose = d.verbose; |
| 61 |
params->decode_block_size = d.decode_block_size; |
| 62 |
} |
| 63 |
|
| 64 |
// This conversion is required because the two types are not memory-compatible |
| 65 |
inline ClusteringParameters from_faiss_c( |
| 66 |
const FaissClusteringParameters* params) { |
| 67 |
ClusteringParameters o; |
| 68 |
o.frozen_centroids = params->frozen_centroids; |
| 69 |
o.max_points_per_centroid = params->max_points_per_centroid; |
| 70 |
o.min_points_per_centroid = params->min_points_per_centroid; |
| 71 |
o.niter = params->niter; |
| 72 |
o.nredo = params->nredo; |
| 73 |
o.seed = params->seed; |
| 74 |
o.spherical = params->spherical; |
| 75 |
o.update_index = params->update_index; |
| 76 |
o.int_centroids = params->int_centroids; |
| 77 |
o.verbose = params->verbose; |
| 78 |
o.decode_block_size = params->decode_block_size; |
| 79 |
return o; |
| 80 |
} |
| 81 |
|
| 82 |
/// getter for centroids (size = k * d) |
| 83 |
void faiss_Clustering_centroids( |
| 84 |
FaissClustering* clustering, |
| 85 |
float** centroids, |
| 86 |
size_t* size) { |
| 87 |
std::vector<float>& v = |
| 88 |
reinterpret_cast<Clustering*>(clustering)->centroids; |
| 89 |
if (centroids) { |
| 90 |
*centroids = v.data(); |
| 91 |
} |
| 92 |
if (size) { |
| 93 |
*size = v.size(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/// getter for iteration stats |
| 98 |
void faiss_Clustering_iteration_stats( |
| 99 |
FaissClustering* clustering, |
| 100 |
FaissClusteringIterationStats** iteration_stats, |
| 101 |
size_t* size) { |
| 102 |
std::vector<ClusteringIterationStats>& v = |
| 103 |
reinterpret_cast<Clustering*>(clustering)->iteration_stats; |
| 104 |
if (iteration_stats) { |
| 105 |
*iteration_stats = |
| 106 |
reinterpret_cast<FaissClusteringIterationStats*>(v.data()); |
| 107 |
} |
| 108 |
if (size) { |
| 109 |
*size = v.size(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/// the only mandatory parameters are k and d |
| 114 |
int faiss_Clustering_new(FaissClustering** p_clustering, int d, int k) { |
| 115 |
try { |
| 116 |
Clustering* c = new Clustering(d, k); |
| 117 |
*p_clustering = reinterpret_cast<FaissClustering*>(c); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
CATCH_AND_HANDLE |
| 121 |
} |
| 122 |
|
| 123 |
int faiss_Clustering_new_with_params( |
| 124 |
FaissClustering** p_clustering, |
| 125 |
int d, |
| 126 |
int k, |
| 127 |
const FaissClusteringParameters* cp) { |
| 128 |
try { |
| 129 |
Clustering* c = new Clustering(d, k, from_faiss_c(cp)); |
| 130 |
*p_clustering = reinterpret_cast<FaissClustering*>(c); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
CATCH_AND_HANDLE |
| 134 |
} |
| 135 |
|
| 136 |
/// Index is used during the assignment stage |
| 137 |
int faiss_Clustering_train( |
| 138 |
FaissClustering* clustering, |
| 139 |
idx_t n, |
| 140 |
const float* x, |
| 141 |
FaissIndex* index) { |
| 142 |
try { |
| 143 |
reinterpret_cast<Clustering*>(clustering) |
| 144 |
->train(n, x, *reinterpret_cast<Index*>(index)); |
| 145 |
return 0; |
| 146 |
} |
| 147 |
CATCH_AND_HANDLE |
| 148 |
} |
| 149 |
|
| 150 |
void faiss_Clustering_free(FaissClustering* clustering) { |
| 151 |
delete reinterpret_cast<Clustering*>(clustering); |
| 152 |
} |
| 153 |
|
| 154 |
int faiss_kmeans_clustering( |
| 155 |
size_t d, |
| 156 |
size_t n, |
| 157 |
size_t k, |
| 158 |
const float* x, |
| 159 |
float* centroids, |
| 160 |
float* q_error) { |
| 161 |
try { |
| 162 |
float out = faiss::kmeans_clustering(d, n, k, x, centroids); |
| 163 |
if (q_error) { |
| 164 |
*q_error = out; |
| 165 |
} |
| 166 |
return 0; |
| 167 |
} |
| 168 |
CATCH_AND_HANDLE |
| 169 |
} |
| 170 |
} |