Skip to content

Commit 3e2b65c

Browse files
authored
Execute tensor product functions with RowFn (#9769)
## Summary - Tracking Issue: #9128 - Depends on: #9768 Moves inner product and cosine similarity to RowFn without changing their arithmetic contracts. ## Changes Inner product keeps the existing left-to-right multiply-and-sum order. Cosine computes that dot product followed by both L2 norms and the existing zero-denominator guard, with no encoded reduction, reassociation, or constant-norm shortcut. Tests cover nulls, empty and zero-width rows, IEEE overflow and underflow, and bitwise agreement between constant encodings and materialized rows. Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
1 parent 47c76eb commit 3e2b65c

4 files changed

Lines changed: 247 additions & 231 deletions

File tree

‎vortex-tensor/src/scalar_fns/arithmetic.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//! Row-level arithmetic shared by tensor scalar functions.
55
//!
66
//! These kernels preserve the scalar functions' left-to-right floating-point arithmetic. Keeping
7-
//! the arithmetic here ensures that fused and single-result operations use the same result contract.
7+
//! the arithmetic here ensures that fused and single-result operations use the same result
8+
//! contract.
89
910
use num_traits::Float;
1011
use vortex_array::dtype::NativePType;
@@ -21,3 +22,14 @@ pub(crate) fn l2_norm_row<T: Float + NativePType>(row: &[T]) -> T {
2122

2223
sum_squared.sqrt()
2324
}
25+
26+
/// Computes `sum(lhs_i * rhs_i)` for one pair of rows.
27+
///
28+
/// Both rows must have the same length. Otherwise, trailing values are ignored and the result is
29+
/// incorrect. Callers preserve the left-to-right multiplication and accumulation order.
30+
pub(crate) fn inner_product_row<T: Float + NativePType>(lhs: &[T], rhs: &[T]) -> T {
31+
lhs.iter()
32+
.zip(rhs)
33+
.map(|(&lhs_value, &rhs_value)| lhs_value * rhs_value)
34+
.fold(T::zero(), |sum, product| sum + product)
35+
}

0 commit comments

Comments
 (0)