1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! SIMD distance kernels on plain slices.
//!
//! These functions need no `Tensor`, no `Runtime` and no client. They take
//! slices and return a value, so a vector-search engine can call them per pair
//! or per query.
//!
//! - Per pair: [`dot_f32`], [`l2_squared_f32`], [`manhattan_f32`],
//! [`cosine_distance_f32`], and their `_f64` twins.
//! - One query against many rows: [`dot_many_f32`], [`l2_squared_many_f32`],
//! [`manhattan_many_f32`], [`cosine_distance_many_f32`], and their `_f64`
//! twins. `rows` is row-major with `d` components per row, and `out` gets one
//! value per row.
//! - Signed 8-bit: [`dot_i8`], [`dot_i8_scaled`], [`dot_i8_many`].
//!
//! Each free function detects the SIMD level of this CPU through a cached
//! read. [`Kernels`] holds one level, so a caller can detect once and reuse
//! it, or pin a lower level such as [`SimdLevel::Scalar`].
//!
//! # Numerics
//!
//! - The SIMD kernels sum in several lanes and fuse multiply-adds. The
//! summation order differs from a sequential scalar loop and between SIMD
//! levels, so results can differ in the last bits.
//! - A NaN input gives NaN. An infinite input gives an infinite or NaN result.
//! - Cosine distance is `1 - dot / (|a| * |b|)`. A zero vector gives 0.
//! The two norms are taken before they are multiplied. Each squared norm
//! must still fit the float type: a component above about 1e19 (f32) or
//! 1e154 (f64) overflows its own square and gives NaN.
//! - For unit-length vectors, cosine distance is `1 - dot`. Use
//! [`dot_many_f32`] and subtract from 1 to skip the two norms.
//! - The i8 dot product is exact. A total outside `i32` range saturates to
//! `i32::MIN` or `i32::MAX`.
//!
//! # Panics
//!
//! Every function panics when the two lengths differ, or when a `*_many_*`
//! shape does not match. An empty pair (`d == 0`) is valid and gives 0.
//!
//! # Example
//!
//! ```
//! use numr::distance;
//!
//! let a = [1.0f32, 2.0, 3.0];
//! let b = [4.0f32, 5.0, 6.0];
//! assert_eq!(distance::dot_f32(&a, &b), 32.0);
//! assert_eq!(distance::l2_squared_f32(&a, &b), 27.0);
//! assert!(distance::cosine_distance_f32(&a, &a).abs() < 1e-6);
//!
//! // One query against two rows of 3 components.
//! let query = [1.0f32, 0.0, 0.0];
//! let rows = [1.0f32, 0.0, 0.0, 0.0, 1.0, 0.0];
//! let mut out = [0.0f32; 2];
//! distance::dot_many_f32(&query, &rows, 3, &mut out);
//! assert_eq!(out, [1.0, 0.0]);
//!
//! // Detect once, reuse for many calls.
//! let k = distance::Kernels::detect();
//! assert_eq!(k.manhattan_f32(&a, &b), 9.0);
//! ```
pub use crateSimdLevel;
pub use ;
pub use ;
pub use ;
pub use Kernels;
pub use ;
pub use ;