Skip to content

Commit adb1884

Browse files
r-barnesfacebook-github-bot
authored andcommitted
Fix shadowed variable in faiss/IndexAdditiveQuantizer.cpp (#4011)
Summary: Pull Request resolved: #4011 Our upcoming compiler upgrade will require us not to have shadowed variables. Such variables have a _high_ bug rate and reduce readability, so we would like to avoid them even if the compiler was not forcing us to do so. This codemod attempts to fix an instance of a shadowed variable. Please review with care: if it's failed the result will be a silent bug. **What's a shadowed variable?** Shadowed variables are variables in an inner scope with the same name as another variable in an outer scope. Having the same name for both variables might be semantically correct, but it can make the code confusing to read! It can also hide subtle bugs. This diff fixes such an issue by renaming the variable. - If you approve of this diff, please use the "Accept & Ship" button :-) Reviewed By: asadoughi Differential Revision: D65347906 fbshipit-source-id: 4dbf5d2ee0300379e897954fe367cdc3186b5521
1 parent 1f2b7ce commit adb1884

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

faiss/IndexAdditiveQuantizer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -528,23 +528,23 @@ void ResidualCoarseQuantizer::search(
528528
float* distances,
529529
idx_t* labels,
530530
const SearchParameters* params_in) const {
531-
float beam_factor = this->beam_factor;
531+
float beam_factor_2 = this->beam_factor;
532532
if (params_in) {
533533
auto params =
534534
dynamic_cast<const SearchParametersResidualCoarseQuantizer*>(
535535
params_in);
536536
FAISS_THROW_IF_NOT_MSG(
537537
params,
538538
"need SearchParametersResidualCoarseQuantizer parameters");
539-
beam_factor = params->beam_factor;
539+
beam_factor_2 = params->beam_factor;
540540
}
541541

542-
if (beam_factor < 0) {
542+
if (beam_factor_2 < 0) {
543543
AdditiveCoarseQuantizer::search(n, x, k, distances, labels);
544544
return;
545545
}
546546

547-
int beam_size = int(k * beam_factor);
547+
int beam_size = int(k * beam_factor_2);
548548
if (beam_size > ntotal) {
549549
beam_size = ntotal;
550550
}

faiss/IndexIVFFastScan.cpp

+18-18
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ IndexIVFFastScan::IndexIVFFastScan() {
5757

5858
void IndexIVFFastScan::init_fastscan(
5959
size_t M,
60-
size_t nbits,
60+
size_t nbits_2,
6161
size_t nlist,
6262
MetricType /* metric */,
63-
int bbs) {
64-
FAISS_THROW_IF_NOT(bbs % 32 == 0);
65-
FAISS_THROW_IF_NOT(nbits == 4);
63+
int bbs_2) {
64+
FAISS_THROW_IF_NOT(bbs_2 % 32 == 0);
65+
FAISS_THROW_IF_NOT(nbits_2 == 4);
6666

6767
this->M = M;
68-
this->nbits = nbits;
69-
this->bbs = bbs;
70-
ksub = (1 << nbits);
68+
this->nbits = nbits_2;
69+
this->bbs = bbs_2;
70+
ksub = (1 << nbits_2);
7171
M2 = roundup(M, 2);
7272
code_size = M2 / 2;
7373

@@ -1029,11 +1029,11 @@ void IndexIVFFastScan::search_implem_12(
10291029

10301030
// prepare the result handlers
10311031

1032-
int qbs2 = this->qbs2 ? this->qbs2 : 11;
1032+
int qbs2_2 = this->qbs2 ? this->qbs2 : 11;
10331033

10341034
std::vector<uint16_t> tmp_bias;
10351035
if (biases.get()) {
1036-
tmp_bias.resize(qbs2);
1036+
tmp_bias.resize(qbs2_2);
10371037
handler.dbias = tmp_bias.data();
10381038
}
10391039

@@ -1046,7 +1046,7 @@ void IndexIVFFastScan::search_implem_12(
10461046
int list_no = qcs[i0].list_no;
10471047
size_t i1 = i0 + 1;
10481048

1049-
while (i1 < qcs.size() && i1 < i0 + qbs2) {
1049+
while (i1 < qcs.size() && i1 < i0 + qbs2_2) {
10501050
if (qcs[i1].list_no != list_no) {
10511051
break;
10521052
}
@@ -1066,7 +1066,7 @@ void IndexIVFFastScan::search_implem_12(
10661066
std::vector<int> q_map(nc), lut_entries(nc);
10671067
AlignedTable<uint8_t> LUT(nc * dim12);
10681068
memset(LUT.get(), -1, nc * dim12);
1069-
int qbs = pq4_preferred_qbs(nc);
1069+
int qbs_2 = pq4_preferred_qbs(nc);
10701070

10711071
for (size_t i = i0; i < i1; i++) {
10721072
const QC& qc = qcs[i];
@@ -1078,7 +1078,7 @@ void IndexIVFFastScan::search_implem_12(
10781078
}
10791079
}
10801080
pq4_pack_LUT_qbs_q_map(
1081-
qbs, M2, dis_tables.get(), lut_entries.data(), LUT.get());
1081+
qbs_2, M2, dis_tables.get(), lut_entries.data(), LUT.get());
10821082

10831083
// access the inverted list
10841084

@@ -1094,7 +1094,7 @@ void IndexIVFFastScan::search_implem_12(
10941094
handler.id_map = ids.get();
10951095

10961096
pq4_accumulate_loop_qbs(
1097-
qbs, list_size, M2, codes.get(), LUT.get(), handler, scaler);
1097+
qbs_2, list_size, M2, codes.get(), LUT.get(), handler, scaler);
10981098
// prepare for next loop
10991099
i0 = i1;
11001100
}
@@ -1232,11 +1232,11 @@ void IndexIVFFastScan::search_implem_14(
12321232
is_max, impl, n, k, local_dis.data(), local_idx.data(), sel));
12331233
handler->begin(normalizers.get());
12341234

1235-
int qbs2 = this->qbs2 ? this->qbs2 : 11;
1235+
int qbs2_2 = this->qbs2 ? this->qbs2 : 11;
12361236

12371237
std::vector<uint16_t> tmp_bias;
12381238
if (biases.get()) {
1239-
tmp_bias.resize(qbs2);
1239+
tmp_bias.resize(qbs2_2);
12401240
handler->dbias = tmp_bias.data();
12411241
}
12421242

@@ -1256,7 +1256,7 @@ void IndexIVFFastScan::search_implem_14(
12561256
std::vector<int> q_map(nc), lut_entries(nc);
12571257
AlignedTable<uint8_t> LUT(nc * dim12);
12581258
memset(LUT.get(), -1, nc * dim12);
1259-
int qbs = pq4_preferred_qbs(nc);
1259+
int qbs_2 = pq4_preferred_qbs(nc);
12601260

12611261
for (size_t i = i0; i < i1; i++) {
12621262
const QC& qc = qcs[i];
@@ -1269,7 +1269,7 @@ void IndexIVFFastScan::search_implem_14(
12691269
}
12701270
}
12711271
pq4_pack_LUT_qbs_q_map(
1272-
qbs, M2, dis_tables.get(), lut_entries.data(), LUT.get());
1272+
qbs_2, M2, dis_tables.get(), lut_entries.data(), LUT.get());
12731273

12741274
// access the inverted list
12751275

@@ -1285,7 +1285,7 @@ void IndexIVFFastScan::search_implem_14(
12851285
handler->id_map = ids.get();
12861286

12871287
pq4_accumulate_loop_qbs(
1288-
qbs,
1288+
qbs_2,
12891289
list_size,
12901290
M2,
12911291
codes.get(),

faiss/impl/NSG.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ void NSG::search_on_graph(
301301
retset[k].flag = false;
302302
int n = retset[k].id;
303303

304-
size_t nneigh = graph.get_neighbors(n, neighbors.data());
305-
for (int m = 0; m < nneigh; m++) {
304+
size_t nneigh_2 = graph.get_neighbors(n, neighbors.data());
305+
for (int m = 0; m < nneigh_2; m++) {
306306
int id = neighbors[m];
307307
if (id > ntotal || vt.get(id)) {
308308
continue;

0 commit comments

Comments
 (0)