Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support element-wise multiplication. #480

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/datatype/operators_svecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,49 @@ fn _vectors_svecf32_operator_minus(lhs: SVecf32Input<'_>, rhs: SVecf32Input<'_>)
SVecf32Output::new(SVecf32Borrowed::new(lhs.dims(), &indexes, &values))
}

/// Calculate the element-wise multiplication of two sparse vectors.
#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_svecf32_operator_mul(lhs: SVecf32Input<'_>, rhs: SVecf32Input<'_>) -> SVecf32Output {
check_matched_dims(lhs.dims() as _, rhs.dims() as _);

let size1 = lhs.len();
let size2 = rhs.len();
let mut pos1 = 0;
let mut pos2 = 0;
let mut pos = 0;
let mut indexes = vec![0; std::cmp::min(size1, size2)];
let mut values = vec![F32::zero(); std::cmp::min(size1, size2)];
let lhs = lhs.for_borrow();
let rhs = rhs.for_borrow();
while pos1 < size1 && pos2 < size2 {
let lhs_index = lhs.indexes()[pos1];
let rhs_index = rhs.indexes()[pos2];
match lhs_index.cmp(&rhs_index) {
std::cmp::Ordering::Less => {
pos1 += 1;
}
std::cmp::Ordering::Equal => {
// only both indexes are not zero, values are multiplied
let lhs_value = lhs.values()[pos1];
let rhs_value = rhs.values()[pos2];
indexes[pos] = lhs_index;
values[pos] = lhs_value * rhs_value;
my-vegetable-has-exploded marked this conversation as resolved.
Show resolved Hide resolved
pos1 += 1;
pos2 += 1;
// only increment pos if the value is not zero
pos += (!values[pos].is_zero()) as usize;
}
std::cmp::Ordering::Greater => {
pos2 += 1;
}
}
}
indexes.truncate(pos);
values.truncate(pos);

SVecf32Output::new(SVecf32Borrowed::new(lhs.dims(), &indexes, &values))
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_svecf32_operator_lt(lhs: SVecf32Input<'_>, rhs: SVecf32Input<'_>) -> bool {
check_matched_dims(lhs.dims() as _, rhs.dims() as _);
Expand Down
11 changes: 11 additions & 0 deletions src/datatype/operators_vecf16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ fn _vectors_vecf16_operator_minus(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) ->
Vecf16Output::new(Vecf16Borrowed::new(&v))
}

/// Calculate the element-wise multiplication of two f16 vectors.
#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_vecf16_operator_mul(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> Vecf16Output {
let n = check_matched_dims(lhs.dims(), rhs.dims());
let mut v = vec![F16::zero(); n];
for i in 0..n {
v[i] = lhs[i] * rhs[i];
}
Vecf16Output::new(Vecf16Borrowed::new(&v))
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_vecf16_operator_lt(lhs: Vecf16Input<'_>, rhs: Vecf16Input<'_>) -> bool {
check_matched_dims(lhs.dims(), rhs.dims());
Expand Down
11 changes: 11 additions & 0 deletions src/datatype/operators_vecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ fn _vectors_vecf32_operator_minus(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) ->
Vecf32Output::new(Vecf32Borrowed::new(&v))
}

/// Calculate the element-wise multiplication of two vectors.
#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_vecf32_operator_mul(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> Vecf32Output {
let n = check_matched_dims(lhs.dims(), rhs.dims());
let mut v = vec![F32::zero(); n];
for i in 0..n {
v[i] = lhs[i] * rhs[i];
}
Vecf32Output::new(Vecf32Borrowed::new(&v))
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_vecf32_operator_lt(lhs: Vecf32Input<'_>, rhs: Vecf32Input<'_>) -> bool {
check_matched_dims(lhs.dims(), rhs.dims());
Expand Down
14 changes: 14 additions & 0 deletions src/datatype/operators_veci8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ fn _vectors_veci8_operator_minus(lhs: Veci8Input<'_>, rhs: Veci8Input<'_>) -> Ve
)
}

/// Calculate the element-wise multiplication of two i8 vectors.
#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_veci8_operator_mul(lhs: Veci8Input<'_>, rhs: Veci8Input<'_>) -> Veci8Output {
check_matched_dims(lhs.len(), rhs.len());
let data = (0..lhs.len())
.map(|i| lhs.index(i) * rhs.index(i))
.collect::<Vec<_>>();
let (vector, alpha, offset) = veci8::i8_quantization(&data);
let (sum, l2_norm) = veci8::i8_precompute(&vector, alpha, offset);
Veci8Output::new(
Veci8Borrowed::new_checked(lhs.len() as u32, &vector, alpha, offset, sum, l2_norm).unwrap(),
)
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_veci8_operator_lt(lhs: Veci8Input<'_>, rhs: Veci8Input<'_>) -> bool {
check_matched_dims(lhs.len(), rhs.len());
Expand Down
28 changes: 28 additions & 0 deletions src/sql/finalize.sql
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,34 @@ CREATE OPERATOR - (
RIGHTARG = veci8
);

CREATE OPERATOR * (
my-vegetable-has-exploded marked this conversation as resolved.
Show resolved Hide resolved
PROCEDURE = _vectors_vecf32_operator_mul,
LEFTARG = vector,
RIGHTARG = vector,
COMMUTATOR = *
);

CREATE OPERATOR * (
PROCEDURE = _vectors_vecf16_operator_mul,
LEFTARG = vecf16,
RIGHTARG = vecf16,
COMMUTATOR = *
);

CREATE OPERATOR * (
PROCEDURE = _vectors_svecf32_operator_mul,
LEFTARG = svector,
RIGHTARG = svector,
COMMUTATOR = *
);

CREATE OPERATOR * (
PROCEDURE = _vectors_veci8_operator_mul,
LEFTARG = veci8,
RIGHTARG = veci8,
COMMUTATOR = *
);

CREATE OPERATOR & (
PROCEDURE = _vectors_bvecf32_operator_and,
LEFTARG = bvector,
Expand Down
5 changes: 5 additions & 0 deletions tests/sqllogictest/fp16.slt
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ SELECT COUNT(1) FROM (SELECT 1 FROM t ORDER BY val <=> '[0.5,0.5,0.5]'::vecf16 l
----
10

query I
SELECT '[1,2,3]'::vecf16 * '[4,5,6]'::vecf16;
----
[4, 10, 18]

statement ok
DROP TABLE t;
5 changes: 5 additions & 0 deletions tests/sqllogictest/int8.slt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ SELECT to_veci8(5, 1, 0, '{0,1,2,0,0}');
----
[0, 1, 2, 0, 0]

query I
SELECT '[2,2,2]'::veci8 * '[2,2,2]'::veci8;
----
[4, 4, 4]

statement error Lengths of values and len are not matched.
SELECT to_veci8(5, 1, 0, '{0,1,2,0,0,0}');

Expand Down
5 changes: 5 additions & 0 deletions tests/sqllogictest/sparse.slt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ SELECT to_svector(5, '{1,2}', '{1,2}');
----
[0, 1, 2, 0, 0]

query I
SELECT to_svector(5, '{1,2}', '{1,1}') * to_svector(5, '{1,3}', '{2,2}');
----
[0, 2, 0, 0, 0]

statement error Lengths of index and value are not matched.
SELECT to_svector(5, '{1,2,3}', '{1,2}');

Expand Down
5 changes: 5 additions & 0 deletions tests/sqllogictest/vector.slt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ SELECT vector_dims(v) FROM unnest(ARRAY['[1,2]'::vector, '[3]']) v;
2
1

query I
SELECT '[1,2,3]'::vector * '[4,5,6]'::vector;
----
[4, 10, 18]

query ?
SELECT avg(v) FROM unnest(ARRAY['[1,2,3]'::vector, '[3,5,7]']) v;
----
Expand Down