faiss 0.2.6 → 0.2.7

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.
Files changed (189) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/ext/faiss/extconf.rb +1 -1
  4. data/lib/faiss/version.rb +1 -1
  5. data/lib/faiss.rb +2 -2
  6. data/vendor/faiss/faiss/AutoTune.cpp +15 -4
  7. data/vendor/faiss/faiss/AutoTune.h +0 -1
  8. data/vendor/faiss/faiss/Clustering.cpp +1 -5
  9. data/vendor/faiss/faiss/Clustering.h +0 -2
  10. data/vendor/faiss/faiss/IVFlib.h +0 -2
  11. data/vendor/faiss/faiss/Index.h +1 -2
  12. data/vendor/faiss/faiss/IndexAdditiveQuantizer.cpp +17 -3
  13. data/vendor/faiss/faiss/IndexAdditiveQuantizer.h +10 -1
  14. data/vendor/faiss/faiss/IndexBinary.h +0 -1
  15. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +2 -1
  16. data/vendor/faiss/faiss/IndexBinaryFlat.h +4 -0
  17. data/vendor/faiss/faiss/IndexBinaryHash.cpp +1 -3
  18. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +273 -48
  19. data/vendor/faiss/faiss/IndexBinaryIVF.h +18 -11
  20. data/vendor/faiss/faiss/IndexFastScan.cpp +13 -10
  21. data/vendor/faiss/faiss/IndexFastScan.h +5 -1
  22. data/vendor/faiss/faiss/IndexFlat.cpp +16 -3
  23. data/vendor/faiss/faiss/IndexFlat.h +1 -1
  24. data/vendor/faiss/faiss/IndexFlatCodes.cpp +5 -0
  25. data/vendor/faiss/faiss/IndexFlatCodes.h +7 -2
  26. data/vendor/faiss/faiss/IndexHNSW.cpp +3 -6
  27. data/vendor/faiss/faiss/IndexHNSW.h +0 -1
  28. data/vendor/faiss/faiss/IndexIDMap.cpp +4 -4
  29. data/vendor/faiss/faiss/IndexIDMap.h +0 -2
  30. data/vendor/faiss/faiss/IndexIVF.cpp +155 -129
  31. data/vendor/faiss/faiss/IndexIVF.h +121 -61
  32. data/vendor/faiss/faiss/IndexIVFAdditiveQuantizer.cpp +2 -2
  33. data/vendor/faiss/faiss/IndexIVFFastScan.cpp +12 -11
  34. data/vendor/faiss/faiss/IndexIVFFastScan.h +6 -1
  35. data/vendor/faiss/faiss/IndexIVFPQ.cpp +221 -165
  36. data/vendor/faiss/faiss/IndexIVFPQ.h +1 -0
  37. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +6 -1
  38. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +0 -2
  39. data/vendor/faiss/faiss/IndexNNDescent.cpp +1 -2
  40. data/vendor/faiss/faiss/IndexNNDescent.h +0 -1
  41. data/vendor/faiss/faiss/IndexNSG.cpp +1 -2
  42. data/vendor/faiss/faiss/IndexPQ.cpp +7 -9
  43. data/vendor/faiss/faiss/IndexRefine.cpp +1 -1
  44. data/vendor/faiss/faiss/IndexReplicas.cpp +3 -4
  45. data/vendor/faiss/faiss/IndexReplicas.h +0 -1
  46. data/vendor/faiss/faiss/IndexRowwiseMinMax.cpp +8 -1
  47. data/vendor/faiss/faiss/IndexRowwiseMinMax.h +7 -0
  48. data/vendor/faiss/faiss/IndexShards.cpp +26 -109
  49. data/vendor/faiss/faiss/IndexShards.h +2 -3
  50. data/vendor/faiss/faiss/IndexShardsIVF.cpp +246 -0
  51. data/vendor/faiss/faiss/IndexShardsIVF.h +42 -0
  52. data/vendor/faiss/faiss/MetaIndexes.cpp +86 -0
  53. data/vendor/faiss/faiss/MetaIndexes.h +29 -0
  54. data/vendor/faiss/faiss/MetricType.h +14 -0
  55. data/vendor/faiss/faiss/VectorTransform.cpp +8 -10
  56. data/vendor/faiss/faiss/VectorTransform.h +1 -3
  57. data/vendor/faiss/faiss/clone_index.cpp +232 -18
  58. data/vendor/faiss/faiss/cppcontrib/SaDecodeKernels.h +25 -3
  59. data/vendor/faiss/faiss/cppcontrib/detail/CoarseBitType.h +7 -0
  60. data/vendor/faiss/faiss/cppcontrib/detail/UintReader.h +78 -0
  61. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-avx2-inl.h +20 -6
  62. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-inl.h +7 -1
  63. data/vendor/faiss/faiss/cppcontrib/sa_decode/Level2-neon-inl.h +21 -7
  64. data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMax-inl.h +7 -0
  65. data/vendor/faiss/faiss/cppcontrib/sa_decode/MinMaxFP16-inl.h +7 -0
  66. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-avx2-inl.h +10 -3
  67. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-inl.h +7 -1
  68. data/vendor/faiss/faiss/cppcontrib/sa_decode/PQ-neon-inl.h +11 -3
  69. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +25 -2
  70. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +76 -29
  71. data/vendor/faiss/faiss/gpu/GpuCloner.h +2 -2
  72. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +14 -13
  73. data/vendor/faiss/faiss/gpu/GpuDistance.h +18 -6
  74. data/vendor/faiss/faiss/gpu/GpuIndex.h +23 -21
  75. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +10 -10
  76. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +11 -12
  77. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +29 -50
  78. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +3 -3
  79. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +8 -8
  80. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +4 -4
  81. data/vendor/faiss/faiss/gpu/impl/IndexUtils.h +2 -5
  82. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +9 -7
  83. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +4 -4
  84. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +2 -2
  85. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +1 -1
  86. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +55 -6
  87. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +20 -6
  88. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +95 -25
  89. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +67 -16
  90. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +4 -4
  91. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +7 -7
  92. data/vendor/faiss/faiss/gpu/test/TestUtils.h +4 -4
  93. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +1 -1
  94. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +6 -0
  95. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +0 -7
  96. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +9 -9
  97. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +1 -1
  98. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +2 -7
  99. data/vendor/faiss/faiss/impl/CodePacker.cpp +67 -0
  100. data/vendor/faiss/faiss/impl/CodePacker.h +71 -0
  101. data/vendor/faiss/faiss/impl/DistanceComputer.h +0 -2
  102. data/vendor/faiss/faiss/impl/HNSW.cpp +3 -7
  103. data/vendor/faiss/faiss/impl/HNSW.h +6 -9
  104. data/vendor/faiss/faiss/impl/IDSelector.cpp +1 -1
  105. data/vendor/faiss/faiss/impl/IDSelector.h +39 -1
  106. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +62 -51
  107. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +11 -12
  108. data/vendor/faiss/faiss/impl/NNDescent.cpp +3 -9
  109. data/vendor/faiss/faiss/impl/NNDescent.h +10 -10
  110. data/vendor/faiss/faiss/impl/NSG.cpp +1 -6
  111. data/vendor/faiss/faiss/impl/NSG.h +4 -7
  112. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +1 -15
  113. data/vendor/faiss/faiss/impl/PolysemousTraining.h +11 -10
  114. data/vendor/faiss/faiss/impl/ProductAdditiveQuantizer.cpp +0 -7
  115. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +25 -12
  116. data/vendor/faiss/faiss/impl/ProductQuantizer.h +2 -4
  117. data/vendor/faiss/faiss/impl/Quantizer.h +6 -3
  118. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +796 -174
  119. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +16 -8
  120. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +3 -5
  121. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +4 -4
  122. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +3 -3
  123. data/vendor/faiss/faiss/impl/ThreadedIndex.h +4 -4
  124. data/vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h +291 -0
  125. data/vendor/faiss/faiss/impl/code_distance/code_distance-generic.h +74 -0
  126. data/vendor/faiss/faiss/impl/code_distance/code_distance.h +123 -0
  127. data/vendor/faiss/faiss/impl/code_distance/code_distance_avx512.h +102 -0
  128. data/vendor/faiss/faiss/impl/index_read.cpp +13 -10
  129. data/vendor/faiss/faiss/impl/index_write.cpp +3 -4
  130. data/vendor/faiss/faiss/impl/kmeans1d.cpp +0 -1
  131. data/vendor/faiss/faiss/impl/kmeans1d.h +3 -3
  132. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +1 -1
  133. data/vendor/faiss/faiss/impl/platform_macros.h +61 -0
  134. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +48 -4
  135. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +18 -4
  136. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +2 -2
  137. data/vendor/faiss/faiss/index_factory.cpp +8 -10
  138. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +29 -12
  139. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +8 -2
  140. data/vendor/faiss/faiss/invlists/DirectMap.cpp +1 -1
  141. data/vendor/faiss/faiss/invlists/DirectMap.h +2 -4
  142. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +118 -18
  143. data/vendor/faiss/faiss/invlists/InvertedLists.h +44 -4
  144. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +3 -3
  145. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +1 -1
  146. data/vendor/faiss/faiss/python/python_callbacks.cpp +1 -1
  147. data/vendor/faiss/faiss/python/python_callbacks.h +1 -1
  148. data/vendor/faiss/faiss/utils/AlignedTable.h +3 -1
  149. data/vendor/faiss/faiss/utils/Heap.cpp +139 -3
  150. data/vendor/faiss/faiss/utils/Heap.h +35 -1
  151. data/vendor/faiss/faiss/utils/approx_topk/approx_topk.h +84 -0
  152. data/vendor/faiss/faiss/utils/approx_topk/avx2-inl.h +196 -0
  153. data/vendor/faiss/faiss/utils/approx_topk/generic.h +138 -0
  154. data/vendor/faiss/faiss/utils/approx_topk/mode.h +34 -0
  155. data/vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h +367 -0
  156. data/vendor/faiss/faiss/utils/distances.cpp +61 -7
  157. data/vendor/faiss/faiss/utils/distances.h +11 -0
  158. data/vendor/faiss/faiss/utils/distances_fused/avx512.cpp +346 -0
  159. data/vendor/faiss/faiss/utils/distances_fused/avx512.h +36 -0
  160. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp +42 -0
  161. data/vendor/faiss/faiss/utils/distances_fused/distances_fused.h +40 -0
  162. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp +352 -0
  163. data/vendor/faiss/faiss/utils/distances_fused/simdlib_based.h +32 -0
  164. data/vendor/faiss/faiss/utils/distances_simd.cpp +515 -327
  165. data/vendor/faiss/faiss/utils/extra_distances-inl.h +17 -1
  166. data/vendor/faiss/faiss/utils/extra_distances.cpp +37 -8
  167. data/vendor/faiss/faiss/utils/extra_distances.h +2 -1
  168. data/vendor/faiss/faiss/utils/fp16-fp16c.h +7 -0
  169. data/vendor/faiss/faiss/utils/fp16-inl.h +7 -0
  170. data/vendor/faiss/faiss/utils/fp16.h +7 -0
  171. data/vendor/faiss/faiss/utils/hamming-inl.h +0 -456
  172. data/vendor/faiss/faiss/utils/hamming.cpp +104 -120
  173. data/vendor/faiss/faiss/utils/hamming.h +21 -10
  174. data/vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h +535 -0
  175. data/vendor/faiss/faiss/utils/hamming_distance/common.h +48 -0
  176. data/vendor/faiss/faiss/utils/hamming_distance/generic-inl.h +519 -0
  177. data/vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h +26 -0
  178. data/vendor/faiss/faiss/utils/hamming_distance/neon-inl.h +614 -0
  179. data/vendor/faiss/faiss/utils/partitioning.cpp +21 -25
  180. data/vendor/faiss/faiss/utils/simdlib_avx2.h +344 -3
  181. data/vendor/faiss/faiss/utils/simdlib_emulated.h +390 -0
  182. data/vendor/faiss/faiss/utils/simdlib_neon.h +655 -130
  183. data/vendor/faiss/faiss/utils/sorting.cpp +692 -0
  184. data/vendor/faiss/faiss/utils/sorting.h +71 -0
  185. data/vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h +165 -0
  186. data/vendor/faiss/faiss/utils/utils.cpp +4 -176
  187. data/vendor/faiss/faiss/utils/utils.h +2 -9
  188. metadata +29 -3
  189. data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +0 -26
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ namespace faiss {
13
+
14
+ /** Indirect sort of a floating-point array
15
+ *
16
+ * @param n size of the array
17
+ * @param vals array to sort, size n
18
+ * @param perm output: permutation of [0..n-1], st.
19
+ * vals[perm[i + 1]] >= vals[perm[i]]
20
+ */
21
+ void fvec_argsort(size_t n, const float* vals, size_t* perm);
22
+
23
+ /** Same as fvec_argsort, parallelized */
24
+ void fvec_argsort_parallel(size_t n, const float* vals, size_t* perm);
25
+
26
+ /// increase verbosity of the bucket_sort functions
27
+ FAISS_API extern int bucket_sort_verbose;
28
+
29
+ /** Bucket sort of a list of values
30
+ *
31
+ * @param vals values to sort, size nval, max value nbucket - 1
32
+ * @param lims output limits of buckets, size nbucket + 1
33
+ * @param perm output buckets, the elements of bucket
34
+ * i are in perm[lims[i]:lims[i + 1]]
35
+ * @param nt number of threads (0 = pure sequential code)
36
+ */
37
+ void bucket_sort(
38
+ size_t nval,
39
+ const uint64_t* vals,
40
+ uint64_t nbucket,
41
+ int64_t* lims,
42
+ int64_t* perm,
43
+ int nt = 0);
44
+
45
+ /** in-place bucket sort (with attention to memory=>int32)
46
+ * on input the values are in a nrow * col matrix
47
+ * we want to store the row numbers in the output.
48
+ *
49
+ * @param vals positive values to sort, size nrow * ncol,
50
+ * max value nbucket - 1
51
+ * @param lims output limits of buckets, size nbucket + 1
52
+ * @param nt number of threads (0 = pure sequential code)
53
+ */
54
+ void matrix_bucket_sort_inplace(
55
+ size_t nrow,
56
+ size_t ncol,
57
+ int32_t* vals,
58
+ int32_t nbucket,
59
+ int64_t* lims,
60
+ int nt = 0);
61
+
62
+ /// same with int64 elements
63
+ void matrix_bucket_sort_inplace(
64
+ size_t nrow,
65
+ size_t ncol,
66
+ int64_t* vals,
67
+ int64_t nbucket,
68
+ int64_t* lims,
69
+ int nt = 0);
70
+
71
+ } // namespace faiss
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its 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
+ // This file contains transposing kernels for AVX2 for
11
+ // tiny float/int32 matrices, such as 8x2.
12
+
13
+ #ifdef __AVX2__
14
+
15
+ #include <immintrin.h>
16
+
17
+ namespace faiss {
18
+
19
+ // 8x2 -> 2x8
20
+ inline void transpose_8x2(
21
+ const __m256 i0,
22
+ const __m256 i1,
23
+ __m256& o0,
24
+ __m256& o1) {
25
+ // say, we have the following as in input:
26
+ // i0: 00 01 10 11 20 21 30 31
27
+ // i1: 40 41 50 51 60 61 70 71
28
+
29
+ // 00 01 10 11 40 41 50 51
30
+ const __m256 r0 = _mm256_permute2f128_ps(i0, i1, _MM_SHUFFLE(0, 2, 0, 0));
31
+ // 20 21 30 31 60 61 70 71
32
+ const __m256 r1 = _mm256_permute2f128_ps(i0, i1, _MM_SHUFFLE(0, 3, 0, 1));
33
+
34
+ // 00 10 20 30 40 50 60 70
35
+ o0 = _mm256_shuffle_ps(r0, r1, _MM_SHUFFLE(2, 0, 2, 0));
36
+ // 01 11 21 31 41 51 61 71
37
+ o1 = _mm256_shuffle_ps(r0, r1, _MM_SHUFFLE(3, 1, 3, 1));
38
+ }
39
+
40
+ // 8x4 -> 4x8
41
+ inline void transpose_8x4(
42
+ const __m256 i0,
43
+ const __m256 i1,
44
+ const __m256 i2,
45
+ const __m256 i3,
46
+ __m256& o0,
47
+ __m256& o1,
48
+ __m256& o2,
49
+ __m256& o3) {
50
+ // say, we have the following as an input:
51
+ // i0: 00 01 02 03 10 11 12 13
52
+ // i1: 20 21 22 23 30 31 32 33
53
+ // i2: 40 41 42 43 50 51 52 53
54
+ // i3: 60 61 62 63 70 71 72 73
55
+
56
+ // 00 01 02 03 40 41 42 43
57
+ const __m256 r0 = _mm256_permute2f128_ps(i0, i2, _MM_SHUFFLE(0, 2, 0, 0));
58
+ // 20 21 22 23 60 61 62 63
59
+ const __m256 r1 = _mm256_permute2f128_ps(i1, i3, _MM_SHUFFLE(0, 2, 0, 0));
60
+ // 10 11 12 13 50 51 52 53
61
+ const __m256 r2 = _mm256_permute2f128_ps(i0, i2, _MM_SHUFFLE(0, 3, 0, 1));
62
+ // 30 31 32 33 70 71 72 73
63
+ const __m256 r3 = _mm256_permute2f128_ps(i1, i3, _MM_SHUFFLE(0, 3, 0, 1));
64
+
65
+ // 00 02 10 12 40 42 50 52
66
+ const __m256 t0 = _mm256_shuffle_ps(r0, r2, _MM_SHUFFLE(2, 0, 2, 0));
67
+ // 01 03 11 13 41 43 51 53
68
+ const __m256 t1 = _mm256_shuffle_ps(r0, r2, _MM_SHUFFLE(3, 1, 3, 1));
69
+ // 20 22 30 32 60 62 70 72
70
+ const __m256 t2 = _mm256_shuffle_ps(r1, r3, _MM_SHUFFLE(2, 0, 2, 0));
71
+ // 21 23 31 33 61 63 71 73
72
+ const __m256 t3 = _mm256_shuffle_ps(r1, r3, _MM_SHUFFLE(3, 1, 3, 1));
73
+
74
+ // 00 10 20 30 40 50 60 70
75
+ o0 = _mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(2, 0, 2, 0));
76
+ // 01 11 21 31 41 51 61 71
77
+ o1 = _mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(2, 0, 2, 0));
78
+ // 02 12 22 32 42 52 62 72
79
+ o2 = _mm256_shuffle_ps(t0, t2, _MM_SHUFFLE(3, 1, 3, 1));
80
+ // 03 13 23 33 43 53 63 73
81
+ o3 = _mm256_shuffle_ps(t1, t3, _MM_SHUFFLE(3, 1, 3, 1));
82
+ }
83
+
84
+ inline void transpose_8x8(
85
+ const __m256 i0,
86
+ const __m256 i1,
87
+ const __m256 i2,
88
+ const __m256 i3,
89
+ const __m256 i4,
90
+ const __m256 i5,
91
+ const __m256 i6,
92
+ const __m256 i7,
93
+ __m256& o0,
94
+ __m256& o1,
95
+ __m256& o2,
96
+ __m256& o3,
97
+ __m256& o4,
98
+ __m256& o5,
99
+ __m256& o6,
100
+ __m256& o7) {
101
+ // say, we have the following as an input:
102
+ // i0: 00 01 02 03 04 05 06 07
103
+ // i1: 10 11 12 13 14 15 16 17
104
+ // i2: 20 21 22 23 24 25 26 27
105
+ // i3: 30 31 32 33 34 35 36 37
106
+ // i4: 40 41 42 43 44 45 46 47
107
+ // i5: 50 51 52 53 54 55 56 57
108
+ // i6: 60 61 62 63 64 65 66 67
109
+ // i7: 70 71 72 73 74 75 76 77
110
+
111
+ // 00 10 01 11 04 14 05 15
112
+ const __m256 r0 = _mm256_unpacklo_ps(i0, i1);
113
+ // 02 12 03 13 06 16 07 17
114
+ const __m256 r1 = _mm256_unpackhi_ps(i0, i1);
115
+ // 20 30 21 31 24 34 25 35
116
+ const __m256 r2 = _mm256_unpacklo_ps(i2, i3);
117
+ // 22 32 23 33 26 36 27 37
118
+ const __m256 r3 = _mm256_unpackhi_ps(i2, i3);
119
+ // 40 50 41 51 44 54 45 55
120
+ const __m256 r4 = _mm256_unpacklo_ps(i4, i5);
121
+ // 42 52 43 53 46 56 47 57
122
+ const __m256 r5 = _mm256_unpackhi_ps(i4, i5);
123
+ // 60 70 61 71 64 74 65 75
124
+ const __m256 r6 = _mm256_unpacklo_ps(i6, i7);
125
+ // 62 72 63 73 66 76 67 77
126
+ const __m256 r7 = _mm256_unpackhi_ps(i6, i7);
127
+
128
+ // 00 10 20 30 04 14 24 34
129
+ const __m256 rr0 = _mm256_shuffle_ps(r0, r2, _MM_SHUFFLE(1, 0, 1, 0));
130
+ // 01 11 21 31 05 15 25 35
131
+ const __m256 rr1 = _mm256_shuffle_ps(r0, r2, _MM_SHUFFLE(3, 2, 3, 2));
132
+ // 02 12 22 32 06 16 26 36
133
+ const __m256 rr2 = _mm256_shuffle_ps(r1, r3, _MM_SHUFFLE(1, 0, 1, 0));
134
+ // 03 13 23 33 07 17 27 37
135
+ const __m256 rr3 = _mm256_shuffle_ps(r1, r3, _MM_SHUFFLE(3, 2, 3, 2));
136
+ // 40 50 60 70 44 54 64 74
137
+ const __m256 rr4 = _mm256_shuffle_ps(r4, r6, _MM_SHUFFLE(1, 0, 1, 0));
138
+ // 41 51 61 71 45 55 65 75
139
+ const __m256 rr5 = _mm256_shuffle_ps(r4, r6, _MM_SHUFFLE(3, 2, 3, 2));
140
+ // 42 52 62 72 46 56 66 76
141
+ const __m256 rr6 = _mm256_shuffle_ps(r5, r7, _MM_SHUFFLE(1, 0, 1, 0));
142
+ // 43 53 63 73 47 57 67 77
143
+ const __m256 rr7 = _mm256_shuffle_ps(r5, r7, _MM_SHUFFLE(3, 2, 3, 2));
144
+
145
+ // 00 10 20 30 40 50 60 70
146
+ o0 = _mm256_permute2f128_ps(rr0, rr4, 0x20);
147
+ // 01 11 21 31 41 51 61 71
148
+ o1 = _mm256_permute2f128_ps(rr1, rr5, 0x20);
149
+ // 02 12 22 32 42 52 62 72
150
+ o2 = _mm256_permute2f128_ps(rr2, rr6, 0x20);
151
+ // 03 13 23 33 43 53 63 73
152
+ o3 = _mm256_permute2f128_ps(rr3, rr7, 0x20);
153
+ // 04 14 24 34 44 54 64 74
154
+ o4 = _mm256_permute2f128_ps(rr0, rr4, 0x31);
155
+ // 05 15 25 35 45 55 65 75
156
+ o5 = _mm256_permute2f128_ps(rr1, rr5, 0x31);
157
+ // 06 16 26 36 46 56 66 76
158
+ o6 = _mm256_permute2f128_ps(rr2, rr6, 0x31);
159
+ // 07 17 27 37 47 57 67 77
160
+ o7 = _mm256_permute2f128_ps(rr3, rr7, 0x31);
161
+ }
162
+
163
+ } // namespace faiss
164
+
165
+ #endif
@@ -423,185 +423,13 @@ void bincode_hist(size_t n, size_t nbits, const uint8_t* codes, int* hist) {
423
423
  }
424
424
  }
425
425
 
426
- size_t ivec_checksum(size_t n, const int* a) {
426
+ size_t ivec_checksum(size_t n, const int32_t* asigned) {
427
+ const uint32_t* a = reinterpret_cast<const uint32_t*>(asigned);
427
428
  size_t cs = 112909;
428
- while (n--)
429
+ while (n--) {
429
430
  cs = cs * 65713 + a[n] * 1686049;
430
- return cs;
431
- }
432
-
433
- namespace {
434
- struct ArgsortComparator {
435
- const float* vals;
436
- bool operator()(const size_t a, const size_t b) const {
437
- return vals[a] < vals[b];
438
- }
439
- };
440
-
441
- struct SegmentS {
442
- size_t i0; // begin pointer in the permutation array
443
- size_t i1; // end
444
- size_t len() const {
445
- return i1 - i0;
446
- }
447
- };
448
-
449
- // see https://en.wikipedia.org/wiki/Merge_algorithm#Parallel_merge
450
- // extended to > 1 merge thread
451
-
452
- // merges 2 ranges that should be consecutive on the source into
453
- // the union of the two on the destination
454
- template <typename T>
455
- void parallel_merge(
456
- const T* src,
457
- T* dst,
458
- SegmentS& s1,
459
- SegmentS& s2,
460
- int nt,
461
- const ArgsortComparator& comp) {
462
- if (s2.len() > s1.len()) { // make sure that s1 larger than s2
463
- std::swap(s1, s2);
464
- }
465
-
466
- // compute sub-ranges for each thread
467
- std::vector<SegmentS> s1s(nt), s2s(nt), sws(nt);
468
- s2s[0].i0 = s2.i0;
469
- s2s[nt - 1].i1 = s2.i1;
470
-
471
- // not sure parallel actually helps here
472
- #pragma omp parallel for num_threads(nt)
473
- for (int t = 0; t < nt; t++) {
474
- s1s[t].i0 = s1.i0 + s1.len() * t / nt;
475
- s1s[t].i1 = s1.i0 + s1.len() * (t + 1) / nt;
476
-
477
- if (t + 1 < nt) {
478
- T pivot = src[s1s[t].i1];
479
- size_t i0 = s2.i0, i1 = s2.i1;
480
- while (i0 + 1 < i1) {
481
- size_t imed = (i1 + i0) / 2;
482
- if (comp(pivot, src[imed])) {
483
- i1 = imed;
484
- } else {
485
- i0 = imed;
486
- }
487
- }
488
- s2s[t].i1 = s2s[t + 1].i0 = i1;
489
- }
490
- }
491
- s1.i0 = std::min(s1.i0, s2.i0);
492
- s1.i1 = std::max(s1.i1, s2.i1);
493
- s2 = s1;
494
- sws[0].i0 = s1.i0;
495
- for (int t = 0; t < nt; t++) {
496
- sws[t].i1 = sws[t].i0 + s1s[t].len() + s2s[t].len();
497
- if (t + 1 < nt) {
498
- sws[t + 1].i0 = sws[t].i1;
499
- }
500
- }
501
- assert(sws[nt - 1].i1 == s1.i1);
502
-
503
- // do the actual merging
504
- #pragma omp parallel for num_threads(nt)
505
- for (int t = 0; t < nt; t++) {
506
- SegmentS sw = sws[t];
507
- SegmentS s1t = s1s[t];
508
- SegmentS s2t = s2s[t];
509
- if (s1t.i0 < s1t.i1 && s2t.i0 < s2t.i1) {
510
- for (;;) {
511
- // assert (sw.len() == s1t.len() + s2t.len());
512
- if (comp(src[s1t.i0], src[s2t.i0])) {
513
- dst[sw.i0++] = src[s1t.i0++];
514
- if (s1t.i0 == s1t.i1)
515
- break;
516
- } else {
517
- dst[sw.i0++] = src[s2t.i0++];
518
- if (s2t.i0 == s2t.i1)
519
- break;
520
- }
521
- }
522
- }
523
- if (s1t.len() > 0) {
524
- assert(s1t.len() == sw.len());
525
- memcpy(dst + sw.i0, src + s1t.i0, s1t.len() * sizeof(dst[0]));
526
- } else if (s2t.len() > 0) {
527
- assert(s2t.len() == sw.len());
528
- memcpy(dst + sw.i0, src + s2t.i0, s2t.len() * sizeof(dst[0]));
529
- }
530
431
  }
531
- }
532
-
533
- }; // namespace
534
-
535
- void fvec_argsort(size_t n, const float* vals, size_t* perm) {
536
- for (size_t i = 0; i < n; i++)
537
- perm[i] = i;
538
- ArgsortComparator comp = {vals};
539
- std::sort(perm, perm + n, comp);
540
- }
541
-
542
- void fvec_argsort_parallel(size_t n, const float* vals, size_t* perm) {
543
- size_t* perm2 = new size_t[n];
544
- // 2 result tables, during merging, flip between them
545
- size_t *permB = perm2, *permA = perm;
546
-
547
- int nt = omp_get_max_threads();
548
- { // prepare correct permutation so that the result ends in perm
549
- // at final iteration
550
- int nseg = nt;
551
- while (nseg > 1) {
552
- nseg = (nseg + 1) / 2;
553
- std::swap(permA, permB);
554
- }
555
- }
556
-
557
- #pragma omp parallel
558
- for (size_t i = 0; i < n; i++)
559
- permA[i] = i;
560
-
561
- ArgsortComparator comp = {vals};
562
-
563
- std::vector<SegmentS> segs(nt);
564
-
565
- // independent sorts
566
- #pragma omp parallel for
567
- for (int t = 0; t < nt; t++) {
568
- size_t i0 = t * n / nt;
569
- size_t i1 = (t + 1) * n / nt;
570
- SegmentS seg = {i0, i1};
571
- std::sort(permA + seg.i0, permA + seg.i1, comp);
572
- segs[t] = seg;
573
- }
574
- int prev_nested = omp_get_nested();
575
- omp_set_nested(1);
576
-
577
- int nseg = nt;
578
- while (nseg > 1) {
579
- int nseg1 = (nseg + 1) / 2;
580
- int sub_nt = nseg % 2 == 0 ? nt : nt - 1;
581
- int sub_nseg1 = nseg / 2;
582
-
583
- #pragma omp parallel for num_threads(nseg1)
584
- for (int s = 0; s < nseg; s += 2) {
585
- if (s + 1 == nseg) { // otherwise isolated segment
586
- memcpy(permB + segs[s].i0,
587
- permA + segs[s].i0,
588
- segs[s].len() * sizeof(size_t));
589
- } else {
590
- int t0 = s * sub_nt / sub_nseg1;
591
- int t1 = (s + 1) * sub_nt / sub_nseg1;
592
- printf("merge %d %d, %d threads\n", s, s + 1, t1 - t0);
593
- parallel_merge(
594
- permA, permB, segs[s], segs[s + 1], t1 - t0, comp);
595
- }
596
- }
597
- for (int s = 0; s < nseg; s += 2)
598
- segs[s / 2] = segs[s];
599
- nseg = nseg1;
600
- std::swap(permA, permB);
601
- }
602
- assert(permA == perm);
603
- omp_set_nested(prev_nested);
604
- delete[] perm2;
432
+ return cs;
605
433
  }
606
434
 
607
435
  const float* fvecs_maybe_subsample(
@@ -19,10 +19,7 @@
19
19
  #include <stdint.h>
20
20
  #include <string>
21
21
 
22
- #ifdef _MSC_VER
23
- #define strtok_r strtok_s
24
- #endif // _MSC_VER
25
-
22
+ #include <faiss/impl/platform_macros.h>
26
23
  #include <faiss/utils/Heap.h>
27
24
 
28
25
  namespace faiss {
@@ -113,10 +110,6 @@ double imbalance_factor(int n, int k, const int64_t* assign);
113
110
  /// same, takes a histogram as input
114
111
  double imbalance_factor(int k, const int* hist);
115
112
 
116
- void fvec_argsort(size_t n, const float* vals, size_t* perm);
117
-
118
- void fvec_argsort_parallel(size_t n, const float* vals, size_t* perm);
119
-
120
113
  /// compute histogram on v
121
114
  int ivec_hist(size_t n, const int* v, int vmax, int* hist);
122
115
 
@@ -128,7 +121,7 @@ int ivec_hist(size_t n, const int* v, int vmax, int* hist);
128
121
  void bincode_hist(size_t n, size_t nbits, const uint8_t* codes, int* hist);
129
122
 
130
123
  /// compute a checksum on a table.
131
- size_t ivec_checksum(size_t n, const int* a);
124
+ size_t ivec_checksum(size_t n, const int32_t* a);
132
125
 
133
126
  /** random subsamples a set of vectors if there are too many of them
134
127
  *
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faiss
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-11 00:00:00.000000000 Z
11
+ date: 2023-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rice
@@ -139,6 +139,8 @@ files:
139
139
  - vendor/faiss/faiss/IndexScalarQuantizer.h
140
140
  - vendor/faiss/faiss/IndexShards.cpp
141
141
  - vendor/faiss/faiss/IndexShards.h
142
+ - vendor/faiss/faiss/IndexShardsIVF.cpp
143
+ - vendor/faiss/faiss/IndexShardsIVF.h
142
144
  - vendor/faiss/faiss/MatrixStats.cpp
143
145
  - vendor/faiss/faiss/MatrixStats.h
144
146
  - vendor/faiss/faiss/MetaIndexes.cpp
@@ -163,7 +165,6 @@ files:
163
165
  - vendor/faiss/faiss/gpu/GpuAutoTune.h
164
166
  - vendor/faiss/faiss/gpu/GpuCloner.cpp
165
167
  - vendor/faiss/faiss/gpu/GpuCloner.h
166
- - vendor/faiss/faiss/gpu/GpuClonerOptions.cpp
167
168
  - vendor/faiss/faiss/gpu/GpuClonerOptions.h
168
169
  - vendor/faiss/faiss/gpu/GpuDistance.h
169
170
  - vendor/faiss/faiss/gpu/GpuFaissAssert.h
@@ -210,6 +211,8 @@ files:
210
211
  - vendor/faiss/faiss/impl/AdditiveQuantizer.h
211
212
  - vendor/faiss/faiss/impl/AuxIndexStructures.cpp
212
213
  - vendor/faiss/faiss/impl/AuxIndexStructures.h
214
+ - vendor/faiss/faiss/impl/CodePacker.cpp
215
+ - vendor/faiss/faiss/impl/CodePacker.h
213
216
  - vendor/faiss/faiss/impl/DistanceComputer.h
214
217
  - vendor/faiss/faiss/impl/FaissAssert.h
215
218
  - vendor/faiss/faiss/impl/FaissException.cpp
@@ -240,6 +243,10 @@ files:
240
243
  - vendor/faiss/faiss/impl/ScalarQuantizer.h
241
244
  - vendor/faiss/faiss/impl/ThreadedIndex-inl.h
242
245
  - vendor/faiss/faiss/impl/ThreadedIndex.h
246
+ - vendor/faiss/faiss/impl/code_distance/code_distance-avx2.h
247
+ - vendor/faiss/faiss/impl/code_distance/code_distance-generic.h
248
+ - vendor/faiss/faiss/impl/code_distance/code_distance.h
249
+ - vendor/faiss/faiss/impl/code_distance/code_distance_avx512.h
243
250
  - vendor/faiss/faiss/impl/index_read.cpp
244
251
  - vendor/faiss/faiss/impl/index_write.cpp
245
252
  - vendor/faiss/faiss/impl/io.cpp
@@ -275,8 +282,19 @@ files:
275
282
  - vendor/faiss/faiss/utils/Heap.h
276
283
  - vendor/faiss/faiss/utils/WorkerThread.cpp
277
284
  - vendor/faiss/faiss/utils/WorkerThread.h
285
+ - vendor/faiss/faiss/utils/approx_topk/approx_topk.h
286
+ - vendor/faiss/faiss/utils/approx_topk/avx2-inl.h
287
+ - vendor/faiss/faiss/utils/approx_topk/generic.h
288
+ - vendor/faiss/faiss/utils/approx_topk/mode.h
289
+ - vendor/faiss/faiss/utils/approx_topk_hamming/approx_topk_hamming.h
278
290
  - vendor/faiss/faiss/utils/distances.cpp
279
291
  - vendor/faiss/faiss/utils/distances.h
292
+ - vendor/faiss/faiss/utils/distances_fused/avx512.cpp
293
+ - vendor/faiss/faiss/utils/distances_fused/avx512.h
294
+ - vendor/faiss/faiss/utils/distances_fused/distances_fused.cpp
295
+ - vendor/faiss/faiss/utils/distances_fused/distances_fused.h
296
+ - vendor/faiss/faiss/utils/distances_fused/simdlib_based.cpp
297
+ - vendor/faiss/faiss/utils/distances_fused/simdlib_based.h
280
298
  - vendor/faiss/faiss/utils/distances_simd.cpp
281
299
  - vendor/faiss/faiss/utils/extra_distances-inl.h
282
300
  - vendor/faiss/faiss/utils/extra_distances.cpp
@@ -287,6 +305,11 @@ files:
287
305
  - vendor/faiss/faiss/utils/hamming-inl.h
288
306
  - vendor/faiss/faiss/utils/hamming.cpp
289
307
  - vendor/faiss/faiss/utils/hamming.h
308
+ - vendor/faiss/faiss/utils/hamming_distance/avx2-inl.h
309
+ - vendor/faiss/faiss/utils/hamming_distance/common.h
310
+ - vendor/faiss/faiss/utils/hamming_distance/generic-inl.h
311
+ - vendor/faiss/faiss/utils/hamming_distance/hamdis-inl.h
312
+ - vendor/faiss/faiss/utils/hamming_distance/neon-inl.h
290
313
  - vendor/faiss/faiss/utils/ordered_key_value.h
291
314
  - vendor/faiss/faiss/utils/partitioning.cpp
292
315
  - vendor/faiss/faiss/utils/partitioning.h
@@ -298,6 +321,9 @@ files:
298
321
  - vendor/faiss/faiss/utils/simdlib_avx2.h
299
322
  - vendor/faiss/faiss/utils/simdlib_emulated.h
300
323
  - vendor/faiss/faiss/utils/simdlib_neon.h
324
+ - vendor/faiss/faiss/utils/sorting.cpp
325
+ - vendor/faiss/faiss/utils/sorting.h
326
+ - vendor/faiss/faiss/utils/transpose/transpose-avx2-inl.h
301
327
  - vendor/faiss/faiss/utils/utils.cpp
302
328
  - vendor/faiss/faiss/utils/utils.h
303
329
  homepage: https://github.com/ankane/faiss-ruby
@@ -1,26 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its 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/gpu/GpuClonerOptions.h>
9
-
10
- namespace faiss {
11
- namespace gpu {
12
-
13
- GpuClonerOptions::GpuClonerOptions()
14
- : indicesOptions(INDICES_64_BIT),
15
- useFloat16CoarseQuantizer(false),
16
- useFloat16(false),
17
- usePrecomputed(false),
18
- reserveVecs(0),
19
- storeTransposed(false),
20
- verbose(false) {}
21
-
22
- GpuMultipleClonerOptions::GpuMultipleClonerOptions()
23
- : shard(false), shard_type(1) {}
24
-
25
- } // namespace gpu
26
- } // namespace faiss