Skip to content

Commit 72fa39d

Browse files
maxwbuckleyclaude
andcommitted
fix: resolve compiler warnings (graph-indexes)
Resolve -Wall -Wextra -Wshadow warnings in graph index files: - Rename constructor/function parameters that shadow member variables - Add explicit casts for sign comparison warnings - Add [[maybe_unused]] attributes where appropriate - Fix unused parameter warnings Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a3a7627 commit 72fa39d

12 files changed

Lines changed: 182 additions & 152 deletions

faiss/IndexBinaryHNSW.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void hnsw_add_vertices(
6262
}
6363

6464
std::vector<omp_lock_t> locks(ntotal);
65-
for (int i = 0; i < ntotal; i++) {
65+
for (size_t i = 0; i < ntotal; i++) {
6666
omp_init_lock(&locks[i]);
6767
}
6868

@@ -73,23 +73,23 @@ void hnsw_add_vertices(
7373
{ // make buckets with vectors of the same level
7474

7575
// build histogram
76-
for (int i = 0; i < n; i++) {
76+
for (size_t i = 0; i < n; i++) {
7777
HNSW::storage_idx_t pt_id = i + n0;
7878
int pt_level = hnsw.levels[pt_id] - 1;
79-
while (pt_level >= hist.size()) {
79+
while (pt_level >= static_cast<int>(hist.size())) {
8080
hist.push_back(0);
8181
}
8282
hist[pt_level]++;
8383
}
8484

8585
// accumulate
8686
std::vector<int> offsets(hist.size() + 1, 0);
87-
for (int i = 0; i < hist.size() - 1; i++) {
87+
for (size_t i = 0; i < hist.size() - 1; i++) {
8888
offsets[i + 1] = offsets[i] + hist[i];
8989
}
9090

9191
// bucket sort
92-
for (int i = 0; i < n; i++) {
92+
for (size_t i = 0; i < n; i++) {
9393
HNSW::storage_idx_t pt_id = i + n0;
9494
int pt_level = hnsw.levels[pt_id] - 1;
9595
order[offsets[pt_level]++] = pt_id;
@@ -101,7 +101,7 @@ void hnsw_add_vertices(
101101

102102
int i1 = n;
103103

104-
for (int pt_level = hist.size() - 1;
104+
for (int pt_level = static_cast<int>(hist.size()) - 1;
105105
pt_level >= int(!index_hnsw.init_level0);
106106
pt_level--) {
107107
int i0 = i1 - hist[pt_level];
@@ -157,7 +157,7 @@ void hnsw_add_vertices(
157157
printf("Done in %.3f ms\n", getmillisecs() - t0);
158158
}
159159

160-
for (int i = 0; i < ntotal; i++) {
160+
for (size_t i = 0; i < ntotal; i++) {
161161
omp_destroy_lock(&locks[i]);
162162
}
163163
}
@@ -172,19 +172,19 @@ IndexBinaryHNSW::IndexBinaryHNSW() {
172172
is_trained = true;
173173
}
174174

175-
IndexBinaryHNSW::IndexBinaryHNSW(int d, int M)
176-
: IndexBinary(d),
175+
IndexBinaryHNSW::IndexBinaryHNSW(int d_, int M)
176+
: IndexBinary(d_),
177177
hnsw(M),
178178
own_fields(true),
179-
storage(new IndexBinaryFlat(d)) {
179+
storage(new IndexBinaryFlat(d_)) {
180180
is_trained = true;
181181
}
182182

183-
IndexBinaryHNSW::IndexBinaryHNSW(IndexBinary* storage, int M)
184-
: IndexBinary(storage->d),
183+
IndexBinaryHNSW::IndexBinaryHNSW(IndexBinary* storage_, int M)
184+
: IndexBinary(storage_->d),
185185
hnsw(M),
186186
own_fields(false),
187-
storage(storage) {
187+
storage(storage_) {
188188
is_trained = true;
189189
}
190190

@@ -242,7 +242,7 @@ void IndexBinaryHNSW::search(
242242
}
243243

244244
#pragma omp parallel for
245-
for (int i = 0; i < n * k; ++i) {
245+
for (idx_t i = 0; i < n * k; ++i) {
246246
distances[i] = std::round(distances_f[i]);
247247
}
248248
}
@@ -253,7 +253,13 @@ void IndexBinaryHNSW::add(idx_t n, const uint8_t* x) {
253253
storage->add(n, x);
254254
ntotal = storage->ntotal;
255255

256-
hnsw_add_vertices(*this, n0, n, x, verbose, hnsw.levels.size() == ntotal);
256+
hnsw_add_vertices(
257+
*this,
258+
n0,
259+
n,
260+
x,
261+
verbose,
262+
hnsw.levels.size() == static_cast<size_t>(ntotal));
257263
}
258264

259265
void IndexBinaryHNSW::reset() {
@@ -330,8 +336,8 @@ IndexBinaryHNSWCagra::IndexBinaryHNSWCagra() : IndexBinaryHNSW() {
330336
storage = nullptr;
331337
}
332338

333-
IndexBinaryHNSWCagra::IndexBinaryHNSWCagra(int d, int M)
334-
: IndexBinaryHNSW(d, M) {
339+
IndexBinaryHNSWCagra::IndexBinaryHNSWCagra(int d_, int M)
340+
: IndexBinaryHNSW(d_, M) {
335341
init_level0 = true;
336342
keep_max_size_level0 = true;
337343
}
@@ -415,7 +421,7 @@ void IndexBinaryHNSWCagra::search(
415421
}
416422

417423
#pragma omp parallel for
418-
for (int i = 0; i < n * k; ++i) {
424+
for (idx_t i = 0; i < n * k; ++i) {
419425
distances[i] = std::round(distances_f[i]);
420426
}
421427
}

faiss/IndexHNSW.cpp

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void hnsw_add_vertices(
8383
}
8484

8585
std::vector<omp_lock_t> locks(ntotal);
86-
for (int i = 0; i < ntotal; i++) {
86+
for (size_t i = 0; i < ntotal; i++) {
8787
omp_init_lock(&locks[i]);
8888
}
8989

@@ -94,23 +94,23 @@ void hnsw_add_vertices(
9494
{ // make buckets with vectors of the same level
9595

9696
// build histogram
97-
for (int i = 0; i < n; i++) {
97+
for (size_t i = 0; i < n; i++) {
9898
storage_idx_t pt_id = i + n0;
9999
int pt_level = hnsw.levels[pt_id] - 1;
100-
while (pt_level >= hist.size()) {
100+
while (pt_level >= static_cast<int>(hist.size())) {
101101
hist.push_back(0);
102102
}
103103
hist[pt_level]++;
104104
}
105105

106106
// accumulate
107107
std::vector<int> offsets(hist.size() + 1, 0);
108-
for (int i = 0; i < hist.size() - 1; i++) {
108+
for (size_t i = 0; i < hist.size() - 1; i++) {
109109
offsets[i + 1] = offsets[i] + hist[i];
110110
}
111111

112112
// bucket sort
113-
for (int i = 0; i < n; i++) {
113+
for (size_t i = 0; i < n; i++) {
114114
storage_idx_t pt_id = i + n0;
115115
int pt_level = hnsw.levels[pt_id] - 1;
116116
order[offsets[pt_level]++] = pt_id;
@@ -125,7 +125,7 @@ void hnsw_add_vertices(
125125

126126
int i1 = n;
127127

128-
for (int pt_level = hist.size() - 1;
128+
for (int pt_level = static_cast<int>(hist.size()) - 1;
129129
pt_level >= int(!index_hnsw.init_level0);
130130
pt_level--) {
131131
int i0 = i1 - hist[pt_level];
@@ -200,7 +200,7 @@ void hnsw_add_vertices(
200200
printf("Done in %.3f ms\n", getmillisecs() - t0);
201201
}
202202

203-
for (int i = 0; i < ntotal; i++) {
203+
for (size_t i = 0; i < ntotal; i++) {
204204
omp_destroy_lock(&locks[i]);
205205
}
206206
}
@@ -211,11 +211,13 @@ void hnsw_add_vertices(
211211
* IndexHNSW implementation
212212
**************************************************************/
213213

214-
IndexHNSW::IndexHNSW(int d, int M, MetricType metric)
215-
: Index(d, metric), hnsw(M) {}
214+
IndexHNSW::IndexHNSW(int d_in, int M, MetricType metric)
215+
: Index(d_in, metric), hnsw(M) {}
216216

217-
IndexHNSW::IndexHNSW(Index* storage, int M)
218-
: Index(storage->d, storage->metric_type), hnsw(M), storage(storage) {
217+
IndexHNSW::IndexHNSW(Index* storage_in, int M)
218+
: Index(storage_in->d, storage_in->metric_type),
219+
hnsw(M),
220+
storage(storage_in) {
219221
metric_arg = storage->metric_arg;
220222
}
221223

@@ -309,7 +311,7 @@ void IndexHNSW::search(
309311

310312
if (is_similarity_metric(this->metric_type)) {
311313
// we need to revert the negated distances
312-
for (size_t i = 0; i < k * n; i++) {
314+
for (idx_t i = 0; i < k * n; i++) {
313315
distances[i] = -distances[i];
314316
}
315317
}
@@ -351,7 +353,13 @@ void IndexHNSW::add(idx_t n, const float* x) {
351353
storage->add(n, x);
352354
ntotal = storage->ntotal;
353355

354-
hnsw_add_vertices(*this, n0, n, x, verbose, hnsw.levels.size() == ntotal);
356+
hnsw_add_vertices(
357+
*this,
358+
n0,
359+
n,
360+
x,
361+
verbose,
362+
hnsw.levels.size() == static_cast<size_t>(ntotal));
355363
}
356364

357365
void IndexHNSW::reset() {
@@ -427,7 +435,7 @@ void IndexHNSW::search_level_0(
427435
FAISS_THROW_IF_NOT(k > 0);
428436
FAISS_THROW_IF_NOT(nprobe > 0);
429437

430-
storage_idx_t ntotal = hnsw.levels.size();
438+
storage_idx_t hnsw_ntotal = hnsw.levels.size();
431439

432440
using RH = HeapBlockResultHandler<HNSW::C>;
433441
RH bres(n, distances, labels, k);
@@ -437,7 +445,7 @@ void IndexHNSW::search_level_0(
437445
std::unique_ptr<DistanceComputer> qdis(
438446
storage_distance_computer(storage));
439447
HNSWStats search_stats;
440-
VisitedTable vt(ntotal, hnsw.use_visited_hashset);
448+
VisitedTable vt(hnsw_ntotal, hnsw.use_visited_hashset);
441449
RH::SingleResultHandler res(bres);
442450

443451
#pragma omp for
@@ -487,7 +495,7 @@ void IndexHNSW::init_level_0_from_knngraph(
487495

488496
std::priority_queue<NodeDistFarther> initial_list;
489497

490-
for (size_t j = 0; j < k; j++) {
498+
for (int j = 0; j < k; j++) {
491499
int v1 = I[i * k + j];
492500
if (v1 == i) {
493501
continue;
@@ -519,7 +527,7 @@ void IndexHNSW::init_level_0_from_entry_points(
519527
const storage_idx_t* points,
520528
const storage_idx_t* nearests) {
521529
std::vector<omp_lock_t> locks(ntotal);
522-
for (int i = 0; i < ntotal; i++) {
530+
for (idx_t i = 0; i < ntotal; i++) {
523531
omp_init_lock(&locks[i]);
524532
}
525533

@@ -551,7 +559,7 @@ void IndexHNSW::init_level_0_from_entry_points(
551559
printf("\n");
552560
}
553561

554-
for (int i = 0; i < ntotal; i++) {
562+
for (idx_t i = 0; i < ntotal; i++) {
555563
omp_destroy_lock(&locks[i]);
556564
}
557565
}
@@ -595,7 +603,7 @@ void IndexHNSW::link_singletons() {
595603

596604
std::vector<bool> seen(ntotal);
597605

598-
for (size_t i = 0; i < ntotal; i++) {
606+
for (idx_t i = 0; i < ntotal; i++) {
599607
size_t begin, end;
600608
hnsw.neighbor_range(i, 0, &begin, &end);
601609
for (size_t j = begin; j < end; j++) {
@@ -624,7 +632,7 @@ void IndexHNSW::link_singletons() {
624632
n_sing_l1);
625633

626634
std::vector<float> recons(singletons.size() * d);
627-
for (int i = 0; i < singletons.size(); i++) {
635+
for (size_t i = 0; i < singletons.size(); i++) {
628636
FAISS_ASSERT(false); // not implemented
629637
}
630638
}
@@ -649,10 +657,10 @@ IndexHNSWFlat::IndexHNSWFlat() {
649657
is_trained = true;
650658
}
651659

652-
IndexHNSWFlat::IndexHNSWFlat(int d, int M, MetricType metric)
660+
IndexHNSWFlat::IndexHNSWFlat(int d_in, int M, MetricType metric)
653661
: IndexHNSW(
654-
(metric == METRIC_L2) ? new IndexFlatL2(d)
655-
: new IndexFlat(d, metric),
662+
(metric == METRIC_L2) ? new IndexFlatL2(d_in)
663+
: new IndexFlat(d_in, metric),
656664
M) {
657665
own_fields = true;
658666
is_trained = true;
@@ -666,14 +674,14 @@ IndexHNSWFlatPanorama::IndexHNSWFlatPanorama()
666674
: IndexHNSWFlat(), cum_sums(), pano(0, 1, 1), num_panorama_levels(0) {}
667675

668676
IndexHNSWFlatPanorama::IndexHNSWFlatPanorama(
669-
int d,
677+
int d_in,
670678
int M,
671-
int num_panorama_levels,
679+
int num_panorama_levels_in,
672680
MetricType metric)
673-
: IndexHNSWFlat(d, M, metric),
681+
: IndexHNSWFlat(d_in, M, metric),
674682
cum_sums(),
675-
pano(d * sizeof(float), num_panorama_levels, 1),
676-
num_panorama_levels(num_panorama_levels) {
683+
pano(d_in * sizeof(float), num_panorama_levels_in, 1),
684+
num_panorama_levels(num_panorama_levels_in) {
677685
// For now, we only support L2 distance.
678686
// Supporting dot product and cosine distance is a trivial addition
679687
// left for future work.
@@ -718,12 +726,12 @@ void IndexHNSWFlatPanorama::permute_entries(const idx_t* perm) {
718726
IndexHNSWPQ::IndexHNSWPQ() = default;
719727

720728
IndexHNSWPQ::IndexHNSWPQ(
721-
int d,
729+
int d_in,
722730
int pq_m,
723731
int M,
724732
int pq_nbits,
725733
MetricType metric)
726-
: IndexHNSW(new IndexPQ(d, pq_m, pq_nbits, metric), M) {
734+
: IndexHNSW(new IndexPQ(d_in, pq_m, pq_nbits, metric), M) {
727735
own_fields = true;
728736
is_trained = false;
729737
}
@@ -738,11 +746,11 @@ void IndexHNSWPQ::train(idx_t n, const float* x) {
738746
**************************************************************/
739747

740748
IndexHNSWSQ::IndexHNSWSQ(
741-
int d,
749+
int d_in,
742750
ScalarQuantizer::QuantizerType qtype,
743751
int M,
744752
MetricType metric)
745-
: IndexHNSW(new IndexScalarQuantizer(d, qtype, metric), M) {
753+
: IndexHNSW(new IndexScalarQuantizer(d_in, qtype, metric), M) {
746754
is_trained = this->storage->is_trained;
747755
own_fields = true;
748756
}
@@ -900,7 +908,7 @@ void IndexHNSW2Level::search(
900908
size_t list_length = index_ivfpq->get_list_size(key);
901909
const idx_t* ids = index_ivfpq->invlists->get_ids(key);
902910

903-
for (int jj = 0; jj < list_length; jj++) {
911+
for (size_t jj = 0; jj < list_length; jj++) {
904912
vt.set(ids[jj]);
905913
}
906914
}
@@ -976,11 +984,11 @@ IndexHNSWCagra::IndexHNSWCagra() {
976984
}
977985

978986
IndexHNSWCagra::IndexHNSWCagra(
979-
int d,
987+
int d_in,
980988
int M,
981989
MetricType metric,
982990
NumericType numeric_type)
983-
: IndexHNSW(d, M, metric) {
991+
: IndexHNSW(d_in, M, metric) {
984992
FAISS_THROW_IF_NOT_MSG(
985993
((metric == METRIC_L2) || (metric == METRIC_INNER_PRODUCT)),
986994
"unsupported metric type for IndexHNSWCagra");

0 commit comments

Comments
 (0)