| 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 "IndexScalarQuantizer_c.h" |
| 11 |
#include <faiss/IndexScalarQuantizer.h> |
| 12 |
#include <faiss/impl/ScalarQuantizer.h> |
| 13 |
#include "macros_impl.h" |
| 14 |
|
| 15 |
using faiss::Index; |
| 16 |
using faiss::IndexIVFScalarQuantizer; |
| 17 |
using faiss::IndexScalarQuantizer; |
| 18 |
|
| 19 |
DEFINE_DESTRUCTOR(IndexScalarQuantizer) |
| 20 |
DEFINE_INDEX_DOWNCAST(IndexScalarQuantizer) |
| 21 |
|
| 22 |
int faiss_IndexScalarQuantizer_new(FaissIndexScalarQuantizer** p_index) { |
| 23 |
try { |
| 24 |
*p_index = reinterpret_cast<FaissIndexScalarQuantizer*>( |
| 25 |
new IndexScalarQuantizer()); |
| 26 |
} |
| 27 |
CATCH_AND_HANDLE |
| 28 |
} |
| 29 |
|
| 30 |
int faiss_IndexScalarQuantizer_new_with( |
| 31 |
FaissIndexScalarQuantizer** p_index, |
| 32 |
idx_t d, |
| 33 |
FaissQuantizerType qt, |
| 34 |
FaissMetricType metric) { |
| 35 |
try { |
| 36 |
IndexScalarQuantizer* index = new IndexScalarQuantizer( |
| 37 |
d, |
| 38 |
static_cast<faiss::ScalarQuantizer::QuantizerType>(qt), |
| 39 |
static_cast<faiss::MetricType>(metric)); |
| 40 |
*p_index = reinterpret_cast<FaissIndexScalarQuantizer*>(index); |
| 41 |
return 0; |
| 42 |
} |
| 43 |
CATCH_AND_HANDLE |
| 44 |
} |
| 45 |
|
| 46 |
DEFINE_DESTRUCTOR(IndexIVFScalarQuantizer) |
| 47 |
DEFINE_INDEX_DOWNCAST(IndexIVFScalarQuantizer) |
| 48 |
|
| 49 |
/// quantizer that maps vectors to inverted lists |
| 50 |
DEFINE_GETTER_PERMISSIVE(IndexIVFScalarQuantizer, FaissIndex*, quantizer) |
| 51 |
|
| 52 |
/// number of possible key values |
| 53 |
DEFINE_GETTER(IndexIVFScalarQuantizer, size_t, nlist) |
| 54 |
/// number of probes at query time |
| 55 |
DEFINE_GETTER(IndexIVFScalarQuantizer, size_t, nprobe) |
| 56 |
DEFINE_SETTER(IndexIVFScalarQuantizer, size_t, nprobe) |
| 57 |
|
| 58 |
/// whether object owns the quantizer |
| 59 |
DEFINE_GETTER(IndexIVFScalarQuantizer, int, own_fields) |
| 60 |
DEFINE_SETTER(IndexIVFScalarQuantizer, int, own_fields) |
| 61 |
|
| 62 |
int faiss_IndexIVFScalarQuantizer_new_with( |
| 63 |
FaissIndexIVFScalarQuantizer** p_index, |
| 64 |
FaissIndex* quantizer, |
| 65 |
size_t d, |
| 66 |
size_t nlist, |
| 67 |
FaissQuantizerType qt) { |
| 68 |
try { |
| 69 |
auto q = reinterpret_cast<Index*>(quantizer); |
| 70 |
auto qt_ = static_cast<faiss::ScalarQuantizer::QuantizerType>(qt); |
| 71 |
IndexIVFScalarQuantizer* index = |
| 72 |
new IndexIVFScalarQuantizer(q, d, nlist, qt_); |
| 73 |
*p_index = reinterpret_cast<FaissIndexIVFScalarQuantizer*>(index); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
CATCH_AND_HANDLE |
| 77 |
} |
| 78 |
|
| 79 |
int faiss_IndexIVFScalarQuantizer_new_with_metric( |
| 80 |
FaissIndexIVFScalarQuantizer** p_index, |
| 81 |
FaissIndex* quantizer, |
| 82 |
size_t d, |
| 83 |
size_t nlist, |
| 84 |
FaissQuantizerType qt, |
| 85 |
FaissMetricType metric, |
| 86 |
int encode_residual) { |
| 87 |
try { |
| 88 |
auto q = reinterpret_cast<Index*>(quantizer); |
| 89 |
auto mt = static_cast<faiss::MetricType>(metric); |
| 90 |
auto er = static_cast<bool>(encode_residual); |
| 91 |
auto qt_ = static_cast<faiss::ScalarQuantizer::QuantizerType>(qt); |
| 92 |
IndexIVFScalarQuantizer* index = |
| 93 |
new IndexIVFScalarQuantizer(q, d, nlist, qt_, mt, er); |
| 94 |
*p_index = reinterpret_cast<FaissIndexIVFScalarQuantizer*>(index); |
| 95 |
return 0; |
| 96 |
} |
| 97 |
CATCH_AND_HANDLE |
| 98 |
} |
| 99 |
|
| 100 |
int faiss_IndexIVFScalarQuantizer_add_core( |
| 101 |
FaissIndexIVFScalarQuantizer* index, |
| 102 |
idx_t n, |
| 103 |
const float* x, |
| 104 |
const idx_t* xids, |
| 105 |
const idx_t* precomputed_idx) { |
| 106 |
try { |
| 107 |
reinterpret_cast<IndexIVFScalarQuantizer*>(index)->add_core( |
| 108 |
n, x, xids, precomputed_idx); |
| 109 |
} |
| 110 |
CATCH_AND_HANDLE |
| 111 |
} |