-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scalar8 & indexing on halfvec (#131)
closes #91 (indexing on halfvec) closes #118 (scalar8) --------- Signed-off-by: usamoi <usamoi@outlook.com>
- Loading branch information
Showing
45 changed files
with
2,137 additions
and
475 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::memory_scalar8::{Scalar8Input, Scalar8Output}; | ||
use crate::types::scalar8::Scalar8Borrowed; | ||
use base::vector::VectorBorrowed; | ||
use pgrx::datum::Internal; | ||
use pgrx::pg_sys::Oid; | ||
|
||
#[pgrx::pg_extern(immutable, strict, parallel_safe)] | ||
fn _vchord_scalar8_send(vector: Scalar8Input<'_>) -> Vec<u8> { | ||
let vector = vector.as_borrowed(); | ||
let mut stream = Vec::<u8>::new(); | ||
stream.extend(vector.dims().to_be_bytes()); | ||
stream.extend(vector.sum_of_x2().to_be_bytes()); | ||
stream.extend(vector.k().to_be_bytes()); | ||
stream.extend(vector.b().to_be_bytes()); | ||
stream.extend(vector.sum_of_code().to_be_bytes()); | ||
for &c in vector.code() { | ||
stream.extend(c.to_be_bytes()); | ||
} | ||
stream | ||
} | ||
|
||
#[pgrx::pg_extern(immutable, strict, parallel_safe)] | ||
fn _vchord_scalar8_recv(internal: Internal, oid: Oid, typmod: i32) -> Scalar8Output { | ||
let _ = (oid, typmod); | ||
let buf = unsafe { internal.get_mut::<pgrx::pg_sys::StringInfoData>().unwrap() }; | ||
|
||
let dims = { | ||
assert!(buf.cursor < i32::MAX - 4 && buf.cursor + 4 <= buf.len); | ||
let raw = unsafe { buf.data.add(buf.cursor as _).cast::<[u8; 4]>().read() }; | ||
buf.cursor += 4; | ||
u32::from_be_bytes(raw) | ||
}; | ||
let sum_of_x2 = { | ||
assert!(buf.cursor < i32::MAX - 4 && buf.cursor + 4 <= buf.len); | ||
let raw = unsafe { buf.data.add(buf.cursor as _).cast::<[u8; 4]>().read() }; | ||
buf.cursor += 4; | ||
f32::from_be_bytes(raw) | ||
}; | ||
let k = { | ||
assert!(buf.cursor < i32::MAX - 4 && buf.cursor + 4 <= buf.len); | ||
let raw = unsafe { buf.data.add(buf.cursor as _).cast::<[u8; 4]>().read() }; | ||
buf.cursor += 4; | ||
f32::from_be_bytes(raw) | ||
}; | ||
let b = { | ||
assert!(buf.cursor < i32::MAX - 4 && buf.cursor + 4 <= buf.len); | ||
let raw = unsafe { buf.data.add(buf.cursor as _).cast::<[u8; 4]>().read() }; | ||
buf.cursor += 4; | ||
f32::from_be_bytes(raw) | ||
}; | ||
let sum_of_code = { | ||
assert!(buf.cursor < i32::MAX - 4 && buf.cursor + 4 <= buf.len); | ||
let raw = unsafe { buf.data.add(buf.cursor as _).cast::<[u8; 4]>().read() }; | ||
buf.cursor += 4; | ||
f32::from_be_bytes(raw) | ||
}; | ||
let code = { | ||
let mut result = Vec::with_capacity(dims as _); | ||
for _ in 0..dims { | ||
result.push({ | ||
assert!(buf.cursor < i32::MAX - 1 && buf.cursor + 1 <= buf.len); | ||
let raw = unsafe { buf.data.add(buf.cursor as _).cast::<[u8; 1]>().read() }; | ||
buf.cursor += 1; | ||
u8::from_be_bytes(raw) | ||
}); | ||
} | ||
result | ||
}; | ||
|
||
if let Some(x) = Scalar8Borrowed::new_checked(sum_of_x2, k, b, sum_of_code, &code) { | ||
Scalar8Output::new(x) | ||
} else { | ||
pgrx::error!("detect data corruption"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::datatype::memory_pgvector_halfvec::PgvectorHalfvecInput; | ||
use crate::datatype::memory_pgvector_vector::PgvectorVectorInput; | ||
use crate::datatype::memory_scalar8::Scalar8Output; | ||
use crate::types::scalar8::Scalar8Borrowed; | ||
use base::simd::ScalarLike; | ||
use half::f16; | ||
|
||
#[pgrx::pg_extern(sql = "")] | ||
fn _vchord_vector_quantize_to_scalar8(vector: PgvectorVectorInput) -> Scalar8Output { | ||
let vector = vector.as_borrowed(); | ||
let sum_of_x2 = f32::reduce_sum_of_x2(vector.slice()); | ||
let (k, b, code) = | ||
base::simd::quantize::quantize(f32::vector_to_f32_borrowed(vector.slice()).as_ref(), 255.0); | ||
let sum_of_code = base::simd::u8::reduce_sum_of_x_as_u32(&code) as f32; | ||
Scalar8Output::new(Scalar8Borrowed::new(sum_of_x2, k, b, sum_of_code, &code)) | ||
} | ||
|
||
#[pgrx::pg_extern(sql = "")] | ||
fn _vchord_halfvec_quantize_to_scalar8(vector: PgvectorHalfvecInput) -> Scalar8Output { | ||
let vector = vector.as_borrowed(); | ||
let sum_of_x2 = f16::reduce_sum_of_x2(vector.slice()); | ||
let (k, b, code) = | ||
base::simd::quantize::quantize(f16::vector_to_f32_borrowed(vector.slice()).as_ref(), 255.0); | ||
let sum_of_code = base::simd::u8::reduce_sum_of_x_as_u32(&code) as f32; | ||
Scalar8Output::new(Scalar8Borrowed::new(sum_of_x2, k, b, sum_of_code, &code)) | ||
} |
Oops, something went wrong.