1
0
mirror of https://github.com/tensorchord/pgvecto.rs.git synced 2025-08-01 06:46:52 +03:00

refactor: add crate "base" (#367)

Signed-off-by: usamoi <usamoi@outlook.com>
This commit is contained in:
usamoi
2024-02-20 15:35:49 +08:00
committed by GitHub
parent d6e0545b93
commit 42fa5835d1
27 changed files with 143 additions and 55 deletions

40
crates/base/Cargo.toml Normal file
View File

@ -0,0 +1,40 @@
[package]
name = "base"
version.workspace = true
edition.workspace = true
[dependencies]
bincode.workspace = true
bytemuck.workspace = true
byteorder.workspace = true
half.workspace = true
libc.workspace = true
log.workspace = true
memmap2.workspace = true
num-traits.workspace = true
rand.workspace = true
rustix.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
uuid.workspace = true
validator.workspace = true
c = { path = "../c" }
detect = { path = "../detect" }
crc32fast = "1.4.0"
crossbeam = "0.8.4"
dashmap = "5.5.3"
parking_lot = "0.12.1"
rayon = "1.8.1"
arc-swap = "1.6.0"
multiversion = "0.7.3"
[lints]
clippy.derivable_impls = "allow"
clippy.len_without_is_empty = "allow"
clippy.needless_range_loop = "allow"
clippy.too_many_arguments = "allow"
rust.internal_features = "allow"
rust.unsafe_op_in_unsafe_fn = "forbid"
rust.unused_lifetimes = "warn"
rust.unused_qualifications = "warn"

7
crates/base/src/lib.rs Normal file
View File

@ -0,0 +1,7 @@
#![feature(core_intrinsics)]
pub mod error;
pub mod scalar;
pub mod search;
pub mod sys;
pub mod vector;

View File

@ -1,4 +1,4 @@
use crate::prelude::global::FloatCast;
use super::FloatCast;
use half::f16;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;

View File

@ -1,4 +1,4 @@
use crate::prelude::global::FloatCast;
use super::FloatCast;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt::{Debug, Display};

View File

@ -0,0 +1,16 @@
mod f16;
mod f32;
pub use f16::F16;
pub use f32::F32;
pub trait FloatCast: Sized {
fn from_f32(x: f32) -> Self;
fn to_f32(self) -> f32;
fn from_f(x: F32) -> Self {
Self::from_f32(x.0)
}
fn to_f(self) -> F32 {
F32(Self::to_f32(self))
}
}

View File

@ -1,4 +1,4 @@
use crate::prelude::F32;
use crate::scalar::F32;
pub type Payload = u64;

View File

@ -0,0 +1,19 @@
mod sparse_f32;
pub use sparse_f32::{SparseF32, SparseF32Ref};
pub trait Vector {
fn dims(&self) -> u16;
}
impl<T> Vector for Vec<T> {
fn dims(&self) -> u16 {
self.len().try_into().unwrap()
}
}
impl<'a, T> Vector for &'a [T] {
fn dims(&self) -> u16 {
self.len().try_into().unwrap()
}
}

View File

@ -1,4 +1,6 @@
use crate::prelude::*;
use super::Vector;
use crate::scalar::F32;
use num_traits::Zero;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -19,6 +19,7 @@ serde_json.workspace = true
thiserror.workspace = true
uuid.workspace = true
validator.workspace = true
base = { path = "../base" }
c = { path = "../c" }
detect = { path = "../detect" }
crc32fast = "1.4.0"

View File

@ -1,5 +1,6 @@
use crate::prelude::*;
use crate::utils::vec2::Vec2;
use base::scalar::FloatCast;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use std::ops::{Index, IndexMut};

View File

@ -5,6 +5,7 @@ use crate::index::IndexOptions;
use crate::prelude::*;
use crate::utils::dir_ops::sync_dir;
use crate::utils::mmap_array::MmapArray;
use base::scalar::FloatCast;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::Arc;

View File

@ -1,4 +1,5 @@
use crate::prelude::*;
use base::scalar::FloatCast;
pub fn cosine(lhs: &[F16], rhs: &[F16]) -> F32 {
#[inline(always)]

View File

@ -1,6 +1,6 @@
use std::borrow::Cow;
use crate::prelude::*;
use base::scalar::FloatCast;
use std::borrow::Cow;
#[derive(Debug, Clone, Copy)]
pub enum F16Cos {}

View File

@ -1,6 +1,6 @@
use std::borrow::Cow;
use crate::prelude::*;
use base::scalar::FloatCast;
use std::borrow::Cow;
#[derive(Debug, Clone, Copy)]
pub enum F16Dot {}

View File

@ -1,6 +1,6 @@
use std::borrow::Cow;
use crate::prelude::*;
use base::scalar::FloatCast;
use std::borrow::Cow;
#[derive(Debug, Clone, Copy)]
pub enum F16L2 {}

View File

@ -1,6 +1,5 @@
use std::borrow::Cow;
use crate::prelude::*;
use std::borrow::Cow;
#[derive(Debug, Clone, Copy)]
pub enum F32L2 {}

View File

@ -43,7 +43,7 @@ pub trait G: Copy + Debug + 'static {
+ Zero
+ num_traits::NumOps
+ num_traits::NumAssignOps
+ FloatCast;
+ base::scalar::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: Vector + Clone + Serialize + for<'a> Deserialize<'a>;
@ -103,33 +103,6 @@ pub trait G: Copy + Debug + 'static {
) -> F32;
}
pub trait FloatCast: Sized {
fn from_f32(x: f32) -> Self;
fn to_f32(self) -> f32;
fn from_f(x: F32) -> Self {
Self::from_f32(x.0)
}
fn to_f(self) -> F32 {
F32(Self::to_f32(self))
}
}
pub trait Vector {
fn dims(&self) -> u16;
}
impl<T> Vector for Vec<T> {
fn dims(&self) -> u16 {
self.len().try_into().unwrap()
}
}
impl<'a, T> Vector for &'a [T] {
fn dims(&self) -> u16 {
self.len().try_into().unwrap()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DynamicVector {
F32(Vec<F32>),

View File

@ -1,15 +1,13 @@
mod error;
mod global;
mod scalar;
mod search;
mod storage;
mod sys;
pub use self::error::*;
pub use self::global::*;
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};
pub use base::error::*;
pub use base::scalar::{F16, F32};
pub use base::search::{Element, Filter, Payload};
pub use base::sys::{Handle, Pointer};
pub use base::vector::{SparseF32, SparseF32Ref, Vector};
pub use num_traits::{Float, Zero};

View File

@ -1,7 +0,0 @@
mod f16;
mod f32;
mod sparse_f32;
pub use f16::F16;
pub use f32::F32;
pub use sparse_f32::{SparseF32, SparseF32Ref};