faiss 0.6.5 → 0.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/faiss/extconf.rb +1 -0
- data/ext/faiss/index.cpp +42 -0
- data/lib/faiss/version.rb +1 -1
- data/vendor/faiss/faiss/IVFlib.cpp +48 -51
- data/vendor/faiss/faiss/IVFlib.h +6 -2
- data/vendor/faiss/faiss/Index.cpp +2 -2
- data/vendor/faiss/faiss/Index.h +3 -3
- data/vendor/faiss/faiss/IndexBinary.cpp +2 -2
- data/vendor/faiss/faiss/IndexBinary.h +2 -2
- data/vendor/faiss/faiss/IndexBinaryFlat.cpp +1 -0
- data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +6 -3
- data/vendor/faiss/faiss/IndexBinaryIVF.cpp +6 -11
- data/vendor/faiss/faiss/IndexFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexFlat.cpp +1 -1
- data/vendor/faiss/faiss/IndexHNSW.cpp +12 -10
- data/vendor/faiss/faiss/IndexHNSW.h +2 -5
- data/vendor/faiss/faiss/IndexIDMap.cpp +17 -4
- data/vendor/faiss/faiss/IndexIDMap.h +10 -5
- data/vendor/faiss/faiss/IndexIVF.cpp +68 -37
- data/vendor/faiss/faiss/IndexIVF.h +37 -1
- data/vendor/faiss/faiss/IndexIVFFastScan.cpp +5 -15
- data/vendor/faiss/faiss/IndexIVFFlat.h +6 -6
- data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.cpp +28 -44
- data/vendor/faiss/faiss/IndexIVFRaBitQFastScan.h +2 -11
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.cpp +1694 -0
- data/vendor/faiss/faiss/IndexIVFRaBitQSSD.h +288 -0
- data/vendor/faiss/faiss/IndexIVFSQFastScan.cpp +7 -2
- data/vendor/faiss/faiss/IndexNNDescent.cpp +21 -0
- data/vendor/faiss/faiss/IndexRaBitQ.cpp +189 -41
- data/vendor/faiss/faiss/IndexRaBitQ.h +5 -0
- data/vendor/faiss/faiss/IndexRaBitQFastScan.cpp +1 -1
- data/vendor/faiss/faiss/IndexRaBitQFastScan.h +2 -2
- data/vendor/faiss/faiss/SuperKMeans.cpp +2 -2
- data/vendor/faiss/faiss/VectorTransform.cpp +156 -9
- data/vendor/faiss/faiss/VectorTransform.h +24 -1
- data/vendor/faiss/faiss/factory_tools.cpp +19 -1
- data/vendor/faiss/faiss/gpu/Dataset.h +66 -14
- data/vendor/faiss/faiss/gpu/GpuIndexBinaryCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/GpuIndexCagra.h +8 -2
- data/vendor/faiss/faiss/gpu/impl/optimal_kmeans/GpuOptimalKMeans.h +184 -0
- data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +12 -4
- data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +6 -5
- data/vendor/faiss/faiss/gpu_metal/GpuIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalCloner.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalDistance.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalFlatKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndex.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexBinaryFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalIndexIVFPQ.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalKernels.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalPythonBridge.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/MetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/StandardMetalResources.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFFlat.h +1 -1
- data/vendor/faiss/faiss/gpu_metal/impl/MetalIVFPQ.h +1 -1
- data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
- data/vendor/faiss/faiss/impl/ClusteringInitialization.cpp +31 -50
- data/vendor/faiss/faiss/impl/ClusteringInitialization.h +0 -15
- data/vendor/faiss/faiss/impl/CodePacker.cpp +2 -2
- data/vendor/faiss/faiss/impl/CodePackerRaBitQ.cpp +2 -2
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.cpp +2167 -0
- data/vendor/faiss/faiss/impl/DeterministicNNDescent.h +301 -0
- data/vendor/faiss/faiss/impl/HNSW.cpp +2 -2
- data/vendor/faiss/faiss/impl/HNSW.h +2 -2
- data/vendor/faiss/faiss/impl/IDSelector.h +5 -4
- data/vendor/faiss/faiss/impl/NNDescent.h +4 -0
- data/vendor/faiss/faiss/impl/PiPNN.cpp +4 -2
- data/vendor/faiss/faiss/impl/PiPNN.h +1 -1
- data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -1
- data/vendor/faiss/faiss/impl/RaBitQUtils.cpp +0 -2
- data/vendor/faiss/faiss/impl/RaBitQUtils.h +29 -0
- data/vendor/faiss/faiss/impl/RaBitQuantizer.cpp +14 -13
- data/vendor/faiss/faiss/impl/ResultHandler.h +116 -0
- data/vendor/faiss/faiss/impl/binary_hamming/IndexBinaryIVF_impl.h +1 -3
- data/vendor/faiss/faiss/impl/expanded_scanners.h +29 -24
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.cpp +68 -6
- data/vendor/faiss/faiss/impl/fast_scan/fast_scan.h +43 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx2.cpp +15 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-avx512.cpp +115 -0
- data/vendor/faiss/faiss/impl/fast_scan/impl-neon.cpp +13 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd256.h +84 -0
- data/vendor/faiss/faiss/impl/fast_scan/kernels_simd512.h +0 -4
- data/vendor/faiss/faiss/impl/fast_scan/rabitq_result_handler.h +0 -1
- data/vendor/faiss/faiss/impl/fast_scan/simd_result_handlers.h +64 -55
- data/vendor/faiss/faiss/impl/hnsw/LockVector.h +1 -2
- data/vendor/faiss/faiss/impl/index_read.cpp +146 -9
- data/vendor/faiss/faiss/impl/index_write.cpp +29 -5
- data/vendor/faiss/faiss/impl/io.cpp +1 -0
- data/vendor/faiss/faiss/impl/io_macros.h +2 -2
- data/vendor/faiss/faiss/impl/mapped_io.cpp +3 -1
- data/vendor/faiss/faiss/impl/maybe_owned_vector.h +78 -41
- data/vendor/faiss/faiss/impl/nndescent/gram-avx2.cpp +121 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-avx512.cpp +124 -0
- data/vendor/faiss/faiss/impl/nndescent/gram-neon.cpp +114 -0
- data/vendor/faiss/faiss/impl/nndescent/gram.h +66 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-avx512.cpp +431 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/BlockCombiner-inl.h +1157 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.cpp +282 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeans.h +226 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.cpp +1567 -0
- data/vendor/faiss/faiss/impl/optimal_kmeans/OptimalKMeansCompressed.h +281 -0
- data/vendor/faiss/faiss/impl/pipnn/HashPrune.cpp +1 -1
- data/vendor/faiss/faiss/impl/platform_macros.h +47 -28
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQScanner_impl.h +6 -1
- data/vendor/faiss/faiss/impl/pq_code_distance/IVFPQ_QueryTables.cpp +17 -17
- data/vendor/faiss/faiss/impl/result_handler/ResultHandler.cpp +68 -1
- data/vendor/faiss/faiss/impl/result_handler/avx2.cpp +70 -2
- data/vendor/faiss/faiss/impl/result_handler/avx512.cpp +2 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/EDENQuantizer.cpp +1 -1
- data/vendor/faiss/faiss/impl/scalar_quantizer/scanners.h +26 -26
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx2.cpp +48 -45
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-impl.h +34 -32
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512-spr.cpp +8 -6
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-avx512.cpp +4 -3
- data/vendor/faiss/faiss/impl/scalar_quantizer/sq-neon.cpp +34 -33
- data/vendor/faiss/faiss/impl/simdlib/simdlib_avx512.h +20 -20
- data/vendor/faiss/faiss/impl/simdlib/simdlib_neon.h +4 -4
- data/vendor/faiss/faiss/impl/zerocopy_io.cpp +2 -1
- data/vendor/faiss/faiss/index_factory.cpp +30 -6
- data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
- data/vendor/faiss/faiss/invlists/DiskIOBackend.cpp +121 -0
- data/vendor/faiss/faiss/invlists/DiskIOBackend.h +71 -0
- data/vendor/faiss/faiss/invlists/InvertedLists.cpp +12 -9
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +26 -0
- data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +5 -0
- data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +9 -24
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.cpp +751 -0
- data/vendor/faiss/faiss/invlists/RaBitQSSDInvertedLists.h +224 -0
- data/vendor/faiss/faiss/svs/IndexSVSFaissUtils.h +2 -7
- data/vendor/faiss/faiss/svs/IndexSVSFlat.cpp +1 -1
- data/vendor/faiss/faiss/svs/IndexSVSIVF.cpp +2 -2
- data/vendor/faiss/faiss/svs/IndexSVSVamana.cpp +4 -4
- data/vendor/faiss/faiss/utils/WorkerThread.h +0 -1
- data/vendor/faiss/faiss/utils/distances.cpp +246 -34
- data/vendor/faiss/faiss/utils/distances.h +145 -2
- data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
- data/vendor/faiss/faiss/utils/hamming-inl.h +3 -0
- data/vendor/faiss/faiss/utils/partitioning.cpp +2 -2
- data/vendor/faiss/faiss/utils/rabitq_simd.h +120 -6
- data/vendor/faiss/faiss/utils/simd_impl/distances_arm_sve.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_autovec-inl.h +7 -3
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx2.cpp +47 -7
- data/vendor/faiss/faiss/utils/simd_impl/distances_avx512.cpp +50 -10
- data/vendor/faiss/faiss/utils/simd_impl/distances_simdlib256.h +3 -3
- data/vendor/faiss/faiss/utils/simd_impl/exhaustive_L2sqr_blas_cmax.h +13 -0
- data/vendor/faiss/faiss/utils/simd_impl/partitioning_simdlib256.h +16 -15
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx2.cpp +131 -36
- data/vendor/faiss/faiss/utils/simd_impl/rabitq_avx512.cpp +156 -45
- data/vendor/faiss/faiss/utils/simd_levels.cpp +3 -3
- data/vendor/faiss/faiss/utils/sorting.cpp +6 -5
- data/vendor/faiss/faiss/utils/utils.cpp +4 -0
- metadata +20 -1
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
+
|
|
7
8
|
#pragma once
|
|
8
9
|
|
|
9
10
|
#include <cstddef>
|
|
@@ -27,8 +28,8 @@ template <typename T>
|
|
|
27
28
|
struct MaybeOwnedVector {
|
|
28
29
|
using value_type = T;
|
|
29
30
|
using self_type = MaybeOwnedVector<T>;
|
|
30
|
-
using iterator =
|
|
31
|
-
using const_iterator =
|
|
31
|
+
using iterator = T*;
|
|
32
|
+
using const_iterator = const T*;
|
|
32
33
|
using size_type = typename std::vector<T>::size_type;
|
|
33
34
|
|
|
34
35
|
bool is_owned = true;
|
|
@@ -167,6 +168,14 @@ struct MaybeOwnedVector {
|
|
|
167
168
|
return c_size * sizeof(T);
|
|
168
169
|
}
|
|
169
170
|
|
|
171
|
+
bool empty() const {
|
|
172
|
+
return c_size == 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
size_t capacity() const {
|
|
176
|
+
return is_owned ? owned_data.capacity() : view_size;
|
|
177
|
+
}
|
|
178
|
+
|
|
170
179
|
T& operator[](const size_t idx) {
|
|
171
180
|
return c_ptr[idx];
|
|
172
181
|
}
|
|
@@ -175,81 +184,80 @@ struct MaybeOwnedVector {
|
|
|
175
184
|
return c_ptr[idx];
|
|
176
185
|
}
|
|
177
186
|
|
|
178
|
-
T&
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
"This operation cannot be performed on a viewed vector");
|
|
187
|
+
T& front() {
|
|
188
|
+
return c_ptr[0];
|
|
189
|
+
}
|
|
182
190
|
|
|
183
|
-
|
|
191
|
+
const T& front() const {
|
|
192
|
+
return c_ptr[0];
|
|
184
193
|
}
|
|
185
194
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
"This operation cannot be performed on a viewed vector");
|
|
195
|
+
T& back() {
|
|
196
|
+
return c_ptr[c_size - 1];
|
|
197
|
+
}
|
|
190
198
|
|
|
191
|
-
|
|
199
|
+
const T& back() const {
|
|
200
|
+
return c_ptr[c_size - 1];
|
|
192
201
|
}
|
|
193
202
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
203
|
+
T& at(const size_t pos) {
|
|
204
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
205
|
+
pos < c_size, "index %zd out of range [0, %zd)", pos, c_size);
|
|
206
|
+
return c_ptr[pos];
|
|
207
|
+
}
|
|
198
208
|
|
|
199
|
-
|
|
209
|
+
const T& at(const size_t pos) const {
|
|
210
|
+
FAISS_THROW_IF_NOT_FMT(
|
|
211
|
+
pos < c_size, "index %zd out of range [0, %zd)", pos, c_size);
|
|
212
|
+
return c_ptr[pos];
|
|
200
213
|
}
|
|
201
214
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
"This operation cannot be performed on a viewed vector");
|
|
215
|
+
iterator begin() {
|
|
216
|
+
return c_ptr;
|
|
217
|
+
}
|
|
206
218
|
|
|
207
|
-
|
|
219
|
+
const_iterator begin() const {
|
|
220
|
+
return c_ptr;
|
|
208
221
|
}
|
|
209
222
|
|
|
210
223
|
iterator end() {
|
|
211
|
-
|
|
212
|
-
is_owned,
|
|
213
|
-
"This operation cannot be performed on a viewed vector");
|
|
214
|
-
|
|
215
|
-
return owned_data.end();
|
|
224
|
+
return c_ptr + c_size;
|
|
216
225
|
}
|
|
217
226
|
|
|
218
227
|
const_iterator end() const {
|
|
219
|
-
|
|
220
|
-
is_owned,
|
|
221
|
-
"This operation cannot be performed on a viewed vector");
|
|
222
|
-
|
|
223
|
-
return owned_data.end();
|
|
228
|
+
return c_ptr + c_size;
|
|
224
229
|
}
|
|
225
230
|
|
|
226
231
|
iterator erase(const_iterator begin, const_iterator end) {
|
|
227
|
-
|
|
232
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
228
233
|
is_owned,
|
|
229
234
|
"This operation cannot be performed on a viewed vector");
|
|
230
235
|
|
|
231
|
-
auto
|
|
236
|
+
const auto first = owned_data.begin() + (begin - c_ptr);
|
|
237
|
+
const auto last = owned_data.begin() + (end - c_ptr);
|
|
238
|
+
const auto result = owned_data.erase(first, last);
|
|
232
239
|
c_ptr = owned_data.data();
|
|
233
240
|
c_size = owned_data.size();
|
|
234
241
|
|
|
235
|
-
return result;
|
|
242
|
+
return c_ptr + (result - owned_data.begin());
|
|
236
243
|
}
|
|
237
244
|
|
|
238
245
|
template <class InputIt>
|
|
239
246
|
iterator insert(const_iterator pos, InputIt first, InputIt last) {
|
|
240
|
-
|
|
247
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
241
248
|
is_owned,
|
|
242
249
|
"This operation cannot be performed on a viewed vector");
|
|
243
250
|
|
|
244
|
-
auto result = owned_data.insert(
|
|
251
|
+
const auto result = owned_data.insert(
|
|
252
|
+
owned_data.begin() + (pos - c_ptr), first, last);
|
|
245
253
|
c_ptr = owned_data.data();
|
|
246
254
|
c_size = owned_data.size();
|
|
247
255
|
|
|
248
|
-
return result;
|
|
256
|
+
return c_ptr + (result - owned_data.begin());
|
|
249
257
|
}
|
|
250
258
|
|
|
251
259
|
void clear() {
|
|
252
|
-
|
|
260
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
253
261
|
is_owned,
|
|
254
262
|
"This operation cannot be performed on a viewed vector");
|
|
255
263
|
|
|
@@ -259,7 +267,7 @@ struct MaybeOwnedVector {
|
|
|
259
267
|
}
|
|
260
268
|
|
|
261
269
|
void resize(const size_t new_size) {
|
|
262
|
-
|
|
270
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
263
271
|
is_owned,
|
|
264
272
|
"This operation cannot be performed on a viewed vector");
|
|
265
273
|
|
|
@@ -269,7 +277,7 @@ struct MaybeOwnedVector {
|
|
|
269
277
|
}
|
|
270
278
|
|
|
271
279
|
void resize(const size_t new_size, const value_type v) {
|
|
272
|
-
|
|
280
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
273
281
|
is_owned,
|
|
274
282
|
"This operation cannot be performed on a viewed vector");
|
|
275
283
|
|
|
@@ -278,6 +286,35 @@ struct MaybeOwnedVector {
|
|
|
278
286
|
c_size = owned_data.size();
|
|
279
287
|
}
|
|
280
288
|
|
|
289
|
+
void assign(const size_t new_size, const value_type v) {
|
|
290
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
291
|
+
is_owned,
|
|
292
|
+
"This operation cannot be performed on a viewed vector");
|
|
293
|
+
|
|
294
|
+
owned_data.assign(new_size, v);
|
|
295
|
+
c_ptr = owned_data.data();
|
|
296
|
+
c_size = owned_data.size();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
void push_back(const value_type v) {
|
|
300
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
301
|
+
is_owned,
|
|
302
|
+
"This operation cannot be performed on a viewed vector");
|
|
303
|
+
|
|
304
|
+
owned_data.push_back(v);
|
|
305
|
+
c_ptr = owned_data.data();
|
|
306
|
+
c_size = owned_data.size();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
void pop_back() {
|
|
310
|
+
FAISS_THROW_IF_NOT_MSG(
|
|
311
|
+
is_owned,
|
|
312
|
+
"This operation cannot be performed on a viewed vector");
|
|
313
|
+
|
|
314
|
+
owned_data.pop_back();
|
|
315
|
+
c_size = owned_data.size();
|
|
316
|
+
}
|
|
317
|
+
|
|
281
318
|
friend void swap(self_type& a, self_type& b) {
|
|
282
319
|
std::swap(a.is_owned, b.is_owned);
|
|
283
320
|
std::swap(a.owned_data, b.owned_data);
|
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
#include <faiss/impl/nndescent/gram.h>
|
|
9
|
+
|
|
10
|
+
#include <algorithm>
|
|
11
|
+
#include <cmath>
|
|
12
|
+
|
|
13
|
+
#include <faiss/impl/simdlib/simdlib_avx2.h>
|
|
14
|
+
|
|
15
|
+
namespace faiss {
|
|
16
|
+
namespace nndescent {
|
|
17
|
+
|
|
18
|
+
using VF = simd8float32_tpl<SIMDLevel::AVX2>;
|
|
19
|
+
constexpr size_t W = sizeof(VF) / sizeof(float);
|
|
20
|
+
// tiles of RI x RJ entries: 12 accumulators and 4 operands fill the 16
|
|
21
|
+
// registers
|
|
22
|
+
constexpr int RI = 3, RJ = 4;
|
|
23
|
+
|
|
24
|
+
/** One tile, a block of I x J Gram matrix entries that the kernel computes
|
|
25
|
+
* together: out[i * ldo + j] = <a[i], b[j]> for i < I, j < J. Each vector
|
|
26
|
+
* that the kernel loads serves J or I products. Each product has its own
|
|
27
|
+
* accumulator, and the tail uses std::fma whatever the compiler contracts, so
|
|
28
|
+
* a product depends only on its two rows. */
|
|
29
|
+
template <int I, int J>
|
|
30
|
+
static void tile(
|
|
31
|
+
const float* const* a,
|
|
32
|
+
const float* const* b,
|
|
33
|
+
size_t d,
|
|
34
|
+
float* out,
|
|
35
|
+
size_t ldo) {
|
|
36
|
+
VF acc[I][J];
|
|
37
|
+
for (int i = 0; i < I; i++) {
|
|
38
|
+
for (int j = 0; j < J; j++) {
|
|
39
|
+
acc[i][j] = VF(0.0f);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
size_t k = 0;
|
|
43
|
+
for (; k + W <= d; k += W) {
|
|
44
|
+
VF x[I];
|
|
45
|
+
for (int i = 0; i < I; i++) {
|
|
46
|
+
x[i] = VF(a[i] + k);
|
|
47
|
+
}
|
|
48
|
+
for (int j = 0; j < J; j++) {
|
|
49
|
+
const VF y(b[j] + k);
|
|
50
|
+
for (int i = 0; i < I; i++) {
|
|
51
|
+
acc[i][j] = fmadd(x[i], y, acc[i][j]);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (int i = 0; i < I; i++) {
|
|
56
|
+
for (int j = 0; j < J; j++) {
|
|
57
|
+
float v = horizontal_add(acc[i][j]);
|
|
58
|
+
for (size_t t = k; t < d; t++) {
|
|
59
|
+
v = std::fma(a[i][t], b[j][t], v);
|
|
60
|
+
}
|
|
61
|
+
out[i * ldo + j] = v;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// a tile of I x nj entries, nj <= J
|
|
67
|
+
template <int I, int J = RJ>
|
|
68
|
+
static void run_tile_cols(
|
|
69
|
+
int nj,
|
|
70
|
+
const float* const* a,
|
|
71
|
+
const float* const* b,
|
|
72
|
+
size_t d,
|
|
73
|
+
float* out,
|
|
74
|
+
size_t ldo) {
|
|
75
|
+
if constexpr (J > 1) {
|
|
76
|
+
if (nj < J) {
|
|
77
|
+
run_tile_cols<I, J - 1>(nj, a, b, d, out, ldo);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
tile<I, J>(a, b, d, out, ldo);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// a tile of ni x nj entries, ni <= I, nj <= RJ
|
|
85
|
+
template <int I = RI>
|
|
86
|
+
static void run_tile(
|
|
87
|
+
int ni,
|
|
88
|
+
int nj,
|
|
89
|
+
const float* const* a,
|
|
90
|
+
const float* const* b,
|
|
91
|
+
size_t d,
|
|
92
|
+
float* out,
|
|
93
|
+
size_t ldo) {
|
|
94
|
+
if constexpr (I > 1) {
|
|
95
|
+
if (ni < I) {
|
|
96
|
+
run_tile<I - 1>(ni, nj, a, b, d, out, ldo);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
run_tile_cols<I>(nj, a, b, d, out, ldo);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
template <>
|
|
104
|
+
void gram_upper_rows<SIMDLevel::AVX2>(
|
|
105
|
+
const float* const* rows,
|
|
106
|
+
size_t r,
|
|
107
|
+
size_t m,
|
|
108
|
+
size_t d,
|
|
109
|
+
float* out) {
|
|
110
|
+
// rows i0 .. i0 + RI - 1 against the rows j > i0, tile by tile
|
|
111
|
+
for (size_t i0 = 0; i0 < r; i0 += RI) {
|
|
112
|
+
const int ni = int(std::min<size_t>(RI, r - i0));
|
|
113
|
+
for (size_t j = i0 + 1; j < m; j += RJ) {
|
|
114
|
+
const int nj = int(std::min<size_t>(RJ, m - j));
|
|
115
|
+
run_tile(ni, nj, rows + i0, rows + j, d, out + i0 * m + j, m);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
} // namespace nndescent
|
|
121
|
+
} // namespace faiss
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
#include <faiss/impl/nndescent/gram.h>
|
|
9
|
+
|
|
10
|
+
#include <immintrin.h>
|
|
11
|
+
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
|
|
14
|
+
namespace faiss {
|
|
15
|
+
namespace nndescent {
|
|
16
|
+
|
|
17
|
+
namespace {
|
|
18
|
+
|
|
19
|
+
// tiles of 4 x 6 entries: 24 accumulators and 5 operands of the 32 registers
|
|
20
|
+
constexpr int RI = 4, RJ = 6;
|
|
21
|
+
|
|
22
|
+
/// out[i * ldo + j] = <a[i], b[j]> for i < I, j < J; the tail is a masked
|
|
23
|
+
/// load, so there is no scalar loop
|
|
24
|
+
template <int I, int J>
|
|
25
|
+
void tile(
|
|
26
|
+
const float* const* a,
|
|
27
|
+
const float* const* b,
|
|
28
|
+
size_t d,
|
|
29
|
+
float* out,
|
|
30
|
+
size_t ldo) {
|
|
31
|
+
__m512 acc[I][J];
|
|
32
|
+
for (int i = 0; i < I; i++) {
|
|
33
|
+
for (int j = 0; j < J; j++) {
|
|
34
|
+
acc[i][j] = _mm512_setzero_ps();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
size_t k = 0;
|
|
38
|
+
for (; k + 16 <= d; k += 16) {
|
|
39
|
+
__m512 x[I];
|
|
40
|
+
for (int i = 0; i < I; i++) {
|
|
41
|
+
x[i] = _mm512_loadu_ps(a[i] + k);
|
|
42
|
+
}
|
|
43
|
+
for (int j = 0; j < J; j++) {
|
|
44
|
+
const __m512 y = _mm512_loadu_ps(b[j] + k);
|
|
45
|
+
for (int i = 0; i < I; i++) {
|
|
46
|
+
acc[i][j] = _mm512_fmadd_ps(x[i], y, acc[i][j]);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (k < d) {
|
|
51
|
+
const __mmask16 mask = __mmask16((1u << (d - k)) - 1);
|
|
52
|
+
__m512 x[I];
|
|
53
|
+
for (int i = 0; i < I; i++) {
|
|
54
|
+
x[i] = _mm512_maskz_loadu_ps(mask, a[i] + k);
|
|
55
|
+
}
|
|
56
|
+
for (int j = 0; j < J; j++) {
|
|
57
|
+
const __m512 y = _mm512_maskz_loadu_ps(mask, b[j] + k);
|
|
58
|
+
for (int i = 0; i < I; i++) {
|
|
59
|
+
acc[i][j] = _mm512_fmadd_ps(x[i], y, acc[i][j]);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (int i = 0; i < I; i++) {
|
|
64
|
+
for (int j = 0; j < J; j++) {
|
|
65
|
+
out[i * ldo + j] = _mm512_reduce_add_ps(acc[i][j]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
template <int I, int J = RJ>
|
|
71
|
+
void run_tile_cols(
|
|
72
|
+
int nj,
|
|
73
|
+
const float* const* a,
|
|
74
|
+
const float* const* b,
|
|
75
|
+
size_t d,
|
|
76
|
+
float* out,
|
|
77
|
+
size_t ldo) {
|
|
78
|
+
if constexpr (J > 1) {
|
|
79
|
+
if (nj < J) {
|
|
80
|
+
run_tile_cols<I, J - 1>(nj, a, b, d, out, ldo);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
tile<I, J>(a, b, d, out, ldo);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
template <int I = RI>
|
|
88
|
+
void run_tile(
|
|
89
|
+
int ni,
|
|
90
|
+
int nj,
|
|
91
|
+
const float* const* a,
|
|
92
|
+
const float* const* b,
|
|
93
|
+
size_t d,
|
|
94
|
+
float* out,
|
|
95
|
+
size_t ldo) {
|
|
96
|
+
if constexpr (I > 1) {
|
|
97
|
+
if (ni < I) {
|
|
98
|
+
run_tile<I - 1>(ni, nj, a, b, d, out, ldo);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
run_tile_cols<I>(nj, a, b, d, out, ldo);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
} // namespace
|
|
106
|
+
|
|
107
|
+
template <>
|
|
108
|
+
void gram_upper_rows<SIMDLevel::AVX512>(
|
|
109
|
+
const float* const* rows,
|
|
110
|
+
size_t r,
|
|
111
|
+
size_t m,
|
|
112
|
+
size_t d,
|
|
113
|
+
float* out) {
|
|
114
|
+
for (size_t i0 = 0; i0 < r; i0 += RI) {
|
|
115
|
+
const int ni = int(std::min<size_t>(RI, r - i0));
|
|
116
|
+
for (size_t j = i0 + 1; j < m; j += RJ) {
|
|
117
|
+
const int nj = int(std::min<size_t>(RJ, m - j));
|
|
118
|
+
run_tile(ni, nj, rows + i0, rows + j, d, out + i0 * m + j, m);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
} // namespace nndescent
|
|
124
|
+
} // namespace faiss
|
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
#include <faiss/impl/nndescent/gram.h>
|
|
9
|
+
|
|
10
|
+
#include <arm_neon.h>
|
|
11
|
+
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
#include <cmath>
|
|
14
|
+
|
|
15
|
+
namespace faiss {
|
|
16
|
+
namespace nndescent {
|
|
17
|
+
|
|
18
|
+
namespace {
|
|
19
|
+
|
|
20
|
+
// tiles of 4 x 6 entries: 24 accumulators and 5 operands of the 32 registers
|
|
21
|
+
constexpr int RI = 4, RJ = 6;
|
|
22
|
+
|
|
23
|
+
template <int I, int J>
|
|
24
|
+
void tile(
|
|
25
|
+
const float* const* a,
|
|
26
|
+
const float* const* b,
|
|
27
|
+
size_t d,
|
|
28
|
+
float* out,
|
|
29
|
+
size_t ldo) {
|
|
30
|
+
float32x4_t acc[I][J];
|
|
31
|
+
for (int i = 0; i < I; i++) {
|
|
32
|
+
for (int j = 0; j < J; j++) {
|
|
33
|
+
acc[i][j] = vdupq_n_f32(0.0f);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
size_t k = 0;
|
|
37
|
+
for (; k + 4 <= d; k += 4) {
|
|
38
|
+
float32x4_t x[I];
|
|
39
|
+
for (int i = 0; i < I; i++) {
|
|
40
|
+
x[i] = vld1q_f32(a[i] + k);
|
|
41
|
+
}
|
|
42
|
+
for (int j = 0; j < J; j++) {
|
|
43
|
+
const float32x4_t y = vld1q_f32(b[j] + k);
|
|
44
|
+
for (int i = 0; i < I; i++) {
|
|
45
|
+
acc[i][j] = vfmaq_f32(acc[i][j], x[i], y);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
for (int i = 0; i < I; i++) {
|
|
50
|
+
for (int j = 0; j < J; j++) {
|
|
51
|
+
float v = vaddvq_f32(acc[i][j]);
|
|
52
|
+
for (size_t t = k; t < d; t++) {
|
|
53
|
+
v = std::fma(a[i][t], b[j][t], v);
|
|
54
|
+
}
|
|
55
|
+
out[i * ldo + j] = v;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
template <int I, int J = RJ>
|
|
61
|
+
void run_tile_cols(
|
|
62
|
+
int nj,
|
|
63
|
+
const float* const* a,
|
|
64
|
+
const float* const* b,
|
|
65
|
+
size_t d,
|
|
66
|
+
float* out,
|
|
67
|
+
size_t ldo) {
|
|
68
|
+
if constexpr (J > 1) {
|
|
69
|
+
if (nj < J) {
|
|
70
|
+
run_tile_cols<I, J - 1>(nj, a, b, d, out, ldo);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
tile<I, J>(a, b, d, out, ldo);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
template <int I = RI>
|
|
78
|
+
void run_tile(
|
|
79
|
+
int ni,
|
|
80
|
+
int nj,
|
|
81
|
+
const float* const* a,
|
|
82
|
+
const float* const* b,
|
|
83
|
+
size_t d,
|
|
84
|
+
float* out,
|
|
85
|
+
size_t ldo) {
|
|
86
|
+
if constexpr (I > 1) {
|
|
87
|
+
if (ni < I) {
|
|
88
|
+
run_tile<I - 1>(ni, nj, a, b, d, out, ldo);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
run_tile_cols<I>(nj, a, b, d, out, ldo);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
} // namespace
|
|
96
|
+
|
|
97
|
+
template <>
|
|
98
|
+
void gram_upper_rows<SIMDLevel::ARM_NEON>(
|
|
99
|
+
const float* const* rows,
|
|
100
|
+
size_t r,
|
|
101
|
+
size_t m,
|
|
102
|
+
size_t d,
|
|
103
|
+
float* out) {
|
|
104
|
+
for (size_t i0 = 0; i0 < r; i0 += RI) {
|
|
105
|
+
const int ni = int(std::min<size_t>(RI, r - i0));
|
|
106
|
+
for (size_t j = i0 + 1; j < m; j += RJ) {
|
|
107
|
+
const int nj = int(std::min<size_t>(RJ, m - j));
|
|
108
|
+
run_tile(ni, nj, rows + i0, rows + j, d, out + i0 * m + j, m);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
} // namespace nndescent
|
|
114
|
+
} // namespace faiss
|
|
@@ -0,0 +1,66 @@
|
|
|
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 <cstddef>
|
|
11
|
+
|
|
12
|
+
#include <faiss/utils/simd_levels.h>
|
|
13
|
+
|
|
14
|
+
namespace faiss {
|
|
15
|
+
namespace nndescent {
|
|
16
|
+
|
|
17
|
+
/** Rows [0, r) of the upper triangle of the Gram matrix of the m vectors
|
|
18
|
+
* rows[0 .. m): out[i * m + j] = <rows[i], rows[j]> for i < r and
|
|
19
|
+
* i < j < m. The Gram matrix of m vectors is the m x m matrix of their inner
|
|
20
|
+
* products. In a local join of DeterministicNNDescent, the first r vectors
|
|
21
|
+
* are the new members, so these are the pairs with at least one new member.
|
|
22
|
+
* The vectors are read in place. The kernel also sets some entries with
|
|
23
|
+
* j <= i. The sums have a fixed order, so the values do not depend on the
|
|
24
|
+
* thread. Defined for AVX2, AVX512 and ARM_NEON. The dispatch uses AVX512 on
|
|
25
|
+
* the higher AVX-512 levels and ARM_NEON on ARM_SVE, and the caller uses BLAS
|
|
26
|
+
* for the other levels. */
|
|
27
|
+
template <SIMDLevel SL>
|
|
28
|
+
void gram_upper_rows(
|
|
29
|
+
const float* const* rows,
|
|
30
|
+
size_t r,
|
|
31
|
+
size_t m,
|
|
32
|
+
size_t d,
|
|
33
|
+
float* out);
|
|
34
|
+
|
|
35
|
+
#ifdef COMPILE_SIMD_AVX2
|
|
36
|
+
template <>
|
|
37
|
+
void gram_upper_rows<SIMDLevel::AVX2>(
|
|
38
|
+
const float* const* rows,
|
|
39
|
+
size_t r,
|
|
40
|
+
size_t m,
|
|
41
|
+
size_t d,
|
|
42
|
+
float* out);
|
|
43
|
+
#endif
|
|
44
|
+
|
|
45
|
+
#ifdef COMPILE_SIMD_AVX512
|
|
46
|
+
template <>
|
|
47
|
+
void gram_upper_rows<SIMDLevel::AVX512>(
|
|
48
|
+
const float* const* rows,
|
|
49
|
+
size_t r,
|
|
50
|
+
size_t m,
|
|
51
|
+
size_t d,
|
|
52
|
+
float* out);
|
|
53
|
+
#endif
|
|
54
|
+
|
|
55
|
+
#ifdef COMPILE_SIMD_ARM_NEON
|
|
56
|
+
template <>
|
|
57
|
+
void gram_upper_rows<SIMDLevel::ARM_NEON>(
|
|
58
|
+
const float* const* rows,
|
|
59
|
+
size_t r,
|
|
60
|
+
size_t m,
|
|
61
|
+
size_t d,
|
|
62
|
+
float* out);
|
|
63
|
+
#endif
|
|
64
|
+
|
|
65
|
+
} // namespace nndescent
|
|
66
|
+
} // namespace faiss
|