contrib/faiss/faiss/impl/code_distance/code_distance.h
Line | Count | Source |
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 <faiss/impl/platform_macros.h> |
11 | | |
12 | | // This directory contains functions to compute a distance |
13 | | // from a given PQ code to a query vector, given that the |
14 | | // distances to a query vector for pq.M codebooks are precomputed. |
15 | | // |
16 | | // The code was originally the part of IndexIVFPQ.cpp. |
17 | | // The baseline implementation can be found in |
18 | | // code_distance-generic.h, distance_single_code_generic(). |
19 | | |
20 | | // The reason for this somewhat unusual structure is that |
21 | | // custom implementations may need to fall off to generic |
22 | | // implementation in certain cases. So, say, avx2 header file |
23 | | // needs to reference the generic header file. This is |
24 | | // why the names of the functions for custom implementations |
25 | | // have this _generic or _avx2 suffix. |
26 | | |
27 | | #ifdef __AVX2__ |
28 | | |
29 | | #include <faiss/impl/code_distance/code_distance-avx2.h> |
30 | | |
31 | | namespace faiss { |
32 | | |
33 | | template <typename PQDecoderT> |
34 | | inline float distance_single_code( |
35 | | // number of subquantizers |
36 | | const size_t M, |
37 | | // number of bits per quantization index |
38 | | const size_t nbits, |
39 | | // precomputed distances, layout (M, ksub) |
40 | | const float* sim_table, |
41 | | // the code |
42 | 0 | const uint8_t* code) { |
43 | 0 | return distance_single_code_avx2<PQDecoderT>(M, nbits, sim_table, code); |
44 | 0 | } Unexecuted instantiation: _ZN5faiss20distance_single_codeINS_10PQDecoder8EEEfmmPKfPKh Unexecuted instantiation: _ZN5faiss20distance_single_codeINS_11PQDecoder16EEEfmmPKfPKh Unexecuted instantiation: _ZN5faiss20distance_single_codeINS_16PQDecoderGenericEEEfmmPKfPKh |
45 | | |
46 | | template <typename PQDecoderT> |
47 | | inline void distance_four_codes( |
48 | | // number of subquantizers |
49 | | const size_t M, |
50 | | // number of bits per quantization index |
51 | | const size_t nbits, |
52 | | // precomputed distances, layout (M, ksub) |
53 | | const float* sim_table, |
54 | | // codes |
55 | | const uint8_t* __restrict code0, |
56 | | const uint8_t* __restrict code1, |
57 | | const uint8_t* __restrict code2, |
58 | | const uint8_t* __restrict code3, |
59 | | // computed distances |
60 | | float& result0, |
61 | | float& result1, |
62 | | float& result2, |
63 | 0 | float& result3) { |
64 | 0 | distance_four_codes_avx2<PQDecoderT>( |
65 | 0 | M, |
66 | 0 | nbits, |
67 | 0 | sim_table, |
68 | 0 | code0, |
69 | 0 | code1, |
70 | 0 | code2, |
71 | 0 | code3, |
72 | 0 | result0, |
73 | 0 | result1, |
74 | 0 | result2, |
75 | 0 | result3); |
76 | 0 | } Unexecuted instantiation: _ZN5faiss19distance_four_codesINS_10PQDecoder8EEEvmmPKfPKhS5_S5_S5_RfS6_S6_S6_ Unexecuted instantiation: _ZN5faiss19distance_four_codesINS_11PQDecoder16EEEvmmPKfPKhS5_S5_S5_RfS6_S6_S6_ Unexecuted instantiation: _ZN5faiss19distance_four_codesINS_16PQDecoderGenericEEEvmmPKfPKhS5_S5_S5_RfS6_S6_S6_ |
77 | | |
78 | | } // namespace faiss |
79 | | |
80 | | #elif defined(__ARM_FEATURE_SVE) |
81 | | |
82 | | #include <faiss/impl/code_distance/code_distance-sve.h> |
83 | | |
84 | | namespace faiss { |
85 | | |
86 | | template <typename PQDecoderT> |
87 | | inline float distance_single_code( |
88 | | // the product quantizer |
89 | | const size_t M, |
90 | | // number of bits per quantization index |
91 | | const size_t nbits, |
92 | | // precomputed distances, layout (M, ksub) |
93 | | const float* sim_table, |
94 | | // the code |
95 | | const uint8_t* code) { |
96 | | return distance_single_code_sve<PQDecoderT>(M, nbits, sim_table, code); |
97 | | } |
98 | | |
99 | | template <typename PQDecoderT> |
100 | | inline void distance_four_codes( |
101 | | // the product quantizer |
102 | | const size_t M, |
103 | | // number of bits per quantization index |
104 | | const size_t nbits, |
105 | | // precomputed distances, layout (M, ksub) |
106 | | const float* sim_table, |
107 | | // codes |
108 | | const uint8_t* __restrict code0, |
109 | | const uint8_t* __restrict code1, |
110 | | const uint8_t* __restrict code2, |
111 | | const uint8_t* __restrict code3, |
112 | | // computed distances |
113 | | float& result0, |
114 | | float& result1, |
115 | | float& result2, |
116 | | float& result3) { |
117 | | distance_four_codes_sve<PQDecoderT>( |
118 | | M, |
119 | | nbits, |
120 | | sim_table, |
121 | | code0, |
122 | | code1, |
123 | | code2, |
124 | | code3, |
125 | | result0, |
126 | | result1, |
127 | | result2, |
128 | | result3); |
129 | | } |
130 | | |
131 | | } // namespace faiss |
132 | | |
133 | | #else |
134 | | |
135 | | #include <faiss/impl/code_distance/code_distance-generic.h> |
136 | | |
137 | | namespace faiss { |
138 | | |
139 | | template <typename PQDecoderT> |
140 | | inline float distance_single_code( |
141 | | // number of subquantizers |
142 | | const size_t M, |
143 | | // number of bits per quantization index |
144 | | const size_t nbits, |
145 | | // precomputed distances, layout (M, ksub) |
146 | | const float* sim_table, |
147 | | // the code |
148 | | const uint8_t* code) { |
149 | | return distance_single_code_generic<PQDecoderT>(M, nbits, sim_table, code); |
150 | | } |
151 | | |
152 | | template <typename PQDecoderT> |
153 | | inline void distance_four_codes( |
154 | | // number of subquantizers |
155 | | const size_t M, |
156 | | // number of bits per quantization index |
157 | | const size_t nbits, |
158 | | // precomputed distances, layout (M, ksub) |
159 | | const float* sim_table, |
160 | | // codes |
161 | | const uint8_t* __restrict code0, |
162 | | const uint8_t* __restrict code1, |
163 | | const uint8_t* __restrict code2, |
164 | | const uint8_t* __restrict code3, |
165 | | // computed distances |
166 | | float& result0, |
167 | | float& result1, |
168 | | float& result2, |
169 | | float& result3) { |
170 | | distance_four_codes_generic<PQDecoderT>( |
171 | | M, |
172 | | nbits, |
173 | | sim_table, |
174 | | code0, |
175 | | code1, |
176 | | code2, |
177 | | code3, |
178 | | result0, |
179 | | result1, |
180 | | result2, |
181 | | result3); |
182 | | } |
183 | | |
184 | | } // namespace faiss |
185 | | |
186 | | #endif |