Skip to content

Commit b73411f

Browse files
authored
Merge branch 'main' into panorama-fp16-storage
2 parents 8f88464 + 7a4e797 commit b73411f

100 files changed

Lines changed: 2501 additions & 407 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

benchs/bench_cppcontrib_sa_decode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ void testMinMaxIndexPQDecoder(
14721472
}
14731473

14741474
//
1475-
int main(int argc, char** argv) {
1475+
int main() {
14761476
// 1 MB points
14771477
const uint64_t INDEX_SIZE = 65536 * 16;
14781478
const uint64_t N_ITERATIONS = 18;

benchs/bench_rabitq_simd.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ void bench_rabitq_and_dot_product_with_sum(benchmark::State& state) {
8282
});
8383
}
8484

85+
void bench_rabitq_and_dot_product_with_sum_fused(benchmark::State& state) {
86+
bench_rabitq_generic(
87+
state,
88+
[](const uint8_t* q, const uint8_t* x, size_t size, size_t qb)
89+
-> int64_t {
90+
auto result = rabitq::bitwise_and_dot_product_with_popcount(
91+
q, x, size, qb);
92+
// Synthetic operation using both inputs for benchmarking.
93+
return result.popcount + result.dot_product;
94+
});
95+
}
96+
8597
template <SIMDLevel SL>
8698
void bench_rabitq_rearrange_impl(benchmark::State& state) {
8799
size_t qb = state.range(0);
@@ -127,6 +139,9 @@ BENCHMARK(bench_rabitq_xor_dot_product)
127139
BENCHMARK(bench_rabitq_and_dot_product_with_sum)
128140
->ArgsProduct({qbs, dims})
129141
->ArgNames({"qb", "d"});
142+
BENCHMARK(bench_rabitq_and_dot_product_with_sum_fused)
143+
->ArgsProduct({qbs, dims})
144+
->ArgNames({"qb", "d"});
130145
BENCHMARK(bench_rabitq_rearrange_scalar)
131146
->ArgsProduct({qbs, dims})
132147
->ArgNames({"qb", "d"});

benchs/bench_result_handler_overhead.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ std::pair<double, double> run_search(
4646
IndexData& data,
4747
int d,
4848
int k,
49-
int nprobe,
50-
const char* factory_string) {
49+
int nprobe) {
5150
ParameterSpace().set_index_parameter(data.index.get(), "nprobe", nprobe);
5251

5352
omp_set_num_threads(1);
@@ -184,8 +183,7 @@ int main() {
184183
std::vector<faiss::BenchmarkResult> results;
185184
for (int k : ks) {
186185
for (int nprobe : nprobes) {
187-
auto [mean, std] = faiss::run_search(
188-
data, d, k, nprobe, index_factory.c_str());
186+
auto [mean, std] = faiss::run_search(data, d, k, nprobe);
189187
results.push_back({index_factory, d, k, nprobe, mean, std});
190188
}
191189
}

c_api/IndexScalarQuantizer_c.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,42 @@ int faiss_IndexIVFScalarQuantizer_add_core(
109109
}
110110
CATCH_AND_HANDLE
111111
}
112+
113+
/** The returned pointer is a borrowed reference to a member of the
114+
* owning index; must not be freed.*/
115+
FaissScalarQuantizer* faiss_IndexScalarQuantizer_sq(
116+
FaissIndexScalarQuantizer* index) {
117+
return reinterpret_cast<FaissScalarQuantizer*>(
118+
&reinterpret_cast<IndexScalarQuantizer*>(index)->sq);
119+
}
120+
121+
/** The returned pointer is a borrowed reference to a member of the
122+
* owning index; must not be freed.*/
123+
FaissScalarQuantizer* faiss_IndexIVFScalarQuantizer_sq(
124+
FaissIndexIVFScalarQuantizer* index) {
125+
return reinterpret_cast<FaissScalarQuantizer*>(
126+
&reinterpret_cast<IndexIVFScalarQuantizer*>(index)->sq);
127+
}
128+
129+
DEFINE_GETTER(ScalarQuantizer, FaissQuantizerType, qtype)
130+
DEFINE_GETTER(ScalarQuantizer, size_t, bits)
131+
DEFINE_GETTER(ScalarQuantizer, size_t, d)
132+
DEFINE_GETTER(ScalarQuantizer, size_t, code_size)
133+
DEFINE_GETTER(ScalarQuantizer, FaissRangeStat, rangestat)
134+
DEFINE_SETTER_STATIC(
135+
ScalarQuantizer,
136+
faiss::ScalarQuantizer::RangeStat,
137+
FaissRangeStat,
138+
rangestat)
139+
DEFINE_GETTER(ScalarQuantizer, float, rangestat_arg)
140+
DEFINE_SETTER(ScalarQuantizer, float, rangestat_arg)
141+
142+
size_t faiss_ScalarQuantizer_trained_size(const FaissScalarQuantizer* sq) {
143+
return reinterpret_cast<const faiss::ScalarQuantizer*>(sq)->trained.size();
144+
}
145+
146+
void faiss_ScalarQuantizer_trained(const FaissScalarQuantizer* sq, float* out) {
147+
const auto& trained =
148+
reinterpret_cast<const faiss::ScalarQuantizer*>(sq)->trained;
149+
memcpy(out, trained.data(), trained.size() * sizeof(float));
150+
}

c_api/IndexScalarQuantizer_c.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,43 @@ typedef enum FaissQuantizerType {
2828
QT_bf16,
2929
QT_8bit_direct_signed, ///< fast indexing of signed int8s ranging from [-128
3030
///< to 127]
31+
QT_0bit, ///< 0 bits per component, centroid-only distance (IVF only; a
32+
///< flat index with QT_0bit stores nothing)
33+
QT_1bit_tqmse, ///< TurboQuant MSE-optimized, 1 bit per component
34+
QT_2bit_tqmse, ///< TurboQuant MSE-optimized, 2 bits per component
35+
QT_3bit_tqmse, ///< TurboQuant MSE-optimized, 3 bits per component
36+
QT_4bit_tqmse, ///< TurboQuant MSE-optimized, 4 bits per component
37+
QT_8bit_tqmse, ///< TurboQuant MSE-optimized, 8 bits per component
38+
QT_2bit_tq, ///< Full TurboQuant (1-bit MSE + 1-bit QJL + factors)
39+
QT_3bit_tq, ///< Full TurboQuant (2-bit MSE + 1-bit QJL + factors)
40+
QT_4bit_tq, ///< Full TurboQuant (3-bit MSE + 1-bit QJL + factors)
41+
QT_5bit_tq, ///< Full TurboQuant (4-bit MSE + 1-bit QJL + factors)
42+
QT_1bit_eden, ///< EDEN Lloyd-Max scalar code, 1 bit per component
43+
QT_2bit_eden, ///< EDEN Lloyd-Max scalar code, 2 bits per component
44+
QT_3bit_eden, ///< EDEN Lloyd-Max scalar code, 3 bits per component
45+
QT_4bit_eden, ///< EDEN Lloyd-Max scalar code, 4 bits per component
46+
QT_5bit_eden, ///< EDEN Lloyd-Max scalar code, 5 bits per component
47+
QT_6bit_eden, ///< EDEN Lloyd-Max scalar code, 6 bits per component
48+
QT_7bit_eden, ///< EDEN Lloyd-Max scalar code, 7 bits per component
49+
QT_8bit_eden, ///< EDEN Lloyd-Max scalar code, 8 bits per component
50+
QT_count
3151
} FaissQuantizerType;
3252

53+
typedef enum FaissRangeStat {
54+
RS_minmax, ///< [min - rs*(max-min), max + rs*(max-min)]
55+
RS_meanstd, ///< [mean - std * rs, mean + std * rs]
56+
RS_quantiles, ///< [Q(rs), Q(1-rs)]
57+
RS_optim, ///< alternate optimization of reconstruction error
58+
} FaissRangeStat;
59+
3360
// forward declaration
3461
typedef enum FaissMetricType FaissMetricType;
3562

63+
/** Opaque type for the ScalarQuantizer codec owned by an index.
64+
* Valid only while the owning index is alive and must not be freed.
65+
*/
66+
FAISS_DECLARE_CLASS(ScalarQuantizer)
67+
3668
/** Opaque type for IndexScalarQuantizer */
3769
FAISS_DECLARE_CLASS_INHERITED(IndexScalarQuantizer, Index)
3870

@@ -90,6 +122,39 @@ int faiss_IndexIVFScalarQuantizer_add_core(
90122
const idx_t* xids,
91123
const idx_t* precomputed_idx);
92124

125+
/** Access the scalar quantizer codec of an IndexScalarQuantizer.
126+
* Borrowed reference, owned by the index; do not free.
127+
*/
128+
FaissScalarQuantizer* faiss_IndexScalarQuantizer_sq(
129+
FaissIndexScalarQuantizer* index);
130+
131+
/** Access the scalar quantizer codec of an IndexIVFScalarQuantizer.
132+
* Borrowed reference, owned by the index; do not free.
133+
*/
134+
FaissScalarQuantizer* faiss_IndexIVFScalarQuantizer_sq(
135+
FaissIndexIVFScalarQuantizer* index);
136+
137+
/// quantizer type of this codec
138+
FAISS_DECLARE_GETTER(ScalarQuantizer, FaissQuantizerType, qtype)
139+
/// bits per scalar code
140+
FAISS_DECLARE_GETTER(ScalarQuantizer, size_t, bits)
141+
/// size of the input vectors
142+
FAISS_DECLARE_GETTER(ScalarQuantizer, size_t, d)
143+
/// bytes per encoded vector
144+
FAISS_DECLARE_GETTER(ScalarQuantizer, size_t, code_size)
145+
/// range estimation strategy (uniform encoder)
146+
FAISS_DECLARE_GETTER_SETTER(ScalarQuantizer, FaissRangeStat, rangestat)
147+
/// argument to the range estimation strategy (rs)
148+
FAISS_DECLARE_GETTER_SETTER(ScalarQuantizer, float, rangestat_arg)
149+
150+
/// Number of trained values
151+
size_t faiss_ScalarQuantizer_trained_size(const FaissScalarQuantizer* sq);
152+
153+
/** Copy the trained values into out, which must hold at least
154+
* faiss_ScalarQuantizer_trained_size() floats.
155+
*/
156+
void faiss_ScalarQuantizer_trained(const FaissScalarQuantizer* sq, float* out);
157+
93158
#ifdef __cplusplus
94159
}
95160
#endif

faiss/IndexAdditiveQuantizer.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ void IndexAdditiveQuantizer::search(
235235
float* distances,
236236
idx_t* labels,
237237
const SearchParameters* params) const {
238-
FAISS_THROW_IF_NOT_MSG(
239-
!params, "search params not supported for this index");
238+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
240239

241240
if (aq->search_type == AdditiveQuantizer::ST_decompress) {
242241
with_VectorDistance(d, metric_type, metric_arg, [&](auto vd) {
@@ -459,8 +458,7 @@ void AdditiveCoarseQuantizer::search(
459458
float* distances,
460459
idx_t* labels,
461460
const SearchParameters* params) const {
462-
FAISS_THROW_IF_NOT_MSG(
463-
!params, "search params not supported for this index");
461+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
464462

465463
if (metric_type == METRIC_INNER_PRODUCT) {
466464
aq->knn_centroids_inner_product(n, x, k, distances, labels);

faiss/IndexAdditiveQuantizerFastScan.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void IndexAdditiveQuantizerFastScan::init(
3636
MetricType metric,
3737
int bbs_) {
3838
FAISS_THROW_IF_NOT(aq_init != nullptr);
39-
FAISS_THROW_IF_NOT(!aq_init->nbits.empty());
39+
FAISS_THROW_IF_MSG(aq_init->nbits.empty(), "nbits must not be empty");
4040
FAISS_THROW_IF_NOT(aq_init->nbits[0] == 4);
4141
if (metric == METRIC_INNER_PRODUCT) {
4242
FAISS_THROW_IF_NOT_MSG(
@@ -192,8 +192,7 @@ void IndexAdditiveQuantizerFastScan::search(
192192
float* distances,
193193
idx_t* labels,
194194
const SearchParameters* params) const {
195-
FAISS_THROW_IF_NOT_MSG(
196-
!params, "search params not supported for this index");
195+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
197196
FAISS_THROW_IF_NOT(k > 0);
198197
bool rescale = (rescale_norm && norm_scale > 1 && metric_type == METRIC_L2);
199198
if (!rescale) {

faiss/IndexBinaryFromFloat.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ void IndexBinaryFromFloat::search(
5555
int32_t* distances,
5656
idx_t* labels,
5757
const SearchParameters* params) const {
58-
FAISS_THROW_IF_NOT_MSG(
59-
!params, "search params not supported for this index");
58+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
6059
FAISS_THROW_IF_NOT(k > 0);
6160

6261
constexpr idx_t bs = 32768;

faiss/IndexBinaryHNSW.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ void IndexBinaryHNSW::reconstruct(idx_t key, uint8_t* recons) const {
292292

293293
DistanceComputer* IndexBinaryHNSW::get_distance_computer() const {
294294
IndexBinaryFlat* flat_storage = dynamic_cast<IndexBinaryFlat*>(storage);
295-
FAISS_THROW_IF_NOT_MSG(
296-
flat_storage != nullptr,
295+
FAISS_THROW_IF_MSG(
296+
flat_storage == nullptr,
297297
"IndexBinaryHNSW requires IndexBinaryFlat storage");
298298
return with_simd_level([&]<SIMDLevel SL>() {
299299
return make_binary_hnsw_distance_computer_fixSL<SL>(
@@ -316,8 +316,8 @@ IndexBinaryHNSWCagra::IndexBinaryHNSWCagra(int d_, int M)
316316
}
317317

318318
void IndexBinaryHNSWCagra::add(idx_t n, const uint8_t* x) {
319-
FAISS_THROW_IF_NOT_MSG(
320-
!base_level_only,
319+
FAISS_THROW_IF_MSG(
320+
base_level_only,
321321
"Cannot add vectors when base_level_only is set to True");
322322

323323
IndexBinaryHNSW::add(n, x);

faiss/IndexBinaryHash.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ void IndexBinaryHash::range_search(
8383
int radius,
8484
RangeSearchResult* result,
8585
const SearchParameters* params) const {
86-
FAISS_THROW_IF_NOT_MSG(
87-
!params, "search params not supported for this index");
86+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
8887
size_t nlist = 0, ndis = 0, n0 = 0;
8988

9089
#pragma omp parallel if (n > 100) reduction(+ : ndis, n0, nlist)
@@ -116,8 +115,7 @@ void IndexBinaryHash::search(
116115
int32_t* distances,
117116
idx_t* labels,
118117
const SearchParameters* params) const {
119-
FAISS_THROW_IF_NOT_MSG(
120-
!params, "search params not supported for this index");
118+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
121119
FAISS_THROW_IF_NOT(k > 0);
122120

123121
using HeapForL2 = CMax<int32_t, idx_t>;
@@ -216,8 +214,7 @@ void IndexBinaryMultiHash::range_search(
216214
int radius,
217215
RangeSearchResult* result,
218216
const SearchParameters* params) const {
219-
FAISS_THROW_IF_NOT_MSG(
220-
!params, "search params not supported for this index");
217+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
221218
size_t nlist = 0, ndis = 0, n0 = 0;
222219

223220
#pragma omp parallel if (n > 100) reduction(+ : ndis, n0, nlist)
@@ -249,8 +246,7 @@ void IndexBinaryMultiHash::search(
249246
int32_t* distances,
250247
idx_t* labels,
251248
const SearchParameters* params) const {
252-
FAISS_THROW_IF_NOT_MSG(
253-
!params, "search params not supported for this index");
249+
FAISS_THROW_IF_MSG(params, "search params not supported for this index");
254250
FAISS_THROW_IF_NOT(k > 0);
255251

256252
using HeapForL2 = CMax<int32_t, idx_t>;

0 commit comments

Comments
 (0)