| 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 "VectorTransform_c.h" |
| 11 |
#include <faiss/VectorTransform.h> |
| 12 |
#include "macros_impl.h" |
| 13 |
|
| 14 |
extern "C" { |
| 15 |
|
| 16 |
DEFINE_DESTRUCTOR(VectorTransform) |
| 17 |
|
| 18 |
DEFINE_GETTER(VectorTransform, int, is_trained) |
| 19 |
|
| 20 |
DEFINE_GETTER(VectorTransform, int, d_in) |
| 21 |
|
| 22 |
DEFINE_GETTER(VectorTransform, int, d_out) |
| 23 |
|
| 24 |
int faiss_VectorTransform_train( |
| 25 |
FaissVectorTransform* vt, |
| 26 |
idx_t n, |
| 27 |
const float* x) { |
| 28 |
try { |
| 29 |
reinterpret_cast<faiss::VectorTransform*>(vt)->train(n, x); |
| 30 |
} |
| 31 |
CATCH_AND_HANDLE |
| 32 |
} |
| 33 |
|
| 34 |
float* faiss_VectorTransform_apply( |
| 35 |
const FaissVectorTransform* vt, |
| 36 |
idx_t n, |
| 37 |
const float* x) { |
| 38 |
return reinterpret_cast<const faiss::VectorTransform*>(vt)->apply(n, x); |
| 39 |
} |
| 40 |
|
| 41 |
void faiss_VectorTransform_apply_noalloc( |
| 42 |
const FaissVectorTransform* vt, |
| 43 |
idx_t n, |
| 44 |
const float* x, |
| 45 |
float* xt) { |
| 46 |
return reinterpret_cast<const faiss::VectorTransform*>(vt)->apply_noalloc( |
| 47 |
n, x, xt); |
| 48 |
} |
| 49 |
|
| 50 |
void faiss_VectorTransform_reverse_transform( |
| 51 |
const FaissVectorTransform* vt, |
| 52 |
idx_t n, |
| 53 |
const float* xt, |
| 54 |
float* x) { |
| 55 |
return reinterpret_cast<const faiss::VectorTransform*>(vt) |
| 56 |
->reverse_transform(n, xt, x); |
| 57 |
} |
| 58 |
|
| 59 |
/********************************************* |
| 60 |
* LinearTransform |
| 61 |
*********************************************/ |
| 62 |
|
| 63 |
DEFINE_DESTRUCTOR(LinearTransform) |
| 64 |
|
| 65 |
DEFINE_GETTER(LinearTransform, int, have_bias) |
| 66 |
|
| 67 |
DEFINE_GETTER(LinearTransform, int, is_orthonormal) |
| 68 |
|
| 69 |
void faiss_LinearTransform_transform_transpose( |
| 70 |
const FaissLinearTransform* vt, |
| 71 |
idx_t n, |
| 72 |
const float* y, |
| 73 |
float* x) { |
| 74 |
return reinterpret_cast<const faiss::LinearTransform*>(vt) |
| 75 |
->transform_transpose(n, y, x); |
| 76 |
} |
| 77 |
|
| 78 |
void faiss_LinearTransform_set_is_orthonormal(FaissLinearTransform* vt) { |
| 79 |
return reinterpret_cast<faiss::LinearTransform*>(vt)->set_is_orthonormal(); |
| 80 |
} |
| 81 |
|
| 82 |
/********************************************* |
| 83 |
* RandomRotationMatrix |
| 84 |
*********************************************/ |
| 85 |
|
| 86 |
DEFINE_DESTRUCTOR(RandomRotationMatrix) |
| 87 |
|
| 88 |
int faiss_RandomRotationMatrix_new_with( |
| 89 |
FaissRandomRotationMatrix** p_vt, |
| 90 |
int d_in, |
| 91 |
int d_out) { |
| 92 |
try { |
| 93 |
*p_vt = reinterpret_cast<FaissRandomRotationMatrix*>( |
| 94 |
new faiss::RandomRotationMatrix(d_in, d_out)); |
| 95 |
} |
| 96 |
CATCH_AND_HANDLE |
| 97 |
} |
| 98 |
|
| 99 |
/********************************************* |
| 100 |
* PCAMatrix |
| 101 |
*********************************************/ |
| 102 |
|
| 103 |
DEFINE_DESTRUCTOR(PCAMatrix) |
| 104 |
|
| 105 |
int faiss_PCAMatrix_new_with( |
| 106 |
FaissPCAMatrix** p_vt, |
| 107 |
int d_in, |
| 108 |
int d_out, |
| 109 |
float eigen_power, |
| 110 |
int random_rotation) { |
| 111 |
try { |
| 112 |
bool random_rotation_ = static_cast<bool>(random_rotation); |
| 113 |
*p_vt = reinterpret_cast<FaissPCAMatrix*>(new faiss::PCAMatrix( |
| 114 |
d_in, d_out, eigen_power, random_rotation_)); |
| 115 |
} |
| 116 |
CATCH_AND_HANDLE |
| 117 |
} |
| 118 |
|
| 119 |
DEFINE_GETTER(PCAMatrix, float, eigen_power) |
| 120 |
|
| 121 |
DEFINE_GETTER(PCAMatrix, int, random_rotation) |
| 122 |
|
| 123 |
DEFINE_GETTER(PCAMatrix, int, balanced_bins) |
| 124 |
|
| 125 |
DEFINE_SETTER(PCAMatrix, int, balanced_bins) |
| 126 |
|
| 127 |
/********************************************* |
| 128 |
* ITQMatrix |
| 129 |
*********************************************/ |
| 130 |
|
| 131 |
DEFINE_DESTRUCTOR(ITQMatrix) |
| 132 |
|
| 133 |
int faiss_ITQMatrix_new_with(FaissITQMatrix** p_vt, int d) { |
| 134 |
try { |
| 135 |
*p_vt = reinterpret_cast<FaissITQMatrix*>(new faiss::ITQMatrix(d)); |
| 136 |
} |
| 137 |
CATCH_AND_HANDLE |
| 138 |
} |
| 139 |
|
| 140 |
DEFINE_DESTRUCTOR(ITQTransform) |
| 141 |
|
| 142 |
int faiss_ITQTransform_new_with( |
| 143 |
FaissITQTransform** p_vt, |
| 144 |
int d_in, |
| 145 |
int d_out, |
| 146 |
int do_pca) { |
| 147 |
try { |
| 148 |
bool do_pca_ = static_cast<bool>(do_pca); |
| 149 |
*p_vt = reinterpret_cast<FaissITQTransform*>( |
| 150 |
new faiss::ITQTransform(d_in, d_out, do_pca_)); |
| 151 |
} |
| 152 |
CATCH_AND_HANDLE |
| 153 |
} |
| 154 |
|
| 155 |
DEFINE_GETTER(ITQTransform, int, do_pca) |
| 156 |
|
| 157 |
/********************************************* |
| 158 |
* OPQMatrix |
| 159 |
*********************************************/ |
| 160 |
|
| 161 |
DEFINE_DESTRUCTOR(OPQMatrix) |
| 162 |
|
| 163 |
int faiss_OPQMatrix_new_with(FaissOPQMatrix** p_vt, int d, int M, int d2) { |
| 164 |
try { |
| 165 |
*p_vt = reinterpret_cast<FaissOPQMatrix*>( |
| 166 |
new faiss::OPQMatrix(d, M, d2)); |
| 167 |
} |
| 168 |
CATCH_AND_HANDLE |
| 169 |
} |
| 170 |
|
| 171 |
DEFINE_GETTER(OPQMatrix, int, verbose) |
| 172 |
DEFINE_SETTER(OPQMatrix, int, verbose) |
| 173 |
|
| 174 |
DEFINE_GETTER(OPQMatrix, int, niter) |
| 175 |
DEFINE_SETTER(OPQMatrix, int, niter) |
| 176 |
|
| 177 |
DEFINE_GETTER(OPQMatrix, int, niter_pq) |
| 178 |
DEFINE_SETTER(OPQMatrix, int, niter_pq) |
| 179 |
|
| 180 |
/********************************************* |
| 181 |
* RemapDimensionsTransform |
| 182 |
*********************************************/ |
| 183 |
|
| 184 |
DEFINE_DESTRUCTOR(RemapDimensionsTransform) |
| 185 |
|
| 186 |
int faiss_RemapDimensionsTransform_new_with( |
| 187 |
FaissRemapDimensionsTransform** p_vt, |
| 188 |
int d_in, |
| 189 |
int d_out, |
| 190 |
int uniform) { |
| 191 |
try { |
| 192 |
bool uniform_ = static_cast<bool>(uniform); |
| 193 |
*p_vt = reinterpret_cast<FaissRemapDimensionsTransform*>( |
| 194 |
new faiss::RemapDimensionsTransform(d_in, d_out, uniform_)); |
| 195 |
} |
| 196 |
CATCH_AND_HANDLE |
| 197 |
} |
| 198 |
|
| 199 |
/********************************************* |
| 200 |
* NormalizationTransform |
| 201 |
*********************************************/ |
| 202 |
|
| 203 |
DEFINE_DESTRUCTOR(NormalizationTransform) |
| 204 |
|
| 205 |
int faiss_NormalizationTransform_new_with( |
| 206 |
FaissNormalizationTransform** p_vt, |
| 207 |
int d, |
| 208 |
float norm) { |
| 209 |
try { |
| 210 |
*p_vt = reinterpret_cast<FaissNormalizationTransform*>( |
| 211 |
new faiss::NormalizationTransform(d, norm)); |
| 212 |
} |
| 213 |
CATCH_AND_HANDLE |
| 214 |
} |
| 215 |
|
| 216 |
DEFINE_GETTER(NormalizationTransform, float, norm) |
| 217 |
|
| 218 |
/********************************************* |
| 219 |
* CenteringTransform |
| 220 |
*********************************************/ |
| 221 |
|
| 222 |
DEFINE_DESTRUCTOR(CenteringTransform) |
| 223 |
|
| 224 |
int faiss_CenteringTransform_new_with(FaissCenteringTransform** p_vt, int d) { |
| 225 |
try { |
| 226 |
*p_vt = reinterpret_cast<FaissCenteringTransform*>( |
| 227 |
new faiss::CenteringTransform(d)); |
| 228 |
} |
| 229 |
CATCH_AND_HANDLE |
| 230 |
} |
| 231 |
} |