Skip to main content

Module vector_math

Module vector_math 

Source
Expand description

Element-wise vector math nodes (type_system_alignment.md §8.2).

These are the first compute-shaped (rather than I/O-shaped) consumers of the typed-vector family: perturbing query vectors, computing ground-truth distances in evaluations, normalizing embeddings. On jit builds the f32 hot loops execute through the cranelift-SIMD kernels (compile::jit::simd — F32X4 chunked with scalar tails); without jit, or if host ISA construction fails, the scalar reference loops below run instead. SIMD accumulation reassociates float addition, so dot products may differ from the scalar reference in the final ulps — both orders are equally valid IEEE 754 sums.

Length mismatches panic with both lengths named: silently truncating to the shorter operand would corrupt distance semantics (“never ignore silently”).

Structs§

HashVec
hash_vec(seed, dim) — deterministic synthetic f32 vector: element i is the SplitMix64 hash of (seed, i) mapped into [-1, 1). The canonical generator for synthetic embeddings — equal seeds always produce the identical vector, so dataset- free vector workloads stay replayable. Pairs with vec_norm for unit vectors.
LidMle
lid_mle(distances, k) — Levina–Bickel maximum-likelihood estimate of the local intrinsic dimensionality at one query point, from its sorted ground-truth nearest-neighbor distances.
VecAddF32
vec_add_f32(a, b) — element-wise sum of two f32 vectors. Panics when the lengths differ.
VecAddF64
vec_add_f64(a, b) — element-wise sum of two f64 vectors. Panics when the lengths differ.
VecCosineF32
vec_cosine_f32(a, b) — cosine similarity of two f32 vectors: dot(a,b) / (|a| * |b|). Returns 0.0 when either vector has zero magnitude (the conventional degenerate-case value: no direction, no similarity). Panics when the lengths differ.
VecCosineF64
vec_cosine_f64(a, b) — cosine similarity of two f64 vectors: dot(a,b) / (|a| * |b|), and 0.0 when either vector has zero magnitude, as vec_cosine_f32 answers. Panics when the lengths differ.
VecDistanceF32
vec_distance_f32(metric, a, b) — the distance between two f32 vectors as a dataset’s ground truth records it, accumulated in f64. Smaller is closer under every metric:
VecDistanceF64
vec_distance_f64(metric, a, b) — the distance between two f64 vectors as a dataset’s ground truth records it, under the metrics and conventions of vec_distance_f32.
VecDotF32
vec_dot_f32(a, b) — dot product of two f32 vectors, widened to f64 on the output wire. Panics when the lengths differ.
VecDotF64
vec_dot_f64(a, b) — dot product of two f64 vectors. Panics when the lengths differ.
VecL2F32
vec_l2_f32(a, b) — Euclidean (L2) distance between two f32 vectors. Panics when the lengths differ.
VecL2F64
vec_l2_f64(a, b) — Euclidean (L2) distance between two f64 vectors. Panics when the lengths differ.
VecNormF32
vec_norm_f32(a) — scale an f32 vector to unit L2 magnitude. A zero vector passes through unchanged (there is no direction to normalize onto, and emitting NaNs would poison downstream distance math silently).
VecNormF64
vec_norm_f64(a) — scale an f64 vector to unit L2 magnitude, a zero vector passing through unchanged as vec_norm_f32 passes it.
VecScaleF32
vec_scale_f32(a, k) — multiply every element of an f32 vector by scalar k (applied at f32 precision).
VecScaleF64
vec_scale_f64(a, k) — multiply every element of an f64 vector by scalar k.
Xxhash3Vec
xxhash3_vec(seed, dim) — deterministic synthetic f32 vector using xxHash3.

Functions§

add_f32
a + b element-wise, as a new vector.
add_f32_into
a + b element-wise into out, which is cleared first.
check_lens
Panics with both lengths named when two operands differ in length.
cosine_f32
The cosine similarity of vec_cosine.
dot_f32
The dot product, through the SIMD kernel where the host has one.
dot_scalar
The scalar dot product: the reference the SIMD kernel is checked against.
hash_vec_into
The vector of hash_vec into out, which is cleared first.
l2sq_f32
The squared L2 distance, through the SIMD kernel where the host has one.
l2sq_scalar
The scalar squared L2 distance: the reference the SIMD kernel is checked against.
lid_mle_of
The estimate of lid_mle.
norm_f32_into
a scaled to unit L2 magnitude into out; a itself when its magnitude is zero.
scale_f32
a * k element-wise, as a new vector.
scale_f32_into
a * k element-wise into out, which is cleared first.
xxhash3_vec_into
The vector of xxhash3_vec into out, which is cleared first.