|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +/** |
| 11 | + * @file dispatching.h |
| 12 | + * @brief Per-SIMD TU dispatch template for fast scan. |
| 13 | + * |
| 14 | + * This header is included once per SIMD TU with THE_LEVEL_TO_DISPATCH |
| 15 | + * set to the desired SIMDLevel. It provides: |
| 16 | + * - ScannerMixIn: wraps a handler + calls kernel at the TU's SIMD level |
| 17 | + * - make_fast_scan_scanner_impl<SL>: factory specialization |
| 18 | + * |
| 19 | + * Usage (in a per-SIMD .cpp file): |
| 20 | + * #define THE_LEVEL_TO_DISPATCH SIMDLevel::AVX2 |
| 21 | + * #include <faiss/impl/fast_scan/dispatching.h> |
| 22 | + * |
| 23 | + * Kernel helpers come from accumulate_loops.h (search_1 multi-BB path) |
| 24 | + * and decompose_qbs.h (QBS path, with if-constexpr guard for 512-bit). |
| 25 | + */ |
| 26 | + |
| 27 | +#ifndef THE_LEVEL_TO_DISPATCH |
| 28 | +#error "Define THE_LEVEL_TO_DISPATCH before including this header" |
| 29 | +#endif |
| 30 | + |
| 31 | +#include <memory> |
| 32 | + |
| 33 | +#include <faiss/impl/fast_scan/accumulate_loops.h> |
| 34 | +#include <faiss/impl/fast_scan/decompose_qbs.h> |
| 35 | +#include <faiss/impl/fast_scan/pq4_fast_scan.h> |
| 36 | + |
| 37 | +namespace faiss { |
| 38 | + |
| 39 | +using namespace simd_result_handlers; |
| 40 | + |
| 41 | +/*************************************************************** |
| 42 | + * ScannerMixIn: wraps a concrete handler + calls accumulation |
| 43 | + * kernels. Lives behind the virtual FastScanCodeScanner interface |
| 44 | + * so callers don't need to know the handler type. |
| 45 | + ***************************************************************/ |
| 46 | + |
| 47 | +template <class Handler> |
| 48 | +struct ScannerMixIn : FastScanCodeScanner { |
| 49 | + Handler handler_; |
| 50 | + |
| 51 | + template <typename... Args> |
| 52 | + explicit ScannerMixIn(Args&&... args) |
| 53 | + : handler_(std::forward<Args>(args)...) {} |
| 54 | + |
| 55 | + SIMDResultHandlerToFloat* handler() override { |
| 56 | + return &handler_; |
| 57 | + } |
| 58 | + |
| 59 | + void accumulate_loop( |
| 60 | + int nq, |
| 61 | + size_t nb, |
| 62 | + int bbs, |
| 63 | + int nsq, |
| 64 | + const uint8_t* codes, |
| 65 | + const uint8_t* LUT, |
| 66 | + int pq2x4_scale, |
| 67 | + size_t block_stride) override { |
| 68 | + if (pq2x4_scale) { |
| 69 | + NormTableScaler<> scaler(pq2x4_scale); |
| 70 | + pq4_accumulate_loop_fixed_scaler( |
| 71 | + nq, |
| 72 | + nb, |
| 73 | + bbs, |
| 74 | + nsq, |
| 75 | + codes, |
| 76 | + LUT, |
| 77 | + handler_, |
| 78 | + scaler, |
| 79 | + block_stride); |
| 80 | + } else { |
| 81 | + DummyScaler<> dummy; |
| 82 | + pq4_accumulate_loop_fixed_scaler( |
| 83 | + nq, |
| 84 | + nb, |
| 85 | + bbs, |
| 86 | + nsq, |
| 87 | + codes, |
| 88 | + LUT, |
| 89 | + handler_, |
| 90 | + dummy, |
| 91 | + block_stride); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + void accumulate_loop_qbs( |
| 96 | + int qbs, |
| 97 | + size_t nb, |
| 98 | + int nsq, |
| 99 | + const uint8_t* codes, |
| 100 | + const uint8_t* LUT, |
| 101 | + int pq2x4_scale, |
| 102 | + size_t block_stride) override { |
| 103 | + if (pq2x4_scale) { |
| 104 | + NormTableScaler<> scaler(pq2x4_scale); |
| 105 | + pq4_accumulate_loop_qbs_fixed_scaler( |
| 106 | + qbs, nb, nsq, codes, LUT, handler_, scaler, block_stride); |
| 107 | + } else { |
| 108 | + DummyScaler<> dummy; |
| 109 | + pq4_accumulate_loop_qbs_fixed_scaler( |
| 110 | + qbs, nb, nsq, codes, LUT, handler_, dummy, block_stride); |
| 111 | + } |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +/*************************************************************** |
| 116 | + * Factory specialization for this SIMD level. |
| 117 | + * |
| 118 | + * Combinatorial dispatch: is_max × with_id_map × handler type |
| 119 | + * k == 1: SingleResultHandler |
| 120 | + * impl even: HeapHandler |
| 121 | + * impl odd: ReservoirHandler (capacity = 2*k) |
| 122 | + ***************************************************************/ |
| 123 | + |
| 124 | +template <> |
| 125 | +std::unique_ptr<FastScanCodeScanner> make_fast_scan_scanner_impl< |
| 126 | + THE_LEVEL_TO_DISPATCH>( |
| 127 | + bool is_max, |
| 128 | + int impl, |
| 129 | + size_t nq, |
| 130 | + size_t ntotal, |
| 131 | + int64_t k, |
| 132 | + float* distances, |
| 133 | + int64_t* ids, |
| 134 | + const IDSelector* sel, |
| 135 | + bool with_id_map) { |
| 136 | + // Helper lambda: given comparator C and with_id_map W, select handler |
| 137 | + auto make = [&]<class C, bool W>() -> std::unique_ptr<FastScanCodeScanner> { |
| 138 | + if (k == 1) { |
| 139 | + using H = SingleResultHandler<C, W>; |
| 140 | + return std::make_unique<ScannerMixIn<H>>( |
| 141 | + nq, ntotal, distances, ids, sel); |
| 142 | + } else if (impl % 2 == 0) { |
| 143 | + using H = HeapHandler<C, W>; |
| 144 | + return std::make_unique<ScannerMixIn<H>>( |
| 145 | + nq, ntotal, k, distances, ids, sel); |
| 146 | + } else { |
| 147 | + using H = ReservoirHandler<C, W>; |
| 148 | + return std::make_unique<ScannerMixIn<H>>( |
| 149 | + nq, ntotal, size_t(k), size_t(2 * k), distances, ids, sel); |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + if (is_max) { |
| 154 | + if (with_id_map) { |
| 155 | + return make.template operator()<CMax<uint16_t, int64_t>, true>(); |
| 156 | + } else { |
| 157 | + return make.template operator()<CMax<uint16_t, int>, false>(); |
| 158 | + } |
| 159 | + } else { |
| 160 | + if (with_id_map) { |
| 161 | + return make.template operator()<CMin<uint16_t, int64_t>, true>(); |
| 162 | + } else { |
| 163 | + return make.template operator()<CMin<uint16_t, int>, false>(); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +} // namespace faiss |
0 commit comments