You've already forked pgvecto.rs
mirror of
https://github.com/tensorchord/pgvecto.rs.git
synced 2025-08-07 03:22:55 +03:00
refactor: add crate "base" (#367)
Signed-off-by: usamoi <usamoi@outlook.com>
This commit is contained in:
32
Cargo.lock
generated
32
Cargo.lock
generated
@@ -143,6 +143,36 @@ dependencies = [
|
|||||||
"rustc-demangle",
|
"rustc-demangle",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "base"
|
||||||
|
version = "0.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"arc-swap",
|
||||||
|
"bincode",
|
||||||
|
"bytemuck",
|
||||||
|
"byteorder",
|
||||||
|
"c",
|
||||||
|
"crc32fast",
|
||||||
|
"crossbeam",
|
||||||
|
"dashmap",
|
||||||
|
"detect",
|
||||||
|
"half 2.3.1",
|
||||||
|
"libc",
|
||||||
|
"log",
|
||||||
|
"memmap2",
|
||||||
|
"multiversion",
|
||||||
|
"num-traits",
|
||||||
|
"parking_lot",
|
||||||
|
"rand",
|
||||||
|
"rayon",
|
||||||
|
"rustix",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"thiserror",
|
||||||
|
"uuid",
|
||||||
|
"validator",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "base64"
|
name = "base64"
|
||||||
version = "0.21.7"
|
version = "0.21.7"
|
||||||
@@ -1739,6 +1769,7 @@ name = "service"
|
|||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arc-swap",
|
"arc-swap",
|
||||||
|
"base",
|
||||||
"bincode",
|
"bincode",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
@@ -2197,6 +2228,7 @@ name = "vectors"
|
|||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
|
"base",
|
||||||
"bincode",
|
"bincode",
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
@@ -30,6 +30,7 @@ serde.workspace = true
|
|||||||
serde_json.workspace = true
|
serde_json.workspace = true
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
validator.workspace = true
|
validator.workspace = true
|
||||||
|
base = { path = "crates/base" }
|
||||||
detect = { path = "crates/detect" }
|
detect = { path = "crates/detect" }
|
||||||
send_fd = { path = "crates/send_fd" }
|
send_fd = { path = "crates/send_fd" }
|
||||||
service = { path = "crates/service" }
|
service = { path = "crates/service" }
|
||||||
|
40
crates/base/Cargo.toml
Normal file
40
crates/base/Cargo.toml
Normal 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
7
crates/base/src/lib.rs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#![feature(core_intrinsics)]
|
||||||
|
|
||||||
|
pub mod error;
|
||||||
|
pub mod scalar;
|
||||||
|
pub mod search;
|
||||||
|
pub mod sys;
|
||||||
|
pub mod vector;
|
@@ -1,4 +1,4 @@
|
|||||||
use crate::prelude::global::FloatCast;
|
use super::FloatCast;
|
||||||
use half::f16;
|
use half::f16;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
@@ -1,4 +1,4 @@
|
|||||||
use crate::prelude::global::FloatCast;
|
use super::FloatCast;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::fmt::{Debug, Display};
|
use std::fmt::{Debug, Display};
|
16
crates/base/src/scalar/mod.rs
Normal file
16
crates/base/src/scalar/mod.rs
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
use crate::prelude::F32;
|
use crate::scalar::F32;
|
||||||
|
|
||||||
pub type Payload = u64;
|
pub type Payload = u64;
|
||||||
|
|
19
crates/base/src/vector/mod.rs
Normal file
19
crates/base/src/vector/mod.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
@@ -1,4 +1,6 @@
|
|||||||
use crate::prelude::*;
|
use super::Vector;
|
||||||
|
use crate::scalar::F32;
|
||||||
|
use num_traits::Zero;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
@@ -19,6 +19,7 @@ serde_json.workspace = true
|
|||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
uuid.workspace = true
|
uuid.workspace = true
|
||||||
validator.workspace = true
|
validator.workspace = true
|
||||||
|
base = { path = "../base" }
|
||||||
c = { path = "../c" }
|
c = { path = "../c" }
|
||||||
detect = { path = "../detect" }
|
detect = { path = "../detect" }
|
||||||
crc32fast = "1.4.0"
|
crc32fast = "1.4.0"
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::utils::vec2::Vec2;
|
use crate::utils::vec2::Vec2;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
use rand::rngs::StdRng;
|
use rand::rngs::StdRng;
|
||||||
use rand::{Rng, SeedableRng};
|
use rand::{Rng, SeedableRng};
|
||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
@@ -5,6 +5,7 @@ use crate::index::IndexOptions;
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::utils::dir_ops::sync_dir;
|
use crate::utils::dir_ops::sync_dir;
|
||||||
use crate::utils::mmap_array::MmapArray;
|
use crate::utils::mmap_array::MmapArray;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
|
|
||||||
pub fn cosine(lhs: &[F16], rhs: &[F16]) -> F32 {
|
pub fn cosine(lhs: &[F16], rhs: &[F16]) -> F32 {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum F16Cos {}
|
pub enum F16Cos {}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum F16Dot {}
|
pub enum F16Dot {}
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum F16L2 {}
|
pub enum F16L2 {}
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum F32L2 {}
|
pub enum F32L2 {}
|
||||||
|
@@ -43,7 +43,7 @@ pub trait G: Copy + Debug + 'static {
|
|||||||
+ Zero
|
+ Zero
|
||||||
+ num_traits::NumOps
|
+ num_traits::NumOps
|
||||||
+ num_traits::NumAssignOps
|
+ num_traits::NumAssignOps
|
||||||
+ FloatCast;
|
+ base::scalar::FloatCast;
|
||||||
type Storage: for<'a> Storage<VectorRef<'a> = Self::VectorRef<'a>>;
|
type Storage: for<'a> Storage<VectorRef<'a> = Self::VectorRef<'a>>;
|
||||||
type L2: for<'a> G<Scalar = Self::Scalar, VectorRef<'a> = &'a [Self::Scalar]>;
|
type L2: for<'a> G<Scalar = Self::Scalar, VectorRef<'a> = &'a [Self::Scalar]>;
|
||||||
type VectorOwned: Vector + Clone + Serialize + for<'a> Deserialize<'a>;
|
type VectorOwned: Vector + Clone + Serialize + for<'a> Deserialize<'a>;
|
||||||
@@ -103,33 +103,6 @@ pub trait G: Copy + Debug + 'static {
|
|||||||
) -> 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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub enum DynamicVector {
|
pub enum DynamicVector {
|
||||||
F32(Vec<F32>),
|
F32(Vec<F32>),
|
||||||
|
@@ -1,15 +1,13 @@
|
|||||||
mod error;
|
|
||||||
mod global;
|
mod global;
|
||||||
mod scalar;
|
|
||||||
mod search;
|
|
||||||
mod storage;
|
mod storage;
|
||||||
mod sys;
|
|
||||||
|
|
||||||
pub use self::error::*;
|
|
||||||
pub use self::global::*;
|
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::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};
|
pub use num_traits::{Float, Zero};
|
||||||
|
@@ -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};
|
|
@@ -2,6 +2,7 @@ use crate::datatype::svecf32::{SVecf32, SVecf32Input, SVecf32Output};
|
|||||||
use crate::datatype::vecf16::{Vecf16, Vecf16Input, Vecf16Output};
|
use crate::datatype::vecf16::{Vecf16, Vecf16Input, Vecf16Output};
|
||||||
use crate::datatype::vecf32::{Vecf32, Vecf32Input, Vecf32Output};
|
use crate::datatype::vecf32::{Vecf32, Vecf32Input, Vecf32Output};
|
||||||
use crate::prelude::check_value_dimensions;
|
use crate::prelude::check_value_dimensions;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
use service::prelude::*;
|
use service::prelude::*;
|
||||||
|
|
||||||
#[pgrx::pg_extern(immutable, parallel_safe, strict)]
|
#[pgrx::pg_extern(immutable, parallel_safe, strict)]
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
use crate::datatype::svecf32::{SVecf32, SVecf32Input, SVecf32Output};
|
use crate::datatype::svecf32::{SVecf32, SVecf32Input, SVecf32Output};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
use service::prelude::*;
|
use service::prelude::*;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
use crate::datatype::vecf16::{Vecf16, Vecf16Input, Vecf16Output};
|
use crate::datatype::vecf16::{Vecf16, Vecf16Input, Vecf16Output};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
use service::prelude::*;
|
use service::prelude::*;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
use crate::datatype::vecf32::{Vecf32, Vecf32Input, Vecf32Output};
|
use crate::datatype::vecf32::{Vecf32, Vecf32Input, Vecf32Output};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use base::scalar::FloatCast;
|
||||||
use service::prelude::*;
|
use service::prelude::*;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user