Skip to content

Commit c1359b0

Browse files
junjieqimeta-codesync[bot]
authored andcommitted
Fix implicit integer precision loss from 64-bit to 32-bit (#5091)
Summary: Pull Request resolved: #5091 Fix 172 `clang-diagnostic-shorten-64-to-32` lint warnings across 29 files by adding explicit `static_cast<int>()` or widening variable types where `size_t`/`idx_t` (64-bit) values were implicitly narrowed to `int`/`int32_t` (32-bit). The fixes fall into two categories: - **Explicit casts**: Where the receiving API requires `int` and the value is known to fit (e.g., vector dimensions, sub-quantizer counts, cluster counts, BLAS parameters) - **Type widening**: Where the variable was unnecessarily narrow (e.g., `int nprobe` → `size_t nprobe`, `int list_no` → `size_t list_no`) Differential Revision: D100588996
1 parent d3ea2fc commit c1359b0

29 files changed

+198
-144
lines changed

faiss/AutoTune.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ double OperatingPoints::t_for_perf(double perf) const {
174174
if (perf > a.back().perf) {
175175
return 1e50;
176176
}
177-
int i0 = -1, i1 = a.size() - 1;
177+
int i0 = -1, i1 = static_cast<int>(a.size()) - 1;
178178
while (i0 + 1 < i1) {
179179
int imed = (i0 + i1 + 1) / 2;
180180
if (a[imed].perf < perf) {
@@ -786,7 +786,7 @@ void ParameterSpace::explore(
786786
// make sure the slowest and fastest experiment are run
787787
perm[0] = 0;
788788
if (n_comb > 1) {
789-
perm[1] = n_comb - 1;
789+
perm[1] = static_cast<int>(n_comb - 1);
790790
rand_perm(&perm[2], n_comb - 2, 1234);
791791
for (size_t i = 2; i < perm.size(); i++) {
792792
perm[i]++;

faiss/Clustering.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ idx_t subsample_training_set(
9292
const idx_t new_nx = clus.k * clus.max_points_per_centroid;
9393
perm.resize(new_nx);
9494
for (idx_t i = 0; i < new_nx; i++) {
95-
perm[i] = rng.rand_int(nx);
95+
perm[i] = rng.rand_int(static_cast<int>(nx));
9696
}
9797
} else {
9898
// use subsampling with a default std rng
@@ -262,7 +262,7 @@ int split_clusters(
262262
}
263263
}
264264

265-
return nsplit;
265+
return static_cast<int>(nsplit);
266266
}
267267

268268
} // namespace
@@ -538,7 +538,7 @@ void Clustering::train_encoded(
538538
obj,
539539
(getmillisecs() - t0) / 1000.0,
540540
t_search_tot / 1000,
541-
imbalance_factor(nx, k, assign.get()),
541+
imbalance_factor(nx, static_cast<int>(k), assign.get()),
542542
nsplit};
543543
iteration_stats.push_back(stats);
544544

@@ -649,7 +649,7 @@ float kmeans_clustering(
649649
size_t k,
650650
const float* x,
651651
float* centroids) {
652-
Clustering clus(d, k);
652+
Clustering clus(static_cast<int>(d), static_cast<int>(k));
653653
clus.verbose = d * n * k > (size_t(1) << 30);
654654
// display logs if > 1Gflop per iteration
655655
IndexFlatL2 index(d);
@@ -700,7 +700,7 @@ void ProgressiveDimClustering::train(
700700
ProgressiveDimIndexFactory& factory) {
701701
int d_prev = 0;
702702

703-
PCAMatrix pca(d, d);
703+
PCAMatrix pca(static_cast<int>(d), static_cast<int>(d));
704704

705705
std::vector<float> xbuf;
706706
if (apply_pca) {
@@ -725,7 +725,7 @@ void ProgressiveDimClustering::train(
725725
}
726726
std::unique_ptr<Index> clustering_index(factory(di));
727727

728-
Clustering clus(di, k, *this);
728+
Clustering clus(di, static_cast<int>(k), *this);
729729
if (d_prev > 0) {
730730
// copy warm-start centroids (padded with 0s)
731731
clus.centroids.resize(k * di);

faiss/IVFlib.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ IndexIVFResidualQuantizer* ivf_residual_from_quantizer(
465465
std::vector<size_t> nbits(nlevel);
466466
std::copy(rq.nbits.begin(), rq.nbits.begin() + nlevel, nbits.begin());
467467
std::unique_ptr<ResidualCoarseQuantizer> rcq(
468-
new ResidualCoarseQuantizer(rq.d, nbits));
468+
new ResidualCoarseQuantizer(static_cast<int>(rq.d), nbits));
469469

470470
// set the coarse quantizer from the 2 first quantizers
471471
rcq->rq.initialize_from(rq);
@@ -528,22 +528,24 @@ void ivf_residual_add_from_flat_codes(
528528
for (idx_t i = 0; i < static_cast<idx_t>(nb); i++) {
529529
const uint8_t* code = &raw_codes[i * code_size];
530530
BitstringReader rd(code, code_size);
531-
idx_t list_no = rd.read(rcq->rq.tot_bits);
531+
idx_t list_no = rd.read(static_cast<int>(rcq->rq.tot_bits));
532532

533533
if (list_no % nt ==
534534
rank) { // each thread takes care of 1/nt of the invlists
535535
// copy AQ indexes one by one
536536
BitstringWriter wr(tmp_code.data(), tmp_code.size());
537537
for (size_t j = 0; j < rq.M; j++) {
538-
int nbit = rq.nbits[j];
538+
int nbit = static_cast<int>(rq.nbits[j]);
539539
wr.write(rd.read(nbit), nbit);
540540
}
541541
// we need to recompute the norm
542542
// decode first, does not use the norm component, so that's
543543
// ok
544544
index->rq.decode(tmp_code.data(), tmp.data(), 1);
545545
float norm = fvec_norm_L2sqr<SL>(tmp.data(), rq.d);
546-
wr.write(rq.encode_norm(norm), rq.norm_bits);
546+
wr.write(
547+
rq.encode_norm(norm),
548+
static_cast<int>(rq.norm_bits));
547549

548550
// add code to the inverted list
549551
invlists.add_entry(list_no, i, tmp_code.data());

faiss/Index.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct Index {
115115
float metric_arg; ///< argument of the metric type
116116

117117
explicit Index(idx_t d_in = 0, MetricType metric = METRIC_L2)
118-
: d(d_in),
118+
: d(static_cast<int>(d_in)),
119119
ntotal(0),
120120
verbose(false),
121121
is_trained(true),

faiss/Index2Layer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ struct DistanceXPQ4 : Distance2Level {
171171
dynamic_cast<IndexFlat*>(storage.q1.quantizer);
172172

173173
FAISS_ASSERT(quantizer);
174-
M = storage.pq.M;
174+
M = static_cast<int>(storage.pq.M);
175175
pq_l1_tab = quantizer->get_xb();
176176
}
177177

@@ -218,8 +218,8 @@ struct Distance2xXPQ4 : Distance2Level {
218218

219219
FAISS_ASSERT(mi);
220220
FAISS_ASSERT(storage.pq.M % 2 == 0);
221-
M_2 = storage.pq.M / 2;
222-
mi_nbits = mi->pq.nbits;
221+
M_2 = static_cast<int>(storage.pq.M / 2);
222+
mi_nbits = static_cast<int>(mi->pq.nbits);
223223
pq_l1_tab = mi->pq.centroids.data();
224224
}
225225

faiss/IndexAdditiveQuantizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ void ResidualCoarseQuantizer::search(
543543

544544
int beam_size = int(k * actual_beam_factor);
545545
if (beam_size > ntotal) {
546-
beam_size = ntotal;
546+
beam_size = static_cast<int>(ntotal);
547547
}
548548
size_t memory_per_point = rq.memory_per_point(beam_size);
549549

faiss/IndexAdditiveQuantizerFastScan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void IndexAdditiveQuantizerFastScan::init(
5656
} else {
5757
M = aq_init->M;
5858
}
59-
init_fastscan(aq_init->d, M, 4, metric, bbs_);
59+
init_fastscan(static_cast<int>(aq_init->d), M, 4, metric, bbs_);
6060

6161
max_train_points = 1024 * ksub * M;
6262
}

faiss/IndexBinary.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
namespace faiss {
1818

1919
IndexBinary::IndexBinary(idx_t d_, MetricType metric)
20-
: d(d_), code_size(d_ / 8), metric_type(metric) {
20+
: d(static_cast<int>(d_)),
21+
code_size(static_cast<int>(d_ / 8)),
22+
metric_type(metric) {
2123
FAISS_THROW_IF_NOT(d_ % 8 == 0);
2224
}
2325

faiss/IndexBinaryHNSW.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ void hnsw_add_vertices(
7474

7575
// build histogram
7676
for (size_t i = 0; i < n; i++) {
77-
HNSW::storage_idx_t pt_id = i + n0;
77+
HNSW::storage_idx_t pt_id =
78+
static_cast<HNSW::storage_idx_t>(i + n0);
7879
int pt_level = hnsw.levels[pt_id] - 1;
7980
while (pt_level >= static_cast<int>(hist.size())) {
8081
hist.push_back(0);
@@ -90,7 +91,8 @@ void hnsw_add_vertices(
9091

9192
// bucket sort
9293
for (size_t i = 0; i < n; i++) {
93-
HNSW::storage_idx_t pt_id = i + n0;
94+
HNSW::storage_idx_t pt_id =
95+
static_cast<HNSW::storage_idx_t>(i + n0);
9496
int pt_level = hnsw.levels[pt_id] - 1;
9597
order[offsets[pt_level]++] = pt_id;
9698
}
@@ -99,7 +101,7 @@ void hnsw_add_vertices(
99101
{ // perform add
100102
RandomGenerator rng2(789);
101103

102-
int i1 = n;
104+
int i1 = static_cast<int>(n);
103105

104106
for (int pt_level = static_cast<int>(hist.size()) - 1;
105107
pt_level >= int(!index_hnsw.init_level0);
@@ -257,7 +259,7 @@ void IndexBinaryHNSW::search(
257259

258260
void IndexBinaryHNSW::add(idx_t n, const uint8_t* x) {
259261
FAISS_THROW_IF_NOT(is_trained);
260-
int n0 = ntotal;
262+
idx_t n0 = ntotal;
261263
storage->add(n, x);
262264
ntotal = storage->ntotal;
263265

@@ -390,7 +392,7 @@ void IndexBinaryHNSWCagra::search(
390392
float distance = (*dis)(idx);
391393

392394
if (distance < nearest_d[i]) {
393-
nearest[i] = idx;
395+
nearest[i] = static_cast<storage_idx_t>(idx);
394396
nearest_d[i] = distance;
395397
}
396398
}

faiss/IndexBinaryIVF.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void IndexBinaryIVF::search(
151151
indexIVF_stats.quantization_time += getmillisecs() - t0;
152152

153153
t0 = getmillisecs();
154-
invlists->prefetch_lists(idx.get(), n * nprobe_);
154+
invlists->prefetch_lists(idx.get(), static_cast<int>(n * nprobe_));
155155

156156
search_preassigned(
157157
n,
@@ -209,7 +209,7 @@ void IndexBinaryIVF::search_and_reconstruct(
209209

210210
quantizer->search(n, x, nprobe_2, coarse_dis.get(), idx.get());
211211

212-
invlists->prefetch_lists(idx.get(), n * nprobe_2);
212+
invlists->prefetch_lists(idx.get(), static_cast<int>(n * nprobe_2));
213213

214214
// search_preassigned() with `store_pairs` enabled to obtain the list_no
215215
// and offset into `codes` for reconstruction
@@ -277,7 +277,7 @@ void IndexBinaryIVF::train(idx_t n, const uint8_t* x) {
277277
printf("Training quantizer on %" PRId64 " vectors in %dD\n", n, d);
278278
}
279279

280-
Clustering clus(d, nlist, cp);
280+
Clustering clus(d, static_cast<int>(nlist), cp);
281281
quantizer->reset();
282282

283283
IndexFlatL2 index_tmp(d);
@@ -705,9 +705,10 @@ void search_knn_hamming_per_invlist(
705705
int32_t* keys = new int32_t[n * nprobe];
706706
std::unique_ptr<int32_t[]> delete_keys(keys);
707707
for (size_t i = 0; i < n * static_cast<size_t>(nprobe); i++) {
708-
keys[i] = keys_in[i];
708+
keys[i] = static_cast<int32_t>(keys_in[i]);
709709
}
710-
matrix_bucket_sort_inplace(n, nprobe, keys, ivf->nlist, lims.data(), 0);
710+
matrix_bucket_sort_inplace(
711+
n, nprobe, keys, static_cast<int32_t>(ivf->nlist), lims.data(), 0);
711712

712713
using C = CMax<int32_t, idx_t>;
713714
heap_heapify<C>(n * k, distances, labels);
@@ -754,7 +755,12 @@ void search_knn_hamming_per_invlist(
754755
} else {
755756
for (; i + BS <= nq; i += BS) {
756757
BlockSearchVariableK<HammingComputer, BS> bc(
757-
code_size, k, x, keys + l0 + i, distances, labels);
758+
code_size,
759+
static_cast<int>(k),
760+
x,
761+
keys + l0 + i,
762+
distances,
763+
labels);
758764
for (idx_t j = 0; j < nb; j++) {
759765
bc.add_bcode(bcodes + j * code_size, ids[j]);
760766
}
@@ -764,7 +770,8 @@ void search_knn_hamming_per_invlist(
764770
// leftovers
765771
for (; i < nq; i++) {
766772
idx_t qno = keys[l0 + i];
767-
HammingComputer hc(x + qno * code_size, code_size);
773+
HammingComputer hc(
774+
x + qno * code_size, static_cast<int>(code_size));
768775
idx_t* __restrict idxi = labels + qno * k;
769776
int32_t* __restrict simi = distances + qno * k;
770777
int32_t simi0 = simi[0];
@@ -868,7 +875,7 @@ void IndexBinaryIVF::range_search(
868875
indexIVF_stats.quantization_time += getmillisecs() - t0;
869876

870877
t0 = getmillisecs();
871-
invlists->prefetch_lists(idx.get(), n * nprobe_2);
878+
invlists->prefetch_lists(idx.get(), static_cast<int>(n * nprobe_2));
872879

873880
range_search_preassigned(n, x, radius, idx.get(), coarse_dis.get(), res);
874881

0 commit comments

Comments
 (0)