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

fix: comparsion of svector #357

Merged
merged 1 commit into from
Feb 18, 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
17 changes: 5 additions & 12 deletions crates/service/src/prelude/global/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub trait G: Copy + Debug + 'static {
+ FloatCast;
type Storage: for<'a> Storage<VectorRef<'a> = Self::VectorRef<'a>>;
type L2: for<'a> G<Scalar = Self::Scalar, VectorRef<'a> = &'a [Self::Scalar]>;
type VectorOwned: VectorOwned + Clone + Serialize + for<'a> Deserialize<'a>;
type VectorRef<'a>: VectorRef<'a> + Copy + 'a
type VectorOwned: Vector + Clone + Serialize + for<'a> Deserialize<'a>;
type VectorRef<'a>: Vector + Copy + 'a
where
Self: 'a;

Expand Down Expand Up @@ -114,24 +114,17 @@ pub trait FloatCast: Sized {
}
}

pub trait VectorOwned {
pub trait Vector {
fn dims(&self) -> u16;
}

pub trait VectorRef<'a> {
fn dims(&self) -> u16;
fn length(&self) -> u16 {
self.dims()
}
}

impl<T> VectorOwned for Vec<T> {
impl<T> Vector for Vec<T> {
fn dims(&self) -> u16 {
self.len().try_into().unwrap()
}
}

impl<'a, T> VectorRef<'a> for &'a [T] {
impl<'a, T> Vector for &'a [T] {
fn dims(&self) -> u16 {
self.len().try_into().unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod sys;

pub use self::error::ServiceError;
pub use self::global::*;
pub use self::scalar::{SparseF32, SparseF32Element, SparseF32Ref, F16, F32};
pub use self::scalar::{SparseF32, SparseF32Ref, F16, F32};
pub use self::search::{Element, Filter, Payload};
pub use self::storage::{DenseMmap, SparseMmap, Storage};
pub use self::sys::{Handle, Pointer};
Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/prelude/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ mod sparse_f32;

pub use f16::F16;
pub use f32::F32;
pub use sparse_f32::{SparseF32, SparseF32Element, SparseF32Ref};
pub use sparse_f32::{SparseF32, SparseF32Ref};
33 changes: 4 additions & 29 deletions crates/service/src/prelude/scalar/sparse_f32.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
use crate::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SparseF32Element {
pub index: u16,
pub value: F32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SparseF32 {
Expand All @@ -22,16 +15,6 @@ pub struct SparseF32Ref<'a> {
pub values: &'a [F32],
}

impl Display for SparseF32Element {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{{{}: {}}}", self.index, self.value)
}
}

unsafe impl bytemuck::Zeroable for SparseF32Element {}

unsafe impl bytemuck::Pod for SparseF32Element {}

impl<'a> From<SparseF32Ref<'a>> for SparseF32 {
fn from(value: SparseF32Ref<'a>) -> Self {
Self {
Expand All @@ -52,20 +35,16 @@ impl<'a> From<&'a SparseF32> for SparseF32Ref<'a> {
}
}

impl VectorOwned for SparseF32 {
impl Vector for SparseF32 {
fn dims(&self) -> u16 {
self.dims
}
}

impl<'a> VectorRef<'a> for SparseF32Ref<'a> {
impl<'a> Vector for SparseF32Ref<'a> {
fn dims(&self) -> u16 {
self.dims
}

fn length(&self) -> u16 {
self.indexes.len().try_into().unwrap()
}
}

impl<'a> SparseF32Ref<'a> {
Expand All @@ -77,11 +56,7 @@ impl<'a> SparseF32Ref<'a> {
dense
}

pub fn iter(&self) -> impl Iterator<Item = SparseF32Element> + 'a {
self.indexes
.iter()
.copied()
.zip(self.values.iter().copied())
.map(|(index, value)| SparseF32Element { index, value })
pub fn length(&self) -> u16 {
self.indexes.len().try_into().unwrap()
}
}
41 changes: 31 additions & 10 deletions src/datatype/svecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,31 @@ impl PartialOrd for SVecf32 {
impl Ord for SVecf32 {
fn cmp(&self, other: &Self) -> Ordering {
assert!(self.dims() == other.dims());
self.data().iter().cmp(other.data().iter())
let lhs = self.data();
let rhs = other.data();
let mut pos = 0;
let size1 = lhs.length() as usize;
let size2 = rhs.length() as usize;
while pos < size1 && pos < size2 {
let lhs_index = lhs.indexes[pos];
let rhs_index = rhs.indexes[pos];
let lhs_value = lhs.values[pos];
let rhs_value = rhs.values[pos];
match lhs_index.cmp(&rhs_index) {
Ordering::Less => return lhs_value.cmp(&F32::zero()),
Ordering::Greater => return F32::zero().cmp(&rhs_value),
Ordering::Equal => match lhs_value.cmp(&rhs_value) {
Ordering::Equal => {}
x => return x,
},
}
pos += 1;
}
match size1.cmp(&size2) {
Ordering::Less => F32::zero().cmp(&rhs.values[pos]),
Ordering::Greater => lhs.values[pos].cmp(&F32::zero()),
Ordering::Equal => Ordering::Equal,
}
}
}

Expand Down Expand Up @@ -640,23 +664,20 @@ fn _vectors_svector_from_array(
}
.friendly();
}
let mut vector: Vec<SparseF32Element> = index
let mut vector: Vec<(u16, F32)> = index
.iter_deny_null()
.zip(value.iter_deny_null())
.map(|(index, value)| {
if index < 0 || index >= dims as i32 {
SessionError::BadValueDimensions.friendly();
}
SparseF32Element {
index: index as u16,
value: F32(value),
}
(index as u16, F32(value))
})
.collect();
vector.sort_unstable_by_key(|x| x.index);
vector.sort_unstable_by_key(|x| x.0);
if vector.len() > 1 {
for i in 0..vector.len() - 1 {
if vector[i].index == vector[i + 1].index {
if vector[i].0 == vector[i + 1].0 {
SessionError::BadLiteral {
hint: "Duplicated index.".to_string(),
}
Expand All @@ -668,8 +689,8 @@ fn _vectors_svector_from_array(
let mut indexes = Vec::<u16>::with_capacity(vector.len());
let mut values = Vec::<F32>::with_capacity(vector.len());
for x in vector {
indexes.push(x.index);
values.push(x.value);
indexes.push(x.0);
values.push(x.1);
}
SVecf32::new_in_postgres(SparseF32Ref {
dims,
Expand Down
Loading