fix: resolve compiler warnings in top-level FAISS source files (#4943)

Summary:
- Fix compiler warnings in AutoTune, Clustering, IVFlib, VectorTransform, clone_index, index_factory, MetaIndexes, IndexReplicas, IndexRowwiseMinMax, IndexScalarQuantizer, IndexShards, IndexShardsIVF
- Fixes include `-Wshadow`, `-Wunused-parameter`, `-Wformat`, and `-Woverloaded-virtual`

All changes are mechanical. No functional changes.

Part 12/13 of the compiler warnings cleanup (split from https://github.com/facebookresearch/faiss/issues/4810 per maintainer request).

Pull Request resolved: https://github.com/facebookresearch/faiss/pull/4943

Test Plan:
- [x] No functional changes — all fixes are mechanical renames and annotations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

Reviewed By: mnorris11

Differential Revision: D96996987

Pulled By: alibeklfc

fbshipit-source-id: 82e71810c4846284b602e045ea8069a1c3bb567f
This commit is contained in:
Max Buckley
2026-03-20 10:49:40 -07:00
committed by meta-codesync[bot]
co-authored by Claude Opus 4.6
parent b3a2914982
commit 44f57ba4c9
13 changed files with 349 additions and 311 deletions
+82 -77
View File
@@ -33,35 +33,36 @@
namespace faiss {
AutoTuneCriterion::AutoTuneCriterion(idx_t nq, idx_t nnn)
: nq(nq), nnn(nnn), gt_nnn(0) {}
AutoTuneCriterion::AutoTuneCriterion(idx_t nq_in, idx_t nnn_in)
: nq(nq_in), nnn(nnn_in), gt_nnn(0) {}
void AutoTuneCriterion::set_groundtruth(
int gt_nnn,
int gt_nnn_in,
const float* gt_D_in,
const idx_t* gt_I_in) {
this->gt_nnn = gt_nnn;
this->gt_nnn = gt_nnn_in;
if (gt_D_in) { // allow null for this, as it is often not used
gt_D.resize(nq * gt_nnn);
memcpy(gt_D.data(), gt_D_in, sizeof(gt_D[0]) * nq * gt_nnn);
gt_D.resize(nq * gt_nnn_in);
memcpy(gt_D.data(), gt_D_in, sizeof(gt_D[0]) * nq * gt_nnn_in);
}
gt_I.resize(nq * gt_nnn);
memcpy(gt_I.data(), gt_I_in, sizeof(gt_I[0]) * nq * gt_nnn);
gt_I.resize(nq * gt_nnn_in);
memcpy(gt_I.data(), gt_I_in, sizeof(gt_I[0]) * nq * gt_nnn_in);
}
OneRecallAtRCriterion::OneRecallAtRCriterion(idx_t nq, idx_t R)
: AutoTuneCriterion(nq, R), R(R) {}
OneRecallAtRCriterion::OneRecallAtRCriterion(idx_t nq_in, idx_t R_in)
: AutoTuneCriterion(nq_in, R_in), R(R_in) {}
double OneRecallAtRCriterion::evaluate(const float* /*D*/, const idx_t* I)
const {
FAISS_THROW_IF_NOT_MSG(
(gt_I.size() == gt_nnn * nq && gt_nnn >= 1 && nnn >= R),
(gt_I.size() == static_cast<size_t>(gt_nnn * nq) && gt_nnn >= 1 &&
nnn >= R),
"ground truth not initialized");
idx_t n_ok = 0;
for (idx_t q = 0; q < nq; q++) {
idx_t gt_nn = gt_I[q * gt_nnn];
const idx_t* I_line = I + q * nnn;
for (int i = 0; i < R; i++) {
for (idx_t i = 0; i < R; i++) {
if (I_line[i] == gt_nn) {
n_ok++;
break;
@@ -71,13 +72,14 @@ double OneRecallAtRCriterion::evaluate(const float* /*D*/, const idx_t* I)
return n_ok / double(nq);
}
IntersectionCriterion::IntersectionCriterion(idx_t nq, idx_t R)
: AutoTuneCriterion(nq, R), R(R) {}
IntersectionCriterion::IntersectionCriterion(idx_t nq_in, idx_t R_in)
: AutoTuneCriterion(nq_in, R_in), R(R_in) {}
double IntersectionCriterion::evaluate(const float* /*D*/, const idx_t* I)
const {
FAISS_THROW_IF_NOT_MSG(
(gt_I.size() == gt_nnn * nq && gt_nnn >= R && nnn >= R),
(gt_I.size() == static_cast<size_t>(gt_nnn * nq) && gt_nnn >= R &&
nnn >= R),
"ground truth not initialized");
int64_t n_ok = 0;
#pragma omp parallel for reduction(+ : n_ok)
@@ -126,7 +128,7 @@ bool OperatingPoints::add(
return false;
}
} else {
int i;
size_t i;
// stricto sensu this should be a bisection
for (i = 0; i < a.size(); i++) {
if (a[i].perf >= perf) {
@@ -145,7 +147,7 @@ bool OperatingPoints::add(
}
}
// remove non-optimal points from array
for (int i = a.size() - 1; i > 0; --i) {
for (size_t i = a.size() - 1; i > 0; --i) {
if (a[i].t < a[i - 1].t) {
a.erase(a.begin() + (i - 1));
}
@@ -157,7 +159,7 @@ int OperatingPoints::merge_with(
const OperatingPoints& other,
const std::string& prefix) {
int n_add = 0;
for (int i = 0; i < other.all_pts.size(); i++) {
for (size_t i = 0; i < other.all_pts.size(); i++) {
const OperatingPoint& op = other.all_pts[i];
if (add(op.perf, op.t, prefix + op.key, op.cno)) {
n_add++;
@@ -191,7 +193,7 @@ void OperatingPoints::all_to_gnuplot(const char* fname) const {
perror("");
abort();
}
for (int i = 0; i < all_pts.size(); i++) {
for (size_t i = 0; i < all_pts.size(); i++) {
const OperatingPoint& op = all_pts[i];
fprintf(f, "%g %g %s\n", op.perf, op.t, op.key.c_str());
}
@@ -206,7 +208,7 @@ void OperatingPoints::optimal_to_gnuplot(const char* fname) const {
abort();
}
double prev_perf = 0.0;
for (int i = 0; i < optimal_pts.size(); i++) {
for (size_t i = 0; i < optimal_pts.size(); i++) {
const OperatingPoint& op = optimal_pts[i];
fprintf(f, "%g %g\n", prev_perf, op.t);
fprintf(f, "%g %g %s\n", op.perf, op.t, op.key.c_str());
@@ -222,11 +224,11 @@ void OperatingPoints::display(bool only_optimal) const {
all_pts.size(),
optimal_pts.size());
for (int i = 0; i < pts.size(); i++) {
for (size_t i = 0; i < pts.size(); i++) {
const OperatingPoint& op = pts[i];
const char* star = "";
if (!only_optimal) {
for (int j = 0; j < optimal_pts.size(); j++) {
for (size_t j = 0; j < optimal_pts.size(); j++) {
if (op.cno == optimal_pts[j].cno) {
star = "*";
break;
@@ -269,7 +271,7 @@ ParameterSpace::ParameterSpace (Index *index):
size_t ParameterSpace::n_combinations() const {
size_t n = 1;
for (int i = 0; i < parameter_ranges.size(); i++) {
for (size_t i = 0; i < parameter_ranges.size(); i++) {
n *= parameter_ranges[i].values.size();
}
return n;
@@ -279,7 +281,7 @@ size_t ParameterSpace::n_combinations() const {
std::string ParameterSpace::combination_name(size_t cno) const {
char buf[1000], *wp = buf;
*wp = 0;
for (int i = 0; i < parameter_ranges.size(); i++) {
for (size_t i = 0; i < parameter_ranges.size(); i++) {
FAISS_THROW_IF_NOT_MSG(
buf + 1000 - wp >= 0, "Overflow detected in snprintf");
const ParameterRange& pr = parameter_ranges[i];
@@ -297,8 +299,8 @@ std::string ParameterSpace::combination_name(size_t cno) const {
}
bool ParameterSpace::combination_ge(size_t c1, size_t c2) const {
for (int i = 0; i < parameter_ranges.size(); i++) {
int nval = parameter_ranges[i].values.size();
for (size_t i = 0; i < parameter_ranges.size(); i++) {
size_t nval = parameter_ranges[i].values.size();
size_t j1 = c1 % nval;
size_t j2 = c2 % nval;
if (!(j1 >= j2)) {
@@ -316,7 +318,7 @@ static void init_pq_ParameterRange(
if (pq.code_size % 4 == 0) {
// Polysemous not supported for code sizes that are not a
// multiple of 4
for (int i = 2; i <= pq.code_size * 8 / 2; i += 2) {
for (size_t i = 2; i <= pq.code_size * 8 / 2; i += 2) {
pr.values.push_back(i);
}
}
@@ -336,22 +338,22 @@ ParameterRange& ParameterSpace::add_range(const std::string& name) {
// Do not use this macro if ix will be unused
#define DC(classname) \
const classname* ix = dynamic_cast<const classname*>(index)
const classname* ix_c = dynamic_cast<const classname*>(index)
/// initialize with reasonable parameters for this type of index
void ParameterSpace::initialize(const Index* index) {
if (DC(IndexPreTransform)) {
index = ix->index;
index = ix_c->index;
}
if (DC(IndexRefine)) {
ParameterRange& pr = add_range("k_factor_rf");
for (int i = 0; i <= 6; i++) {
pr.values.push_back(1 << i);
}
index = ix->base_index;
index = ix_c->base_index;
}
if (DC(IndexPreTransform)) {
index = ix->index;
index = ix_c->index;
}
if (DC(IndexIVFInterface)) {
@@ -359,14 +361,14 @@ void ParameterSpace::initialize(const Index* index) {
ParameterRange& pr = add_range("nprobe");
for (int i = 0; i < 13; i++) {
size_t nprobe = 1 << i;
if (nprobe >= ix->nlist) {
if (nprobe >= ix_c->nlist) {
break;
}
pr.values.push_back(nprobe);
}
}
ParameterSpace ivf_pspace;
ivf_pspace.initialize(ix->quantizer);
ivf_pspace.initialize(ix_c->quantizer);
for (const ParameterRange& p : ivf_pspace.parameter_ranges) {
ParameterRange& pr = add_range("quantizer_" + p.name);
@@ -375,16 +377,16 @@ void ParameterSpace::initialize(const Index* index) {
}
if (DC(IndexPQ)) {
ParameterRange& pr = add_range("ht");
init_pq_ParameterRange(ix->pq, pr);
init_pq_ParameterRange(ix_c->pq, pr);
}
if (DC(IndexIVFPQ)) {
ParameterRange& pr = add_range("ht");
init_pq_ParameterRange(ix->pq, pr);
init_pq_ParameterRange(ix_c->pq, pr);
}
if (DC(IndexIVF)) {
const MultiIndexQuantizer* miq =
dynamic_cast<const MultiIndexQuantizer*>(ix->quantizer);
dynamic_cast<const MultiIndexQuantizer*>(ix_c->quantizer);
if (miq) {
ParameterRange& pr_max_codes = add_range("max_codes");
for (int i = 8; i < 20; i++) {
@@ -415,7 +417,7 @@ static void set_index_parameters_common(
const ParameterSpace* ps,
size_t cno,
SetParamFunc set_param) {
for (int i = 0; i < ps->parameter_ranges.size(); i++) {
for (size_t i = 0; i < ps->parameter_ranges.size(); i++) {
const ParameterRange& pr = ps->parameter_ranges[i];
size_t j = cno % pr.values.size();
cno /= pr.values.size();
@@ -481,7 +483,7 @@ void ParameterSpace::set_index_parameters(
// non-const version
// Do not use this macro if ix will be unused
#define DC(classname) classname* ix = dynamic_cast<classname*>(index)
#define DC(classname) classname* ix_ = dynamic_cast<classname*>(index)
void ParameterSpace::set_index_parameter(
Index* index,
@@ -496,11 +498,11 @@ void ParameterSpace::set_index_parameter(
// and fall through to also enable it on sub-indexes
}
if (DC(IndexIDMap)) {
set_index_parameter(ix->index, name, val);
set_index_parameter(ix_->index, name, val);
return;
}
if (DC(IndexPreTransform)) {
set_index_parameter(ix->index, name, val);
set_index_parameter(ix_->index, name, val);
return;
}
if (DC(IndexShardsIVF)) {
@@ -509,7 +511,7 @@ void ParameterSpace::set_index_parameter(
if (name.find("quantizer_") == 0 && name != "nprobe" &&
name != "quantizer_nprobe") {
std::string sub_name = name.substr(strlen("quantizer_"));
set_index_parameter(ix->quantizer, sub_name, val);
set_index_parameter(ix_->quantizer, sub_name, val);
return;
}
}
@@ -518,16 +520,16 @@ void ParameterSpace::set_index_parameter(
auto fn = [this, name, val](int /* no */, Index* subIndex) {
set_index_parameter(subIndex, name, val);
};
ix->runOnIndex(fn);
ix_->runOnIndex(fn);
return;
}
if (DC(IndexRefine)) {
if (name == "k_factor_rf") {
ix->k_factor = int(val);
ix_->k_factor = int(val);
return;
}
// otherwise it is for the sub-index
set_index_parameter(ix->base_index, name, val);
set_index_parameter(ix_->base_index, name, val);
return;
}
@@ -537,25 +539,26 @@ void ParameterSpace::set_index_parameter(
if (name == "nprobe") {
if (DC(IndexIVF)) {
ix->nprobe = int(val);
ix_->nprobe = int(val);
return;
}
}
if (name == "ht") {
if (DC(IndexPQ)) {
if (val >= ix->pq.code_size * 8) {
ix->search_type = IndexPQ::ST_PQ;
if (val >= ix_->pq.code_size * 8) {
ix_->search_type = IndexPQ::ST_PQ;
} else {
ix->search_type = IndexPQ::ST_polysemous;
ix->polysemous_ht = int(val);
ix_->search_type = IndexPQ::ST_polysemous;
ix_->polysemous_ht = int(val);
}
return;
} else if (DC(IndexIVFPQ)) {
if (val >= ix->pq.code_size * 8) {
ix->polysemous_ht = 0;
}
if (DC(IndexIVFPQ)) {
if (val >= ix_->pq.code_size * 8) {
ix_->polysemous_ht = 0;
} else {
ix->polysemous_ht = int(val);
ix_->polysemous_ht = int(val);
}
return;
}
@@ -563,31 +566,31 @@ void ParameterSpace::set_index_parameter(
if (name == "k_factor") {
if (DC(IndexIVFPQR)) {
ix->k_factor = val;
ix_->k_factor = val;
return;
}
}
if (name == "max_codes") {
if (DC(IndexIVF)) {
ix->max_codes = std::isfinite(val) ? size_t(val) : 0;
ix_->max_codes = std::isfinite(val) ? size_t(val) : 0;
return;
}
}
if (name == "prune_headroom") {
if (DC(IndexHNSW)) {
ix->hnsw.prune_headroom = val;
ix_->hnsw.prune_headroom = val;
return;
}
}
if (name == "efConstruction") {
if (DC(IndexHNSW)) {
ix->hnsw.efConstruction = int(val);
ix_->hnsw.efConstruction = int(val);
return;
}
if (DC(IndexIVF)) {
if (IndexHNSW* cq = dynamic_cast<IndexHNSW*>(ix->quantizer)) {
if (IndexHNSW* cq = dynamic_cast<IndexHNSW*>(ix_->quantizer)) {
cq->hnsw.efConstruction = int(val);
return;
}
@@ -596,11 +599,11 @@ void ParameterSpace::set_index_parameter(
if (name == "efSearch") {
if (DC(IndexHNSW)) {
ix->hnsw.efSearch = int(val);
ix_->hnsw.efSearch = int(val);
return;
}
if (DC(IndexIVF)) {
if (IndexHNSW* cq = dynamic_cast<IndexHNSW*>(ix->quantizer)) {
if (IndexHNSW* cq = dynamic_cast<IndexHNSW*>(ix_->quantizer)) {
cq->hnsw.efSearch = int(val);
return;
}
@@ -610,7 +613,7 @@ void ParameterSpace::set_index_parameter(
if (name.find("quantizer_") == 0) {
if (DC(IndexIVF)) {
std::string sub_name = name.substr(strlen("quantizer_"));
set_index_parameter(ix->quantizer, sub_name, val);
set_index_parameter(ix_->quantizer, sub_name, val);
return;
}
}
@@ -635,7 +638,7 @@ void ParameterSpace::set_index_parameter(
}
if (DC(IndexBinaryIDMap)) {
set_index_parameter(ix->index, name, val);
set_index_parameter(ix_->index, name, val);
return;
}
@@ -645,26 +648,26 @@ void ParameterSpace::set_index_parameter(
if (name == "nprobe") {
if (DC(IndexBinaryIVF)) {
ix->nprobe = int(val);
ix_->nprobe = int(val);
return;
}
}
if (name == "max_codes") {
if (DC(IndexBinaryIVF)) {
ix->max_codes = std::isfinite(val) ? size_t(val) : 0;
ix_->max_codes = std::isfinite(val) ? size_t(val) : 0;
return;
}
}
if (name == "efConstruction") {
if (DC(IndexBinaryHNSW)) {
ix->hnsw.efConstruction = int(val);
ix_->hnsw.efConstruction = int(val);
return;
}
if (DC(IndexBinaryIVF)) {
if (IndexBinaryHNSW* cq =
dynamic_cast<IndexBinaryHNSW*>(ix->quantizer)) {
dynamic_cast<IndexBinaryHNSW*>(ix_->quantizer)) {
cq->hnsw.efConstruction = int(val);
return;
}
@@ -673,12 +676,12 @@ void ParameterSpace::set_index_parameter(
if (name == "efSearch") {
if (DC(IndexBinaryHNSW)) {
ix->hnsw.efSearch = int(val);
ix_->hnsw.efSearch = int(val);
return;
}
if (DC(IndexBinaryIVF)) {
if (IndexBinaryHNSW* cq =
dynamic_cast<IndexBinaryHNSW*>(ix->quantizer)) {
dynamic_cast<IndexBinaryHNSW*>(ix_->quantizer)) {
cq->hnsw.efSearch = int(val);
return;
}
@@ -688,7 +691,7 @@ void ParameterSpace::set_index_parameter(
if (name.find("quantizer_") == 0) {
if (DC(IndexBinaryIVF)) {
std::string sub_name = name.substr(strlen("quantizer_"));
set_index_parameter(ix->quantizer, sub_name, val);
set_index_parameter(ix_->quantizer, sub_name, val);
return;
}
}
@@ -705,11 +708,11 @@ void ParameterSpace::display() const {
printf("ParameterSpace, %zd parameters, %zd combinations:\n",
parameter_ranges.size(),
n_combinations());
for (int i = 0; i < parameter_ranges.size(); i++) {
for (size_t i = 0; i < parameter_ranges.size(); i++) {
const ParameterRange& pr = parameter_ranges[i];
printf(" %s: ", pr.name.c_str());
char sep = '[';
for (int j = 0; j < pr.values.size(); j++) {
for (size_t j = 0; j < pr.values.size(); j++) {
printf("%c %g", sep, pr.values[j]);
sep = ',';
}
@@ -741,7 +744,8 @@ void ParameterSpace::explore(
const AutoTuneCriterion& crit,
OperatingPoints* ops) const {
FAISS_THROW_IF_NOT_MSG(
nq == crit.nq, "criterion does not have the same nb of queries");
nq == static_cast<size_t>(crit.nq),
"criterion does not have the same nb of queries");
size_t n_comb = n_combinations();
@@ -772,7 +776,7 @@ void ParameterSpace::explore(
return;
}
int n_exp = n_experiments;
size_t n_exp = static_cast<size_t>(n_experiments);
if (n_exp > n_comb) {
n_exp = n_comb;
@@ -784,7 +788,7 @@ void ParameterSpace::explore(
if (n_comb > 1) {
perm[1] = n_comb - 1;
rand_perm(&perm[2], n_comb - 2, 1234);
for (int i = 2; i < perm.size(); i++) {
for (size_t i = 2; i < perm.size(); i++) {
perm[i]++;
}
}
@@ -793,7 +797,7 @@ void ParameterSpace::explore(
size_t cno = perm[xp];
if (verbose) {
printf(" %zd/%d: cno=%zd %s ",
printf(" %zd/%zd: cno=%zd %s ",
xp,
n_exp,
cno,
@@ -803,7 +807,7 @@ void ParameterSpace::explore(
{
double lower_bound_t = 0.0;
double upper_bound_perf = 1.0;
for (int i = 0; i < ops->all_pts.size(); i++) {
for (size_t i = 0; i < ops->all_pts.size(); i++) {
update_bounds(
cno,
ops->all_pts[i],
@@ -834,7 +838,8 @@ void ParameterSpace::explore(
do {
if (thread_over_batches) {
#pragma omp parallel for
for (idx_t q0 = 0; q0 < nq; q0 += batchsize) {
for (int64_t q0 = 0; q0 < static_cast<int64_t>(nq);
q0 += batchsize) {
size_t q1 = q0 + batchsize;
if (q1 > nq) {
q1 = nq;
+24 -21
View File
@@ -28,10 +28,10 @@
namespace faiss {
Clustering::Clustering(int d, int k) : d(d), k(k) {}
Clustering::Clustering(int d_, int k_) : d(d_), k(k_) {}
Clustering::Clustering(int d, int k, const ClusteringParameters& cp)
: ClusteringParameters(cp), d(d), k(k) {}
Clustering::Clustering(int d_, int k_, const ClusteringParameters& cp)
: ClusteringParameters(cp), d(d_), k(k_) {}
void Clustering::post_process_centroids() {
if (spherical) {
@@ -165,7 +165,8 @@ void compute_centroids(
int64_t ci = assign[i];
assert(ci >= 0 && ci < k + k_frozen);
ci -= k_frozen;
if (ci >= c0 && ci < c1) {
if (ci >= static_cast<int64_t>(c0) &&
ci < static_cast<int64_t>(c1)) {
float* c = centroids + ci * d;
const float* xi;
if (!codec) {
@@ -192,7 +193,7 @@ void compute_centroids(
}
#pragma omp parallel for
for (idx_t ci = 0; ci < k; ci++) {
for (idx_t ci = 0; ci < static_cast<idx_t>(k); ci++) {
if (hassign[ci] == 0) {
continue;
}
@@ -272,7 +273,7 @@ void Clustering::train_encoded(
Index& index,
const float* weights) {
FAISS_THROW_IF_NOT_FMT(
nx >= k,
nx >= static_cast<idx_t>(k),
"Number of training points (%" PRId64
") should be at least "
"as large as number of clusters (%zd)",
@@ -280,13 +281,13 @@ void Clustering::train_encoded(
k);
FAISS_THROW_IF_NOT_FMT(
(!codec || codec->d == d),
(!codec || static_cast<size_t>(codec->d) == d),
"Codec dimension %d not the same as data dimension %d",
int(codec->d),
int(d));
FAISS_THROW_IF_NOT_FMT(
index.d == d,
static_cast<size_t>(index.d) == d,
"Index dimension %d not the same as data dimension %d",
int(index.d),
int(d));
@@ -309,7 +310,7 @@ void Clustering::train_encoded(
std::unique_ptr<float[]> del3;
size_t line_size = codec ? codec->sa_code_size() : sizeof(float) * d;
if (nx > k * max_points_per_centroid) {
if (static_cast<size_t>(nx) > k * max_points_per_centroid) {
uint8_t* x_new;
float* weights_new;
nx = subsample_training_set(
@@ -318,7 +319,7 @@ void Clustering::train_encoded(
x = x_new;
del3.reset(weights_new);
weights = weights_new;
} else if (nx < k * min_points_per_centroid) {
} else if (static_cast<size_t>(nx) < k * min_points_per_centroid) {
fprintf(stderr,
"WARNING clustering %" PRId64
" points to %zd centroids: "
@@ -328,7 +329,7 @@ void Clustering::train_encoded(
idx_t(k) * min_points_per_centroid);
}
if (nx == k) {
if (static_cast<size_t>(nx) == k) {
// this is a corner case, just copy training set to clusters
if (verbose) {
printf("Number of training points (%" PRId64
@@ -486,9 +487,10 @@ void Clustering::train_encoded(
} else {
// search by blocks of decode_block_size vectors
size_t code_size = codec->sa_code_size();
for (size_t i0 = 0; i0 < nx; i0 += decode_block_size) {
for (size_t i0 = 0; i0 < static_cast<size_t>(nx);
i0 += decode_block_size) {
size_t i1 = i0 + decode_block_size;
if (i1 > nx) {
if (i1 > static_cast<size_t>(nx)) {
i1 = nx;
}
codec->sa_decode(
@@ -609,16 +611,16 @@ void Clustering::train_encoded(
}
}
Clustering1D::Clustering1D(int k) : Clustering(1, k) {}
Clustering1D::Clustering1D(int k_) : Clustering(1, k_) {}
Clustering1D::Clustering1D(int k, const ClusteringParameters& cp)
: Clustering(1, k, cp) {}
Clustering1D::Clustering1D(int k_, const ClusteringParameters& cp)
: Clustering(1, k_, cp) {}
void Clustering1D::train_exact(idx_t n, const float* x) {
const float* xt = x;
std::unique_ptr<uint8_t[]> del;
if (n > k * max_points_per_centroid) {
if (static_cast<size_t>(n) > k * max_points_per_centroid) {
uint8_t* x_new;
float* weights_new;
n = subsample_training_set(
@@ -669,13 +671,14 @@ Index* ProgressiveDimIndexFactory::operator()(int dim) {
return new IndexFlatL2(dim);
}
ProgressiveDimClustering::ProgressiveDimClustering(int d, int k) : d(d), k(k) {}
ProgressiveDimClustering::ProgressiveDimClustering(int d_, int k_)
: d(d_), k(k_) {}
ProgressiveDimClustering::ProgressiveDimClustering(
int d,
int k,
int d_,
int k_,
const ProgressiveDimClusteringParameters& cp)
: ProgressiveDimClusteringParameters(cp), d(d), k(k) {}
: ProgressiveDimClusteringParameters(cp), d(d_), k(k_) {}
namespace {
+23 -14
View File
@@ -37,7 +37,7 @@ void check_compatible_for_merge(const Index* index0, const Index* index1) {
FAISS_THROW_IF_NOT_MSG(pt1, "both indexes should be pretransforms");
FAISS_THROW_IF_NOT(pt0->chain.size() == pt1->chain.size());
for (int i = 0; i < pt0->chain.size(); i++) {
for (size_t i = 0; i < pt0->chain.size(); i++) {
FAISS_THROW_IF_NOT(typeid(pt0->chain[i]) == typeid(pt1->chain[i]));
}
@@ -180,9 +180,9 @@ void search_and_return_centroids(
}
}
SlidingIndexWindow::SlidingIndexWindow(Index* index) : index(index) {
SlidingIndexWindow::SlidingIndexWindow(Index* index_) : index(index_) {
n_slice = 0;
IndexIVF* index_ivf = const_cast<IndexIVF*>(extract_index_ivf(index));
IndexIVF* index_ivf = const_cast<IndexIVF*>(extract_index_ivf(index_));
ils = dynamic_cast<ArrayInvertedLists*>(index_ivf->invlists);
FAISS_THROW_IF_NOT_MSG(
ils, "only supports indexes with ArrayInvertedLists");
@@ -249,7 +249,7 @@ void SlidingIndexWindow::step(const Index* sub_index, bool remove_oldest) {
IndexIVF* index_ivf = extract_index_ivf(index);
if (remove_oldest && ils2) {
for (int i = 0; i < nlist; i++) {
for (size_t i = 0; i < nlist; i++) {
std::vector<size_t>& sizesi = sizes[i];
size_t amount_to_remove = sizesi[0];
index_ivf->ntotal += ils2->ids[i].size() - amount_to_remove;
@@ -265,7 +265,7 @@ void SlidingIndexWindow::step(const Index* sub_index, bool remove_oldest) {
sizesi[n_slice - 1] = ils->ids[i].size();
}
} else if (ils2) {
for (int i = 0; i < nlist; i++) {
for (size_t i = 0; i < nlist; i++) {
index_ivf->ntotal += ils2->ids[i].size();
shift_and_add(ils->ids[i], 0, ils2->ids[i]);
shift_and_add(ils->codes[i], 0, ils2->codes[i]);
@@ -273,7 +273,7 @@ void SlidingIndexWindow::step(const Index* sub_index, bool remove_oldest) {
}
n_slice++;
} else if (remove_oldest) {
for (int i = 0; i < nlist; i++) {
for (size_t i = 0; i < nlist; i++) {
size_t amount_to_remove = sizes[i][0];
index_ivf->ntotal -= amount_to_remove;
remove_from_begin(ils->ids[i], amount_to_remove);
@@ -296,7 +296,8 @@ void SlidingIndexWindow::step(const Index* sub_index, bool remove_oldest) {
ArrayInvertedLists* get_invlist_range(const Index* index, long i0, long i1) {
const IndexIVF* ivf = extract_index_ivf(index);
FAISS_THROW_IF_NOT(0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
FAISS_THROW_IF_NOT(
0 <= i0 && i0 <= i1 && static_cast<size_t>(i1) <= ivf->nlist);
const InvertedLists* src = ivf->invlists;
@@ -319,12 +320,14 @@ void set_invlist_range(
ArrayInvertedLists* src) {
IndexIVF* ivf = extract_index_ivf(index);
FAISS_THROW_IF_NOT(0 <= i0 && i0 <= i1 && i1 <= ivf->nlist);
FAISS_THROW_IF_NOT(
0 <= i0 && i0 <= i1 && static_cast<size_t>(i1) <= ivf->nlist);
ArrayInvertedLists* dst = dynamic_cast<ArrayInvertedLists*>(ivf->invlists);
FAISS_THROW_IF_NOT_MSG(dst, "only ArrayInvertedLists supported");
FAISS_THROW_IF_NOT(
src->nlist == i1 - i0 && dst->code_size == src->code_size);
src->nlist == static_cast<size_t>(i1 - i0) &&
dst->code_size == src->code_size);
size_t ntotal = index->ntotal;
for (long i = i0; i < i1; i++) {
@@ -342,7 +345,7 @@ static size_t count_ndis(
const idx_t* Iq) {
size_t nb_dis = 0;
const InvertedLists* il = index_ivf->invlists;
for (idx_t i = 0; i < n_list_scan; i++) {
for (size_t i = 0; i < n_list_scan; i++) {
if (Iq[i] >= 0) {
nb_dis += il->list_size(Iq[i]);
}
@@ -457,7 +460,7 @@ void range_search_with_parameters(
IndexIVFResidualQuantizer* ivf_residual_from_quantizer(
const ResidualQuantizer& rq,
int nlevel) {
FAISS_THROW_IF_NOT(nlevel > 0 && nlevel + 1 < rq.M);
FAISS_THROW_IF_NOT(nlevel > 0 && static_cast<size_t>(nlevel + 1) < rq.M);
std::vector<size_t> nbits(nlevel);
std::copy(rq.nbits.begin(), rq.nbits.begin() + nlevel, nbits.begin());
@@ -477,7 +480,7 @@ IndexIVFResidualQuantizer* ivf_residual_from_quantizer(
// build a IVFResidualQuantizer from that
std::vector<size_t> nbits_refined;
for (int i = nlevel; i < rq.M; i++) {
for (size_t i = nlevel; i < rq.M; i++) {
nbits_refined.push_back(rq.nbits[i]);
}
std::unique_ptr<IndexIVFResidualQuantizer> index(
@@ -522,7 +525,7 @@ void ivf_residual_add_from_flat_codes(
int rank = omp_get_thread_num();
#pragma omp for
for (idx_t i = 0; i < nb; i++) {
for (idx_t i = 0; i < static_cast<idx_t>(nb); i++) {
const uint8_t* code = &raw_codes[i * code_size];
BitstringReader rd(code, code_size);
idx_t list_no = rd.read(rcq->rq.tot_bits);
@@ -531,7 +534,7 @@ void ivf_residual_add_from_flat_codes(
rank) { // each thread takes care of 1/nt of the invlists
// copy AQ indexes one by one
BitstringWriter wr(tmp_code.data(), tmp_code.size());
for (int j = 0; j < rq.M; j++) {
for (size_t j = 0; j < rq.M; j++) {
int nbit = rq.nbits[j];
wr.write(rd.read(nbit), nbit);
}
@@ -598,7 +601,10 @@ void handle_ivf(
sharded_centroids[i].data());
}
char fname[256];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
snprintf(fname, 256, filename_template.c_str(), i);
#pragma GCC diagnostic pop
faiss::write_index(sharded_index, fname);
delete sharded_index;
}
@@ -649,7 +655,10 @@ void handle_binary_ivf(
sharded_centroids[i].data());
}
char fname[256];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
snprintf(fname, 256, filename_template.c_str(), i);
#pragma GCC diagnostic pop
faiss::write_index_binary(sharded_index, fname);
delete sharded_index;
}
+6 -6
View File
@@ -16,7 +16,7 @@ namespace {
// IndexBinary needs to update the code_size when d is set...
void sync_d(Index* index) {}
void sync_d(Index* /*index*/) {}
void sync_d(IndexBinary* index) {
FAISS_THROW_IF_NOT(index->d % 8 == 0);
@@ -30,14 +30,14 @@ IndexReplicasTemplate<IndexT>::IndexReplicasTemplate(bool threaded)
: ThreadedIndex<IndexT>(threaded) {}
template <typename IndexT>
IndexReplicasTemplate<IndexT>::IndexReplicasTemplate(idx_t d, bool threaded)
: ThreadedIndex<IndexT>(d, threaded) {
IndexReplicasTemplate<IndexT>::IndexReplicasTemplate(idx_t d_in, bool threaded)
: ThreadedIndex<IndexT>(static_cast<int>(d_in), threaded) {
sync_d(this);
}
template <typename IndexT>
IndexReplicasTemplate<IndexT>::IndexReplicasTemplate(int d, bool threaded)
: ThreadedIndex<IndexT>(d, threaded) {
IndexReplicasTemplate<IndexT>::IndexReplicasTemplate(int d_in, bool threaded)
: ThreadedIndex<IndexT>(d_in, threaded) {
sync_d(this);
}
@@ -71,7 +71,7 @@ void IndexReplicasTemplate<IndexT>::onAfterAddIndex(IndexT* index) {
}
template <typename IndexT>
void IndexReplicasTemplate<IndexT>::onAfterRemoveIndex(IndexT* index) {
void IndexReplicasTemplate<IndexT>::onAfterRemoveIndex(IndexT* /*index*/) {
syncWithSubIndexes();
}
+14 -9
View File
@@ -162,9 +162,14 @@ void sa_decode_impl(
// allocate tmp buffers
std::vector<uint8_t> tmp(
(chunk_size < n_input ? chunk_size : n_input) * old_code_size);
(chunk_size < static_cast<size_t>(n_input)
? chunk_size
: static_cast<size_t>(n_input)) *
old_code_size);
std::vector<StorageMinMaxFP16> minmax(
(chunk_size < n_input ? chunk_size : n_input));
(chunk_size < static_cast<size_t>(n_input)
? chunk_size
: static_cast<size_t>(n_input)));
// all the elements to process
size_t n_left = n_input;
@@ -339,9 +344,9 @@ int rowwise_minmax_sa_decode_bs = 16384;
* IndexRowwiseMinMaxBase implementation
********************************************************/
IndexRowwiseMinMaxBase::IndexRowwiseMinMaxBase(Index* index)
: Index(index->d, index->metric_type),
index{index},
IndexRowwiseMinMaxBase::IndexRowwiseMinMaxBase(Index* index_in)
: Index(index_in->d, index_in->metric_type),
index{index_in},
own_fields{false} {}
IndexRowwiseMinMaxBase::IndexRowwiseMinMaxBase()
@@ -376,8 +381,8 @@ void IndexRowwiseMinMaxBase::reset() {
* IndexRowwiseMinMaxFP16 implementation
********************************************************/
IndexRowwiseMinMaxFP16::IndexRowwiseMinMaxFP16(Index* index)
: IndexRowwiseMinMaxBase(index) {}
IndexRowwiseMinMaxFP16::IndexRowwiseMinMaxFP16(Index* index_in)
: IndexRowwiseMinMaxBase(index_in) {}
IndexRowwiseMinMaxFP16::IndexRowwiseMinMaxFP16() : IndexRowwiseMinMaxBase() {}
@@ -411,8 +416,8 @@ void IndexRowwiseMinMaxFP16::train_inplace(idx_t n, float* x) {
* IndexRowwiseMinMax implementation
********************************************************/
IndexRowwiseMinMax::IndexRowwiseMinMax(Index* index)
: IndexRowwiseMinMaxBase(index) {}
IndexRowwiseMinMax::IndexRowwiseMinMax(Index* index_in)
: IndexRowwiseMinMaxBase(index_in) {}
IndexRowwiseMinMax::IndexRowwiseMinMax() : IndexRowwiseMinMaxBase() {}
+13 -12
View File
@@ -26,10 +26,10 @@ namespace faiss {
********************************************************************/
IndexScalarQuantizer::IndexScalarQuantizer(
int d,
int d_in,
ScalarQuantizer::QuantizerType qtype,
MetricType metric)
: IndexFlatCodes(0, d, metric), sq(d, qtype) {
: IndexFlatCodes(0, d_in, metric), sq(d_in, qtype) {
is_trained = qtype == ScalarQuantizer::QT_fp16 ||
qtype == ScalarQuantizer::QT_8bit_direct ||
qtype == ScalarQuantizer::QT_bf16 ||
@@ -117,16 +117,17 @@ void IndexScalarQuantizer::sa_decode(idx_t n, const uint8_t* bytes, float* x)
********************************************************************/
IndexIVFScalarQuantizer::IndexIVFScalarQuantizer(
Index* quantizer,
size_t d,
size_t nlist,
Index* quantizer_,
size_t d_,
size_t nlist_,
ScalarQuantizer::QuantizerType qtype,
MetricType metric,
bool by_residual,
bool own_invlists)
: IndexIVF(quantizer, d, nlist, 0, metric, own_invlists), sq(d, qtype) {
bool by_residual_,
bool own_invlists_)
: IndexIVF(quantizer_, d_, nlist_, 0, metric, own_invlists_),
sq(d_, qtype) {
code_size = sq.code_size;
this->by_residual = by_residual;
this->by_residual = by_residual_;
if (invlists) {
// was not known at construction time
invlists->code_size = code_size;
@@ -141,7 +142,7 @@ IndexIVFScalarQuantizer::IndexIVFScalarQuantizer() : IndexIVF() {
void IndexIVFScalarQuantizer::train_encoder(
idx_t n,
const float* x,
const idx_t* assign) {
const idx_t* /*assign*/) {
sq.train(n, x);
}
@@ -208,7 +209,7 @@ void IndexIVFScalarQuantizer::sa_decode(idx_t n, const uint8_t* codes, float* x)
squant->decode_vector(code + coarse_size, xi);
if (by_residual) {
quantizer->reconstruct(list_no, residual.data());
for (size_t j = 0; j < d; j++) {
for (size_t j = 0; j < static_cast<size_t>(d); j++) {
xi[j] += residual[j];
}
}
@@ -236,7 +237,7 @@ void IndexIVFScalarQuantizer::add_core(
int rank = omp_get_thread_num();
// each thread takes care of a subset of lists
for (size_t i = 0; i < n; i++) {
for (idx_t i = 0; i < n; i++) {
int64_t list_no = coarse_idx[i];
if (list_no >= 0 && list_no % nt == rank) {
int64_t id = xids ? xids[i] : ntotal + i;
+9 -8
View File
@@ -46,27 +46,28 @@ void translate_labels(int64_t n, idx_t* labels, int64_t translation) {
template <typename IndexT>
IndexShardsTemplate<IndexT>::IndexShardsTemplate(
idx_t d,
idx_t d_,
bool threaded,
bool successive_ids)
: ThreadedIndex<IndexT>(d, threaded), successive_ids(successive_ids) {
bool successive_ids_)
: ThreadedIndex<IndexT>(static_cast<int>(d_), threaded),
successive_ids(successive_ids_) {
sync_d(this);
}
template <typename IndexT>
IndexShardsTemplate<IndexT>::IndexShardsTemplate(
int d,
int d_,
bool threaded,
bool successive_ids)
: ThreadedIndex<IndexT>(d, threaded), successive_ids(successive_ids) {
bool successive_ids_)
: ThreadedIndex<IndexT>(d_, threaded), successive_ids(successive_ids_) {
sync_d(this);
}
template <typename IndexT>
IndexShardsTemplate<IndexT>::IndexShardsTemplate(
bool threaded,
bool successive_ids)
: ThreadedIndex<IndexT>(threaded), successive_ids(successive_ids) {
bool successive_ids_)
: ThreadedIndex<IndexT>(threaded), successive_ids(successive_ids_) {
sync_d(this);
}
+20 -14
View File
@@ -43,13 +43,14 @@ void translate_labels(int64_t n, idx_t* labels, int64_t translation) {
************************************************************/
IndexShardsIVF::IndexShardsIVF(
Index* quantizer,
size_t nlist,
Index* quantizer_,
size_t nlist_,
bool threaded,
bool successive_ids)
: IndexShardsTemplate<Index>(quantizer->d, threaded, successive_ids),
Level1Quantizer(quantizer, nlist) {
is_trained = quantizer->is_trained && quantizer->ntotal == nlist;
bool successive_ids_)
: IndexShardsTemplate<Index>(quantizer_->d, threaded, successive_ids_),
Level1Quantizer(quantizer_, nlist_) {
is_trained = quantizer_->is_trained &&
quantizer_->ntotal == static_cast<idx_t>(nlist_);
}
void IndexShardsIVF::addIndex(Index* index) {
@@ -73,11 +74,11 @@ void IndexShardsIVF::train(idx_t n, const component_t* x) {
for (size_t i = 0; i < indices_.size(); i++) {
Index* index = indices_[i].first;
auto index_ivf = dynamic_cast<IndexIVFInterface*>(index);
Index* quantizer = index_ivf->quantizer;
if (!quantizer->is_trained) {
quantizer->train(nlist, centroids.data());
Index* sub_quantizer = index_ivf->quantizer;
if (!sub_quantizer->is_trained) {
sub_quantizer->train(nlist, centroids.data());
}
quantizer->add(nlist, centroids.data());
sub_quantizer->add(nlist, centroids.data());
// finish training
index->train(n, x);
}
@@ -132,9 +133,9 @@ void IndexShardsIVF::add_with_ids(
}
ids = aids.data();
}
idx_t d = this->d;
idx_t cur_d = this->d;
auto fn = [n, ids, x, nshard, d, Iq](int no, Index* index) {
auto fn = [n, ids, x, nshard, cur_d, Iq](int no, Index* index) {
idx_t i0 = (idx_t)no * n / nshard;
idx_t i1 = ((idx_t)no + 1) * n / nshard;
auto index_ivf = dynamic_cast<IndexIVF*>(index);
@@ -144,7 +145,10 @@ void IndexShardsIVF::add_with_ids(
}
index_ivf->add_core(
i1 - i0, x + i0 * d, ids ? ids + i0 : nullptr, Iq.data() + i0);
i1 - i0,
x + i0 * cur_d,
ids ? ids + i0 : nullptr,
Iq.data() + i0);
if (index->verbose) {
printf("end add shard %d on %" PRId64 " points\n", no, i1 - i0);
@@ -199,7 +203,9 @@ void IndexShardsIVF::search(
auto index = dynamic_cast<const IndexIVFInterface*>(indexIn);
FAISS_THROW_IF_NOT_MSG(index->nprobe == nprobe, "inconsistent nprobe");
FAISS_THROW_IF_NOT_MSG(
index->nprobe == static_cast<size_t>(nprobe),
"inconsistent nprobe");
index->search_preassigned(
n,
+17 -16
View File
@@ -27,8 +27,8 @@ namespace faiss {
* IndexSplitVectors implementation
*******************************************************/
IndexSplitVectors::IndexSplitVectors(idx_t d, bool threaded)
: Index(d), own_fields(false), threaded(threaded), sum_d(0) {}
IndexSplitVectors::IndexSplitVectors(idx_t d_in, bool threaded_in)
: Index(d_in), own_fields(false), threaded(threaded_in), sum_d(0) {}
void IndexSplitVectors::add_sub_index(Index* index) {
sub_indexes.push_back(index);
@@ -44,7 +44,7 @@ void IndexSplitVectors::sync_with_sub_indexes() {
metric_type = index0->metric_type;
is_trained = index0->is_trained;
ntotal = index0->ntotal;
for (int i = 1; i < sub_indexes.size(); i++) {
for (size_t i = 1; i < sub_indexes.size(); i++) {
Index* index = sub_indexes[i];
FAISS_THROW_IF_NOT(metric_type == index->metric_type);
FAISS_THROW_IF_NOT(ntotal == index->ntotal);
@@ -88,7 +88,7 @@ void IndexSplitVectors::search(
n);
}
const Index* sub_index = index->sub_indexes[no];
int64_t sub_d = sub_index->d, d = index->d;
int64_t sub_d = sub_index->d;
idx_t ofs = 0;
for (int i = 0; i < no; i++) {
ofs += index->sub_indexes[i]->d;
@@ -107,17 +107,18 @@ void IndexSplitVectors::search(
};
if (!threaded) {
for (int i = 0; i < nshard; i++) {
query_func(i);
for (int64_t i = 0; i < nshard; i++) {
query_func(static_cast<int>(i));
}
} else {
std::vector<std::unique_ptr<WorkerThread>> threads;
std::vector<std::future<bool>> v;
for (int i = 0; i < nshard; i++) {
for (int64_t i = 0; i < nshard; i++) {
threads.emplace_back(new WorkerThread());
WorkerThread* wt = threads.back().get();
v.emplace_back(wt->add([i, query_func]() { query_func(i); }));
v.emplace_back(wt->add(
[i, query_func]() { query_func(static_cast<int>(i)); }));
}
// Blocking wait for completion
@@ -155,7 +156,7 @@ void IndexSplitVectors::reset() {
IndexSplitVectors::~IndexSplitVectors() {
if (own_fields) {
for (int s = 0; s < sub_indexes.size(); s++) {
for (size_t s = 0; s < sub_indexes.size(); s++) {
delete sub_indexes[s];
}
}
@@ -166,12 +167,12 @@ IndexSplitVectors::~IndexSplitVectors() {
*/
IndexRandom::IndexRandom(
idx_t d,
idx_t ntotal,
int64_t seed,
MetricType metric_type)
: Index(d, metric_type), seed(seed) {
this->ntotal = ntotal;
idx_t d_in,
idx_t ntotal_in,
int64_t seed_in,
MetricType metric_type_in)
: Index(d_in, metric_type_in), seed(seed_in) {
this->ntotal = ntotal_in;
is_trained = true;
}
@@ -234,7 +235,7 @@ void IndexRandom::search(
void IndexRandom::reconstruct(idx_t key, float* recons) const {
RandomGenerator rng(seed + 123332 + key);
for (size_t i = 0; i < d; i++) {
for (int i = 0; i < d; i++) {
recons[i] = rng.rand_float();
}
}
+70 -64
View File
@@ -158,9 +158,9 @@ void VectorTransform::check_identical(const VectorTransform& other) const {
*********************************************/
/// both d_in > d_out and d_out < d_in are supported
LinearTransform::LinearTransform(int d_in, int d_out, bool have_bias)
: VectorTransform(d_in, d_out),
have_bias(have_bias),
LinearTransform::LinearTransform(int din, int dout, bool have_bias_in)
: VectorTransform(din, dout),
have_bias(have_bias_in),
is_orthonormal(false),
verbose(false) {
is_trained = false; // will be trained when A and b are initialized
@@ -171,9 +171,10 @@ void LinearTransform::apply_noalloc(idx_t n, const float* x, float* xt) const {
float c_factor;
if (have_bias) {
FAISS_THROW_IF_NOT_MSG(b.size() == d_out, "Bias not initialized");
FAISS_THROW_IF_NOT_MSG(
b.size() == static_cast<size_t>(d_out), "Bias not initialized");
float* xi = xt;
for (int i = 0; i < n; i++)
for (idx_t i = 0; i < n; i++)
for (int j = 0; j < d_out; j++)
*xi++ = b[j];
c_factor = 1.0;
@@ -182,7 +183,8 @@ void LinearTransform::apply_noalloc(idx_t n, const float* x, float* xt) const {
}
FAISS_THROW_IF_NOT_MSG(
A.size() == d_out * d_in, "Transformation matrix not initialized");
A.size() == static_cast<size_t>(d_out) * d_in,
"Transformation matrix not initialized");
float one = 1;
FINTEGER nbiti = d_out, ni = n, di = d_in;
@@ -249,7 +251,7 @@ void LinearTransform::set_is_orthonormal() {
}
double eps = 4e-5;
FAISS_ASSERT(A.size() >= d_out * d_in);
FAISS_ASSERT(A.size() >= static_cast<size_t>(d_out) * d_in);
{
std::vector<float> ATA(d_out * d_out);
FINTEGER dii = d_in, doi = d_out;
@@ -301,7 +303,7 @@ void LinearTransform::print_if_verbose(
if (!verbose)
return;
printf("matrix %s: %d*%d [\n", name, n, d);
FAISS_THROW_IF_NOT(mat.size() >= n * d);
FAISS_THROW_IF_NOT(mat.size() >= static_cast<size_t>(n) * d);
for (int i = 0; i < n; i++) {
for (int j = 0; j < d; j++) {
printf("%10.5g ", mat[i * d + j]);
@@ -477,13 +479,13 @@ void HadamardRotation::check_identical(const VectorTransform& other) const {
*********************************************/
PCAMatrix::PCAMatrix(
int d_in,
int d_out,
float eigen_power,
bool random_rotation)
: LinearTransform(d_in, d_out, true),
eigen_power(eigen_power),
random_rotation(random_rotation) {
int din,
int dout,
float eigen_power_in,
bool random_rotation_in)
: LinearTransform(din, dout, true),
eigen_power(eigen_power_in),
random_rotation(random_rotation_in) {
is_trained = false;
max_points_per_d = 1000;
balanced_bins = 0;
@@ -534,15 +536,16 @@ void eig(size_t d_in, double* cov, double* eigenvalues, int verbose) {
if (verbose && d_in <= 10) {
printf("info=%ld new eigvals=[", long(info));
for (int j = 0; j < d_in; j++)
for (size_t j = 0; j < d_in; j++)
printf("%g ", eigenvalues[j]);
printf("]\n");
double* ci = cov;
printf("eigenvecs=\n");
for (int i = 0; i < d_in; i++) {
for (int j = 0; j < d_in; j++)
for (size_t i = 0; i < d_in; i++) {
for (size_t j = 0; j < d_in; j++) {
printf("%10.4g ", *ci++);
}
printf("\n");
}
}
@@ -550,11 +553,11 @@ void eig(size_t d_in, double* cov, double* eigenvalues, int verbose) {
// revert order of eigenvectors & values
for (int i = 0; i < d_in / 2; i++) {
for (size_t i = 0; i < d_in / 2; i++) {
std::swap(eigenvalues[i], eigenvalues[d_in - 1 - i]);
double* v1 = cov + i * d_in;
double* v2 = cov + (d_in - 1 - i) * d_in;
for (int j = 0; j < d_in; j++)
for (size_t j = 0; j < d_in; j++)
std::swap(v1[j], v2[j]);
}
}
@@ -571,7 +574,7 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
mean.resize(d_in, 0.0);
if (have_bias) { // we may want to skip the bias
const float* xi = x;
for (int i = 0; i < n; i++) {
for (idx_t i = 0; i < n; i++) {
for (int j = 0; j < d_in; j++)
mean[j] += *xi++;
}
@@ -632,15 +635,17 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
PCAMat[i] = covd[i];
eigenvalues.resize(d_in);
for (size_t i = 0; i < d_in; i++)
for (int i = 0; i < d_in; i++)
eigenvalues[i] = eigenvaluesd[i];
} else {
std::vector<float> xc(n * d_in);
for (size_t i = 0; i < n; i++)
for (size_t j = 0; j < d_in; j++)
for (idx_t i = 0; i < n; i++) {
for (int j = 0; j < d_in; j++) {
xc[i * d_in + j] = x[i * d_in + j] - mean[j];
}
}
// compute Gram matrix
std::vector<float> gram(n * n);
@@ -662,9 +667,10 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
if (verbose && d_in <= 10) {
float* ci = gram.data();
printf("gram=\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
for (idx_t i = 0; i < n; i++) {
for (idx_t j = 0; j < n; j++) {
printf("%10g ", *ci++);
}
printf("\n");
}
}
@@ -686,7 +692,7 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
eigenvalues.resize(d_in);
// fill in only the n first ones
for (size_t i = 0; i < n; i++)
for (idx_t i = 0; i < n; i++)
eigenvalues[i] = eigenvaluesd[i];
{ // compute PCAMat = x' * v
@@ -711,7 +717,7 @@ void PCAMatrix::train(idx_t n, const float* x_in) {
if (verbose && d_in <= 10) {
float* ci = PCAMat.data();
printf("PCAMat=\n");
for (int i = 0; i < n; i++) {
for (idx_t i = 0; i < n; i++) {
for (int j = 0; j < d_in; j++)
printf("%10g ", *ci++);
printf("\n");
@@ -735,7 +741,7 @@ void PCAMatrix::copy_from(const PCAMatrix& other) {
void PCAMatrix::prepare_Ab() {
FAISS_THROW_IF_NOT_FMT(
d_out * d_in <= PCAMat.size(),
static_cast<size_t>(d_out) * d_in <= PCAMat.size(),
"PCA matrix cannot output %d dimensions from %d ",
d_out,
d_in);
@@ -989,13 +995,13 @@ void ITQMatrix::train(idx_t n, const float* xf) {
is_trained = true;
}
ITQTransform::ITQTransform(int d_in, int d_out, bool do_pca)
: VectorTransform(d_in, d_out),
do_pca(do_pca),
itq(d_out),
pca_then_itq(d_in, d_out, false) {
if (!do_pca) {
FAISS_THROW_IF_NOT(d_in == d_out);
ITQTransform::ITQTransform(int din, int dout, bool do_pca_in)
: VectorTransform(din, dout),
do_pca(do_pca_in),
itq(dout),
pca_then_itq(din, dout, false) {
if (!do_pca_in) {
FAISS_THROW_IF_NOT(din == dout);
}
max_train_per_dim = 10;
is_trained = false;
@@ -1103,8 +1109,8 @@ void ITQTransform::check_identical(const VectorTransform& other_in) const {
* OPQMatrix
*********************************************/
OPQMatrix::OPQMatrix(int d, int M, int d2)
: LinearTransform(d, d2 == -1 ? d : d2, false), M(M) {
OPQMatrix::OPQMatrix(int d, int M_in, int d2)
: LinearTransform(d, d2 == -1 ? d : d2, false), M(M_in) {
is_trained = false;
// OPQ is quite expensive to train, so set this right.
max_train_points = 256 * 256;
@@ -1150,15 +1156,15 @@ void OPQMatrix::train(idx_t n, const float* x_in) {
{
std::vector<float> sum(d);
const float* xi = x;
for (size_t i = 0; i < n; i++) {
for (idx_t i = 0; i < n; i++) {
for (int j = 0; j < d_in; j++)
sum[j] += *xi++;
}
for (int i = 0; i < d; i++)
for (size_t i = 0; i < d; i++)
sum[i] /= n;
float* yi = xtrain.data();
xi = x;
for (size_t i = 0; i < n; i++) {
for (idx_t i = 0; i < n; i++) {
for (int j = 0; j < d_in; j++)
*yi++ = *xi++ - sum[j];
yi += d - d_in;
@@ -1313,7 +1319,7 @@ void OPQMatrix::train(idx_t n, const float* x_in) {
}
// revert A matrix
if (d > d_in) {
if (d > static_cast<size_t>(d_in)) {
for (long i = 0; i < d_out; i++)
memmove(&A[i * d_in], &A[i * d], sizeof(A[0]) * d_in);
A.resize(d_in * d_out);
@@ -1327,8 +1333,8 @@ void OPQMatrix::train(idx_t n, const float* x_in) {
* NormalizationTransform
*********************************************/
NormalizationTransform::NormalizationTransform(int d, float norm)
: VectorTransform(d, d), norm(norm) {}
NormalizationTransform::NormalizationTransform(int d, float norm_in)
: VectorTransform(d, d), norm(norm_in) {}
NormalizationTransform::NormalizationTransform()
: VectorTransform(-1, -1), norm(-1) {}
@@ -1370,12 +1376,12 @@ void CenteringTransform::train(idx_t n, const float* x) {
FAISS_THROW_IF_NOT_MSG(n > 0, "need at least one training vector");
mean.resize(d_in, 0);
for (idx_t i = 0; i < n; i++) {
for (size_t j = 0; j < d_in; j++) {
for (int j = 0; j < d_in; j++) {
mean[j] += *x++;
}
}
for (size_t j = 0; j < d_in; j++) {
for (int j = 0; j < d_in; j++) {
mean[j] /= n;
}
is_trained = true;
@@ -1386,7 +1392,7 @@ void CenteringTransform::apply_noalloc(idx_t n, const float* x, float* xt)
FAISS_THROW_IF_NOT(is_trained);
for (idx_t i = 0; i < n; i++) {
for (size_t j = 0; j < d_in; j++) {
for (int j = 0; j < d_in; j++) {
*xt++ = *x++ - mean[j];
}
}
@@ -1397,7 +1403,7 @@ void CenteringTransform::reverse_transform(idx_t n, const float* xt, float* x)
FAISS_THROW_IF_NOT(is_trained);
for (idx_t i = 0; i < n; i++) {
for (size_t j = 0; j < d_in; j++) {
for (int j = 0; j < d_in; j++) {
*x++ = *xt++ + mean[j];
}
}
@@ -1416,36 +1422,36 @@ void CenteringTransform::check_identical(
*********************************************/
RemapDimensionsTransform::RemapDimensionsTransform(
int d_in,
int d_out,
int din,
int dout,
const int* map_in)
: VectorTransform(d_in, d_out) {
map.resize(d_out);
for (int i = 0; i < d_out; i++) {
: VectorTransform(din, dout) {
map.resize(dout);
for (int i = 0; i < dout; i++) {
map[i] = map_in[i];
FAISS_THROW_IF_NOT(map[i] == -1 || (map[i] >= 0 && map[i] < d_in));
FAISS_THROW_IF_NOT(map[i] == -1 || (map[i] >= 0 && map[i] < din));
}
}
RemapDimensionsTransform::RemapDimensionsTransform(
int d_in,
int d_out,
int din,
int dout,
bool uniform)
: VectorTransform(d_in, d_out) {
map.resize(d_out, -1);
: VectorTransform(din, dout) {
map.resize(dout, -1);
if (uniform) {
if (d_in < d_out) {
for (int i = 0; i < d_in; i++) {
map[i * d_out / d_in] = i;
if (din < dout) {
for (int i = 0; i < din; i++) {
map[i * dout / din] = i;
}
} else {
for (int i = 0; i < d_out; i++) {
map[i] = i * d_in / d_out;
for (int i = 0; i < dout; i++) {
map[i] = i * din / dout;
}
}
} else {
for (int i = 0; i < d_in && i < d_out; i++)
for (int i = 0; i < din && i < dout; i++)
map[i] = i;
}
}
+16 -16
View File
@@ -26,8 +26,8 @@ struct VectorTransform {
int d_in; ///! input dimension
int d_out; ///! output dimension
explicit VectorTransform(int d_in = 0, int d_out = 0)
: d_in(d_in), d_out(d_out), is_trained(true) {}
explicit VectorTransform(int d_in_val = 0, int d_out_val = 0)
: d_in(d_in_val), d_out(d_out_val), is_trained(true) {}
/// set if the VectorTransform does not require training, or if
/// training is done already
@@ -82,9 +82,9 @@ struct LinearTransform : VectorTransform {
/// both d_in > d_out and d_out < d_in are supported
explicit LinearTransform(
int d_in = 0,
int d_out = 0,
bool have_bias = false);
int din = 0,
int dout = 0,
bool have_bias_in = false);
/// same as apply, but result is pre-allocated
void apply_noalloc(idx_t n, const float* x, float* xt) const override;
@@ -114,8 +114,8 @@ struct LinearTransform : VectorTransform {
/// Randomly rotate a set of vectors
struct RandomRotationMatrix : LinearTransform {
/// both d_in > d_out and d_out < d_in are supported
RandomRotationMatrix(int d_in, int d_out)
: LinearTransform(d_in, d_out, false) {}
RandomRotationMatrix(int d_in_val, int d_out_val)
: LinearTransform(d_in_val, d_out_val, false) {}
/// must be called before the transform is used
void init(int seed);
@@ -183,10 +183,10 @@ struct PCAMatrix : LinearTransform {
// the final matrix is computed after random rotation and/or whitening
explicit PCAMatrix(
int d_in = 0,
int d_out = 0,
float eigen_power = 0,
bool random_rotation = false);
int din = 0,
int dout = 0,
float eigen_power_in = 0,
bool random_rotation_in = false);
/// train on n vectors. If n < d_in then the eigenvector matrix
/// will be completed with 0s
@@ -233,7 +233,7 @@ struct ITQTransform : VectorTransform {
// concatenation of PCA + ITQ transformation
LinearTransform pca_then_itq;
explicit ITQTransform(int d_in = 0, int d_out = 0, bool do_pca = false);
explicit ITQTransform(int din = 0, int dout = 0, bool do_pca_in = false);
void train(idx_t n, const float* x) override;
@@ -267,7 +267,7 @@ struct OPQMatrix : LinearTransform {
ProductQuantizer* pq = nullptr;
/// if d2 != -1, output vectors of this dimension
explicit OPQMatrix(int d = 0, int M = 1, int d2 = -1);
explicit OPQMatrix(int d = 0, int M_in = 1, int d2 = -1);
void train(idx_t n, const float* x) override;
};
@@ -280,12 +280,12 @@ struct RemapDimensionsTransform : VectorTransform {
/// -1 -> set output to 0
std::vector<int> map;
RemapDimensionsTransform(int d_in, int d_out, const int* map);
RemapDimensionsTransform(int din, int dout, const int* map);
/// remap input to output, skipping or inserting dimensions as needed
/// if uniform: distribute dimensions uniformly
/// otherwise just take the d_out first ones.
RemapDimensionsTransform(int d_in, int d_out, bool uniform = true);
RemapDimensionsTransform(int din, int dout, bool uniform = true);
void apply_noalloc(idx_t n, const float* x, float* xt) const override;
@@ -301,7 +301,7 @@ struct RemapDimensionsTransform : VectorTransform {
struct NormalizationTransform : VectorTransform {
float norm;
explicit NormalizationTransform(int d, float norm = 2.0);
explicit NormalizationTransform(int d, float norm_in = 2.0);
NormalizationTransform();
void apply_noalloc(idx_t n, const float* x, float* xt) const override;
+46 -46
View File
@@ -173,7 +173,7 @@ IndexRowwiseMinMaxBase* clone_IndexRowwiseMinMax(
}
}
#define TRYCAST(classname) classname* res = dynamic_cast<classname*>(index)
#define TRYCAST(classname, var) auto* var = dynamic_cast<classname*>(index)
void reset_AdditiveQuantizerIndex(Index* index) {
auto clone_ProductQuantizers =
@@ -182,50 +182,50 @@ void reset_AdditiveQuantizerIndex(Index* index) {
q = dynamic_cast<AdditiveQuantizer*>(clone_Quantizer(q));
}
};
if (TRYCAST(IndexIVFLocalSearchQuantizerFastScan)) {
res->aq = &res->lsq;
} else if (TRYCAST(IndexIVFResidualQuantizerFastScan)) {
res->aq = &res->rq;
} else if (TRYCAST(IndexIVFProductLocalSearchQuantizerFastScan)) {
res->aq = &res->plsq;
clone_ProductQuantizers(res->plsq.quantizers);
} else if (TRYCAST(IndexIVFProductResidualQuantizerFastScan)) {
res->aq = &res->prq;
clone_ProductQuantizers(res->prq.quantizers);
} else if (TRYCAST(IndexIVFLocalSearchQuantizer)) {
res->aq = &res->lsq;
} else if (TRYCAST(IndexIVFResidualQuantizer)) {
res->aq = &res->rq;
} else if (TRYCAST(IndexIVFProductLocalSearchQuantizer)) {
res->aq = &res->plsq;
clone_ProductQuantizers(res->plsq.quantizers);
} else if (TRYCAST(IndexIVFProductResidualQuantizer)) {
res->aq = &res->prq;
clone_ProductQuantizers(res->prq.quantizers);
} else if (TRYCAST(IndexLocalSearchQuantizerFastScan)) {
res->aq = &res->lsq;
} else if (TRYCAST(IndexResidualQuantizerFastScan)) {
res->aq = &res->rq;
} else if (TRYCAST(IndexProductLocalSearchQuantizerFastScan)) {
res->aq = &res->plsq;
clone_ProductQuantizers(res->plsq.quantizers);
} else if (TRYCAST(IndexProductResidualQuantizerFastScan)) {
res->aq = &res->prq;
clone_ProductQuantizers(res->prq.quantizers);
} else if (TRYCAST(IndexLocalSearchQuantizer)) {
res->aq = &res->lsq;
} else if (TRYCAST(IndexResidualQuantizer)) {
res->aq = &res->rq;
} else if (TRYCAST(IndexProductLocalSearchQuantizer)) {
res->aq = &res->plsq;
clone_ProductQuantizers(res->plsq.quantizers);
} else if (TRYCAST(IndexProductResidualQuantizer)) {
res->aq = &res->prq;
clone_ProductQuantizers(res->prq.quantizers);
} else if (TRYCAST(LocalSearchCoarseQuantizer)) {
res->aq = &res->lsq;
} else if (TRYCAST(ResidualCoarseQuantizer)) {
res->aq = &res->rq;
if (TRYCAST(IndexIVFLocalSearchQuantizerFastScan, r1)) {
r1->aq = &r1->lsq;
} else if (TRYCAST(IndexIVFResidualQuantizerFastScan, r2)) {
r2->aq = &r2->rq;
} else if (TRYCAST(IndexIVFProductLocalSearchQuantizerFastScan, r3)) {
r3->aq = &r3->plsq;
clone_ProductQuantizers(r3->plsq.quantizers);
} else if (TRYCAST(IndexIVFProductResidualQuantizerFastScan, r4)) {
r4->aq = &r4->prq;
clone_ProductQuantizers(r4->prq.quantizers);
} else if (TRYCAST(IndexIVFLocalSearchQuantizer, r5)) {
r5->aq = &r5->lsq;
} else if (TRYCAST(IndexIVFResidualQuantizer, r6)) {
r6->aq = &r6->rq;
} else if (TRYCAST(IndexIVFProductLocalSearchQuantizer, r7)) {
r7->aq = &r7->plsq;
clone_ProductQuantizers(r7->plsq.quantizers);
} else if (TRYCAST(IndexIVFProductResidualQuantizer, r8)) {
r8->aq = &r8->prq;
clone_ProductQuantizers(r8->prq.quantizers);
} else if (TRYCAST(IndexLocalSearchQuantizerFastScan, r9)) {
r9->aq = &r9->lsq;
} else if (TRYCAST(IndexResidualQuantizerFastScan, r10)) {
r10->aq = &r10->rq;
} else if (TRYCAST(IndexProductLocalSearchQuantizerFastScan, r11)) {
r11->aq = &r11->plsq;
clone_ProductQuantizers(r11->plsq.quantizers);
} else if (TRYCAST(IndexProductResidualQuantizerFastScan, r12)) {
r12->aq = &r12->prq;
clone_ProductQuantizers(r12->prq.quantizers);
} else if (TRYCAST(IndexLocalSearchQuantizer, r13)) {
r13->aq = &r13->lsq;
} else if (TRYCAST(IndexResidualQuantizer, r14)) {
r14->aq = &r14->rq;
} else if (TRYCAST(IndexProductLocalSearchQuantizer, r15)) {
r15->aq = &r15->plsq;
clone_ProductQuantizers(r15->plsq.quantizers);
} else if (TRYCAST(IndexProductResidualQuantizer, r16)) {
r16->aq = &r16->prq;
clone_ProductQuantizers(r16->prq.quantizers);
} else if (TRYCAST(LocalSearchCoarseQuantizer, r17)) {
r17->aq = &r17->lsq;
} else if (TRYCAST(ResidualCoarseQuantizer, r18)) {
r18->aq = &r18->rq;
} else {
FAISS_THROW_MSG(
"clone not supported for this type of additive quantizer index");
@@ -319,7 +319,7 @@ Index* Cloner::clone_Index(const Index* index) {
res->metric_arg = ipt->metric_arg;
res->index = clone_Index(ipt->index);
for (int i = 0; i < ipt->chain.size(); i++) {
for (size_t i = 0; i < ipt->chain.size(); i++) {
res->chain.push_back(clone_VectorTransform(ipt->chain[i]));
}
res->own_fields = true;
+9 -8
View File
@@ -45,7 +45,6 @@
#include <faiss/IndexRefine.h>
#include <faiss/IndexRowwiseMinMax.h>
#include <faiss/IndexScalarQuantizer.h>
#include <faiss/MetaIndexes.h>
#include <faiss/VectorTransform.h>
#include <faiss/IndexBinaryFlat.h>
@@ -91,10 +90,10 @@ void find_matching_parentheses(
int begin = 0) {
int st = 0;
i0 = i1 = 0;
for (int i = begin; i < s.length(); i++) {
for (size_t i = begin; i < s.length(); i++) {
if (s[i] == '(') {
if (st == 0) {
i0 = i;
i0 = static_cast<int>(i);
}
st++;
}
@@ -102,7 +101,7 @@ void find_matching_parentheses(
if (s[i] == ')') {
st--;
if (st == 0) {
i1 = i;
i1 = static_cast<int>(i);
return;
}
if (st < 0) {
@@ -206,7 +205,7 @@ std::vector<size_t> aq_parse_nbits(std::string stok) {
VectorTransform* parse_VectorTransform(const std::string& description, int d) {
std::smatch sm;
auto match = [&sm, description](std::string pattern) {
auto match = [&sm, description](const std::string& pattern) {
return re_match(description, pattern, sm);
};
if (match("PCA(W?)(R?)([0-9]+)")) {
@@ -264,7 +263,7 @@ Index* parse_coarse_quantizer(
size_t& nlist,
bool& use_2layer) {
std::smatch sm;
auto match = [&sm, description](std::string pattern) {
auto match = [&sm, description](const std::string& pattern) {
return re_match(description, pattern, sm);
};
use_2layer = false;
@@ -294,7 +293,9 @@ Index* parse_coarse_quantizer(
if (match("IVF([0-9]+[kM]?)\\(Index([0-9])\\)")) {
nlist = parse_nlist(sm[1].str());
int no = std::stoi(sm[2].str());
FAISS_ASSERT(no >= 0 && no < parenthesis_indexes.size());
FAISS_ASSERT(
no >= 0 &&
static_cast<size_t>(no) < parenthesis_indexes.size());
return parenthesis_indexes[no].release();
}
@@ -964,7 +965,7 @@ std::unique_ptr<Index> index_factory_sub(
int i0, i1;
find_matching_parentheses(description, i0, i1, begin);
std::string sub_description = description.substr(i0 + 1, i1 - i0 - 1);
int no = parenthesis_indexes.size();
size_t no = parenthesis_indexes.size();
parenthesis_indexes.push_back(
index_factory_sub(d, sub_description, metric));
description = description.substr(0, i0 + 1) + "Index" +