Skip to content

Commit 3557502

Browse files
committed
Execute tensor L2 norm with RowFn
Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
1 parent 2db7f1e commit 3557502

9 files changed

Lines changed: 773 additions & 322 deletions

File tree

‎vortex-tensor/benches/l2_norm.rs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ use divan::Bencher;
1515
use divan::counter::ItemsCount;
1616
use mimalloc::MiMalloc;
1717
use vortex_array::ArrayRef;
18+
use vortex_array::EmptyMetadata;
1819
use vortex_array::IntoArray;
1920
use vortex_array::VortexSessionExecute;
21+
use vortex_array::arrays::ConstantArray;
2022
use vortex_array::arrays::FixedSizeListArray;
2123
use vortex_array::arrays::MaskedArray;
2224
use vortex_array::arrays::PrimitiveArray;
25+
use vortex_array::arrays::scalar_fn::ScalarFnFactoryExt;
26+
use vortex_array::dtype::DType;
27+
use vortex_array::dtype::Nullability;
28+
use vortex_array::dtype::PType;
29+
use vortex_array::scalar::Scalar;
30+
use vortex_array::scalar_fn::EmptyOptions;
2331
use vortex_array::validity::Validity;
2432
use vortex_buffer::Buffer;
2533
use vortex_tensor::scalar_fns::l2_norm::L2Norm;
@@ -54,13 +62,25 @@ fn vectors(width: usize) -> ArrayRef {
5462
Vector::try_new_vector_array(storage).unwrap()
5563
}
5664

65+
fn constant_vector(width: usize) -> ArrayRef {
66+
let element_dtype = DType::Primitive(PType::F64, Nullability::NonNullable);
67+
let children = (0..width)
68+
.map(|i| Scalar::primitive(((i % 97) as f64) - 48.0, Nullability::NonNullable))
69+
.collect();
70+
let storage = Scalar::fixed_size_list(element_dtype, children, Nullability::NonNullable);
71+
let vector = Scalar::extension::<Vector>(EmptyMetadata, storage);
72+
ConstantArray::new(vector, ELEMENTS / width).into_array()
73+
}
74+
5775
fn bench_l2_norm(bencher: Bencher, input: ArrayRef) {
5876
let session = vortex_array::array_session();
5977
bencher
6078
.counter(ItemsCount::new(input.len()))
6179
.with_inputs(|| {
6280
(
63-
L2Norm::try_new_array(input.clone()).unwrap().into_array(),
81+
L2Norm
82+
.try_new_array(input.len(), EmptyOptions, [input.clone()])
83+
.unwrap(),
6484
session.create_execution_ctx(),
6585
)
6686
})
@@ -80,3 +100,17 @@ fn nullable(bencher: Bencher, width: usize) {
80100
.into_array();
81101
bench_l2_norm(bencher, input);
82102
}
103+
104+
#[divan::bench(args = WIDTHS)]
105+
fn constant(bencher: Bencher, width: usize) {
106+
bench_l2_norm(bencher, constant_vector(width));
107+
}
108+
109+
#[divan::bench(args = WIDTHS)]
110+
fn nullable_constant(bencher: Bencher, width: usize) {
111+
let validity = Validity::from_iter((0..ELEMENTS / width).map(|i| i % 8 != 0));
112+
let input = MaskedArray::try_new(constant_vector(width), validity)
113+
.unwrap()
114+
.into_array();
115+
bench_l2_norm(bencher, input);
116+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use vortex_array::IntoArray;
1010
use vortex_array::arrays::PrimitiveArray;
1111
use vortex_array::arrays::ScalarFnArray;
1212
use vortex_array::arrays::scalar_fn::ScalarFnArrayView;
13+
use vortex_array::arrays::scalar_fn::ScalarFnFactoryExt;
1314
use vortex_array::arrays::scalar_fn::plugin::ScalarFnArrayParts;
1415
use vortex_array::arrays::scalar_fn::plugin::ScalarFnArrayVTable;
1516
use vortex_array::dtype::DType;
@@ -144,8 +145,8 @@ impl ScalarFnVTable for CosineSimilarity {
144145
let validity = lhs_ref.validity()?.and(rhs_ref.validity()?)?;
145146

146147
// Compute inner product and norms as columnar operations, and propagate the options.
147-
let norm_lhs_arr = L2Norm::try_new_array(lhs_ref.clone())?;
148-
let norm_rhs_arr = L2Norm::try_new_array(rhs_ref.clone())?;
148+
let norm_lhs_arr = L2Norm.try_new_array(len, EmptyOptions, [lhs_ref.clone()])?;
149+
let norm_rhs_arr = L2Norm.try_new_array(len, EmptyOptions, [rhs_ref.clone()])?;
149150
let dot_arr = InnerProduct::try_new_array(lhs_ref, rhs_ref)?;
150151

151152
// Execute to get the inner product and norms of the arrays. We only fully decompress
@@ -288,7 +289,7 @@ impl CosineSimilarity {
288289

289290
let normalized_norms: PrimitiveArray = normalized_norms.execute(ctx)?;
290291

291-
let norm_arr = L2Norm::try_new_array(plain_ref.clone())?;
292+
let norm_arr = L2Norm.try_new_array(len, EmptyOptions, [plain_ref.clone()])?;
292293
let plain_norm: PrimitiveArray = norm_arr.into_array().execute(ctx)?;
293294

294295
// TODO(connor): Ideally we would have a `SafeDiv` binary numeric operation.

0 commit comments

Comments
 (0)