__init__.pyi 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. from typing import Any, Union, Literal, Optional, TypeAlias
  2. # A lot of annotation features a depend on the Python version:
  3. # - `typing.TypeAlias` Type aliases are supported from Python 3.10 to 3.11
  4. # - `type` Type statements are supported from Python 3.12, replacing `typing.TypeAlias`
  5. # - `typing.Literal` Literal types are supported from Python 3.8
  6. #
  7. # We can't and shouldn't use a different `.pyi` file for every single Python version.
  8. # So let's assume we are targeting Python 3.11 and we have NumPy available.
  9. from numpy.typing import NDArray
  10. _BufferType: TypeAlias = Union[NDArray[Any], memoryview]
  11. _MetricType = Literal[
  12. "euclidean",
  13. "sqeuclidean",
  14. "inner",
  15. "dot",
  16. "cosine",
  17. "cos",
  18. "hamming",
  19. "jaccard",
  20. "kullbackleibler",
  21. "kl",
  22. "jensenshannon",
  23. "js",
  24. "intersection",
  25. "bilinear",
  26. "mahalanobis",
  27. "fma",
  28. "wsum",
  29. ]
  30. _IntegralType = Literal[
  31. "bin8",
  32. # Signed integers
  33. "int8",
  34. "int16",
  35. "int32",
  36. "int64",
  37. # Unsigned integers
  38. "uint8",
  39. "uint16",
  40. "uint32",
  41. "uint64",
  42. ]
  43. _FloatType = Literal[
  44. "f32",
  45. "float32",
  46. "f16",
  47. "float16",
  48. "f64",
  49. "float64",
  50. "bf16", #! Not supported by NumPy
  51. "bfloat16", #! Not supported by NumPy
  52. ]
  53. _ComplexType = Literal[
  54. "complex32", #! Not supported by NumPy
  55. "bcomplex32", #! Not supported by NumPy
  56. "complex64",
  57. "complex128",
  58. ]
  59. class DistancesTensor(memoryview): ...
  60. # ---------------------------------------------------------------------
  61. # Controlling SIMD capabilities
  62. def get_capabilities() -> dict[str, bool]: ...
  63. def enable_capability(capability: str, /) -> None: ...
  64. def disable_capability(capability: str, /) -> None: ...
  65. # Accessing function pointers
  66. def pointer_to_euclidean(dtype: Union[_IntegralType, _FloatType], /) -> int: ...
  67. def pointer_to_sqeuclidean(dtype: Union[_IntegralType, _FloatType], /) -> int: ...
  68. def pointer_to_cosine(dtype: Union[_IntegralType, _FloatType], /) -> int: ...
  69. def pointer_to_inner(dtype: Union[_FloatType, _ComplexType], /) -> int: ...
  70. def pointer_to_dot(dtype: Union[_FloatType, _ComplexType], /) -> int: ...
  71. def pointer_to_vdot(dtype: Union[_FloatType, _ComplexType], /) -> int: ...
  72. def pointer_to_hamming(dtype: _IntegralType, /) -> int: ...
  73. def pointer_to_jaccard(dtype: _IntegralType, /) -> int: ...
  74. def pointer_to_jensenshannon(dtype: _FloatType, /) -> int: ...
  75. def pointer_to_kullbackleibler(dtype: _FloatType, /) -> int: ...
  76. # ---------------------------------------------------------------------
  77. # All pairwise distances, similar to: `scipy.spatial.distance.cdist`.
  78. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.cdist.html
  79. def cdist(
  80. a: _BufferType,
  81. b: _BufferType,
  82. /,
  83. metric: _MetricType = "euclidean",
  84. *,
  85. threads: int = 1,
  86. dtype: Optional[Union[_IntegralType, _FloatType, _ComplexType]] = None,
  87. out: Optional[_BufferType] = None,
  88. out_dtype: Optional[Union[_FloatType, _ComplexType]] = None,
  89. ) -> Optional[Union[float, complex, DistancesTensor]]: ...
  90. # ---------------------------------------------------------------------
  91. # Vector-vector dot products for real and complex numbers
  92. # ---------------------------------------------------------------------
  93. # Inner product, similar to: `numpy.inner`.
  94. # https://numpy.org/doc/stable/reference/generated/numpy.inner.html
  95. def inner(
  96. a: _BufferType,
  97. b: _BufferType,
  98. /,
  99. dtype: Optional[Union[_FloatType, _ComplexType]] = None,
  100. *,
  101. out: Optional[_BufferType] = None,
  102. out_dtype: Optional[Union[_FloatType, _ComplexType]] = None,
  103. ) -> Optional[Union[float, complex, DistancesTensor]]: ...
  104. # Dot product, similar to: `numpy.dot`.
  105. # https://numpy.org/doc/stable/reference/generated/numpy.dot.html
  106. def dot(
  107. a: _BufferType,
  108. b: _BufferType,
  109. /,
  110. dtype: Optional[Union[_FloatType, _ComplexType]] = None,
  111. *,
  112. out: Optional[_BufferType] = None,
  113. out_dtype: Union[_FloatType, _ComplexType] = None,
  114. ) -> Optional[Union[float, complex, DistancesTensor]]: ...
  115. # Vector-vector dot product for complex conjugates, similar to: `numpy.vdot`.
  116. # https://numpy.org/doc/stable/reference/generated/numpy.vdot.html
  117. def vdot(
  118. a: _BufferType,
  119. b: _BufferType,
  120. /,
  121. dtype: Optional[_ComplexType] = None,
  122. *,
  123. out: Optional[Union[float, complex, DistancesTensor]] = None,
  124. out_dtype: Optional[_ComplexType] = None,
  125. ) -> Optional[Union[complex, DistancesTensor]]: ...
  126. # ---------------------------------------------------------------------
  127. # Vector-vector spatial distance metrics for real and integer numbers
  128. # ---------------------------------------------------------------------
  129. # Vector-vector squared Euclidean distance, similar to: `scipy.spatial.distance.sqeuclidean`.
  130. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.sqeuclidean.html
  131. # https://numpy.org/doc/stable/reference/generated/numpy.linalg.norm.html
  132. def sqeuclidean(
  133. a: _BufferType,
  134. b: _BufferType,
  135. /,
  136. dtype: Optional[Union[_IntegralType, _FloatType]] = None,
  137. *,
  138. out: Optional[_BufferType] = None,
  139. out_dtype: Union[_FloatType] = None,
  140. ) -> Optional[Union[float, DistancesTensor]]: ...
  141. # Vector-vector cosine distance, similar to: `scipy.spatial.distance.cosine`.
  142. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.cosine.html
  143. def cosine(
  144. a: _BufferType,
  145. b: _BufferType,
  146. /,
  147. dtype: Optional[Union[_IntegralType, _FloatType]] = None,
  148. *,
  149. out: Optional[_BufferType] = None,
  150. out_dtype: Union[_FloatType] = None,
  151. ) -> Optional[Union[float, DistancesTensor]]: ...
  152. # ---------------------------------------------------------------------
  153. # Vector-vector similarity functions for binary vectors
  154. # ---------------------------------------------------------------------
  155. # Vector-vector Hamming distance, similar to: `scipy.spatial.distance.hamming`.
  156. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.hamming.html
  157. def hamming(
  158. a: _BufferType,
  159. b: _BufferType,
  160. /,
  161. dtype: Optional[_IntegralType] = None,
  162. *,
  163. out: Optional[_BufferType] = None,
  164. out_dtype: Union[_FloatType] = None,
  165. ) -> Optional[Union[float, DistancesTensor]]: ...
  166. # Vector-vector Jaccard distance, similar to: `scipy.spatial.distance.jaccard`.
  167. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.jaccard.html
  168. def jaccard(
  169. a: _BufferType,
  170. b: _BufferType,
  171. /,
  172. dtype: Optional[_IntegralType] = None,
  173. *,
  174. out: Optional[_BufferType] = None,
  175. out_dtype: Union[_FloatType] = None,
  176. ) -> Optional[Union[float, DistancesTensor]]: ...
  177. # ---------------------------------------------------------------------
  178. # Vector-vector similarity between probability distributions
  179. # ---------------------------------------------------------------------
  180. # Vector-vector Jensen-Shannon distance, similar to: `scipy.spatial.distance.jensenshannon`.
  181. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.jensenshannon.html
  182. def jensenshannon(
  183. a: _BufferType,
  184. b: _BufferType,
  185. /,
  186. dtype: Optional[_FloatType] = None,
  187. *,
  188. out: Optional[_BufferType] = None,
  189. out_dtype: Union[_FloatType] = None,
  190. ) -> Optional[Union[float, DistancesTensor]]: ...
  191. # Vector-vector Kullback-Leibler divergence, similar to: `scipy.spatial.distance.kullback_leibler`.
  192. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.kullback_leibler.html
  193. def kullbackleibler(
  194. a: _BufferType,
  195. b: _BufferType,
  196. /,
  197. dtype: Optional[_FloatType] = None,
  198. *,
  199. out: Optional[_BufferType] = None,
  200. out_dtype: Union[_FloatType] = None,
  201. ) -> Optional[Union[float, DistancesTensor]]: ...
  202. # ---------------------------------------------------------------------
  203. # Vector-vector similarity between vectors in curved spaces
  204. # ---------------------------------------------------------------------
  205. # Vector-vector bilinear distance, similar to: `numpy.dot(a, metric_tensor @ vector2)`.
  206. # https://numpy.org/doc/stable/reference/generated/numpy.dot.html
  207. def bilinear(
  208. a: _BufferType,
  209. b: _BufferType,
  210. metric_tensor: _BufferType,
  211. /,
  212. dtype: Optional[_FloatType] = None,
  213. ) -> float: ...
  214. # Vector-vector Mahalanobis distance, similar to: `scipy.spatial.distance.mahalanobis`.
  215. # https://docs.scipy.org/doc/scipy-1.11.4/reference/generated/scipy.spatial.distance.mahalanobis.html
  216. def mahalanobis(
  217. a: _BufferType,
  218. b: _BufferType,
  219. inverse_covariance: _BufferType,
  220. /,
  221. dtype: Optional[_FloatType] = None,
  222. ) -> float: ...
  223. # ---------------------------------------------------------------------
  224. # Vector-vector similarity between sparse vectors
  225. # ---------------------------------------------------------------------
  226. # Vector-vector intersection similarity, similar to: `numpy.intersect1d`.
  227. # https://numpy.org/doc/stable/reference/generated/numpy.intersect1d.html
  228. def intersection(array1: _BufferType, array2: _BufferType, /) -> float: ...
  229. # ---------------------------------------------------------------------
  230. # Vector-vector math: FMA, WSum
  231. # ---------------------------------------------------------------------
  232. # Vector-vector element-wise fused-multiply add.
  233. def fma(
  234. a: _BufferType,
  235. b: _BufferType,
  236. c: _BufferType,
  237. /,
  238. dtype: Optional[Union[_FloatType, _IntegralType]] = None,
  239. *,
  240. alpha: float = 1,
  241. beta: float = 1,
  242. out: Optional[_BufferType] = None,
  243. ) -> Optional[DistancesTensor]: ...
  244. # Vector-vector element-wise weighted sum.
  245. def wsum(
  246. a: _BufferType,
  247. b: _BufferType,
  248. /,
  249. dtype: Optional[Union[_FloatType, _IntegralType]] = None,
  250. *,
  251. alpha: float = 1,
  252. beta: float = 1,
  253. out: Optional[_BufferType] = None,
  254. ) -> Optional[DistancesTensor]: ...