Vector Similarity Functions 3x-200x Faster than SciPy and NumPy
Project description
SimSIMD 📏
Efficient Alternative to scipy.spatial.distance
and numpy.inner
SimSIMD leverages SIMD intrinsics, capabilities that only select compilers effectively utilize. This framework supports conventional AVX2 instructions on x86, NEON on Arm, as well as rare AVX-512 FP16 instructions on x86 and Scalable Vector Extensions (SVE) on Arm. Designed specifically for Machine Learning contexts, it's optimized for handling high-dimensional vector embeddings.
- ✅ 3-200x faster than NumPy and SciPy distance functions.
- ✅ Euclidean (L2), Inner Product, and Cosine (Angular) spatial distances.
- ✅ Hamming (~ Manhattan) and Jaccard (~ Tanimoto) binary distances.
- ✅ Kullback-Leibler and Jensen–Shannon divergences for probability distributions.
- ✅ Single-precision
f32
, half-precisionf16
,i8
, and binary vectors. - ✅ Compatible with GCC and Clang on MacOS and Linux, and MinGW on Windows.
- ✅ Compatible with NumPy, PyTorch, TensorFlow, and other tensors.
- ✅ Has no dependencies, not even LibC.
- ✅ JavaScript API.
- ✅ C API.
Benchmarks
Apple M2 Pro
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:
Kind | f32 improvement |
f16 improvement |
i8 improvement |
Conventional method | SimSIMD |
---|---|---|---|---|---|
Cosine | 32 x | 79 x | 133 x | scipy.spatial.distance.cosine |
cosine |
Euclidean ² | 5 x | 26 x | 17 x | scipy.spatial.distance.sqeuclidean |
sqeuclidean |
Inner Product | 2 x | 9 x | 18 x | numpy.inner |
inner |
Jensen Shannon | 31 x | 53 x | scipy.spatial.distance.jensenshannon |
jensenshannon |
Intel Sapphire Rapids
On the Intel Sapphire Rapids platform, SimSIMD was benchmarked against auto-vectorized code using GCC 12. GCC handles single-precision float
, but might not be the best choice for int8
and _Float16
arrays, which has been part of the C language since 2011.
Kind | GCC 12 f32 |
GCC 12 f16 |
SimSIMD f16 |
f16 improvement |
---|---|---|---|---|
Cosine | 3.28 M/s | 336.29 k/s | 6.88 M/s | 20 x |
Euclidean ² | 4.62 M/s | 147.25 k/s | 5.32 M/s | 36 x |
Inner Product | 3.81 M/s | 192.02 k/s | 5.99 M/s | 31 x |
Jensen Shannon | 1.18 M/s | 18.13 k/s | 2.14 M/s | 118 x |
Technical Insights:
- Uses Arm SVE and x86 AVX-512's masked loads to eliminate tail
for
-loops. - Uses AVX-512 FP16 for half-precision operations, that few compilers vectorize.
- Substitutes LibC's
sqrt
calls with bithacks using Jan Kadlec's constant. - Avoids slow PyBind11 and SWIG, directly using the CPython C API.
- Avoids slow
PyArg_ParseTuple
and manually unpacks argument tuples.
Broader Benchmarking Results:
Using in Python
Installation
pip install simsimd
Distance Between 2 Vectors
import simsimd
import numpy as np
vec1 = np.random.randn(1536).astype(np.float32)
vec2 = np.random.randn(1536).astype(np.float32)
dist = simsimd.cosine(vec1, vec2)
Supported functions include cosine
, inner
, sqeuclidean
, hamming
, and jaccard
.
Distance Between 2 Batches
batch1 = np.random.randn(100, 1536).astype(np.float32)
batch2 = np.random.randn(100, 1536).astype(np.float32)
dist = simsimd.cosine(batch1, batch2)
If either batch has more than one vector, the other batch must have one or same number of vectors. If it contains just one, the value is broadcasted.
All Pairwise Distances
For calculating distances between all possible pairs of rows across two matrices (akin to scipy.spatial.distance.cdist
):
matrix1 = np.random.randn(1000, 1536).astype(np.float32)
matrix2 = np.random.randn(10, 1536).astype(np.float32)
distances = simsimd.cdist(matrix1, matrix2, metric="cosine")
Multithreading
By default, computations use a single CPU core. To optimize and utilize all CPU cores on Linux systems, add the threads=0
argument. Alternatively, specify a custom number of threads:
distances = simsimd.cdist(matrix1, matrix2, metric="cosine", threads=0)
Hardware Backend Capabilities
To view a list of hardware backends that SimSIMD supports:
print(simsimd.get_capabilities())
Using Python API with USearch
Want to use it in Python with USearch?
You can wrap the raw C function pointers SimSIMD backends into a CompiledMetric
, and pass it to USearch, similar to how it handles Numba's JIT-compiled code.
from usearch.index import Index, CompiledMetric, MetricKind, MetricSignature
from simsimd import pointer_to_sqeuclidean, pointer_to_cosine, pointer_to_inner
metric = CompiledMetric(
pointer=pointer_to_cosine("f16"),
kind=MetricKind.Cos,
signature=MetricSignature.ArrayArraySize,
)
index = Index(256, metric=metric)
Using SimSIMD in JavaScript
After you add simsimd
as a dependency and npm install
, you will be able to call SimSIMD function on various TypedArray
variants:
const { sqeuclidean, cosine, inner, hamming, jaccard } = require('simsimd');
const vectorA = new Float32Array([1.0, 2.0, 3.0]);
const vectorB = new Float32Array([4.0, 5.0, 6.0]);
const distance = sqeuclidean(vectorA, vectorB);
console.log('Squared Euclidean Distance:', distance);
Using SimSIMD in C
If you're aiming to utilize the _Float16
functionality with SimSIMD, ensure your development environment is compatible with C 11. For other functionalities of SimSIMD, C 99 compatibility will suffice.
For integration within a CMake-based project, add the following segment to your CMakeLists.txt
:
FetchContent_Declare(
simsimd
GIT_REPOSITORY https://github.com/ashvardanian/simsimd.git
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(simsimd)
include_directories(${simsimd_SOURCE_DIR}/include)
Stay updated with the latest advancements by always using the most recent compiler available for your platform. This ensures that you benefit from the newest intrinsics.
Should you wish to integrate SimSIMD within USearch, simply compile USearch with the flag USEARCH_USE_SIMSIMD=1
. Notably, this is the default setting on the majority of platforms.
Benchmarking and Contributing
To rerun experiments utilize the following command:
cmake -DCMAKE_BUILD_TYPE=Release -DSIMSIMD_BUILD_BENCHMARKS=1 -B ./build_release
cmake --build build_release --config Release
./build_release/simsimd_bench
./build_release/simsimd_bench --benchmark_filter=js
To test and benchmark with Python bindings:
pip install -e .
pytest python/test.py -s -x
python python/bench.py --n 1000 --ndim 1000000 # batch size and dimensions
To test and benchmark JavaScript bindings:
npm install --dev
npm test
npm run bench
To test and benchmark GoLang bindings:
cd golang
go test # To test
go test -run=^$ -bench=. -benchmem # To benchmark
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for simsimd-3.5.4-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9953c65d67b26030a3ea7fc8b9159973f6135af55e076e903af257e31a9f407 |
|
MD5 | 1eb97429b4018005644789330f42ee31 |
|
BLAKE2b-256 | 60a6b4fee93c8b18adf0b0f96644cc7e91bc2f8832f9a0273dc9aac654ad3f6b |
Hashes for simsimd-3.5.4-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89b1c1bcb04ad49bbb9f7a24687c9d27d6ed508387845e70e9d728bf4b7fa7fa |
|
MD5 | 2a11540ef4f0698e7fbf8d53ad02da1a |
|
BLAKE2b-256 | c709e2d344f8407411240f189a4c1fdc2d53bdbbc694dc4c012a698a3daa070e |
Hashes for simsimd-3.5.4-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63b57ffef09658bb661349afc36e00e0a2d1fd83c40355d499d844107d6ad11f |
|
MD5 | a36b2ed1682ac56509a41ee625ea2be1 |
|
BLAKE2b-256 | 06f33b869ae29baf0abfb6bda860114478f64fa30e59af8c2eca205f7a636b8e |
Hashes for simsimd-3.5.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e572572606111107c2c2562c17ead7a9c6411af07772bc0d2fe1bf027553595e |
|
MD5 | 0615f2df94bb01b5a7ce8b4d69200d40 |
|
BLAKE2b-256 | eb693d5a1be2103930f7d626bc0dc5d4765c8b32826cbc8ed236a5795d4c8a9b |
Hashes for simsimd-3.5.4-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba2e55618a882ac42d4a7e1921572d64311485fe09856155679b694fbd63483a |
|
MD5 | d661974702ed34c9decbaddacfec1871 |
|
BLAKE2b-256 | 8caf3f4faa3c2df522cd1155de7b30eac43b020733d3535f6de2622d1f38e762 |
Hashes for simsimd-3.5.4-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ab7e6c2b1b883ea72d65c318b75443123463649d5ffe353fce1ab5c1183531c |
|
MD5 | 7eebeed047400e17acc9d42b14db5efa |
|
BLAKE2b-256 | 609a645640819e7cd475b0d04f7df3c2a1e8ff3bac07b8ecfff54dd81d42b794 |
Hashes for simsimd-3.5.4-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4292c5e28b1f292363e7260a4d9592173dcc11dc356b1c37eb22bd35cd9cde72 |
|
MD5 | bd012bc60cb4ecd2d268ec3a81b0b3a2 |
|
BLAKE2b-256 | 93963479ab8095c9072763712f7b32dc1a04a28cd064dc4aeaa1b2a23e3bca13 |
Hashes for simsimd-3.5.4-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e46f9a57b1a8f792dedce77a9f18a1c4276874b678652d1c1551a4fa719b06a |
|
MD5 | 8fdaf21cd6d22cd59342f9ac37f7bc44 |
|
BLAKE2b-256 | a381b790aa76bf3362eba149d156ea7a2617c16f79d0b02f14f3c3cb88daa9c8 |
Hashes for simsimd-3.5.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 256f3c89a66e212fa743ea7707733817b7b0a90621690f0145a21063eb8abaec |
|
MD5 | 68cc1f583df9fa33ac69a9a8e90ecd8d |
|
BLAKE2b-256 | 46842eb506d605bbdb0c7a74a620c2fba0fd61997c0758bc0ddbf6f46c4941f7 |
Hashes for simsimd-3.5.4-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b577af4d58871da470875ed4db890b220f1033f39b16f542c75c2bd676acd2a |
|
MD5 | 503d6b1b5558d350749647ffb3d78bae |
|
BLAKE2b-256 | 13b09b2eb2e19a5e8558d11c3a619053ec74c247aaaa5827077e6e2b8fbb27a1 |
Hashes for simsimd-3.5.4-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55f61bee0e21be44d31d1ac02ba9aa071db7b40fe127179f0db94370836090b5 |
|
MD5 | ad4a59f1feefe2086067dfba132cd1ca |
|
BLAKE2b-256 | 46b2a588549d486f437641cc576eaeed9c49171efd31726fafef7a1de60a0502 |
Hashes for simsimd-3.5.4-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6117b14b562cecee6d288983a4ab92f744e52121a157f8ed77680dd6c77904d1 |
|
MD5 | 5d32094d3895810a8db29e60669e4111 |
|
BLAKE2b-256 | d54bb8c1d3afe7337156f453da16f257657e987b9bc53796dfddff025b3499a4 |
Hashes for simsimd-3.5.4-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea35336bb5ca9f39df31d3b51c5c24b8bfe16e4fd9c67d6966f465bb7ab61043 |
|
MD5 | 4187bdd9ea68aeae8b389fa99b5c98da |
|
BLAKE2b-256 | 7202f91bb35d04d8d202899c411369d8e3a45b8d1497cbb987a2f268bf1a620e |
Hashes for simsimd-3.5.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd20be3f5af1979682fbfc37f357e0640e6bf382feccd71c8235dd5b347aac15 |
|
MD5 | 73cbdb7b21c5f8ee8f66198c3c674b9b |
|
BLAKE2b-256 | d9ceee76017636e701f8ca28750ae026ca5b746f937fa2da456e6a30f129f13d |
Hashes for simsimd-3.5.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7984cd26462975a05c90157a5be30ca6ac52b01d5875c97a716dfba64b35f2e |
|
MD5 | 28b929f5ef644955dd546f91e89ae449 |
|
BLAKE2b-256 | c7f29bf30f8b528e4d9fde6b11b6f5de54114ecd76ab2cfc7c7eb1b9429ffb9d |
Hashes for simsimd-3.5.4-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50ec4e6f2ceff61f933d3e8bc65f692ff2350c4713d3e7320c697ebd59796bac |
|
MD5 | a70884608466df9e48a4d9f86cf1515c |
|
BLAKE2b-256 | 18a02e23146204ab0dfade2f6688dacbc56a0a27f457bf755bbc363bda1a9646 |
Hashes for simsimd-3.5.4-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b843b1abdb280c27834cde1634a2968922a529081313aa9dc351e6c5228193d |
|
MD5 | 7f159b990ce3b66c727a2b8ef0786d9b |
|
BLAKE2b-256 | 7a5ed200d3bf3442da7e712e2e9ff3e8af4c97ba707d980dd4e1279224fcbec2 |
Hashes for simsimd-3.5.4-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff6be0a3d78661e82cefa109bf32561cf45ab9ad338512d5b0b798675e48916f |
|
MD5 | c2c40a4083641079e6a83dcdfe19d147 |
|
BLAKE2b-256 | 2d1ea8ebc0cb536fef05d288ff266395b4849ade6041a598e2ac0634b1970963 |
Hashes for simsimd-3.5.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 126a17d6a8508ee60f880da8143350310deb3b7a6051efeb408f0cf8a8d215c4 |
|
MD5 | 305fa0804307a7444d49db3606cdc800 |
|
BLAKE2b-256 | eeec1640ac3e6a69726de3d757bee50dbe10c274336cfa9ee803721ee8995c0b |
Hashes for simsimd-3.5.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c81b66f3aad7188afc910348cdff989846c7fb6b53af9338f6999dc873d3f99 |
|
MD5 | 510f8bc4b4b9525bdaeafe47810f5bf0 |
|
BLAKE2b-256 | 519a011df12eb68b3b6d912fa27bc11e4686452e546bd5ece584b16f5a5252dd |
Hashes for simsimd-3.5.4-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5da7dc19702a0f626ba3599b9d3d798f3f8ceeda9d2e30366f5c90500181922 |
|
MD5 | 4bbfa3eed62c63cecc106fadd3dcb5e6 |
|
BLAKE2b-256 | 3dc7c49a5e61d268c22506bc91b18f5afa839d1a0c21e96c812830d8fd995d06 |
Hashes for simsimd-3.5.4-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5e715086095331a00e61f92dfd456790e38deaef57005514c92541db94c4643 |
|
MD5 | 80863bd08e2dac33a949bccf907cddb5 |
|
BLAKE2b-256 | 84d8bde3fcf36acda44f28e0e272e53e3e3ae619205637cbe48649bfa51363f6 |
Hashes for simsimd-3.5.4-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa3ba90a628c2cefeb6ab0708762abc8135dee78adcdd3efafb38eadbc42e7f2 |
|
MD5 | 2bcc2aefe9e12ea7b3133ae2db51bb7b |
|
BLAKE2b-256 | 2b9586b6b8cfdb9b428ef3e7111bbc1cb13426ece19e1b0ec9e1fddfc6a96882 |
Hashes for simsimd-3.5.4-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc83484b5d62eeba2b1e1c01548ac433d5e23f9223190e910c8d130403b2d800 |
|
MD5 | 15418387bd563e8fe149d5df05112ece |
|
BLAKE2b-256 | b575afcd1d7bedfdd8cb4640cf1069e79d1a33f7b41947dacae44d6f2fc1d5de |
Hashes for simsimd-3.5.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1ac077512428b7803acc252db8e3455fd90949d04937ed693489e208d2c5cb4 |
|
MD5 | 61327c5e79a227f2614b0ee22e36b510 |
|
BLAKE2b-256 | 3c8bec6471a6d79258acfec8bd377802d3bfea4c4978082ee334891fa3511633 |
Hashes for simsimd-3.5.4-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 897b31066354a61f917093be41f301eb8a61c97b55ec2543dc31f8ef763167cd |
|
MD5 | bd4bb5a455d761397f643a56fe113f82 |
|
BLAKE2b-256 | 4c7cb5df74526fe5592ebf2e61ecafdcc2db3da528afa1227b25b593e961efc3 |
Hashes for simsimd-3.5.4-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 371fdb15f10544e40c8ee2d49f8f1117690be676c0c99261c3ed754b981ee61b |
|
MD5 | 1f85d759dbea5d82cea184649a057677 |
|
BLAKE2b-256 | 01bfc1022b990c360ab78dd87f362ee899824cc8f706d0e005748625bbf2e8d7 |