Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi committed Aug 12, 2024
1 parent 8a31a16 commit dae810c
Show file tree
Hide file tree
Showing 30 changed files with 153 additions and 184 deletions.
2 changes: 1 addition & 1 deletion crates/base/src/operator/bvector_dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for BVectorDot {
const DISTANCE_KIND: DistanceKind = DistanceKind::Dot;

fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> F32 {
bvector::dot(lhs, rhs) * (-1.0)
lhs.operator_dot(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/bvector_hamming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for BVectorHamming {
const DISTANCE_KIND: DistanceKind = DistanceKind::Hamming;

fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> F32 {
bvector::hamming(lhs, rhs)
lhs.operator_hamming(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/bvector_jaccard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for BVectorJaccard {
const DISTANCE_KIND: DistanceKind = DistanceKind::Jaccard;

fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> F32 {
F32(1.0) - bvector::jaccard(lhs, rhs)
lhs.operator_jaccard(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/svecf32_dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for SVecf32Dot {
const DISTANCE_KIND: DistanceKind = DistanceKind::Dot;

fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> F32 {
svecf32::dot(lhs, rhs) * (-1.0)
lhs.operator_dot(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/svecf32_l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for SVecf32L2 {
const DISTANCE_KIND: DistanceKind = DistanceKind::L2;

fn distance(lhs: SVecf32Borrowed<'_>, rhs: SVecf32Borrowed<'_>) -> F32 {
svecf32::sl2(lhs, rhs)
lhs.operator_l2(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/vecf16_dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for Vecf16Dot {
const DISTANCE_KIND: DistanceKind = DistanceKind::Dot;

fn distance(lhs: Vecf16Borrowed<'_>, rhs: Vecf16Borrowed<'_>) -> F32 {
vecf16::dot(lhs.slice(), rhs.slice()) * (-1.0)
lhs.operator_dot(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/vecf16_l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for Vecf16L2 {
const DISTANCE_KIND: DistanceKind = DistanceKind::L2;

fn distance(lhs: Vecf16Borrowed<'_>, rhs: Vecf16Borrowed<'_>) -> F32 {
vecf16::sl2(lhs.slice(), rhs.slice())
lhs.operator_l2(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/vecf32_dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for Vecf32Dot {
const DISTANCE_KIND: DistanceKind = DistanceKind::Dot;

fn distance(lhs: Vecf32Borrowed<'_>, rhs: Vecf32Borrowed<'_>) -> F32 {
vecf32::dot(lhs.slice(), rhs.slice()) * (-1.0)
lhs.operator_dot(rhs)
}
}
2 changes: 1 addition & 1 deletion crates/base/src/operator/vecf32_l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ impl Operator for Vecf32L2 {
const DISTANCE_KIND: DistanceKind = DistanceKind::L2;

fn distance(lhs: Vecf32Borrowed<'_>, rhs: Vecf32Borrowed<'_>) -> F32 {
vecf32::sl2(lhs.slice(), rhs.slice())
lhs.operator_l2(rhs)
}
}
4 changes: 2 additions & 2 deletions crates/base/src/scalar/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,14 @@ impl ScalarLike for F32 {
}

#[multiversion(v4, v3, v2, neon, fallback)]
fn euclid_distance(lhs: &[F32], rhs: &[F32]) -> F32 {
fn impl_l2(lhs: &[F32], rhs: &[F32]) -> F32 {
assert!(lhs.len() == rhs.len());
let n = lhs.len();
let mut d2 = F32::zero();
for i in 0..n {
let d = lhs[i].to_f() - rhs[i].to_f();
d2 += d * d;
}
d2.sqrt()
d2
}
}
4 changes: 2 additions & 2 deletions crates/base/src/scalar/half_f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,14 @@ impl ScalarLike for F16 {
}

#[multiversion(v4, v3, v2, neon, fallback)]
fn euclid_distance(lhs: &[F16], rhs: &[F16]) -> F32 {
fn impl_l2(lhs: &[F16], rhs: &[F16]) -> F32 {
assert!(lhs.len() == rhs.len());
let n = lhs.len();
let mut d2 = F32::zero();
for i in 0..n {
let d = lhs[i].to_f() - rhs[i].to_f();
d2 += d * d;
}
d2.sqrt()
d2
}
}
3 changes: 1 addition & 2 deletions crates/base/src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ pub trait ScalarLike:
fn from_f(x: F32) -> Self;
fn to_f(self) -> F32;

// it's not a good place, but there is no good place
fn euclid_distance(lhs: &[Self], rhs: &[Self]) -> F32;
fn impl_l2(lhs: &[Self], rhs: &[Self]) -> F32;
}
4 changes: 2 additions & 2 deletions crates/base/src/vector/bvector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl<'a> VectorBorrowed for BVectorBorrowed<'a> {
}

#[inline(always)]
fn length(&self) -> F32 {
fn norm(&self) -> F32 {
length(*self)
}

Expand All @@ -172,7 +172,7 @@ impl<'a> VectorBorrowed for BVectorBorrowed<'a> {

#[inline(always)]
fn operator_jaccard(self, rhs: Self) -> F32 {
jaccard(self, rhs)
F32(1.0) - jaccard(self, rhs)
}

#[inline(always)]
Expand Down
2 changes: 1 addition & 1 deletion crates/base/src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait VectorBorrowed: Copy + PartialEq + PartialOrd {

fn to_vec(&self) -> Vec<Self::Scalar>;

fn length(&self) -> F32;
fn norm(&self) -> F32;

fn operator_dot(self, rhs: Self) -> F32;

Expand Down
4 changes: 2 additions & 2 deletions crates/base/src/vector/svecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'a> VectorBorrowed for SVecf32Borrowed<'a> {
}

#[inline(always)]
fn length(&self) -> F32 {
fn norm(&self) -> F32 {
length(*self)
}

Expand All @@ -197,7 +197,7 @@ impl<'a> VectorBorrowed for SVecf32Borrowed<'a> {

#[inline(always)]
fn operator_cos(self, rhs: Self) -> F32 {
F32(1.0) - dot(self, rhs) / (self.length() * rhs.length())
F32(1.0) - dot(self, rhs) / (self.norm() * rhs.norm())
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions crates/base/src/vector/vecf16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'a> VectorBorrowed for Vecf16Borrowed<'a> {
}

#[inline(always)]
fn length(&self) -> F32 {
fn norm(&self) -> F32 {
length(self.0).to_f()
}

Expand All @@ -121,7 +121,7 @@ impl<'a> VectorBorrowed for Vecf16Borrowed<'a> {

#[inline(always)]
fn operator_cos(self, rhs: Self) -> F32 {
F32(1.0) - dot(self.slice(), rhs.slice()) / (self.length() * rhs.length())
F32(1.0) - dot(self.slice(), rhs.slice()) / (self.norm() * rhs.norm())
}

#[inline(always)]
Expand Down
4 changes: 2 additions & 2 deletions crates/base/src/vector/vecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<'a> VectorBorrowed for Vecf32Borrowed<'a> {
}

#[inline(always)]
fn length(&self) -> F32 {
fn norm(&self) -> F32 {
length(self.0)
}

Expand All @@ -121,7 +121,7 @@ impl<'a> VectorBorrowed for Vecf32Borrowed<'a> {

#[inline(always)]
fn operator_cos(self, rhs: Self) -> F32 {
F32(1.0) - dot(self.slice(), rhs.slice()) / (self.length() * rhs.length())
F32(1.0) - dot(self.slice(), rhs.slice()) / (self.norm() * rhs.norm())
}

#[inline(always)]
Expand Down
10 changes: 5 additions & 5 deletions crates/k_means/src/elkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<S: ScalarLike, F: FnMut(&mut [S])> ElkanKMeans<S, F> {
for i in 0..c {
let mut sum = F32::zero();
for j in 0..n {
dis[j] = S::euclid_distance(&samples[(j,)], &centroids[(i,)]);
dis[j] = S::impl_l2(&samples[(j,)], &centroids[(i,)]).sqrt();
}
for j in 0..n {
lowerbound[(j, i)] = dis[j];
Expand Down Expand Up @@ -107,7 +107,7 @@ impl<S: ScalarLike, F: FnMut(&mut [S])> ElkanKMeans<S, F> {
let mut sp = vec![F32::zero(); c];
for i in 0..c {
for j in 0..c {
dist0[(i, j)] = S::euclid_distance(&centroids[(i,)], &centroids[(j,)]) * 0.5;
dist0[(i, j)] = S::impl_l2(&centroids[(i,)], &centroids[(j,)]).sqrt() * 0.5;
}
}
for i in 0..c {
Expand All @@ -126,7 +126,7 @@ impl<S: ScalarLike, F: FnMut(&mut [S])> ElkanKMeans<S, F> {
let mut dis = vec![F32::zero(); n];
for i in 0..n {
if upperbound[i] > sp[assign[i]] {
dis[i] = S::euclid_distance(&samples[(i,)], &centroids[(assign[i],)]);
dis[i] = S::impl_l2(&samples[(i,)], &centroids[(assign[i],)]).sqrt();
}
}
for i in 0..n {
Expand All @@ -149,7 +149,7 @@ impl<S: ScalarLike, F: FnMut(&mut [S])> ElkanKMeans<S, F> {
continue;
}
if minimal > lowerbound[(i, j)] || minimal > dist0[(assign[i], j)] {
let dis = S::euclid_distance(&samples[(i,)], &centroids[(j,)]);
let dis = S::impl_l2(&samples[(i,)], &centroids[(j,)]).sqrt();
lowerbound[(i, j)] = dis;
if dis < minimal {
minimal = dis;
Expand Down Expand Up @@ -211,7 +211,7 @@ impl<S: ScalarLike, F: FnMut(&mut [S])> ElkanKMeans<S, F> {
// Step 5, 6
let mut dist1 = vec![F32::zero(); c];
for i in 0..c {
dist1[i] = S::euclid_distance(&old_centroids[(i,)], &centroids[(i,)]);
dist1[i] = S::impl_l2(&old_centroids[(i,)], &centroids[(i,)]).sqrt();
}
for i in 0..n {
for j in 0..c {
Expand Down
4 changes: 2 additions & 2 deletions crates/k_means/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn k_means_lookup<S: ScalarLike>(vector: &[S], centroids: &Vec2<S>) -> usize
assert_ne!(centroids.shape_0(), 0);
let mut result = (F32::infinity(), 0);
for i in 0..centroids.shape_0() {
let dis = S::euclid_distance(vector, &centroids[(i,)]);
let dis = S::impl_l2(vector, &centroids[(i,)]);
result = std::cmp::min(result, (dis, i));
}
result.1
Expand All @@ -62,7 +62,7 @@ pub fn k_means_lookup_many<S: ScalarLike>(vector: &[S], centroids: &Vec2<S>) ->
assert_ne!(centroids.shape_0(), 0);
let mut seq = Vec::new();
for i in 0..centroids.shape_0() {
let dis = S::euclid_distance(vector, &centroids[(i,)]);
let dis = S::impl_l2(vector, &centroids[(i,)]);
seq.push((dis, i));
}
seq
Expand Down
6 changes: 3 additions & 3 deletions crates/k_means/src/lloyd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<S: ScalarLike, F: Fn(&mut [S]) + Sync> LloydKMeans<S, F> {
for i in 0..c {
let mut sum = F32::zero();
for j in 0..n {
dis[j] = S::euclid_distance(&samples[(j,)], &centroids[i]);
dis[j] = S::impl_l2(&samples[(j,)], &centroids[i]);
}
for j in 0..n {
if dis[j] * dis[j] < weight[j] {
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<S: ScalarLike, F: Fn(&mut [S]) + Sync> LloydKMeans<S, F> {
let mut minimal = F32::infinity();
let mut target = 0;
for i in 0..c {
let dis = S::euclid_distance(&samples[(j,)], &centroids[i]);
let dis = S::impl_l2(&samples[(j,)], &centroids[i]);
if dis < minimal {
minimal = dis;
target = i;
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<S: ScalarLike, F: Fn(&mut [S]) + Sync> LloydKMeans<S, F> {
.map(|i| {
let mut result = (F32::infinity(), 0);
for j in 0..c {
let dis = S::euclid_distance(&samples[(i,)], &centroids[j]);
let dis = S::impl_l2(&samples[(i,)], &centroids[j]);
result = std::cmp::min(result, (dis, j));
}
result.1
Expand Down
2 changes: 1 addition & 1 deletion src/datatype/functions_bvector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn _vectors_bvector_dims(vector: BVectorInput<'_>) -> i32 {

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_bvector_norm(vector: BVectorInput<'_>) -> f32 {
vector.as_borrowed().length().to_f32()
vector.as_borrowed().norm().to_f32()
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
Expand Down
26 changes: 1 addition & 25 deletions src/datatype/functions_svecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::memory_svecf32::*;
use crate::error::*;
use base::scalar::*;
use base::vector::*;
use num_traits::Zero;

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_svecf32_dims(vector: SVecf32Input<'_>) -> i32 {
Expand All @@ -11,7 +10,7 @@ fn _vectors_svecf32_dims(vector: SVecf32Input<'_>) -> i32 {

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_svecf32_norm(vector: SVecf32Input<'_>) -> f32 {
vector.as_borrowed().length().to_f32()
vector.as_borrowed().norm().to_f32()
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
Expand Down Expand Up @@ -60,26 +59,3 @@ fn _vectors_to_svector(
}
SVecf32Output::new(SVecf32Borrowed::new(dims, &indexes, &values))
}

/// divide a sparse vector by a scalar.
#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_svecf32_div(vector: SVecf32Input<'_>, scalar: f32) -> SVecf32Output {
let scalar = F32(scalar);
let vector = vector.as_borrowed();
let indexes = vector.indexes();
let values = vector.values();
let mut new_indexes = Vec::<u32>::with_capacity(indexes.len());
let mut new_values = Vec::<F32>::with_capacity(values.len());
for (value, index) in values.iter().zip(indexes.iter()) {
let v = *value / scalar;
if !v.is_zero() {
new_values.push(v);
new_indexes.push(*index);
}
}
SVecf32Output::new(SVecf32Borrowed::new(
vector.dims(),
&new_indexes,
&new_values,
))
}
2 changes: 1 addition & 1 deletion src/datatype/functions_vecf16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn _vectors_vecf16_dims(vector: Vecf16Input<'_>) -> i32 {

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_vecf16_norm(vector: Vecf16Input<'_>) -> f32 {
vector.as_borrowed().length().to_f32()
vector.as_borrowed().norm().to_f32()
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
Expand Down
2 changes: 1 addition & 1 deletion src/datatype/functions_vecf32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn _vectors_vecf32_dims(vector: Vecf32Input<'_>) -> i32 {

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
fn _vectors_vecf32_norm(vector: Vecf32Input<'_>) -> f32 {
vector.as_borrowed().length().to_f32()
vector.as_borrowed().norm().to_f32()
}

#[pgrx::pg_extern(immutable, strict, parallel_safe)]
Expand Down
Loading

0 comments on commit dae810c

Please sign in to comment.