Skip to content

Commit 2988965

Browse files
connortsui20claude
andcommitted
Port the tensor scalar functions to RowFn
Progress towards #9128. `vortex.tensor.l2_norm`, `vortex.tensor.inner_product`, and `vortex.tensor.cosine_similarity` become row functions over a `TensorRow` element, which yields a slice of the extension array's storage straight to the closure. Lifting supplies the null propagation, constant folding, nullability, validity, and options serde that the three kernels each wrote by hand. Cosine similarity hoists the norm of a broadcast query vector into `visit_prepared_into`'s prepare step. The prepared and per-row arms must agree bit for bit, which only holds while both accumulate in the same order, so `l2_norm_row` moves into `utils.rs` and both call it. `BinaryTensorOpMetadata` and `build_tensor_array` move there too, shared by the two binary operators and by the normalized encoding. Constant folding through `try_build_constant_normalized` is now derived from the row closure, so the export is gone. The tests move out of the three kernel modules and into `scalar_fns/tests/`. Signed-off-by: Connor Tsui <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent 4f63e7c commit 2988965

17 files changed

Lines changed: 1882 additions & 1570 deletions

File tree

‎vortex-tensor/benches/cosine_similarity.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ use vortex_array::arrays::ConstantArray;
2222
use vortex_array::arrays::ExtensionArray;
2323
use vortex_array::arrays::FixedSizeListArray;
2424
use vortex_array::arrays::PrimitiveArray;
25+
use vortex_array::arrays::scalar_fn::ScalarFnFactoryExt;
2526
use vortex_array::dtype::DType;
2627
use vortex_array::dtype::Nullability;
2728
use vortex_array::dtype::PType;
2829
use vortex_array::scalar::Scalar;
30+
use vortex_array::scalar_fn::EmptyOptions;
2931
use vortex_array::validity::Validity;
3032
use vortex_buffer::Buffer;
3133
use vortex_tensor::scalar_fns::cosine_similarity::CosineSimilarity;
@@ -85,9 +87,9 @@ fn bench_cosine(bencher: Bencher, lhs: ArrayRef, rhs: ArrayRef) {
8587
bencher
8688
.with_inputs(|| {
8789
(
88-
CosineSimilarity::try_new_array(lhs.clone(), rhs.clone())
89-
.unwrap()
90-
.into_array(),
90+
CosineSimilarity
91+
.try_new_array(lhs.len(), EmptyOptions, [lhs.clone(), rhs.clone()])
92+
.unwrap(),
9193
session.create_execution_ctx(),
9294
)
9395
})

‎vortex-tensor/benches/inner_product.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use vortex_array::VortexSessionExecute;
2020
use vortex_array::arrays::FixedSizeListArray;
2121
use vortex_array::arrays::MaskedArray;
2222
use vortex_array::arrays::PrimitiveArray;
23+
use vortex_array::arrays::scalar_fn::ScalarFnFactoryExt;
24+
use vortex_array::scalar_fn::EmptyOptions;
2325
use vortex_array::validity::Validity;
2426
use vortex_buffer::Buffer;
2527
use vortex_tensor::scalar_fns::inner_product::InnerProduct;
@@ -62,9 +64,9 @@ fn bench_inner_product(bencher: Bencher, lhs: ArrayRef, rhs: ArrayRef) {
6264
.counter(ItemsCount::new(lhs.len()))
6365
.with_inputs(|| {
6466
(
65-
InnerProduct::try_new_array(lhs.clone(), rhs.clone())
66-
.unwrap()
67-
.into_array(),
67+
InnerProduct
68+
.try_new_array(lhs.len(), EmptyOptions, [lhs.clone(), rhs.clone()])
69+
.unwrap(),
6870
session.create_execution_ctx(),
6971
)
7072
})

‎vortex-tensor/benches/l2_norm.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use vortex_array::VortexSessionExecute;
2020
use vortex_array::arrays::FixedSizeListArray;
2121
use vortex_array::arrays::MaskedArray;
2222
use vortex_array::arrays::PrimitiveArray;
23+
use vortex_array::arrays::scalar_fn::ScalarFnFactoryExt;
24+
use vortex_array::scalar_fn::EmptyOptions;
2325
use vortex_array::validity::Validity;
2426
use vortex_buffer::Buffer;
2527
use vortex_tensor::scalar_fns::l2_norm::L2Norm;
@@ -60,7 +62,9 @@ fn bench_l2_norm(bencher: Bencher, input: ArrayRef) {
6062
.counter(ItemsCount::new(input.len()))
6163
.with_inputs(|| {
6264
(
63-
L2Norm::try_new_array(input.clone()).unwrap().into_array(),
65+
L2Norm
66+
.try_new_array(input.len(), EmptyOptions, [input.clone()])
67+
.unwrap(),
6468
session.create_execution_ctx(),
6569
)
6670
})

‎vortex-tensor/src/encodings/normalized/execute.rs‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use vortex_array::arrays::fixed_size_list::FixedSizeListArrayExt;
1414
use vortex_array::arrays::fixed_size_list::FixedSizeListArraySlotsExt;
1515
use vortex_array::builtins::ArrayBuiltins;
1616
use vortex_array::dtype::DType;
17-
use vortex_array::dtype::NativePType;
1817
use vortex_array::match_each_float_ptype;
1918
use vortex_array::scalar::Scalar;
2019
use vortex_array::scalar_fn::fns::operators::Operator;
@@ -24,6 +23,7 @@ use vortex_error::VortexExpect;
2423
use vortex_error::VortexResult;
2524

2625
use crate::matcher::AnyTensor;
26+
use crate::utils::build_tensor_array;
2727
use crate::utils::extract_flat_elements;
2828
use crate::utils::unit_norm_tolerance;
2929

@@ -115,26 +115,6 @@ fn denormalize_constant_norms(
115115
Ok(ExtensionArray::new(dtype.as_extension().clone(), storage.into_array()).into_array())
116116
}
117117

118-
/// Rebuilds a tensor-like extension array from flat primitive elements.
119-
fn build_tensor_array<T: NativePType>(
120-
dtype: DType,
121-
tensor_flat_size: usize,
122-
row_count: usize,
123-
validity: Validity,
124-
elements: Buffer<T>,
125-
) -> VortexResult<ArrayRef> {
126-
let list_size =
127-
u32::try_from(tensor_flat_size).vortex_expect("tensor flat size must fit into `u32`");
128-
129-
// SAFETY: Tensor elements are always non-nullable, so the validity carries no length.
130-
let elements = unsafe { PrimitiveArray::new_unchecked(elements, Validity::NonNullable) };
131-
132-
let storage =
133-
FixedSizeListArray::try_new(elements.into_array(), list_size, validity, row_count)?;
134-
135-
Ok(ExtensionArray::new(dtype.as_extension().clone(), storage.into_array()).into_array())
136-
}
137-
138118
/// Returns the flattened element count of each row of a tensor-like extension dtype.
139119
fn tensor_flat_size(dtype: &DType) -> usize {
140120
dtype

‎vortex-tensor/src/encodings/normalized/mod.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub use array::NormalizedSlots;
3131
mod compress;
3232
pub use compress::NormalizedScheme;
3333
pub use compress::normalize;
34-
pub(crate) use compress::try_build_constant_normalized;
3534

3635
mod execute;
3736

0 commit comments

Comments
 (0)