
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.
These algorithms generally have linear complexity in time, constant complexity in space, and are data-parallel.
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.
Ironically, even with decades of evolution in compilers and numerical computing, most libraries can be 3-200x slower than hardware potential even on the most popular hardware, like 64-bit x86 and Arm CPUs.
SimSIMD attempts to fill that gap.
1️⃣ SimSIMD functions are practically as fast as memcpy.
2️⃣ SimSIMD compiles to more platforms than NumPy (105 vs 35) and has more backends than most BLAS implementations.
SimSIMD provides over 100 SIMD-optimized kernels for various distance and similarity measures, accelerating search in USearch and several DBMS products. Implemented distance functions include:
Moreover, SimSIMD...
f64, f32, and f16 real & complex vectors.i8 integral and b8 binary vectors.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. They, however, also work on AMD CPUs. 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. You can learn more about the technical implementation details in the following blog-posts:
for-loops.sqrt calls with bit-hacks using Jan Kadlec's constant.PyArg_ParseTuple for speed.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 |
|---|---|---|---|---|---|
| Inner Product | 2 x | 9 x | 18 x | numpy.inner | inner |
| Cosine Distance | 32 x | 79 x | 133 x | scipy.spatial.distance.cosine | cosine |
| Euclidean Distance ² | 5 x | 26 x | 17 x | scipy.spatial.distance.sqeuclidean | sqeuclidean |
| Jensen-Shannon Divergence | 31 x | 53 x | scipy.spatial.distance.jensenshannon | jensenshannon |
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 have been part of the C language since 2011.
| Kind | GCC 12 f32 | GCC 12 f16 | SimSIMD f16 | f16 improvement |
|---|---|---|---|---|
| Inner Product | 3,810 K/s | 192 K/s | 5,990 K/s | 31 x |
| Cosine Distance | 3,280 K/s | 336 K/s | 6,880 K/s | 20 x |
| Euclidean Distance ² | 4,620 K/s | 147 K/s | 5,320 K/s | 36 x |
| Jensen-Shannon Divergence | 1,180 K/s | 18 K/s | 2,140 K/s | 118 x |
Broader Benchmarking Results:
The package is intended to replace the usage of numpy.inner, numpy.dot, and scipy.spatial.distance.
Aside from drastic performance improvements, SimSIMD significantly improves accuracy in mixed precision setups.
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.
The same applies to processing f16 values with f32 precision.
Use the following snippet to install SimSIMD and list available hardware acceleration options available on your machine:
pip install simsimd python -c "import simsimd; print(simsimd.get_capabilities())"
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.
Dot products are supported for both real and complex numbers:
vec1 = np.random.randn(768).astype(np.float64) + 1j * np.random.randn(768).astype(np.float64) vec2 = np.random.randn(768).astype(np.float64) + 1j * np.random.randn(768).astype(np.float64) dist = simsimd.dot(vec1.astype(np.complex128), vec2.astype(np.complex128)) dist = simsimd.dot(vec1.astype(np.complex64), vec2.astype(np.complex64)) dist = simsimd.vdot(vec1.astype(np.complex64), vec2.astype(np.complex64)) # conjugate, same as `np.vdot`
Unlike SciPy, SimSIMD allows explicitly stating the precision of the input vectors, which is especially useful for mixed-precision setups.
dist = simsimd.cosine(vec1, vec2, "i8") dist = simsimd.cosine(vec1, vec2, "f16") dist = simsimd.cosine(vec1, vec2, "f32") dist = simsimd.cosine(vec1, vec2, "f64")
It also allows using SimSIMD for half-precision complex numbers, which NumPy does not support.
For that, view data as continuous even-length np.float16 vectors and override type-resolution with complex32 string.
vec1 = np.random.randn(1536).astype(np.float16) vec2 = np.random.randn(1536).astype(np.float16) simd.dot(vec1, vec2, "complex32") simd.vdot(vec1, vec2, "complex32")
Every distance function can be used not only for one-to-one but also one-to-many and many-to-many distance calculations. For one-to-many:
vec1 = np.random.randn(1536).astype(np.float32) # rank 1 tensor batch1 = np.random.randn(1, 1536).astype(np.float32) # rank 2 tensor batch2 = np.random.randn(100, 1536).astype(np.float32) dist_rank1 = simsimd.cosine(vec1, batch2) dist_rank2 = simsimd.cosine(batch1, batch2)
All distance functions in SimSIMD can be used to compute many-to-many distances. For two batches of 100 vectors to compute 100 distances, one would call it like this:
batch1 = np.random.randn(100, 1536).astype(np.float32) batch2 = np.random.randn(100, 1536).astype(np.float32) dist = simsimd.cosine(batch1, batch2)
Input matrices must have identical shapes. This functionality isn't natively present in NumPy or SciPy, and generally requires creating intermediate arrays, which is inefficient and memory-consuming.
One can use SimSIMD to compute distances between all possible pairs of rows across two matrices (akin to scipy.spatial.distance.cdist).
The resulting object will have a type DistancesTensor, zero-copy compatible with NumPy and other libraries.
For two arrays of 10 and 1,000 entries, the resulting tensor will have 10,000 cells:
import numpy as np from simsimd import cdist, DistancesTensor matrix1 = np.random.randn(1000, 1536).astype(np.float32) matrix2 = np.random.randn(10, 1536).astype(np.float32) distances: DistancesTensor = simsimd.cdist(matrix1, matrix2, metric="cosine") # zero-copy distances_array: np.ndarray = np.array(distances, copy=True) # now managed by NumPy
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)
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)
To install, add the following to your Cargo.toml:
[dependencies] simsimd = "..."
Before using the SimSIMD library, ensure you have imported the necessary traits and types into your Rust source file.
The library provides several traits for different distance/similarity kinds - SpatialSimilarity, BinarySimilarity, and ProbabilitySimilarity.
use simsimd::SpatialSimilarity; fn main() { let vector_a: Vec<f32> = vec![1.0, 2.0, 3.0]; let vector_b: Vec<f32> = vec![4.0, 5.0, 6.0]; // Compute the cosine similarity between vector_a and vector_b let cosine_similarity = f32::cosine(&vector_a, &vector_b) .expect("Vectors must be of the same length"); println!("Cosine Similarity: {}", cosine_similarity); // Compute the squared Euclidean distance between vector_a and vector_b let sq_euclidean_distance = f32::sqeuclidean(&vector_a, &vector_b) .expect("Vectors must be of the same length"); println!("Squared Euclidean Distance: {}", sq_euclidean_distance); }
Spatial similarity functions are available for f64, f32, f16, and i8 types.
use simsimd::SpatialSimilarity; use simsimd::ComplexProducts; fn main() { let vector_a: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0]; let vector_b: Vec<f32> = vec![5.0, 6.0, 7.0, 8.0]; // Compute the inner product between vector_a and vector_b let inner_product = SpatialSimilarity::dot(&vector_a, &vector_b) .expect("Vectors must be of the same length"); println!("Inner Product: {}", inner_product); // Compute the complex inner product between complex_vector_a and complex_vector_b let complex_inner_product = ComplexProducts::dot(&vector_a, &vector_b) .expect("Vectors must be of the same length"); let complex_conjugate_inner_product = ComplexProducts::vdot(&vector_a,


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出 。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发 流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频


实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号