Skip to content

Commit e23c661

Browse files
mdouzemeta-codesync[bot]
authored andcommitted
Replace dispatch_HammingComputer with with_HammingComputer (#5126)
Summary: Pull Request resolved: #5126 Replace the dispatch_HammingComputer + Run_XXX consumer struct pattern with with_HammingComputer that takes a C++20 template lambda directly. This eliminates boilerplate wrapper structs across 8 files. Before: struct Run_foo { using T = void; template<class HC, class... T> void f(T... a) { foo<HC>(a...); } }; Run_foo r; dispatch_HammingComputer(code_size, r, args...); After: with_HammingComputer(code_size, [&]<class HC>() { foo<HC>(args...); }); Reviewed By: algoriddle Differential Revision: D101350351 fbshipit-source-id: 02a346e8c33ffdb49153cbe13415b748f0a1e847
1 parent ed7e1f2 commit e23c661

8 files changed

Lines changed: 78 additions & 178 deletions

File tree

faiss/IndexBinaryHNSW.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,23 +309,17 @@ struct FlatHammingDis : DistanceComputer {
309309
}
310310
};
311311

312-
struct BuildDistanceComputer {
313-
using T = DistanceComputer*;
314-
template <class HammingComputer>
315-
DistanceComputer* f(IndexBinaryFlat* flat_storage) {
316-
return new FlatHammingDis<HammingComputer>(*flat_storage);
317-
}
318-
};
319-
320312
} // namespace
321313

322314
DistanceComputer* IndexBinaryHNSW::get_distance_computer() const {
323315
IndexBinaryFlat* flat_storage = dynamic_cast<IndexBinaryFlat*>(storage);
324316
FAISS_THROW_IF_NOT_MSG(
325317
flat_storage != nullptr,
326318
"IndexBinaryHNSW requires IndexBinaryFlat storage");
327-
BuildDistanceComputer bd;
328-
return dispatch_HammingComputer(code_size, bd, flat_storage);
319+
return with_HammingComputer(
320+
code_size, [&]<class HammingComputer>() -> DistanceComputer* {
321+
return new FlatHammingDis<HammingComputer>(*flat_storage);
322+
});
329323
}
330324

331325
/**************************************************************

faiss/IndexBinaryHash.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,6 @@ void search_single_query_template(
175175
} while (fe.next());
176176
}
177177

178-
struct Run_search_single_query {
179-
using T = void;
180-
template <class HammingComputer, class... Types>
181-
T f(Types*... args) {
182-
search_single_query_template<HammingComputer>(*args...);
183-
}
184-
};
185-
186178
template <class SearchResults>
187179
void search_single_query(
188180
const IndexBinaryHash& index,
@@ -191,9 +183,10 @@ void search_single_query(
191183
size_t& n0,
192184
size_t& nlist,
193185
size_t& ndis) {
194-
Run_search_single_query r;
195-
dispatch_HammingComputer(
196-
index.code_size, r, &index, &q, &res, &n0, &nlist, &ndis);
186+
with_HammingComputer(index.code_size, [&]<class HammingComputer>() {
187+
search_single_query_template<HammingComputer>(
188+
index, q, res, n0, nlist, ndis);
189+
});
197190
}
198191

199192
} // anonymous namespace
@@ -343,14 +336,6 @@ static void verify_shortlist(
343336
}
344337
}
345338

346-
struct Run_verify_shortlist {
347-
using T = void;
348-
template <class HammingComputer, class... Types>
349-
void f(Types... args) {
350-
verify_shortlist<HammingComputer>(args...);
351-
}
352-
};
353-
354339
template <class SearchResults>
355340
void search_1_query_multihash(
356341
const IndexBinaryMultiHash& index,
@@ -387,9 +372,9 @@ void search_1_query_multihash(
387372
ndis += shortlist.size();
388373

389374
// verify shortlist
390-
Run_verify_shortlist r;
391-
dispatch_HammingComputer(
392-
index.code_size, r, index.storage, xi, shortlist, res);
375+
with_HammingComputer(index.code_size, [&]<class HammingComputer>() {
376+
verify_shortlist<HammingComputer>(index.storage, xi, shortlist, res);
377+
});
393378
}
394379

395380
} // anonymous namespace

faiss/IndexBinaryIVF.cpp

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -784,40 +784,16 @@ void search_knn_hamming_per_invlist(
784784
}
785785
}
786786

787-
struct Run_search_knn_hamming_per_invlist {
788-
using T = void;
789-
790-
template <class HammingComputer, class... Types>
791-
void f(Types... args) {
792-
search_knn_hamming_per_invlist<HammingComputer>(args...);
793-
}
794-
};
795-
796-
template <bool store_pairs>
797-
struct Run_search_knn_hamming_count {
798-
using T = void;
799-
800-
template <class HammingComputer, class... Types>
801-
void f(Types... args) {
802-
search_knn_hamming_count<HammingComputer, store_pairs>(args...);
803-
}
804-
};
805-
806-
struct BuildScanner {
807-
using T = BinaryInvertedListScanner*;
808-
809-
template <class HammingComputer>
810-
T f(size_t code_size, bool store_pairs) {
811-
return new IVFBinaryScannerL2<HammingComputer>(code_size, store_pairs);
812-
}
813-
};
814-
815787
} // anonymous namespace
816788

817789
BinaryInvertedListScanner* IndexBinaryIVF::get_InvertedListScanner(
818790
bool store_pairs) const {
819-
BuildScanner bs;
820-
return dispatch_HammingComputer(code_size, bs, code_size, store_pairs);
791+
return with_HammingComputer(
792+
code_size,
793+
[&]<class HammingComputer>() -> BinaryInvertedListScanner* {
794+
return new IVFBinaryScannerL2<HammingComputer>(
795+
code_size, store_pairs);
796+
});
821797
}
822798

823799
void IndexBinaryIVF::search_preassigned(
@@ -831,23 +807,23 @@ void IndexBinaryIVF::search_preassigned(
831807
bool store_pairs,
832808
const IVFSearchParameters* params) const {
833809
if (per_invlist_search) {
834-
Run_search_knn_hamming_per_invlist r;
835-
// clang-format off
836-
dispatch_HammingComputer(
837-
code_size, r, this, n, x, k,
838-
cidx, cdis, dis, idx, store_pairs, params);
839-
// clang-format on
810+
with_HammingComputer(code_size, [&]<class HammingComputer>() {
811+
search_knn_hamming_per_invlist<HammingComputer>(
812+
this, n, x, k, cidx, cdis, dis, idx, store_pairs, params);
813+
});
840814
} else if (use_heap) {
841815
search_knn_hamming_heap(
842816
this, n, x, k, cidx, cdis, dis, idx, store_pairs, params);
843817
} else if (store_pairs) { // !use_heap && store_pairs
844-
Run_search_knn_hamming_count<true> r;
845-
dispatch_HammingComputer(
846-
code_size, r, this, n, x, cidx, k, dis, idx, params);
818+
with_HammingComputer(code_size, [&]<class HammingComputer>() {
819+
search_knn_hamming_count<HammingComputer, true>(
820+
this, n, x, cidx, k, dis, idx, params);
821+
});
847822
} else { // !use_heap && !store_pairs
848-
Run_search_knn_hamming_count<false> r;
849-
dispatch_HammingComputer(
850-
code_size, r, this, n, x, cidx, k, dis, idx, params);
823+
with_HammingComputer(code_size, [&]<class HammingComputer>() {
824+
search_knn_hamming_count<HammingComputer, false>(
825+
this, n, x, cidx, k, dis, idx, params);
826+
});
851827
}
852828
}
853829

faiss/IndexIVFPQ.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,23 +1167,15 @@ struct IVFPQScannerT : QueryTables {
11671167
}
11681168
}
11691169

1170-
template <class SearchResultType>
1171-
struct Run_scan_list_polysemous_hc {
1172-
using T = void;
1173-
template <class HammingComputer, class... Types>
1174-
void f(const IVFPQScannerT* scanner, Types... args) {
1175-
scanner->scan_list_polysemous_hc<HammingComputer, SearchResultType>(
1176-
args...);
1177-
}
1178-
};
1179-
11801170
template <class SearchResultType>
11811171
void scan_list_polysemous(
11821172
size_t ncode,
11831173
const uint8_t* codes,
11841174
SearchResultType& res) const {
1185-
Run_scan_list_polysemous_hc<SearchResultType> r;
1186-
dispatch_HammingComputer(pq.code_size, r, this, ncode, codes, res);
1175+
with_HammingComputer(pq.code_size, [&]<class HammingComputer>() {
1176+
this->scan_list_polysemous_hc<HammingComputer, SearchResultType>(
1177+
ncode, codes, res);
1178+
});
11871179
}
11881180
};
11891181

faiss/IndexIVFSpectralHash.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,17 @@ struct IVFScanner : InvertedListScanner {
299299
}
300300
};
301301

302-
struct BuildScanner {
303-
using T = InvertedListScanner*;
304-
305-
template <class HammingComputer>
306-
static T f(const IndexIVFSpectralHash* index, bool store_pairs) {
307-
return new IVFScanner<HammingComputer>(index, store_pairs);
308-
}
309-
};
310-
311302
} // anonymous namespace
312303

313304
InvertedListScanner* IndexIVFSpectralHash::get_InvertedListScanner(
314305
bool store_pairs,
315306
const IDSelector* sel,
316307
const IVFSearchParameters*) const {
317308
FAISS_THROW_IF_NOT(!sel);
318-
BuildScanner bs;
319-
return dispatch_HammingComputer(code_size, bs, this, store_pairs);
309+
return with_HammingComputer(
310+
code_size, [&]<class HammingComputer>() -> InvertedListScanner* {
311+
return new IVFScanner<HammingComputer>(this, store_pairs);
312+
});
320313
}
321314

322315
void IndexIVFSpectralHash::replace_vt(VectorTransform* vt_in, bool own) {

faiss/IndexPQ.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,6 @@ size_t polysemous_inner_loop(
323323
return n_pass_i;
324324
}
325325

326-
struct Run_polysemous_inner_loop {
327-
using T = size_t;
328-
template <class HammingComputer, class... Types>
329-
size_t f(Types... args) {
330-
return polysemous_inner_loop<HammingComputer>(args...);
331-
}
332-
};
333-
334326
} // anonymous namespace
335327

336328
void IndexPQ::search_core_polysemous(
@@ -381,17 +373,17 @@ void IndexPQ::search_core_polysemous(
381373
maxheap_heapify(k, heap_dis, heap_ids);
382374

383375
if (!generalized_hamming) {
384-
Run_polysemous_inner_loop r;
385-
n_pass += dispatch_HammingComputer(
386-
pq.code_size,
387-
r,
388-
this,
389-
dis_table_qi,
390-
q_code,
391-
k,
392-
heap_dis,
393-
heap_ids,
394-
param_polysemous_ht);
376+
n_pass += with_HammingComputer(
377+
pq.code_size, [&]<class HammingComputer>() -> size_t {
378+
return polysemous_inner_loop<HammingComputer>(
379+
this,
380+
dis_table_qi,
381+
q_code,
382+
k,
383+
heap_dis,
384+
heap_ids,
385+
param_polysemous_ht);
386+
});
395387

396388
} else { // generalized hamming
397389
switch (pq.code_size) {

faiss/utils/hamming.cpp

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -332,30 +332,6 @@ void hamming_range_search(
332332
}
333333
}
334334

335-
struct Run_hammings_knn_hc {
336-
using T = void;
337-
template <class HammingComputer, class... Types>
338-
void f(Types... args) {
339-
hammings_knn_hc<HammingComputer>(args...);
340-
}
341-
};
342-
343-
struct Run_hammings_knn_mc {
344-
using T = void;
345-
template <class HammingComputer, class... Types>
346-
void f(Types... args) {
347-
hammings_knn_mc<HammingComputer>(args...);
348-
}
349-
};
350-
351-
struct Run_hamming_range_search {
352-
using T = void;
353-
template <class HammingComputer, class... Types>
354-
void f(Types... args) {
355-
hamming_range_search<HammingComputer>(args...);
356-
}
357-
};
358-
359335
} // namespace
360336

361337
/* Functions to maps vectors to bits. Assume proper allocation done beforehand,
@@ -511,19 +487,10 @@ void hammings_knn_hc(
511487
int order,
512488
ApproxTopK_mode_t approx_topk_mode,
513489
const faiss::IDSelector* sel) {
514-
Run_hammings_knn_hc r;
515-
dispatch_HammingComputer(
516-
ncodes,
517-
r,
518-
ncodes,
519-
ha,
520-
a,
521-
b,
522-
nb,
523-
order,
524-
true,
525-
approx_topk_mode,
526-
sel);
490+
with_HammingComputer(ncodes, [&]<class HammingComputer>() {
491+
hammings_knn_hc<HammingComputer>(
492+
ncodes, ha, a, b, nb, order, true, approx_topk_mode, sel);
493+
});
527494
}
528495

529496
void hammings_knn_mc(
@@ -536,9 +503,10 @@ void hammings_knn_mc(
536503
int32_t* __restrict distances,
537504
int64_t* __restrict labels,
538505
const faiss::IDSelector* sel) {
539-
Run_hammings_knn_mc r;
540-
dispatch_HammingComputer(
541-
ncodes, r, ncodes, a, b, na, nb, k, distances, labels, sel);
506+
with_HammingComputer(ncodes, [&]<class HammingComputer>() {
507+
hammings_knn_mc<HammingComputer>(
508+
ncodes, a, b, na, nb, k, distances, labels, sel);
509+
});
542510
}
543511

544512
void hamming_range_search(
@@ -550,9 +518,10 @@ void hamming_range_search(
550518
size_t code_size,
551519
RangeSearchResult* result,
552520
const faiss::IDSelector* sel) {
553-
Run_hamming_range_search r;
554-
dispatch_HammingComputer(
555-
code_size, r, a, b, na, nb, radius, code_size, result, sel);
521+
with_HammingComputer(code_size, [&]<class HammingComputer>() {
522+
hamming_range_search<HammingComputer>(
523+
a, b, na, nb, radius, code_size, result, sel);
524+
});
556525
}
557526

558527
/* Count number of matches given a max threshold */

0 commit comments

Comments
 (0)