usearch 2.15.2 → 2.15.3
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.
- package/README.md +1 -1
- package/binding.gyp +1 -1
- package/c/CMakeLists.txt +10 -3
- package/cpp/README.md +1 -1
- package/cpp/bench.cpp +2 -2
- package/include/usearch/index.hpp +24 -13
- package/include/usearch/index_dense.hpp +45 -88
- package/include/usearch/index_plugins.hpp +126 -38
- package/javascript/usearch.test.js +15 -13
- package/package.json +1 -1
- package/prebuilds/darwin-arm64+x64/usearch.node +0 -0
- package/prebuilds/linux-arm64/usearch.node +0 -0
- package/prebuilds/linux-x64/usearch.node +0 -0
- package/simsimd/CODE_OF_CONDUCT.md +128 -0
- package/simsimd/CONTRIBUTING.md +18 -8
- package/simsimd/Cargo.lock +1 -1
- package/simsimd/Cargo.toml +1 -1
- package/simsimd/MANIFEST.in +2 -0
- package/simsimd/README.md +446 -158
- package/simsimd/build.rs +11 -1
- package/simsimd/c/lib.c +172 -40
- package/simsimd/include/simsimd/binary.h +11 -11
- package/simsimd/include/simsimd/curved.h +804 -0
- package/simsimd/include/simsimd/dot.h +333 -334
- package/simsimd/include/simsimd/geospatial.h +9 -3
- package/simsimd/include/simsimd/mesh.h +69 -0
- package/simsimd/include/simsimd/probability.h +178 -150
- package/simsimd/include/simsimd/simsimd.h +581 -107
- package/simsimd/include/simsimd/sparse.h +807 -0
- package/simsimd/include/simsimd/spatial.h +737 -502
- package/simsimd/include/simsimd/types.h +145 -75
- package/simsimd/javascript/simsimd.ts +4 -4
- package/simsimd/package.json +1 -1
- package/simsimd/rust/lib.rs +165 -0
package/simsimd/README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-

|
|
1
|
+

|
|
4
2
|
|
|
5
3
|
Computing dot-products, similarity measures, and distances between low- and high-dimensional vectors is ubiquitous in Machine Learning, Scientific Computing, Geo-Spatial Analysis, and Information Retrieval.
|
|
6
|
-
These algorithms generally have linear complexity in time, constant complexity in space, and are data-parallel.
|
|
7
|
-
In other words, it is easily parallelizable and vectorizable and often available in packages like BLAS and LAPACK, as well as higher-level `numpy` and `scipy` Python libraries.
|
|
4
|
+
These algorithms generally have linear complexity in time, constant or linear complexity in space, and are data-parallel.
|
|
5
|
+
In other words, it is easily parallelizable and vectorizable and often available in packages like BLAS (level 1) and LAPACK, as well as higher-level `numpy` and `scipy` Python libraries.
|
|
8
6
|
Ironically, even with decades of evolution in compilers and numerical computing, [most libraries can be 3-200x slower than hardware potential][benchmarks] even on the most popular hardware, like 64-bit x86 and Arm CPUs.
|
|
9
|
-
|
|
7
|
+
Moreover, most lack mixed-precision support, which is crucial for modern AI!
|
|
8
|
+
The rare few that support minimal mixed precision, run only on one platform, and are vendor-locked, by companies like Intel and Nvidia.
|
|
9
|
+
SimSIMD provides an alternative.
|
|
10
10
|
1️⃣ SimSIMD functions are practically as fast as `memcpy`.
|
|
11
|
-
2️⃣
|
|
11
|
+
2️⃣ Unlike BLAS, most kernels are designed for mixed-precision and bit-level operations.
|
|
12
|
+
3️⃣ SimSIMD [compiles to more platforms than NumPy (105 vs 35)][compatibility] and has more backends than most BLAS implementations, and more high-level interfaces than most libraries.
|
|
12
13
|
|
|
13
14
|
[benchmarks]: https://ashvardanian.com/posts/simsimd-faster-scipy
|
|
14
15
|
[compatibility]: https://pypi.org/project/simsimd/#files
|
|
@@ -41,40 +42,50 @@ SimSIMD attempts to fill that gap.
|
|
|
41
42
|
|
|
42
43
|
## Features
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
__SimSIMD__ (Arabic: "سيمسيم دي") is a library of __over 200 SIMD-optimized kernels__ for distance and similarity measures, boosting search performance in [USearch](https://github.com/unum-cloud/usearch) and several database systems.
|
|
46
|
+
Named after the iconic ["Open Sesame"](https://en.wikipedia.org/wiki/Open_sesame) command from _Ali Baba and the Forty Thieves_, it opens the doors to a modern treasure: maximizing the potential of today's hardware for high resource utilization.
|
|
45
47
|
Implemented distance functions include:
|
|
46
48
|
|
|
47
|
-
- Euclidean (L2) and Cosine (Angular) spatial distances for Vector Search.
|
|
48
|
-
- Dot-Products for real & complex vectors for DSP & Quantum computing.
|
|
49
|
-
- Hamming (~ Manhattan) and Jaccard (~ Tanimoto) bit-level distances.
|
|
50
|
-
-
|
|
51
|
-
-
|
|
52
|
-
-
|
|
53
|
-
|
|
49
|
+
- Euclidean (L2) and Cosine (Angular) spatial distances for Vector Search. _[docs][docs-spatial]_
|
|
50
|
+
- Dot-Products for real & complex vectors for DSP & Quantum computing. _[docs][docs-dot]_
|
|
51
|
+
- Hamming (~ Manhattan) and Jaccard (~ Tanimoto) bit-level distances. _[docs][docs-binary]_
|
|
52
|
+
- Set Intersections for Sparse Vectors and Text Analysis. _[docs][docs-sparse]_
|
|
53
|
+
- Mahalanobis distance and Quadratic forms for Scientific Computing. _[docs][docs-curved]_
|
|
54
|
+
- Kullback-Leibler and Jensen–Shannon divergences for probability distributions. _[docs][docs-probability]_
|
|
55
|
+
- For Levenshtein, Needleman–Wunsch, and Smith-Waterman, check [StringZilla][stringzilla].
|
|
56
|
+
- 🔜 Haversine and Vincenty's formulae for Geospatial Analysis.
|
|
57
|
+
|
|
58
|
+
[docs-spatial]: #cosine-similarity-reciprocal-square-root-and-newton-raphson-iteration
|
|
59
|
+
[docs-curved]: #curved-spaces-mahalanobis-distance-and-bilinear-quadratic-forms
|
|
60
|
+
[docs-sparse]: #set-intersection-galloping-and-binary-search
|
|
61
|
+
[docs-binary]: https://github.com/ashvardanian/SimSIMD/pull/138
|
|
62
|
+
[docs-dot]: #complex-dot-products-conjugate-dot-products-and-complex-numbers
|
|
63
|
+
[docs-probability]: #logarithms-in-kullback-leibler--jensenshannon-divergences
|
|
54
64
|
[scipy]: https://docs.scipy.org/doc/scipy/reference/spatial.distance.html#module-scipy.spatial.distance
|
|
55
65
|
[numpy]: https://numpy.org/doc/stable/reference/generated/numpy.inner.html
|
|
56
66
|
[stringzilla]: https://github.com/ashvardanian/stringzilla
|
|
57
67
|
|
|
58
68
|
Moreover, SimSIMD...
|
|
59
69
|
|
|
60
|
-
- handles `f64`, `f32`, and `
|
|
61
|
-
- handles `i8` integral and `b8`
|
|
70
|
+
- handles `f64`, `f32`, `f16`, and `bf16` real & complex vectors.
|
|
71
|
+
- handles `i8` integral and `b8` bit vectors.
|
|
62
72
|
- is a zero-dependency [header-only C 99](#using-simsimd-in-c) library.
|
|
63
|
-
- has bindings for [Python](#using-simsimd-in-python), [Rust](#using-simsimd-in-rust) and [
|
|
64
|
-
- has Arm backends for NEON
|
|
65
|
-
- has x86 backends for Haswell, Skylake, Ice Lake, and Sapphire Rapids.
|
|
73
|
+
- has bindings for [Python](#using-simsimd-in-python), [Rust](#using-simsimd-in-rust) and [JS](#using-simsimd-in-javascript).
|
|
74
|
+
- has Arm backends for NEON, Scalable Vector Extensions (SVE), and SVE2.
|
|
75
|
+
- has x86 backends for Haswell, Skylake, Ice Lake, Genoa, and Sapphire Rapids.
|
|
76
|
+
- with both compile-time and runtime CPU feature detection easily integrates anywhere!
|
|
66
77
|
|
|
67
|
-
Due to the high-level of fragmentation of SIMD support in different x86 CPUs, SimSIMD uses the names of select Intel CPU generations for its backends.
|
|
78
|
+
Due to the high-level of fragmentation of SIMD support in different x86 CPUs, SimSIMD generally uses the names of select Intel CPU generations for its backends.
|
|
68
79
|
They, however, also work on AMD CPUs.
|
|
69
80
|
Intel Haswell is compatible with AMD Zen 1/2/3, while AMD Genoa Zen 4 covers AVX-512 instructions added to Intel Skylake and Ice Lake.
|
|
70
81
|
You can learn more about the technical implementation details in the following blog-posts:
|
|
71
82
|
|
|
72
83
|
- [Uses Horner's method for polynomial approximations, beating GCC 12 by 119x](https://ashvardanian.com/posts/gcc-12-vs-avx512fp16/).
|
|
73
84
|
- [Uses Arm SVE and x86 AVX-512's masked loads to eliminate tail `for`-loops](https://ashvardanian.com/posts/simsimd-faster-scipy/#tails-of-the-past-the-significance-of-masked-loads).
|
|
74
|
-
- [
|
|
75
|
-
- [
|
|
76
|
-
-
|
|
77
|
-
-
|
|
85
|
+
- [Substitutes LibC's `sqrt` with Newton Raphson iterations](https://github.com/ashvardanian/SimSIMD/releases/tag/v5.4.0).
|
|
86
|
+
- [Uses Galloping and SVE2 histograms to intersect sparse vectors](https://ashvardanian.com/posts/simd-set-intersections-sve2-avx512/).
|
|
87
|
+
- For Python: [avoids slow PyBind11, SWIG, & `PyArg_ParseTuple`](https://ashvardanian.com/posts/pybind11-cpython-tutorial/) [using faster calling convention](https://ashvardanian.com/posts/discount-on-keyword-arguments-in-python/).
|
|
88
|
+
- For JavaScript: [uses typed arrays and NAPI for zero-copy calls](https://ashvardanian.com/posts/javascript-ai-vector-search/).
|
|
78
89
|
|
|
79
90
|
## Benchmarks
|
|
80
91
|
|
|
@@ -82,12 +93,12 @@ You can learn more about the technical implementation details in the following b
|
|
|
82
93
|
|
|
83
94
|
Given 1000 embeddings from OpenAI Ada API with 1536 dimensions, running on the Apple M2 Pro Arm CPU with NEON support, here's how SimSIMD performs against conventional methods:
|
|
84
95
|
|
|
85
|
-
| Kind | `f32` improvement | `f16` improvement | `i8` improvement | Conventional method
|
|
86
|
-
| :------------------------ | ----------------: | ----------------: | ---------------: |
|
|
87
|
-
| Inner Product
|
|
88
|
-
| Cosine Distance | __32 x__ | __79 x__ | __133 x__ | `scipy
|
|
89
|
-
| Euclidean Distance ² | __5 x__ | __26 x__ | __17 x__ | `scipy
|
|
90
|
-
| Jensen-Shannon Divergence | __31 x__ | __53 x__ | | `scipy
|
|
96
|
+
| Kind | `f32` improvement | `f16` improvement | `i8` improvement | Conventional method | SimSIMD |
|
|
97
|
+
| :------------------------ | ----------------: | ----------------: | ---------------: | :------------------------ | :-------------- |
|
|
98
|
+
| Inner (Dot) Product | __2 x__ | __9 x__ | __18 x__ | `numpy.inner` | `inner` |
|
|
99
|
+
| Cosine Distance | __32 x__ | __79 x__ | __133 x__ | `scipy.*.*.cosine` | `cosine` |
|
|
100
|
+
| Euclidean Distance ² | __5 x__ | __26 x__ | __17 x__ | `scipy.*.*.sqeuclidean` | `sqeuclidean` |
|
|
101
|
+
| Jensen-Shannon Divergence | __31 x__ | __53 x__ | | `scipy.*.*.jensenshannon` | `jensenshannon` |
|
|
91
102
|
|
|
92
103
|
### Against GCC Auto-Vectorization
|
|
93
104
|
|
|
@@ -112,7 +123,7 @@ __Broader Benchmarking Results__:
|
|
|
112
123
|
The package is intended to replace the usage of `numpy.inner`, `numpy.dot`, and `scipy.spatial.distance`.
|
|
113
124
|
Aside from drastic performance improvements, SimSIMD significantly improves accuracy in mixed precision setups.
|
|
114
125
|
NumPy and SciPy, processing `i8` or `f16` vectors, will use the same types for accumulators, while SimSIMD can combine `i8` enumeration, `i16` multiplication, and `i32` accumulation to avoid overflows entirely.
|
|
115
|
-
The same applies to processing `f16` values with `f32` precision.
|
|
126
|
+
The same applies to processing `f16` and `bf16` values with `f32` precision.
|
|
116
127
|
|
|
117
128
|
### Installation
|
|
118
129
|
|
|
@@ -123,6 +134,9 @@ pip install simsimd
|
|
|
123
134
|
python -c "import simsimd; print(simsimd.get_capabilities())"
|
|
124
135
|
```
|
|
125
136
|
|
|
137
|
+
With precompiled binaries, SimSIMD ships `.pyi` interface files for type hinting and static analysis.
|
|
138
|
+
You can check all the available functions in [`python/annotations/__init__.pyi`](https://github.com/ashvardanian/SimSIMD/blob/main/python/annotations/__init__.pyi).
|
|
139
|
+
|
|
126
140
|
### One-to-One Distance
|
|
127
141
|
|
|
128
142
|
```py
|
|
@@ -134,7 +148,7 @@ vec2 = np.random.randn(1536).astype(np.float32)
|
|
|
134
148
|
dist = simsimd.cosine(vec1, vec2)
|
|
135
149
|
```
|
|
136
150
|
|
|
137
|
-
Supported functions include `cosine`, `inner`, `sqeuclidean`, `hamming`, and `
|
|
151
|
+
Supported functions include `cosine`, `inner`, `sqeuclidean`, `hamming`, `jaccard`, `kulbackleibler`, `jensenshannon`, and `intersect`.
|
|
138
152
|
Dot products are supported for both real and complex numbers:
|
|
139
153
|
|
|
140
154
|
```py
|
|
@@ -153,6 +167,8 @@ dist = simsimd.cosine(vec1, vec2, "i8")
|
|
|
153
167
|
dist = simsimd.cosine(vec1, vec2, "f16")
|
|
154
168
|
dist = simsimd.cosine(vec1, vec2, "f32")
|
|
155
169
|
dist = simsimd.cosine(vec1, vec2, "f64")
|
|
170
|
+
dist = simsimd.hamming(vec1, vec2, "bits")
|
|
171
|
+
dist = simsimd.jaccard(vec1, vec2, "bits")
|
|
156
172
|
```
|
|
157
173
|
|
|
158
174
|
It also allows using SimSIMD for half-precision complex numbers, which NumPy does not support.
|
|
@@ -165,6 +181,22 @@ simd.dot(vec1, vec2, "complex32")
|
|
|
165
181
|
simd.vdot(vec1, vec2, "complex32")
|
|
166
182
|
```
|
|
167
183
|
|
|
184
|
+
When dealing with sparse representations and integer sets, you can apply the `intersect` function to two 1-dimensional arrays of `uint16` or `uint32` integers:
|
|
185
|
+
|
|
186
|
+
```py
|
|
187
|
+
from random import randint
|
|
188
|
+
import numpy as np
|
|
189
|
+
import simsimd as simd
|
|
190
|
+
|
|
191
|
+
length1, length2 = randint(1, 100), randint(1, 100)
|
|
192
|
+
vec1 = np.random.randint(0, 1000, length1).astype(np.uint16)
|
|
193
|
+
vec2 = np.random.randint(0, 1000, length2).astype(np.uint16)
|
|
194
|
+
|
|
195
|
+
slow_result = np.intersect1d(vec1, vec2)
|
|
196
|
+
fast_result = simd.intersect(vec1, vec2)
|
|
197
|
+
assert slow_result == fast_result
|
|
198
|
+
```
|
|
199
|
+
|
|
168
200
|
### One-to-Many Distances
|
|
169
201
|
|
|
170
202
|
Every distance function can be used not only for one-to-one but also one-to-many and many-to-many distance calculations.
|
|
@@ -205,18 +237,44 @@ from simsimd import cdist, DistancesTensor
|
|
|
205
237
|
|
|
206
238
|
matrix1 = np.random.randn(1000, 1536).astype(np.float32)
|
|
207
239
|
matrix2 = np.random.randn(10, 1536).astype(np.float32)
|
|
208
|
-
distances: DistancesTensor = simsimd.cdist(matrix1, matrix2, metric="cosine")
|
|
209
|
-
distances_array: np.ndarray = np.array(distances, copy=True)
|
|
240
|
+
distances: DistancesTensor = simsimd.cdist(matrix1, matrix2, metric="cosine") # zero-copy, managed by SimSIMD
|
|
241
|
+
distances_array: np.ndarray = np.array(distances, copy=True) # now managed by NumPy
|
|
210
242
|
```
|
|
211
243
|
|
|
212
|
-
### Multithreading
|
|
244
|
+
### Multithreading and Memory Usage
|
|
213
245
|
|
|
214
246
|
By default, computations use a single CPU core.
|
|
215
|
-
To
|
|
216
|
-
|
|
247
|
+
To override this behavior, use the `threads` argument.
|
|
248
|
+
Set it to `0` to use all available CPU cores.
|
|
249
|
+
Here is an example of dealing with large sets of binary vectors:
|
|
250
|
+
|
|
251
|
+
```py
|
|
252
|
+
ndim = 1536 # OpenAI Ada embeddings
|
|
253
|
+
matrix1 = np.packbits(np.random.randint(2, size=(10_000, ndim)).astype(np.uint8))
|
|
254
|
+
matrix2 = np.packbits(np.random.randint(2, size=(1_000, ndim)).astype(np.uint8))
|
|
255
|
+
|
|
256
|
+
distances = simsimd.cdist(matrix1, matrix2,
|
|
257
|
+
metric="hamming", # Unlike SciPy, SimSIMD doesn't divide by the number of dimensions
|
|
258
|
+
out_dtype="u8", # so we can use `u8` instead of `f64` to save memory.
|
|
259
|
+
threads=0, # Use all CPU cores with OpenMP.
|
|
260
|
+
dtype="b8", # Override input argument type to `b8` eight-bit words.
|
|
261
|
+
)
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
By default, the output distances will be stored in double-precision `f64` floating-point numbers.
|
|
265
|
+
That behavior may not be space-efficient, especially if you are computing the hamming distance between short binary vectors, that will generally fit into 8x smaller `u8` or `u16` types.
|
|
266
|
+
To override this behavior, use the `dtype` argument.
|
|
267
|
+
|
|
268
|
+
### Helper Functions
|
|
269
|
+
|
|
270
|
+
You can turn specific backends on or off depending on the exact environment.
|
|
271
|
+
A common case may be avoiding AVX-512 on older AMD CPUs and [Intel Ice Lake](https://travisdowns.github.io/blog/2020/08/19/icl-avx512-freq.html) CPUs to ensure the CPU doesn't change the frequency license and throttle performance.
|
|
217
272
|
|
|
218
273
|
```py
|
|
219
|
-
|
|
274
|
+
$ simsimd.get_capabilities()
|
|
275
|
+
> {'serial': True, 'neon': False, 'sve': False, 'neon_f16': False, 'sve_f16': False, 'neon_bf16': False, 'sve_bf16': False, 'neon_i8': False, 'sve_i8': False, 'haswell': True, 'skylake': True, 'ice': True, 'genoa': True, 'sapphire': True}
|
|
276
|
+
$ simsimd.disable_capability("sapphire")
|
|
277
|
+
$ simsimd.enable_capability("sapphire")
|
|
220
278
|
```
|
|
221
279
|
|
|
222
280
|
### Using Python API with USearch
|
|
@@ -409,9 +467,9 @@ expected = np.inner(a_f32rounded, b_f32rounded)
|
|
|
409
467
|
result = simd.inner(a_bf16, b_bf16, "bf16")
|
|
410
468
|
```
|
|
411
469
|
|
|
412
|
-
### Dynamic Dispatch
|
|
470
|
+
### Dynamic Dispatch in Rust
|
|
413
471
|
|
|
414
|
-
SimSIMD provides a dynamic dispatch mechanism to select the most advanced micro-kernel for the current CPU.
|
|
472
|
+
SimSIMD provides a [dynamic dispatch](#dynamic-dispatch) mechanism to select the most advanced micro-kernel for the current CPU.
|
|
415
473
|
You can query supported backends and use the `SimSIMD::capabilities` function to select the best one.
|
|
416
474
|
|
|
417
475
|
```rust
|
|
@@ -448,7 +506,7 @@ console.log('Squared Euclidean Distance:', distance);
|
|
|
448
506
|
```
|
|
449
507
|
|
|
450
508
|
Other numeric types and precision levels are supported as well.
|
|
451
|
-
For double-
|
|
509
|
+
For double-precision floating-point numbers, use `Float64Array`:
|
|
452
510
|
|
|
453
511
|
```js
|
|
454
512
|
const vectorA = new Float64Array([1.0, 2.0, 3.0]);
|
|
@@ -457,11 +515,11 @@ const distance = cosine(vectorA, vectorB);
|
|
|
457
515
|
```
|
|
458
516
|
|
|
459
517
|
When doing machine learning and vector search with high-dimensional vectors you may want to quantize them to 8-bit integers.
|
|
460
|
-
You may want to project values from the $[-1, 1]$ range to the $[-
|
|
518
|
+
You may want to project values from the $[-1, 1]$ range to the $[-127, 127]$ range and then cast them to `Int8Array`:
|
|
461
519
|
|
|
462
520
|
```js
|
|
463
|
-
const quantizedVectorA = new
|
|
464
|
-
const quantizedVectorB = new
|
|
521
|
+
const quantizedVectorA = new Int8Array(vectorA.map(v => (v * 127)));
|
|
522
|
+
const quantizedVectorB = new Int8Array(vectorB.map(v => (v * 127)));
|
|
465
523
|
const distance = cosine(quantizedVectorA, quantizedVectorB);
|
|
466
524
|
```
|
|
467
525
|
|
|
@@ -500,22 +558,25 @@ int main() {
|
|
|
500
558
|
simsimd_f32_t vector_a[1536];
|
|
501
559
|
simsimd_f32_t vector_b[1536];
|
|
502
560
|
simsimd_metric_punned_t distance_function = simsimd_metric_punned(
|
|
503
|
-
simsimd_metric_cos_k,
|
|
561
|
+
simsimd_metric_cos_k, // Metric kind, like the angular cosine distance
|
|
504
562
|
simsimd_datatype_f32_k, // Data type, like: f16, f32, f64, i8, b8, and complex variants
|
|
505
|
-
simsimd_cap_any_k);
|
|
563
|
+
simsimd_cap_any_k); // Which CPU capabilities are we allowed to use
|
|
506
564
|
simsimd_distance_t distance;
|
|
507
565
|
distance_function(vector_a, vector_b, 1536, &distance);
|
|
508
566
|
return 0;
|
|
509
567
|
}
|
|
510
568
|
```
|
|
511
569
|
|
|
512
|
-
### Dynamic Dispatch
|
|
570
|
+
### Dynamic Dispatch in C
|
|
513
571
|
|
|
514
572
|
To avoid hard-coding the backend, you can rely on `c/lib.c` to prepackage all possible backends in one binary, and select the most recent CPU features at runtime.
|
|
515
|
-
That feature of the C library is called dynamic dispatch and is extensively used in the Python, JavaScript, and Rust bindings.
|
|
573
|
+
That feature of the C library is called [dynamic dispatch](#dynamic-dispatch) and is extensively used in the Python, JavaScript, and Rust bindings.
|
|
516
574
|
To test which CPU features are available on the machine at runtime, use the following APIs:
|
|
517
575
|
|
|
518
576
|
```c
|
|
577
|
+
int uses_dynamic_dispatch = simsimd_uses_dynamic_dispatch(); // Check if dynamic dispatch was enabled
|
|
578
|
+
simsimd_capability_t capabilities = simsimd_capabilities(); // Returns a bitmask
|
|
579
|
+
|
|
519
580
|
int uses_neon = simsimd_uses_neon();
|
|
520
581
|
int uses_sve = simsimd_uses_sve();
|
|
521
582
|
int uses_haswell = simsimd_uses_haswell();
|
|
@@ -523,11 +584,9 @@ int uses_skylake = simsimd_uses_skylake();
|
|
|
523
584
|
int uses_ice = simsimd_uses_ice();
|
|
524
585
|
int uses_genoa = simsimd_uses_genoa();
|
|
525
586
|
int uses_sapphire = simsimd_uses_sapphire();
|
|
526
|
-
|
|
527
|
-
simsimd_capability_t capabilities = simsimd_capabilities();
|
|
528
587
|
```
|
|
529
588
|
|
|
530
|
-
To
|
|
589
|
+
To override compilation settings and switch between runtime and compile-time dispatch, define the following macro:
|
|
531
590
|
|
|
532
591
|
```c
|
|
533
592
|
#define SIMSIMD_DYNAMIC_DISPATCH 1 // or 0
|
|
@@ -647,116 +706,345 @@ To explicitly disable half-precision support, define the following macro before
|
|
|
647
706
|
#include <simsimd/simsimd.h>
|
|
648
707
|
```
|
|
649
708
|
|
|
650
|
-
###
|
|
709
|
+
### Compilation Settings and Debugging
|
|
710
|
+
|
|
711
|
+
`SIMSIMD_DYNAMIC_DISPATCH`:
|
|
712
|
+
|
|
713
|
+
> By default, SimSIMD is a header-only library.
|
|
714
|
+
> But if you are running on different generations of devices, it makes sense to pre-compile the library for all supported generations at once, and dispatch at runtime.
|
|
715
|
+
> This flag does just that and is used to produce the `simsimd.so` shared library, as well as the Python and other bindings.
|
|
716
|
+
|
|
717
|
+
`SIMSIMD_TARGET_ARM` (`SIMSIMD_TARGET_NEON`, `SIMSIMD_TARGET_SVE`, `SIMSIMD_TARGET_SVE2`, `SIMSIMD_TARGET_NEON_F16`, `SIMSIMD_TARGET_SVE_F16`, `SIMSIMD_TARGET_NEON_BF16`, `SIMSIMD_TARGET_SVE_BF16`), `SIMSIMD_TARGET_X86` (`SIMSIMD_TARGET_HASWELL`, `SIMSIMD_TARGET_SKYLAKE`, `SIMSIMD_TARGET_ICE`, `SIMSIMD_TARGET_GENOA`, `SIMSIMD_TARGET_SAPPHIRE`):
|
|
718
|
+
|
|
719
|
+
> By default, SimSIMD automatically infers the target architecture and pre-compiles as many kernels as possible.
|
|
720
|
+
> In some cases, you may want to explicitly disable some of the kernels.
|
|
721
|
+
> Most often it's due to compiler support issues, like the lack of some recent intrinsics or low-precision numeric types.
|
|
722
|
+
> In other cases, you may want to disable some kernels to speed up the compilation process and trim the binary size.
|
|
723
|
+
|
|
724
|
+
`SIMSIMD_SQRT`, `SIMSIMD_RSQRT`, `SIMSIMD_LOG`:
|
|
725
|
+
|
|
726
|
+
> By default, for __non__-SIMD backends, SimSIMD may use `libc` functions like `sqrt` and `log`.
|
|
727
|
+
> Those are generally very accurate, but slow, and introduce a dependency on the C standard library.
|
|
728
|
+
> To avoid that you can override those definitions with your custom implementations, like: `#define SIMSIMD_RSQRT(x) (1 / sqrt(x))`.
|
|
729
|
+
|
|
730
|
+
## Algorithms & Design Decisions 📚
|
|
731
|
+
|
|
732
|
+
In general there are a few principles that SimSIMD follows:
|
|
733
|
+
|
|
734
|
+
- Avoid loop unrolling.
|
|
735
|
+
- Never allocate memory.
|
|
736
|
+
- Never throw exceptions or set `errno`.
|
|
737
|
+
- Keep all function arguments the size of the pointer.
|
|
738
|
+
- Avoid returning from public interfaces, use out-arguments instead.
|
|
739
|
+
- Don't over-optimize for old CPUs and single- and double-precision floating-point numbers.
|
|
740
|
+
- Prioritize mixed-precision and integer operations, and new ISA extensions.
|
|
741
|
+
|
|
742
|
+
Last, but not the least - don't build unless there is a demand for it.
|
|
743
|
+
So if you have a specific use-case, please open an issue or a pull request, and ideally, bring in more users with similar needs.
|
|
744
|
+
|
|
745
|
+
### Cosine Similarity, Reciprocal Square Root, and Newton-Raphson Iteration
|
|
746
|
+
|
|
747
|
+
The cosine similarity is the most common and straightforward metric used in machine learning and information retrieval.
|
|
748
|
+
Interestingly, there are multiple ways to shoot yourself in the foot when computing it.
|
|
749
|
+
The cosine similarity is the inverse of the cosine distance, which is the cosine of the angle between two vectors.
|
|
750
|
+
|
|
751
|
+
```math
|
|
752
|
+
\text{CosineSimilarity}(a, b) = \frac{a \cdot b}{\|a\| \cdot \|b\|}
|
|
753
|
+
```
|
|
754
|
+
|
|
755
|
+
```math
|
|
756
|
+
\text{CosineDistance}(a, b) = 1 - \frac{a \cdot b}{\|a\| \cdot \|b\|}
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
In NumPy terms, SimSIMD implementation is similar to:
|
|
760
|
+
|
|
761
|
+
```python
|
|
762
|
+
import numpy as np
|
|
763
|
+
|
|
764
|
+
def cos_numpy(a: np.ndarray, b: np.ndarray) -> float:
|
|
765
|
+
ab, a2, b2 = np.dot(a, b), np.dot(a, a), np.dot(b, b) # Fused in SimSIMD
|
|
766
|
+
if a2 == 0 and b2 == 0: result = 0 # Same in SciPy
|
|
767
|
+
elif ab == 0: result = 1 # Division by zero error in SciPy
|
|
768
|
+
else: result = 1 - ab / (sqrt(a2) * sqrt(b2)) # Bigger rounding error in SciPy
|
|
769
|
+
return result
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
In SciPy, however, the cosine distance is computed as `1 - ab / np.sqrt(a2 * b2)`.
|
|
773
|
+
It handles the edge case of a zero and non-zero argument pair differently, resulting in a division by zero error.
|
|
774
|
+
It's not only less efficient, but also less accurate, given how the reciprocal square roots are computed.
|
|
775
|
+
The C standard library provides the `sqrt` function, which is generally very accurate, but slow.
|
|
776
|
+
The `rsqrt` in-hardware implementations are faster, but have different accuracy characteristics.
|
|
777
|
+
|
|
778
|
+
- SSE `rsqrtps` and AVX `vrsqrtps`: $1.5 \times 2^{-12}$ maximal error.
|
|
779
|
+
- AVX-512 `vrsqrt14pd` instruction: $2^{-14}$ maximal error.
|
|
780
|
+
- NEON `frsqrte` instruction has no clear error bounds.
|
|
781
|
+
|
|
782
|
+
To overcome the limitations of the `rsqrt` instruction, SimSIMD uses the Newton-Raphson iteration to refine the initial estimate for high-precision floating-point numbers.
|
|
783
|
+
It can be defined as:
|
|
784
|
+
|
|
785
|
+
```math
|
|
786
|
+
x_{n+1} = x_n \cdot (3 - x_n \cdot x_n) / 2
|
|
787
|
+
```
|
|
788
|
+
|
|
789
|
+
On 1536-dimensional inputs on Intel Sapphire Rapids CPU a single such iteration can result in a 2-3 orders of magnitude relative error reduction:
|
|
790
|
+
|
|
791
|
+
| Datatype | NumPy Error | SimSIMD w/out Iteration | SimSIMD |
|
|
792
|
+
| :--------- | ------------------: | ----------------------: | ------------------: |
|
|
793
|
+
| `bfloat16` | 1.89e-08 ± 1.59e-08 | 3.07e-07 ± 3.09e-07 | 3.53e-09 ± 2.70e-09 |
|
|
794
|
+
| `float16` | 1.67e-02 ± 1.44e-02 | 2.68e-05 ± 1.95e-05 | 2.02e-05 ± 1.39e-05 |
|
|
795
|
+
| `float32` | 2.21e-08 ± 1.65e-08 | 3.47e-07 ± 3.49e-07 | 3.77e-09 ± 2.84e-09 |
|
|
796
|
+
| `float64` | 0.00e+00 ± 0.00e+00 | 3.80e-07 ± 4.50e-07 | 1.35e-11 ± 1.85e-11 |
|
|
797
|
+
|
|
798
|
+
### Curved Spaces, Mahalanobis Distance, and Bilinear Quadratic Forms
|
|
799
|
+
|
|
800
|
+
The Mahalanobis distance is a generalization of the Euclidean distance, which takes into account the covariance of the data.
|
|
801
|
+
It's very similar in its form to the bilinear form, which is a generalization of the dot product.
|
|
802
|
+
|
|
803
|
+
```math
|
|
804
|
+
\text{BilinearForm}(a, b, M) = a^T M b
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
```math
|
|
808
|
+
\text{Mahalanobis}(a, b, M) = \sqrt{(a - b)^T M^{-1} (a - b)}
|
|
809
|
+
```
|
|
810
|
+
|
|
811
|
+
Bilinear Forms can be seen as one of the most important linear algebraic operations, surprisingly missing in BLAS and LAPACK.
|
|
812
|
+
They are versatile and appear in various domains:
|
|
813
|
+
|
|
814
|
+
- In Quantum Mechanics, the expectation value of an observable $A$ in a state $\psi$ is given by $\langle \psi | A | \psi \rangle$, which is a bilinear form.
|
|
815
|
+
- In Machine Learning, in Support Vector Machines (SVMs), bilinear forms define kernel functions that measure similarity between data points.
|
|
816
|
+
- In Differential Geometry, the metric tensor, which defines distances and angles on a manifold, is a bilinear form on the tangent space.
|
|
817
|
+
- In Economics, payoff functions in certain Game Theoretic problems can be modeled as bilinear forms of players' strategies.
|
|
818
|
+
- In Physics, interactions between electric and magnetic fields can be expressed using bilinear forms.
|
|
819
|
+
|
|
820
|
+
Broad applications aside, the lack of a specialized primitive for bilinear forms in BLAS and LAPACK means significant performance overhead.
|
|
821
|
+
A $vector * matrix * vector$ product is a scalar, whereas its constituent parts ($vector * matrix$ and $matrix * vector$) are vectors:
|
|
822
|
+
|
|
823
|
+
- They need memory to be stored in: $O(n)$ allocation.
|
|
824
|
+
- The data will be written to memory and read back, wasting CPU cycles.
|
|
825
|
+
|
|
826
|
+
SimSIMD doesn't produce intermediate vector results, like `a @ M @ b`, but computes the bilinear form directly.
|
|
827
|
+
|
|
828
|
+
### Set Intersection, Galloping, and Binary Search
|
|
829
|
+
|
|
830
|
+
The set intersection operation is generally defined as the number of elements that are common between two sets, represented as sorted arrays of integers.
|
|
831
|
+
The most common way to compute it is a linear scan:
|
|
832
|
+
|
|
833
|
+
```c
|
|
834
|
+
size_t intersection_size(int *a, int *b, size_t n, size_t m) {
|
|
835
|
+
size_t i = 0, j = 0, count = 0;
|
|
836
|
+
while (i < n && j < m) {
|
|
837
|
+
if (a[i] < b[j]) i++;
|
|
838
|
+
else if (a[i] > b[j]) j++;
|
|
839
|
+
else i++, j++, count++;
|
|
840
|
+
}
|
|
841
|
+
return count;
|
|
842
|
+
}
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
Alternatively, one can use the binary search to find the elements in the second array that are present in the first one.
|
|
846
|
+
On every step the checked region of the second array is halved, which is called the _galloping search_.
|
|
847
|
+
It's faster, but only when large arrays of very different sizes are intersected.
|
|
848
|
+
Third approach is to use the SIMD instructions to compare multiple elements at once:
|
|
849
|
+
|
|
850
|
+
- Using string-intersection instructions on x86, like `pcmpestrm`.
|
|
851
|
+
- Using integer-intersection instructions in AVX-512, like `vp2intersectd`.
|
|
852
|
+
- Using vanilla equality checks present in all SIMD instruction sets.
|
|
853
|
+
|
|
854
|
+
After benchmarking, the last approach was chosen, as it's the most flexible and often the fastest.
|
|
855
|
+
|
|
856
|
+
### Complex Dot Products, Conjugate Dot Products, and Complex Numbers
|
|
857
|
+
|
|
858
|
+
Complex dot products are a generalization of the dot product to complex numbers.
|
|
859
|
+
They are supported by most BLAS packages, but almost never in mixed precision.
|
|
860
|
+
SimSIMD defines `dot` and `vdot` kernels as:
|
|
861
|
+
|
|
862
|
+
```math
|
|
863
|
+
\text{dot}(a, b) = \sum_{i=0}^{n-1} a_i \cdot b_i
|
|
864
|
+
```
|
|
865
|
+
|
|
866
|
+
```math
|
|
867
|
+
\text{vdot}(a, b) = \sum_{i=0}^{n-1} a_i \cdot \bar{b_i}
|
|
868
|
+
```
|
|
869
|
+
|
|
870
|
+
Where $\bar{b_i}$ is the complex conjugate of $b_i$.
|
|
871
|
+
Putting that into Python code for scalar arrays:
|
|
872
|
+
|
|
873
|
+
```python
|
|
874
|
+
def dot(a: List[number], b: List[number]) -> number:
|
|
875
|
+
a_real, a_imaginary = a[0::2], a[1::2]
|
|
876
|
+
b_real, b_imaginary = b[0::2], b[1::2]
|
|
877
|
+
ab_real, ab_imaginary = 0, 0
|
|
878
|
+
for ar, ai, br, bi in zip(a_real, a_imaginary, b_real, b_imaginary):
|
|
879
|
+
ab_real += ar * br - ai * bi
|
|
880
|
+
ab_imaginary += ar * bi + ai * br
|
|
881
|
+
return ab_real, ab_imaginary
|
|
882
|
+
|
|
883
|
+
def vdot(a: List[number], b: List[number]) -> number:
|
|
884
|
+
a_real, a_imaginary = a[0::2], a[1::2]
|
|
885
|
+
b_real, b_imaginary = b[0::2], b[1::2]
|
|
886
|
+
ab_real, ab_imaginary = 0, 0
|
|
887
|
+
for ar, ai, br, bi in zip(a_real, a_imaginary, b_real, b_imaginary):
|
|
888
|
+
ab_real += ar * br + ai * bi
|
|
889
|
+
ab_imaginary += ar * bi - ai * br
|
|
890
|
+
return ab_real, ab_imaginary
|
|
891
|
+
```
|
|
892
|
+
|
|
893
|
+
### Logarithms in Kullback-Leibler & Jensen–Shannon Divergences
|
|
894
|
+
|
|
895
|
+
The Kullback-Leibler divergence is a measure of how one probability distribution diverges from a second, expected probability distribution.
|
|
896
|
+
Jensen-Shannon divergence is a symmetrized and smoothed version of the Kullback-Leibler divergence, which can be used as a distance metric between probability distributions.
|
|
897
|
+
|
|
898
|
+
```math
|
|
899
|
+
\text{KL}(P || Q) = \sum_{i} P(i) \log \frac{P(i)}{Q(i)}
|
|
900
|
+
```
|
|
901
|
+
|
|
902
|
+
```math
|
|
903
|
+
\text{JS}(P, Q) = \frac{1}{2} \text{KL}(P || M) + \frac{1}{2} \text{KL}(Q || M), M = \frac{P + Q}{2}
|
|
904
|
+
```
|
|
905
|
+
|
|
906
|
+
Both functions are defined for non-negative numbers, and the logarithm is a key part of their computation.
|
|
907
|
+
|
|
908
|
+
### Dynamic Dispatch
|
|
909
|
+
|
|
910
|
+
Most popular software is precompiled and distributed with fairly conservative CPU optimizations, to ensure compatibility with older hardware.
|
|
911
|
+
Database Management platforms, like ClickHouse, and Web Browsers, like Google Chrome,need to run on billions of devices, and they can't afford to be picky about the CPU features.
|
|
912
|
+
For such users SimSIMD provides a dynamic dispatch mechanism, which selects the most advanced micro-kernel for the current CPU at runtime.
|
|
913
|
+
|
|
914
|
+
<table>
|
|
915
|
+
<tr>
|
|
916
|
+
<th>Subset</th>
|
|
917
|
+
<th>F</th>
|
|
918
|
+
<th>CD</th>
|
|
919
|
+
<th>ER</th>
|
|
920
|
+
<th>PF</th>
|
|
921
|
+
<th>4FMAPS</th>
|
|
922
|
+
<th>4VNNIW</th>
|
|
923
|
+
<th>VPOPCNTDQ</th>
|
|
924
|
+
<th>VL</th>
|
|
925
|
+
<th>DQ</th>
|
|
926
|
+
<th>BW</th>
|
|
927
|
+
<th>IFMA</th>
|
|
928
|
+
<th>VBMI</th>
|
|
929
|
+
<th>VNNI</th>
|
|
930
|
+
<th>BF16</th>
|
|
931
|
+
<th>VBMI2</th>
|
|
932
|
+
<th>BITALG</th>
|
|
933
|
+
<th>VPCLMULQDQ</th>
|
|
934
|
+
<th>GFNI</th>
|
|
935
|
+
<th>VAES</th>
|
|
936
|
+
<th>VP2INTERSECT</th>
|
|
937
|
+
<th>FP16</th>
|
|
938
|
+
</tr>
|
|
939
|
+
<tr>
|
|
940
|
+
<td><a href="https://en.wikipedia.org/wiki/Xeon_Phi#Knights_Landing">Knights Landing</a> (Xeon Phi x200, 2016)</td>
|
|
941
|
+
<td colspan="2" rowspan="9" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
942
|
+
<td colspan="2" rowspan="2" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
943
|
+
<td colspan="17" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
944
|
+
</tr>
|
|
945
|
+
<tr>
|
|
946
|
+
<td><a href="https://en.wikipedia.org/wiki/Xeon_Phi#Knights_Mill">Knights Mill</a> (Xeon Phi x205, 2017)</td>
|
|
947
|
+
<td colspan="3" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
948
|
+
<td colspan="14" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
949
|
+
</tr>
|
|
950
|
+
<tr>
|
|
951
|
+
<td>
|
|
952
|
+
<a href="https://en.wikipedia.org/wiki/Skylake_(microarchitecture)#Skylake-SP_(14_nm)_Scalable_Performance">Skylake-SP</a>,
|
|
953
|
+
<a href="https://en.wikipedia.org/wiki/Skylake_(microarchitecture)#Mainstream_desktop_processors">Skylake-X</a> (2017)
|
|
954
|
+
</td>
|
|
955
|
+
<td colspan="4" rowspan="11" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
956
|
+
<td rowspan="4" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
957
|
+
<td colspan="3" rowspan="4" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
958
|
+
<td colspan="11" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
959
|
+
</tr>
|
|
960
|
+
<tr>
|
|
961
|
+
<td><a href="https://en.wikipedia.org/wiki/Cannon_Lake_(microarchitecture)">Cannon Lake</a> (2018)</td>
|
|
962
|
+
<td colspan="2" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
963
|
+
<td colspan="9" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
964
|
+
</tr>
|
|
965
|
+
<tr>
|
|
966
|
+
<td><a href="https://en.wikipedia.org/wiki/Cascade_Lake_(microarchitecture)">Cascade Lake</a> (2019)</td>
|
|
967
|
+
<td colspan="2" rowspan="2" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
968
|
+
<td rowspan="2" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
969
|
+
<td colspan="8" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
970
|
+
</tr>
|
|
971
|
+
<tr>
|
|
972
|
+
<td><a href="https://en.wikipedia.org/wiki/Cooper_Lake_(microarchitecture)">Cooper Lake</a> (2020)</td>
|
|
973
|
+
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
974
|
+
<td colspan="7" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
975
|
+
</tr>
|
|
976
|
+
<tr>
|
|
977
|
+
<td><a href="https://en.wikipedia.org/wiki/Ice_Lake_(microarchitecture)">Ice Lake</a> (2019)</td>
|
|
978
|
+
<td colspan="7" rowspan="3" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
979
|
+
<td rowspan="3" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
980
|
+
<td colspan="5" rowspan="3" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
981
|
+
<td colspan="2" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
982
|
+
</tr>
|
|
983
|
+
<tr>
|
|
984
|
+
<td><a href="https://en.wikipedia.org/wiki/Tiger_Lake_(microarchitecture)">Tiger Lake</a> (2020)</td>
|
|
985
|
+
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
986
|
+
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
987
|
+
</tr>
|
|
988
|
+
<tr>
|
|
989
|
+
<td><a href="https://en.wikipedia.org/wiki/Rocket_Lake">Rocket Lake</a> (2021)</td>
|
|
990
|
+
<td colspan="2" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
991
|
+
</tr>
|
|
992
|
+
<tr>
|
|
993
|
+
<td><a href="https://en.wikipedia.org/wiki/Alder_Lake">Alder Lake</a> (2021)</td>
|
|
994
|
+
<td colspan="2" style="background:#FFB;color:black;vertical-align:middle;text-align:center;">Partial</td>
|
|
995
|
+
<td colspan="15" style="background:#FFB;color:black;vertical-align:middle;text-align:center;">Partial</td>
|
|
996
|
+
</tr>
|
|
997
|
+
<tr>
|
|
998
|
+
<td><a href="https://en.wikipedia.org/wiki/Zen_4">Zen 4</a> (2022)</td>
|
|
999
|
+
<td colspan="2" rowspan="3" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
1000
|
+
<td colspan="13" rowspan="3" style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
1001
|
+
<td colspan="2" style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
1002
|
+
</tr>
|
|
1003
|
+
<tr>
|
|
1004
|
+
<td><a href="https://en.wikipedia.org/wiki/Sapphire_Rapids_(microprocessor)">Sapphire Rapids</a> (2023)</td>
|
|
1005
|
+
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
1006
|
+
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
1007
|
+
</tr>
|
|
1008
|
+
<tr>
|
|
1009
|
+
<td><a href="https://en.wikipedia.org/wiki/Zen_5">Zen 5</a> (2024)</td>
|
|
1010
|
+
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;">Yes</td>
|
|
1011
|
+
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;">No</td>
|
|
1012
|
+
</tr>
|
|
1013
|
+
</table>
|
|
1014
|
+
|
|
1015
|
+
You can compile SimSIMD on an old CPU, like Intel Haswell, and run it on a new one, like AMD Genoa, and it will automatically use the most advanced instructions available.
|
|
1016
|
+
Reverse is also true, you can compile on a new CPU and run on an old one, and it will automatically fall back to the most basic instructions.
|
|
1017
|
+
Moreover, the very first time you prove for CPU capabilities with `simsimd_capabilities()`, it initializes the dynamic dispatch mechanism, and all subsequent calls will be faster and won't face race conditions in multi-threaded environments.
|
|
1018
|
+
|
|
1019
|
+
## Target Specific Backends
|
|
651
1020
|
|
|
652
1021
|
SimSIMD exposes all kernels for all backends, and you can select the most advanced one for the current CPU without relying on built-in dispatch mechanisms.
|
|
1022
|
+
That's handy for testing and benchmarking, but also in case you want to dispatch a very specific kernel for a very specific CPU, bypassing SimSIMD assignment logic.
|
|
653
1023
|
All of the function names follow the same pattern: `simsimd_{function}_{type}_{backend}`.
|
|
654
1024
|
|
|
655
|
-
- The backend can be `serial`, `haswell`, `skylake`, `ice`, `sapphire`, `neon`, or `sve`.
|
|
656
|
-
- The type can be `f64`, `f32`, `f16`, `f64c`, `f32c`, `f16c`, `i8`, or `b8`.
|
|
657
|
-
- The function can be `dot`, `vdot`, `cos`, `l2sq`, `hamming`, `jaccard`, `kl`, or `
|
|
1025
|
+
- The backend can be `serial`, `haswell`, `skylake`, `ice`, `genoa`, `sapphire`, `neon`, or `sve`.
|
|
1026
|
+
- The type can be `f64`, `f32`, `f16`, `bf16`, `f64c`, `f32c`, `f16c`, `bf16c`, `i8`, or `b8`.
|
|
1027
|
+
- The function can be `dot`, `vdot`, `cos`, `l2sq`, `hamming`, `jaccard`, `kl`, `js`, or `intersect`.
|
|
658
1028
|
|
|
659
1029
|
To avoid hard-coding the backend, you can use the `simsimd_metric_punned_t` to pun the function pointer and the `simsimd_capabilities` function to get the available backends at runtime.
|
|
1030
|
+
To match all the function names, consider a RegEx:
|
|
660
1031
|
|
|
661
|
-
```
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
simsimd_kl_f32_neon
|
|
681
|
-
simsimd_dot_f32_skylake
|
|
682
|
-
simsimd_cos_f32_skylake
|
|
683
|
-
simsimd_l2sq_f32_skylake
|
|
684
|
-
simsimd_js_f32_skylake
|
|
685
|
-
simsimd_kl_f32_skylake
|
|
686
|
-
simsimd_dot_f32_serial
|
|
687
|
-
simsimd_cos_f32_serial
|
|
688
|
-
simsimd_l2sq_f32_serial
|
|
689
|
-
simsimd_js_f32_serial
|
|
690
|
-
simsimd_kl_f32_serial
|
|
691
|
-
simsimd_dot_f16_sve
|
|
692
|
-
simsimd_cos_f16_sve
|
|
693
|
-
simsimd_l2sq_f16_sve
|
|
694
|
-
simsimd_dot_f16_neon
|
|
695
|
-
simsimd_cos_f16_neon
|
|
696
|
-
simsimd_l2sq_f16_neon
|
|
697
|
-
simsimd_js_f16_neon
|
|
698
|
-
simsimd_kl_f16_neon
|
|
699
|
-
simsimd_dot_f16_sapphire
|
|
700
|
-
simsimd_cos_f16_sapphire
|
|
701
|
-
simsimd_l2sq_f16_sapphire
|
|
702
|
-
simsimd_js_f16_sapphire
|
|
703
|
-
simsimd_kl_f16_sapphire
|
|
704
|
-
simsimd_dot_f16_haswell
|
|
705
|
-
simsimd_cos_f16_haswell
|
|
706
|
-
simsimd_l2sq_f16_haswell
|
|
707
|
-
simsimd_js_f16_haswell
|
|
708
|
-
simsimd_kl_f16_haswell
|
|
709
|
-
simsimd_dot_f16_serial
|
|
710
|
-
simsimd_cos_f16_serial
|
|
711
|
-
simsimd_l2sq_f16_serial
|
|
712
|
-
simsimd_js_f16_serial
|
|
713
|
-
simsimd_kl_f16_serial
|
|
714
|
-
simsimd_cos_i8_neon
|
|
715
|
-
simsimd_cos_i8_neon
|
|
716
|
-
simsimd_l2sq_i8_neon
|
|
717
|
-
simsimd_cos_i8_ice
|
|
718
|
-
simsimd_cos_i8_ice
|
|
719
|
-
simsimd_l2sq_i8_ice
|
|
720
|
-
simsimd_cos_i8_haswell
|
|
721
|
-
simsimd_cos_i8_haswell
|
|
722
|
-
simsimd_l2sq_i8_haswell
|
|
723
|
-
simsimd_cos_i8_serial
|
|
724
|
-
simsimd_cos_i8_serial
|
|
725
|
-
simsimd_l2sq_i8_serial
|
|
726
|
-
simsimd_hamming_b8_sve
|
|
727
|
-
simsimd_jaccard_b8_sve
|
|
728
|
-
simsimd_hamming_b8_neon
|
|
729
|
-
simsimd_jaccard_b8_neon
|
|
730
|
-
simsimd_hamming_b8_ice
|
|
731
|
-
simsimd_jaccard_b8_ice
|
|
732
|
-
simsimd_hamming_b8_haswell
|
|
733
|
-
simsimd_jaccard_b8_haswell
|
|
734
|
-
simsimd_hamming_b8_serial
|
|
735
|
-
simsimd_jaccard_b8_serial
|
|
736
|
-
simsimd_dot_f32c_sve
|
|
737
|
-
simsimd_vdot_f32c_sve
|
|
738
|
-
simsimd_dot_f32c_neon
|
|
739
|
-
simsimd_vdot_f32c_neon
|
|
740
|
-
simsimd_dot_f32c_haswell
|
|
741
|
-
simsimd_vdot_f32c_haswell
|
|
742
|
-
simsimd_dot_f32c_skylake
|
|
743
|
-
simsimd_vdot_f32c_skylake
|
|
744
|
-
simsimd_dot_f32c_serial
|
|
745
|
-
simsimd_vdot_f32c_serial
|
|
746
|
-
simsimd_dot_f64c_sve
|
|
747
|
-
simsimd_vdot_f64c_sve
|
|
748
|
-
simsimd_dot_f64c_skylake
|
|
749
|
-
simsimd_vdot_f64c_skylake
|
|
750
|
-
simsimd_dot_f64c_serial
|
|
751
|
-
simsimd_vdot_f64c_serial
|
|
752
|
-
simsimd_dot_f16c_sve
|
|
753
|
-
simsimd_vdot_f16c_sve
|
|
754
|
-
simsimd_dot_f16c_neon
|
|
755
|
-
simsimd_vdot_f16c_neon
|
|
756
|
-
simsimd_dot_f16c_haswell
|
|
757
|
-
simsimd_vdot_f16c_haswell
|
|
758
|
-
simsimd_dot_f16c_sapphire
|
|
759
|
-
simsimd_vdot_f16c_sapphire
|
|
760
|
-
simsimd_dot_f16c_serial
|
|
761
|
-
simsimd_vdot_f16c_serial
|
|
762
|
-
```
|
|
1032
|
+
```regex
|
|
1033
|
+
SIMSIMD_PUBLIC void simsimd_\w+_\w+_\w+\(
|
|
1034
|
+
```
|
|
1035
|
+
|
|
1036
|
+
On Linux, you can use the following command to list all unique functions:
|
|
1037
|
+
|
|
1038
|
+
```sh
|
|
1039
|
+
$ grep -oP 'SIMSIMD_PUBLIC void simsimd_\w+_\w+_\w+\(' include/simsimd/*.h | sort | uniq
|
|
1040
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_haswell(
|
|
1041
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_ice(
|
|
1042
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_neon(
|
|
1043
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_serial(
|
|
1044
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_sve(
|
|
1045
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_haswell(
|
|
1046
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_ice(
|
|
1047
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_neon(
|
|
1048
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_serial(
|
|
1049
|
+
> include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_sve(
|
|
1050
|
+
```
|