|
| 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 | +#include <memory> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +#include <faiss/impl/CodePacker.h> |
| 14 | +#include <faiss/impl/fast_scan/FastScanDistancePostProcessing.h> |
| 15 | +#include <faiss/impl/fast_scan/simd_result_handlers.h> |
| 16 | +#include <faiss/utils/Heap.h> |
| 17 | + |
| 18 | +namespace faiss { |
| 19 | + |
| 20 | +// Forward declaration — full definition needed only in implementation |
| 21 | +struct IndexIVFRaBitQFastScan; |
| 22 | + |
| 23 | +namespace simd_result_handlers { |
| 24 | + |
| 25 | +/** SIMD result handler for IndexIVFRaBitQFastScan that applies |
| 26 | + * RaBitQ-specific distance corrections during batch processing. |
| 27 | + * |
| 28 | + * This handler processes batches of 32 distance computations from SIMD |
| 29 | + * kernels, applies RaBitQ distance formula adjustments (factors and |
| 30 | + * normalizers), and immediately updates result heaps. This eliminates the |
| 31 | + * need for post-processing and provides significant performance benefits. |
| 32 | + * |
| 33 | + * Key optimizations: |
| 34 | + * - Direct heap integration with no intermediate result storage |
| 35 | + * - Batch-level computation of normalizers and query factors |
| 36 | + * - Specialized handling for both centered and non-centered quantization |
| 37 | + * modes |
| 38 | + * - Efficient inner product metric corrections |
| 39 | + * - Uses runtime boolean for multi-bit mode |
| 40 | + * |
| 41 | + * @tparam C Comparator type (CMin/CMax) for heap operations |
| 42 | + * @tparam SL SIMD level for dynamic dispatch |
| 43 | + */ |
| 44 | +template <class C, SIMDLevel SL = SINGLE_SIMD_LEVEL_256> |
| 45 | +struct IVFRaBitQHeapHandler : ResultHandlerCompare<C, true, SL> { |
| 46 | + using RHC = ResultHandlerCompare<C, true, SL>; |
| 47 | + using typename RHC::simd16uint16; |
| 48 | + |
| 49 | + const IndexIVFRaBitQFastScan* index; |
| 50 | + float* heap_distances; // [nq * k] |
| 51 | + int64_t* heap_labels; // [nq * k] |
| 52 | + const size_t nq, k; |
| 53 | + size_t current_list_no = 0; |
| 54 | + const uint8_t* list_codes_ptr = nullptr; // raw block data for list |
| 55 | + std::vector<int> |
| 56 | + probe_indices; // probe index for each query in current batch |
| 57 | + const FastScanDistancePostProcessing* |
| 58 | + context; // Processing context with query factors |
| 59 | + const bool is_multibit; // Whether to use multi-bit two-stage search |
| 60 | + size_t nup = 0; // Number of heap updates |
| 61 | + |
| 62 | + // Cached block-layout constants (invariant for handler lifetime) |
| 63 | + const size_t storage_size; |
| 64 | + const size_t packed_block_size; |
| 65 | + const size_t full_block_size; |
| 66 | + std::unique_ptr<CodePacker> packer; // cached for unpack in hot path |
| 67 | + |
| 68 | + // Use float-based comparator for heap operations |
| 69 | + using Cfloat = typename std::conditional< |
| 70 | + C::is_max, |
| 71 | + CMax<float, int64_t>, |
| 72 | + CMin<float, int64_t>>::type; |
| 73 | + |
| 74 | + IVFRaBitQHeapHandler( |
| 75 | + const IndexIVFRaBitQFastScan* idx, |
| 76 | + size_t nq_val, |
| 77 | + size_t k_val, |
| 78 | + float* distances, |
| 79 | + int64_t* labels, |
| 80 | + const FastScanDistancePostProcessing* ctx = nullptr, |
| 81 | + bool multibit = false); |
| 82 | + |
| 83 | + void handle(size_t q, size_t b, simd16uint16 d0, simd16uint16 d1) override; |
| 84 | + |
| 85 | + /// Override base class virtual method to receive context information |
| 86 | + void set_list_context(size_t list_no, const std::vector<int>& probe_map) |
| 87 | + override; |
| 88 | + |
| 89 | + void begin(const float* norms) override; |
| 90 | + |
| 91 | + void end() override; |
| 92 | + |
| 93 | + size_t num_updates() override { |
| 94 | + return nup; |
| 95 | + } |
| 96 | + |
| 97 | + private: |
| 98 | + /// Compute full multi-bit distance for a candidate vector (multi-bit |
| 99 | + /// only) |
| 100 | + /// @param db_idx Global database vector index |
| 101 | + /// @param local_q Batch-local query index (for probe_indices access) |
| 102 | + /// @param global_q Global query index (for storage indexing) |
| 103 | + /// @param local_offset Offset within the current inverted list |
| 104 | + float compute_full_multibit_distance( |
| 105 | + size_t /*db_idx*/, |
| 106 | + size_t local_q, |
| 107 | + size_t global_q, |
| 108 | + size_t local_offset) const; |
| 109 | +}; |
| 110 | + |
| 111 | +} // namespace simd_result_handlers |
| 112 | + |
| 113 | +} // namespace faiss |
0 commit comments