Skip to content

Commit 17e0e1d

Browse files
committed
boilerplate with NormVector encoding
Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
1 parent 7efebc4 commit 17e0e1d

7 files changed

Lines changed: 205 additions & 2 deletions

File tree

vortex-tensor/src/encodings/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
pub mod norm;
5+
// mod spherical;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use vortex::array::ArrayRef;
5+
6+
/// A normalized array that stores unit-normalized vectors alongside their original L2 norms.
7+
///
8+
/// Each vector in the array is divided by its L2 norm, producing a unit-normalized vector. The
9+
/// original norms are stored separately so that the original vectors can be reconstructed.
10+
#[derive(Debug, Clone)]
11+
pub struct NormVectorArray {
12+
/// The backing vector array that has been unit normalized.
13+
///
14+
/// The underlying elements of the vector array must be floating-point.
15+
vector_array: ArrayRef,
16+
17+
/// The L2 (Frobenius) norms of each vector.
18+
///
19+
/// This must have the same dtype as the elements of the vector array.
20+
norms: ArrayRef,
21+
}
22+
23+
impl NormVectorArray {
24+
/// Returns a reference to the backing vector array that has been unit normalized.
25+
pub fn vector_array(&self) -> &ArrayRef {
26+
&self.vector_array
27+
}
28+
29+
/// Returns a reference to the L2 (Frobenius) norms of each vector.
30+
pub fn norms(&self) -> &ArrayRef {
31+
&self.norms
32+
}
33+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
mod array;
5+
pub use array::NormVectorArray;
6+
7+
// pub(crate) mod compute;
8+
9+
mod vtable;
10+
pub use vtable::NormVector;
11+
12+
// #[cfg(test)]
13+
// mod tests;
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use std::hash::Hasher;
5+
6+
use vortex::array::ArrayRef;
7+
use vortex::array::EmptyMetadata;
8+
use vortex::array::ExecutionCtx;
9+
use vortex::array::ExecutionStep;
10+
use vortex::array::Precision;
11+
use vortex::array::buffer::BufferHandle;
12+
use vortex::array::serde::ArrayChildren;
13+
use vortex::array::stats::StatsSetRef;
14+
use vortex::array::vtable;
15+
use vortex::array::vtable::ArrayId;
16+
use vortex::array::vtable::VTable;
17+
use vortex::array::vtable::ValidityVTableFromChild;
18+
use vortex::dtype::DType;
19+
use vortex::error::VortexResult;
20+
use vortex::session::VortexSession;
21+
22+
use crate::encodings::norm::array::NormVectorArray;
23+
24+
mod operations;
25+
mod validity;
26+
27+
vtable!(NormVector);
28+
29+
#[derive(Debug)]
30+
pub struct NormVector;
31+
32+
impl VTable for NormVector {
33+
type Array = NormVectorArray;
34+
type Metadata = EmptyMetadata;
35+
type OperationsVTable = Self;
36+
type ValidityVTable = ValidityVTableFromChild;
37+
38+
fn id(_array: &NormVectorArray) -> ArrayId {
39+
ArrayId::new_ref("vortex.tensor.norm_vector")
40+
}
41+
42+
fn len(array: &NormVectorArray) -> usize {
43+
array.vector_array().len()
44+
}
45+
46+
fn dtype(array: &NormVectorArray) -> &DType {
47+
array.vector_array().dtype()
48+
}
49+
50+
fn stats(array: &NormVectorArray) -> StatsSetRef<'_> {
51+
array.vector_array().statistics()
52+
}
53+
54+
fn array_hash<H: Hasher>(array: &NormVectorArray, state: &mut H, precision: Precision) {
55+
todo!()
56+
}
57+
58+
fn array_eq(array: &NormVectorArray, other: &NormVectorArray, precision: Precision) -> bool {
59+
todo!()
60+
}
61+
62+
fn nbuffers(array: &NormVectorArray) -> usize {
63+
todo!()
64+
}
65+
66+
fn buffer(array: &NormVectorArray, idx: usize) -> BufferHandle {
67+
todo!()
68+
}
69+
70+
fn buffer_name(array: &NormVectorArray, idx: usize) -> Option<String> {
71+
todo!()
72+
}
73+
74+
fn nchildren(array: &NormVectorArray) -> usize {
75+
todo!()
76+
}
77+
78+
fn child(array: &NormVectorArray, idx: usize) -> ArrayRef {
79+
todo!()
80+
}
81+
82+
fn child_name(array: &NormVectorArray, idx: usize) -> String {
83+
todo!()
84+
}
85+
86+
fn metadata(array: &NormVectorArray) -> VortexResult<Self::Metadata> {
87+
todo!()
88+
}
89+
90+
fn serialize(metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
91+
todo!()
92+
}
93+
94+
fn deserialize(
95+
bytes: &[u8],
96+
_dtype: &DType,
97+
_len: usize,
98+
_buffers: &[BufferHandle],
99+
_session: &VortexSession,
100+
) -> VortexResult<Self::Metadata> {
101+
todo!()
102+
}
103+
104+
fn build(
105+
dtype: &DType,
106+
len: usize,
107+
metadata: &Self::Metadata,
108+
buffers: &[BufferHandle],
109+
children: &dyn ArrayChildren,
110+
) -> VortexResult<NormVectorArray> {
111+
todo!()
112+
}
113+
114+
fn with_children(array: &mut NormVectorArray, children: Vec<ArrayRef>) -> VortexResult<()> {
115+
todo!()
116+
}
117+
118+
fn execute(array: &NormVectorArray, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionStep> {
119+
todo!()
120+
}
121+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use vortex::array::vtable::OperationsVTable;
5+
use vortex::error::VortexResult;
6+
use vortex::scalar::Scalar;
7+
8+
use crate::encodings::norm::array::NormVectorArray;
9+
use crate::encodings::norm::vtable::NormVector;
10+
11+
impl OperationsVTable<NormVector> for NormVector {
12+
fn scalar_at(array: &NormVectorArray, index: usize) -> VortexResult<Scalar> {
13+
todo!()
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use vortex::array::ArrayRef;
5+
use vortex::array::vtable::ValidityChild;
6+
7+
use crate::encodings::norm::array::NormVectorArray;
8+
use crate::encodings::norm::vtable::NormVector;
9+
10+
impl ValidityChild<NormVector> for NormVector {
11+
fn validity_child(array: &NormVectorArray) -> &ArrayRef {
12+
array.vector_array()
13+
}
14+
}

vortex-tensor/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
//! including unit vectors, spherical coordinates, and similarity measures such as cosine
66
//! similarity.
77
8+
pub mod matcher;
9+
pub mod scalar_fns;
10+
811
pub mod fixed_shape;
912
pub mod vector;
1013

11-
pub mod matcher;
12-
pub mod scalar_fns;
14+
pub mod encodings;

0 commit comments

Comments
 (0)