faiss 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) 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/vendor/faiss/benchs/bench_6bit_codec.cpp +80 -0
  6. data/vendor/faiss/c_api/AutoTune_c.h +2 -0
  7. data/vendor/faiss/c_api/IndexShards_c.cpp +0 -6
  8. data/vendor/faiss/c_api/IndexShards_c.h +1 -4
  9. data/vendor/faiss/c_api/gpu/GpuAutoTune_c.cpp +4 -2
  10. data/vendor/faiss/c_api/gpu/GpuClonerOptions_c.cpp +1 -1
  11. data/vendor/faiss/c_api/gpu/GpuIndex_c.cpp +1 -1
  12. data/vendor/faiss/c_api/gpu/GpuResources_c.cpp +1 -1
  13. data/vendor/faiss/c_api/gpu/StandardGpuResources_c.cpp +1 -1
  14. data/vendor/faiss/demos/demo_imi_flat.cpp +5 -2
  15. data/vendor/faiss/demos/demo_imi_pq.cpp +6 -2
  16. data/vendor/faiss/demos/demo_ivfpq_indexing.cpp +7 -2
  17. data/vendor/faiss/{AutoTune.cpp → faiss/AutoTune.cpp} +9 -9
  18. data/vendor/faiss/{AutoTune.h → faiss/AutoTune.h} +0 -0
  19. data/vendor/faiss/{Clustering.cpp → faiss/Clustering.cpp} +13 -12
  20. data/vendor/faiss/{Clustering.h → faiss/Clustering.h} +0 -0
  21. data/vendor/faiss/{DirectMap.cpp → faiss/DirectMap.cpp} +0 -0
  22. data/vendor/faiss/{DirectMap.h → faiss/DirectMap.h} +0 -0
  23. data/vendor/faiss/{IVFlib.cpp → faiss/IVFlib.cpp} +86 -11
  24. data/vendor/faiss/{IVFlib.h → faiss/IVFlib.h} +26 -8
  25. data/vendor/faiss/{Index.cpp → faiss/Index.cpp} +0 -0
  26. data/vendor/faiss/{Index.h → faiss/Index.h} +1 -1
  27. data/vendor/faiss/{Index2Layer.cpp → faiss/Index2Layer.cpp} +12 -11
  28. data/vendor/faiss/{Index2Layer.h → faiss/Index2Layer.h} +0 -0
  29. data/vendor/faiss/{IndexBinary.cpp → faiss/IndexBinary.cpp} +2 -1
  30. data/vendor/faiss/{IndexBinary.h → faiss/IndexBinary.h} +0 -0
  31. data/vendor/faiss/{IndexBinaryFlat.cpp → faiss/IndexBinaryFlat.cpp} +0 -0
  32. data/vendor/faiss/{IndexBinaryFlat.h → faiss/IndexBinaryFlat.h} +0 -0
  33. data/vendor/faiss/{IndexBinaryFromFloat.cpp → faiss/IndexBinaryFromFloat.cpp} +1 -0
  34. data/vendor/faiss/{IndexBinaryFromFloat.h → faiss/IndexBinaryFromFloat.h} +0 -0
  35. data/vendor/faiss/{IndexBinaryHNSW.cpp → faiss/IndexBinaryHNSW.cpp} +1 -2
  36. data/vendor/faiss/{IndexBinaryHNSW.h → faiss/IndexBinaryHNSW.h} +0 -0
  37. data/vendor/faiss/{IndexBinaryHash.cpp → faiss/IndexBinaryHash.cpp} +16 -7
  38. data/vendor/faiss/{IndexBinaryHash.h → faiss/IndexBinaryHash.h} +2 -1
  39. data/vendor/faiss/{IndexBinaryIVF.cpp → faiss/IndexBinaryIVF.cpp} +10 -16
  40. data/vendor/faiss/{IndexBinaryIVF.h → faiss/IndexBinaryIVF.h} +1 -1
  41. data/vendor/faiss/{IndexFlat.cpp → faiss/IndexFlat.cpp} +0 -0
  42. data/vendor/faiss/{IndexFlat.h → faiss/IndexFlat.h} +0 -0
  43. data/vendor/faiss/{IndexHNSW.cpp → faiss/IndexHNSW.cpp} +63 -32
  44. data/vendor/faiss/{IndexHNSW.h → faiss/IndexHNSW.h} +0 -0
  45. data/vendor/faiss/{IndexIVF.cpp → faiss/IndexIVF.cpp} +129 -46
  46. data/vendor/faiss/{IndexIVF.h → faiss/IndexIVF.h} +7 -3
  47. data/vendor/faiss/{IndexIVFFlat.cpp → faiss/IndexIVFFlat.cpp} +6 -5
  48. data/vendor/faiss/{IndexIVFFlat.h → faiss/IndexIVFFlat.h} +0 -0
  49. data/vendor/faiss/{IndexIVFPQ.cpp → faiss/IndexIVFPQ.cpp} +9 -8
  50. data/vendor/faiss/{IndexIVFPQ.h → faiss/IndexIVFPQ.h} +4 -2
  51. data/vendor/faiss/{IndexIVFPQR.cpp → faiss/IndexIVFPQR.cpp} +3 -1
  52. data/vendor/faiss/{IndexIVFPQR.h → faiss/IndexIVFPQR.h} +0 -0
  53. data/vendor/faiss/{IndexIVFSpectralHash.cpp → faiss/IndexIVFSpectralHash.cpp} +1 -1
  54. data/vendor/faiss/{IndexIVFSpectralHash.h → faiss/IndexIVFSpectralHash.h} +0 -0
  55. data/vendor/faiss/{IndexLSH.cpp → faiss/IndexLSH.cpp} +0 -0
  56. data/vendor/faiss/{IndexLSH.h → faiss/IndexLSH.h} +0 -0
  57. data/vendor/faiss/{IndexLattice.cpp → faiss/IndexLattice.cpp} +0 -0
  58. data/vendor/faiss/{IndexLattice.h → faiss/IndexLattice.h} +0 -0
  59. data/vendor/faiss/{IndexPQ.cpp → faiss/IndexPQ.cpp} +6 -6
  60. data/vendor/faiss/{IndexPQ.h → faiss/IndexPQ.h} +3 -1
  61. data/vendor/faiss/{IndexPreTransform.cpp → faiss/IndexPreTransform.cpp} +0 -0
  62. data/vendor/faiss/{IndexPreTransform.h → faiss/IndexPreTransform.h} +0 -0
  63. data/vendor/faiss/{IndexReplicas.cpp → faiss/IndexReplicas.cpp} +102 -10
  64. data/vendor/faiss/{IndexReplicas.h → faiss/IndexReplicas.h} +6 -0
  65. data/vendor/faiss/{IndexScalarQuantizer.cpp → faiss/IndexScalarQuantizer.cpp} +3 -3
  66. data/vendor/faiss/{IndexScalarQuantizer.h → faiss/IndexScalarQuantizer.h} +0 -0
  67. data/vendor/faiss/{IndexShards.cpp → faiss/IndexShards.cpp} +37 -12
  68. data/vendor/faiss/{IndexShards.h → faiss/IndexShards.h} +3 -4
  69. data/vendor/faiss/{InvertedLists.cpp → faiss/InvertedLists.cpp} +2 -2
  70. data/vendor/faiss/{InvertedLists.h → faiss/InvertedLists.h} +1 -0
  71. data/vendor/faiss/{MatrixStats.cpp → faiss/MatrixStats.cpp} +0 -0
  72. data/vendor/faiss/{MatrixStats.h → faiss/MatrixStats.h} +0 -0
  73. data/vendor/faiss/{MetaIndexes.cpp → faiss/MetaIndexes.cpp} +5 -3
  74. data/vendor/faiss/{MetaIndexes.h → faiss/MetaIndexes.h} +0 -0
  75. data/vendor/faiss/{MetricType.h → faiss/MetricType.h} +0 -0
  76. data/vendor/faiss/{OnDiskInvertedLists.cpp → faiss/OnDiskInvertedLists.cpp} +141 -3
  77. data/vendor/faiss/{OnDiskInvertedLists.h → faiss/OnDiskInvertedLists.h} +27 -7
  78. data/vendor/faiss/{VectorTransform.cpp → faiss/VectorTransform.cpp} +4 -3
  79. data/vendor/faiss/{VectorTransform.h → faiss/VectorTransform.h} +0 -0
  80. data/vendor/faiss/{clone_index.cpp → faiss/clone_index.cpp} +0 -0
  81. data/vendor/faiss/{clone_index.h → faiss/clone_index.h} +0 -0
  82. data/vendor/faiss/{gpu → faiss/gpu}/GpuAutoTune.cpp +0 -0
  83. data/vendor/faiss/{gpu → faiss/gpu}/GpuAutoTune.h +0 -0
  84. data/vendor/faiss/{gpu → faiss/gpu}/GpuCloner.cpp +14 -14
  85. data/vendor/faiss/{gpu → faiss/gpu}/GpuCloner.h +6 -7
  86. data/vendor/faiss/{gpu → faiss/gpu}/GpuClonerOptions.cpp +0 -0
  87. data/vendor/faiss/{gpu → faiss/gpu}/GpuClonerOptions.h +0 -0
  88. data/vendor/faiss/{gpu → faiss/gpu}/GpuDistance.h +12 -4
  89. data/vendor/faiss/{gpu → faiss/gpu}/GpuFaissAssert.h +0 -0
  90. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndex.h +3 -9
  91. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexBinaryFlat.h +7 -7
  92. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexFlat.h +35 -10
  93. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVF.h +1 -2
  94. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVFFlat.h +4 -3
  95. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVFPQ.h +21 -4
  96. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndexIVFScalarQuantizer.h +4 -3
  97. data/vendor/faiss/{gpu → faiss/gpu}/GpuIndicesOptions.h +0 -0
  98. data/vendor/faiss/faiss/gpu/GpuResources.cpp +200 -0
  99. data/vendor/faiss/faiss/gpu/GpuResources.h +264 -0
  100. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +572 -0
  101. data/vendor/faiss/{gpu → faiss/gpu}/StandardGpuResources.h +83 -15
  102. data/vendor/faiss/{gpu → faiss/gpu}/impl/RemapIndices.cpp +0 -0
  103. data/vendor/faiss/{gpu → faiss/gpu}/impl/RemapIndices.h +0 -0
  104. data/vendor/faiss/{gpu → faiss/gpu}/perf/IndexWrapper-inl.h +1 -1
  105. data/vendor/faiss/{gpu → faiss/gpu}/perf/IndexWrapper.h +1 -1
  106. data/vendor/faiss/{gpu → faiss/gpu}/perf/PerfClustering.cpp +1 -1
  107. data/vendor/faiss/{gpu → faiss/gpu}/perf/PerfIVFPQAdd.cpp +0 -0
  108. data/vendor/faiss/{gpu → faiss/gpu}/perf/WriteIndex.cpp +0 -0
  109. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexBinaryFlat.cpp +0 -0
  110. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexFlat.cpp +1 -1
  111. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexIVFFlat.cpp +0 -0
  112. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuIndexIVFPQ.cpp +141 -52
  113. data/vendor/faiss/{gpu → faiss/gpu}/test/TestGpuMemoryException.cpp +0 -0
  114. data/vendor/faiss/{gpu → faiss/gpu}/test/TestUtils.cpp +4 -2
  115. data/vendor/faiss/{gpu → faiss/gpu}/test/TestUtils.h +0 -0
  116. data/vendor/faiss/{gpu → faiss/gpu}/test/demo_ivfpq_indexing_gpu.cpp +7 -5
  117. data/vendor/faiss/{gpu → faiss/gpu}/utils/DeviceUtils.h +1 -1
  118. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +213 -0
  119. data/vendor/faiss/{gpu → faiss/gpu}/utils/StackDeviceMemory.h +25 -40
  120. data/vendor/faiss/{gpu → faiss/gpu}/utils/StaticUtils.h +0 -0
  121. data/vendor/faiss/{gpu → faiss/gpu}/utils/Timer.cpp +0 -0
  122. data/vendor/faiss/{gpu → faiss/gpu}/utils/Timer.h +0 -0
  123. data/vendor/faiss/{impl → faiss/impl}/AuxIndexStructures.cpp +1 -0
  124. data/vendor/faiss/{impl → faiss/impl}/AuxIndexStructures.h +3 -1
  125. data/vendor/faiss/{impl → faiss/impl}/FaissAssert.h +1 -0
  126. data/vendor/faiss/{impl → faiss/impl}/FaissException.cpp +26 -0
  127. data/vendor/faiss/{impl → faiss/impl}/FaissException.h +4 -0
  128. data/vendor/faiss/{impl → faiss/impl}/HNSW.cpp +26 -26
  129. data/vendor/faiss/{impl → faiss/impl}/HNSW.h +19 -11
  130. data/vendor/faiss/{impl → faiss/impl}/PolysemousTraining.cpp +1 -1
  131. data/vendor/faiss/{impl → faiss/impl}/PolysemousTraining.h +1 -1
  132. data/vendor/faiss/{impl → faiss/impl}/ProductQuantizer-inl.h +0 -1
  133. data/vendor/faiss/{impl → faiss/impl}/ProductQuantizer.cpp +9 -9
  134. data/vendor/faiss/{impl → faiss/impl}/ProductQuantizer.h +0 -0
  135. data/vendor/faiss/{impl → faiss/impl}/ScalarQuantizer.cpp +63 -39
  136. data/vendor/faiss/{impl → faiss/impl}/ScalarQuantizer.h +1 -1
  137. data/vendor/faiss/{impl → faiss/impl}/ThreadedIndex-inl.h +0 -0
  138. data/vendor/faiss/{impl → faiss/impl}/ThreadedIndex.h +0 -0
  139. data/vendor/faiss/{impl → faiss/impl}/index_read.cpp +99 -116
  140. data/vendor/faiss/{impl → faiss/impl}/index_write.cpp +15 -50
  141. data/vendor/faiss/{impl → faiss/impl}/io.cpp +15 -10
  142. data/vendor/faiss/{impl → faiss/impl}/io.h +22 -8
  143. data/vendor/faiss/faiss/impl/io_macros.h +57 -0
  144. data/vendor/faiss/{impl → faiss/impl}/lattice_Zn.cpp +52 -36
  145. data/vendor/faiss/{impl → faiss/impl}/lattice_Zn.h +3 -3
  146. data/vendor/faiss/faiss/impl/platform_macros.h +24 -0
  147. data/vendor/faiss/{index_factory.cpp → faiss/index_factory.cpp} +33 -12
  148. data/vendor/faiss/{index_factory.h → faiss/index_factory.h} +0 -0
  149. data/vendor/faiss/{index_io.h → faiss/index_io.h} +55 -1
  150. data/vendor/faiss/faiss/python/python_callbacks.cpp +112 -0
  151. data/vendor/faiss/faiss/python/python_callbacks.h +45 -0
  152. data/vendor/faiss/{utils → faiss/utils}/Heap.cpp +5 -5
  153. data/vendor/faiss/{utils → faiss/utils}/Heap.h +1 -3
  154. data/vendor/faiss/{utils → faiss/utils}/WorkerThread.cpp +0 -0
  155. data/vendor/faiss/{utils → faiss/utils}/WorkerThread.h +0 -0
  156. data/vendor/faiss/{utils → faiss/utils}/distances.cpp +28 -13
  157. data/vendor/faiss/{utils → faiss/utils}/distances.h +2 -1
  158. data/vendor/faiss/{utils → faiss/utils}/distances_simd.cpp +5 -5
  159. data/vendor/faiss/{utils → faiss/utils}/extra_distances.cpp +8 -7
  160. data/vendor/faiss/{utils → faiss/utils}/extra_distances.h +0 -0
  161. data/vendor/faiss/{utils → faiss/utils}/hamming-inl.h +1 -3
  162. data/vendor/faiss/{utils → faiss/utils}/hamming.cpp +8 -7
  163. data/vendor/faiss/{utils → faiss/utils}/hamming.h +7 -1
  164. data/vendor/faiss/{utils → faiss/utils}/random.cpp +5 -5
  165. data/vendor/faiss/{utils → faiss/utils}/random.h +0 -0
  166. data/vendor/faiss/{utils → faiss/utils}/utils.cpp +27 -28
  167. data/vendor/faiss/{utils → faiss/utils}/utils.h +4 -0
  168. data/vendor/faiss/misc/test_blas.cpp +4 -1
  169. data/vendor/faiss/tests/test_binary_flat.cpp +0 -2
  170. data/vendor/faiss/tests/test_dealloc_invlists.cpp +6 -1
  171. data/vendor/faiss/tests/test_ivfpq_codec.cpp +4 -1
  172. data/vendor/faiss/tests/test_ivfpq_indexing.cpp +6 -4
  173. data/vendor/faiss/tests/test_lowlevel_ivf.cpp +12 -5
  174. data/vendor/faiss/tests/test_merge.cpp +6 -3
  175. data/vendor/faiss/tests/test_ondisk_ivf.cpp +7 -2
  176. data/vendor/faiss/tests/test_pairs_decoding.cpp +5 -1
  177. data/vendor/faiss/tests/test_params_override.cpp +7 -2
  178. data/vendor/faiss/tests/test_sliding_ivf.cpp +10 -4
  179. data/vendor/faiss/tutorial/cpp/1-Flat.cpp +14 -8
  180. data/vendor/faiss/tutorial/cpp/2-IVFFlat.cpp +11 -7
  181. data/vendor/faiss/tutorial/cpp/3-IVFPQ.cpp +12 -7
  182. data/vendor/faiss/tutorial/cpp/4-GPU.cpp +6 -3
  183. data/vendor/faiss/tutorial/cpp/5-Multiple-GPUs.cpp +7 -3
  184. metadata +154 -153
  185. data/vendor/faiss/gpu/GpuResources.cpp +0 -52
  186. data/vendor/faiss/gpu/GpuResources.h +0 -73
  187. data/vendor/faiss/gpu/StandardGpuResources.cpp +0 -303
  188. data/vendor/faiss/gpu/utils/DeviceMemory.cpp +0 -77
  189. data/vendor/faiss/gpu/utils/DeviceMemory.h +0 -71
  190. data/vendor/faiss/gpu/utils/MemorySpace.cpp +0 -89
  191. data/vendor/faiss/gpu/utils/MemorySpace.h +0 -44
  192. data/vendor/faiss/gpu/utils/StackDeviceMemory.cpp +0 -239
@@ -14,6 +14,9 @@
14
14
 
15
15
 
16
16
  #include <cstdio>
17
+ #include <typeinfo>
18
+ #include <string>
19
+ #include <vector>
17
20
 
18
21
  /** I/O functions can read/write to a filename, a file handle or to an
19
22
  * object that abstracts the medium.
@@ -42,11 +45,15 @@ void write_index_binary (const IndexBinary *idx, FILE *f);
42
45
  void write_index_binary (const IndexBinary *idx, IOWriter *writer);
43
46
 
44
47
  // The read_index flags are implemented only for a subset of index types.
45
- const int IO_FLAG_MMAP = 1; // try to memmap if possible
46
48
  const int IO_FLAG_READ_ONLY = 2;
47
49
  // strip directory component from ondisk filename, and assume it's in
48
50
  // the same directory as the index file
49
51
  const int IO_FLAG_ONDISK_SAME_DIR = 4;
52
+ // don't load IVF data to RAM, only list sizes
53
+ const int IO_FLAG_SKIP_IVF_DATA = 8;
54
+ // try to memmap data (useful for OnDiskInvertedLists)
55
+ const int IO_FLAG_MMAP = IO_FLAG_SKIP_IVF_DATA | 0x646f0000;
56
+
50
57
 
51
58
  Index *read_index (const char *fname, int io_flags = 0);
52
59
  Index *read_index (FILE * f, int io_flags = 0);
@@ -69,6 +76,53 @@ void write_InvertedLists (const InvertedLists *ils, IOWriter *f);
69
76
  InvertedLists *read_InvertedLists (IOReader *reader, int io_flags = 0);
70
77
 
71
78
 
79
+ #ifndef _MSC_VER
80
+ /** Callbacks to handle other types of InvertedList objects.
81
+ *
82
+ * The callbacks should be registered with add_callback before calling
83
+ * read_index or read_InvertedLists. The callbacks for
84
+ * OnDiskInvertedLists are registrered by default. The invlist type is
85
+ * identified by:
86
+ *
87
+ * - the key (a fourcc) at read time
88
+ * - the class name (as given by typeid.name) at write time
89
+ */
90
+ struct InvertedListsIOHook {
91
+ const std::string key; ///< string version of the fourcc
92
+ const std::string classname; ///< typeid.name
93
+
94
+ InvertedListsIOHook(const std::string & key, const std::string & classname);
95
+
96
+ /// write the index to the IOWriter (including the fourcc)
97
+ virtual void write(const InvertedLists *ils, IOWriter *f) const = 0;
98
+
99
+ /// called when the fourcc matches this class's fourcc
100
+ virtual InvertedLists * read(IOReader *f, int io_flags) const = 0;
101
+
102
+ /** read from a ArrayInvertedLists into this invertedlist type.
103
+ * For this to work, the callback has to be enabled and the io_flag has to be set to
104
+ * IO_FLAG_SKIP_IVF_DATA | (16 upper bits of the fourcc)
105
+ */
106
+ virtual InvertedLists * read_ArrayInvertedLists(
107
+ IOReader *f, int io_flags,
108
+ size_t nlist, size_t code_size,
109
+ const std::vector<size_t> &sizes) const = 0;
110
+
111
+ virtual ~InvertedListsIOHook() {}
112
+
113
+ /**************************** Manage the set of callbacks ******/
114
+
115
+ // transfers ownership
116
+ static void add_callback(InvertedListsIOHook *);
117
+ static void print_callbacks();
118
+ static InvertedListsIOHook* lookup(int h);
119
+ static InvertedListsIOHook* lookup_classname(const std::string & classname);
120
+
121
+ };
122
+
123
+ #endif // !_MSC_VER
124
+
125
+
72
126
  } // namespace faiss
73
127
 
74
128
 
@@ -0,0 +1,112 @@
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/python/python_callbacks.h>
9
+ #include <faiss/impl/FaissAssert.h>
10
+
11
+ namespace {
12
+
13
+ struct PyThreadLock {
14
+ PyGILState_STATE gstate;
15
+
16
+ PyThreadLock () {
17
+ gstate = PyGILState_Ensure();
18
+ }
19
+
20
+ ~PyThreadLock () {
21
+ PyGILState_Release(gstate);
22
+ }
23
+ };
24
+
25
+
26
+ };
27
+
28
+
29
+ /***********************************************************
30
+ * Callbacks for IO reader and writer
31
+ ***********************************************************/
32
+
33
+ PyCallbackIOWriter::PyCallbackIOWriter(PyObject *callback, size_t bs):
34
+ callback(callback), bs(bs) {
35
+ PyThreadLock gil;
36
+ Py_INCREF(callback);
37
+ name = "PyCallbackIOWriter";
38
+ }
39
+
40
+
41
+
42
+ size_t PyCallbackIOWriter::operator()(const void *ptrv, size_t size, size_t nitems) {
43
+ size_t ws = size * nitems;
44
+ const char *ptr = (const char*)ptrv;
45
+ PyThreadLock gil;
46
+ while(ws > 0) {
47
+ size_t wi = ws > bs ? bs : ws;
48
+ PyObject * result = PyObject_CallFunction(
49
+ callback, "(N)", PyBytes_FromStringAndSize(ptr, wi));
50
+ if (result == NULL) {
51
+ FAISS_THROW_MSG("py err");
52
+ }
53
+ // TODO check nb of bytes written
54
+ ptr += wi;
55
+ ws -= wi;
56
+ Py_DECREF(result);
57
+ }
58
+ return nitems;
59
+ }
60
+
61
+ PyCallbackIOWriter::~PyCallbackIOWriter() {
62
+ PyThreadLock gil;
63
+ Py_DECREF(callback);
64
+ }
65
+
66
+
67
+ PyCallbackIOReader::PyCallbackIOReader(PyObject *callback, size_t bs):
68
+ callback(callback), bs(bs) {
69
+ PyThreadLock gil;
70
+ Py_INCREF(callback);
71
+ name = "PyCallbackIOReader";
72
+ }
73
+
74
+ size_t PyCallbackIOReader::operator()(void *ptrv, size_t size, size_t nitems)
75
+ {
76
+ size_t rs = size * nitems;
77
+ size_t nb = 0;
78
+ char *ptr = (char*)ptrv;
79
+ PyThreadLock gil;
80
+ while(rs > 0) {
81
+ size_t ri = rs > bs ? bs : rs;
82
+ PyObject * result = PyObject_CallFunction(callback, "(n)", ri);
83
+ if (result == NULL) {
84
+ FAISS_THROW_MSG("propagate py error");
85
+ }
86
+ if(!PyBytes_Check(result)) {
87
+ Py_DECREF(result);
88
+ FAISS_THROW_MSG("read callback did not return a bytes object");
89
+ }
90
+ size_t sz = PyBytes_Size(result);
91
+ if (sz == 0) {
92
+ Py_DECREF(result);
93
+ break;
94
+ }
95
+ nb += sz;
96
+ if (sz > rs) {
97
+ Py_DECREF(result);
98
+ FAISS_THROW_FMT("read callback returned %zd bytes (asked %zd)",
99
+ sz, rs);
100
+ }
101
+ memcpy(ptr, PyBytes_AsString(result), sz);
102
+ Py_DECREF(result);
103
+ ptr += sz;
104
+ rs -= sz;
105
+ }
106
+ return nb / size;
107
+ }
108
+
109
+ PyCallbackIOReader::~PyCallbackIOReader() {
110
+ PyThreadLock gil;
111
+ Py_DECREF(callback);
112
+ }
@@ -0,0 +1,45 @@
1
+ #pragma once
2
+
3
+ #include "Python.h"
4
+ #include <faiss/impl/io.h>
5
+ #include <faiss/InvertedLists.h>
6
+
7
+ // all callbacks have to acquire the GIL on input
8
+
9
+
10
+ /***********************************************************
11
+ * Callbacks for IO reader and writer
12
+ ***********************************************************/
13
+
14
+ struct PyCallbackIOWriter: faiss::IOWriter {
15
+
16
+ PyObject * callback;
17
+ size_t bs; // maximum write size
18
+
19
+ /** Callback: Python function that takes a bytes object and
20
+ * returns the number of bytes successfully written.
21
+ */
22
+ explicit PyCallbackIOWriter(PyObject *callback,
23
+ size_t bs = 1024 * 1024);
24
+
25
+ size_t operator()(const void *ptrv, size_t size, size_t nitems) override;
26
+
27
+ ~PyCallbackIOWriter() override;
28
+
29
+ };
30
+
31
+
32
+ struct PyCallbackIOReader: faiss::IOReader {
33
+ PyObject * callback;
34
+ size_t bs; // maximum buffer size
35
+
36
+ /** Callback: Python function that takes a size and returns a
37
+ * bytes object with the resulting read */
38
+ explicit PyCallbackIOReader(PyObject *callback,
39
+ size_t bs = 1024 * 1024);
40
+
41
+ size_t operator()(void *ptrv, size_t size, size_t nitems) override;
42
+
43
+ ~PyCallbackIOReader() override;
44
+
45
+ };
@@ -19,7 +19,7 @@ template <typename C>
19
19
  void HeapArray<C>::heapify ()
20
20
  {
21
21
  #pragma omp parallel for
22
- for (size_t j = 0; j < nh; j++)
22
+ for (int64_t j = 0; j < nh; j++)
23
23
  heap_heapify<C> (k, val + j * k, ids + j * k);
24
24
  }
25
25
 
@@ -27,7 +27,7 @@ template <typename C>
27
27
  void HeapArray<C>::reorder ()
28
28
  {
29
29
  #pragma omp parallel for
30
- for (size_t j = 0; j < nh; j++)
30
+ for (int64_t j = 0; j < nh; j++)
31
31
  heap_reorder<C> (k, val + j * k, ids + j * k);
32
32
  }
33
33
 
@@ -38,7 +38,7 @@ void HeapArray<C>::addn (size_t nj, const T *vin, TI j0,
38
38
  if (ni == -1) ni = nh;
39
39
  assert (i0 >= 0 && i0 + ni <= nh);
40
40
  #pragma omp parallel for
41
- for (size_t i = i0; i < i0 + ni; i++) {
41
+ for (int64_t i = i0; i < i0 + ni; i++) {
42
42
  T * __restrict simi = get_val(i);
43
43
  TI * __restrict idxi = get_ids (i);
44
44
  const T *ip_line = vin + (i - i0) * nj;
@@ -65,7 +65,7 @@ void HeapArray<C>::addn_with_ids (
65
65
  if (ni == -1) ni = nh;
66
66
  assert (i0 >= 0 && i0 + ni <= nh);
67
67
  #pragma omp parallel for
68
- for (size_t i = i0; i < i0 + ni; i++) {
68
+ for (int64_t i = i0; i < i0 + ni; i++) {
69
69
  T * __restrict simi = get_val(i);
70
70
  TI * __restrict idxi = get_ids (i);
71
71
  const T *ip_line = vin + (i - i0) * nj;
@@ -87,7 +87,7 @@ void HeapArray<C>::per_line_extrema (
87
87
  TI * out_ids) const
88
88
  {
89
89
  #pragma omp parallel for
90
- for (size_t j = 0; j < nh; j++) {
90
+ for (int64_t j = 0; j < nh; j++) {
91
91
  int64_t imin = -1;
92
92
  typename C::T xval = C::Crev::neutral ();
93
93
  const typename C::T * x_ = val + j * k;
@@ -57,10 +57,8 @@ struct CMin {
57
57
  inline static bool cmp (T a, T b) {
58
58
  return a < b;
59
59
  }
60
- // value that will be popped first -> must be smaller than all others
61
- // for int types this is not strictly the smallest val (-max - 1)
62
60
  inline static T neutral () {
63
- return -std::numeric_limits<T>::max();
61
+ return std::numeric_limits<T>::lowest();
64
62
  }
65
63
  };
66
64
 
@@ -9,6 +9,7 @@
9
9
 
10
10
  #include <faiss/utils/distances.h>
11
11
 
12
+ #include <algorithm>
12
13
  #include <cstdio>
13
14
  #include <cassert>
14
15
  #include <cstring>
@@ -93,7 +94,7 @@ void fvec_norms_L2 (float * __restrict nr,
93
94
  {
94
95
 
95
96
  #pragma omp parallel for
96
- for (size_t i = 0; i < nx; i++) {
97
+ for (int64_t i = 0; i < nx; i++) {
97
98
  nr[i] = sqrtf (fvec_norm_L2sqr (x + i * d, d));
98
99
  }
99
100
  }
@@ -103,7 +104,7 @@ void fvec_norms_L2sqr (float * __restrict nr,
103
104
  size_t d, size_t nx)
104
105
  {
105
106
  #pragma omp parallel for
106
- for (size_t i = 0; i < nx; i++)
107
+ for (int64_t i = 0; i < nx; i++)
107
108
  nr[i] = fvec_norm_L2sqr (x + i * d, d);
108
109
  }
109
110
 
@@ -112,7 +113,7 @@ void fvec_norms_L2sqr (float * __restrict nr,
112
113
  void fvec_renorm_L2 (size_t d, size_t nx, float * __restrict x)
113
114
  {
114
115
  #pragma omp parallel for
115
- for (size_t i = 0; i < nx; i++) {
116
+ for (int64_t i = 0; i < nx; i++) {
116
117
  float * __restrict xi = x + i * d;
117
118
 
118
119
  float nr = fvec_norm_L2sqr (xi, d);
@@ -158,7 +159,7 @@ static void knn_inner_product_sse (const float * x,
158
159
  size_t i1 = std::min(i0 + check_period, nx);
159
160
 
160
161
  #pragma omp parallel for
161
- for (size_t i = i0; i < i1; i++) {
162
+ for (int64_t i = i0; i < i1; i++) {
162
163
  const float * x_i = x + i * d;
163
164
  const float * y_j = y;
164
165
 
@@ -198,7 +199,7 @@ static void knn_L2sqr_sse (
198
199
  size_t i1 = std::min(i0 + check_period, nx);
199
200
 
200
201
  #pragma omp parallel for
201
- for (size_t i = i0; i < i1; i++) {
202
+ for (int64_t i = i0; i < i1; i++) {
202
203
  const float * x_i = x + i * d;
203
204
  const float * y_j = y;
204
205
  size_t j;
@@ -312,7 +313,7 @@ static void knn_L2sqr_blas (const float * x,
312
313
 
313
314
  /* collect minima */
314
315
  #pragma omp parallel for
315
- for (size_t i = i0; i < i1; i++) {
316
+ for (int64_t i = i0; i < i1; i++) {
316
317
  float * __restrict simi = res->get_val(i);
317
318
  int64_t * __restrict idxi = res->get_ids (i);
318
319
  const float *ip_line = ip_block + (i - i0) * (j1 - j0);
@@ -420,7 +421,7 @@ void fvec_inner_products_by_idx (float * __restrict ip,
420
421
  size_t d, size_t nx, size_t ny)
421
422
  {
422
423
  #pragma omp parallel for
423
- for (size_t j = 0; j < nx; j++) {
424
+ for (int64_t j = 0; j < nx; j++) {
424
425
  const int64_t * __restrict idsj = ids + j * ny;
425
426
  const float * xj = x + j * d;
426
427
  float * __restrict ipj = ip + j * ny;
@@ -443,7 +444,7 @@ void fvec_L2sqr_by_idx (float * __restrict dis,
443
444
  size_t d, size_t nx, size_t ny)
444
445
  {
445
446
  #pragma omp parallel for
446
- for (size_t j = 0; j < nx; j++) {
447
+ for (int64_t j = 0; j < nx; j++) {
447
448
  const int64_t * __restrict idsj = ids + j * ny;
448
449
  const float * xj = x + j * d;
449
450
  float * __restrict disj = dis + j * ny;
@@ -462,7 +463,7 @@ void pairwise_indexed_L2sqr (
462
463
  float *dis)
463
464
  {
464
465
  #pragma omp parallel for
465
- for (size_t j = 0; j < n; j++) {
466
+ for (int64_t j = 0; j < n; j++) {
466
467
  if (ix[j] >= 0 && iy[j] >= 0) {
467
468
  dis[j] = fvec_L2sqr (x + d * ix[j], y + d * iy[j], d);
468
469
  }
@@ -476,7 +477,7 @@ void pairwise_indexed_inner_product (
476
477
  float *dis)
477
478
  {
478
479
  #pragma omp parallel for
479
- for (size_t j = 0; j < n; j++) {
480
+ for (int64_t j = 0; j < n; j++) {
480
481
  if (ix[j] >= 0 && iy[j] >= 0) {
481
482
  dis[j] = fvec_inner_product (x + d * ix[j], y + d * iy[j], d);
482
483
  }
@@ -495,7 +496,7 @@ void knn_inner_products_by_idx (const float * x,
495
496
  size_t k = res->k;
496
497
 
497
498
  #pragma omp parallel for
498
- for (size_t i = 0; i < nx; i++) {
499
+ for (int64_t i = 0; i < nx; i++) {
499
500
  const float * x_ = x + i * d;
500
501
  const int64_t * idsi = ids + i * ny;
501
502
  size_t j;
@@ -526,7 +527,7 @@ void knn_L2sqr_by_idx (const float * x,
526
527
  size_t k = res->k;
527
528
 
528
529
  #pragma omp parallel for
529
- for (size_t i = 0; i < nx; i++) {
530
+ for (int64_t i = 0; i < nx; i++) {
530
531
  const float * x_ = x + i * d;
531
532
  const int64_t * __restrict idsi = ids + i * ny;
532
533
  float * __restrict simi = res->get_val(i);
@@ -649,7 +650,7 @@ static void range_search_sse (const float * x,
649
650
  RangeSearchPartialResult pres (res);
650
651
 
651
652
  #pragma omp for
652
- for (size_t i = 0; i < nx; i++) {
653
+ for (int64_t i = 0; i < nx; i++) {
653
654
  const float * x_ = x + i * d;
654
655
  const float * y_ = y;
655
656
  size_t j;
@@ -760,5 +761,19 @@ void pairwise_L2sqr (int64_t d,
760
761
 
761
762
  }
762
763
 
764
+ void inner_product_to_L2sqr(float* __restrict dis,
765
+ const float* nr1,
766
+ const float* nr2,
767
+ size_t n1, size_t n2)
768
+ {
769
+
770
+ #pragma omp parallel for
771
+ for (int64_t j = 0; j < n1; j++) {
772
+ float* disj = dis + j * n2;
773
+ for (size_t i = 0; i < n2; i++)
774
+ disj[i] = nr1[j] + nr2[i] - 2 * disj[i];
775
+ }
776
+ }
777
+
763
778
 
764
779
  } // namespace faiss
@@ -15,6 +15,7 @@
15
15
  #include <stdint.h>
16
16
 
17
17
  #include <faiss/utils/Heap.h>
18
+ #include <faiss/impl/platform_macros.h>
18
19
 
19
20
 
20
21
  namespace faiss {
@@ -153,7 +154,7 @@ void pairwise_indexed_inner_product (
153
154
  ***************************************************************************/
154
155
 
155
156
  // threshold on nx above which we switch to BLAS to compute distances
156
- extern int distance_compute_blas_threshold;
157
+ FAISS_API extern int distance_compute_blas_threshold;
157
158
 
158
159
  /** Return the k nearest neighors of each of the nx vectors x among the ny
159
160
  * vector y, w.r.t to max inner product