Skip to content

Commit

Permalink
test: strengthen AVX2/AVX512 code tests and remove flasky bvector test
Browse files Browse the repository at this point in the history
Signed-off-by: usamoi <usamoi@outlook.com>
  • Loading branch information
usamoi committed Aug 29, 2024
1 parent 41f11a4 commit 86410d5
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 164 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod distance;
pub mod index;
pub mod operator;
pub mod pod;
pub mod rand;
pub mod scalar;
pub mod search;
pub mod vector;
Expand Down
9 changes: 9 additions & 0 deletions crates/common/src/rand.rs → crates/base/src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ where
_ => unreachable!(),
}
}

pub fn sample_u32_sorted<R>(rng: &mut R, length: u32, amount: u32) -> Vec<u32>
where
R: Rng + ?Sized,
{
let mut x = sample_u32(rng, length, amount);
x.sort();
x
}
166 changes: 111 additions & 55 deletions crates/base/src/scalar/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,22 +263,32 @@ mod reduce_sum_of_xy {
#[cfg(all(target_arch = "x86_64", test))]
#[test]
fn reduce_sum_of_xy_v4_avx512fp16_test() {
use rand::Rng;
const EPSILON: f32 = 2.0;
detect::init();
if !detect::v4_avx512fp16::detect() {
println!("test {} ... skipped (v4_avx512fp16)", module_path!());
return;
}
for _ in 0..300 {
let n = 4000;
let lhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let rhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let specialized = unsafe { reduce_sum_of_xy_v4_avx512fp16(&lhs, &rhs) };
let fallback = unsafe { reduce_sum_of_xy_fallback(&lhs, &rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
let mut rng = rand::thread_rng();
for _ in 0..256 {
let n = 4016;
let lhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
let rhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
for z in 3984..4016 {
let lhs = &lhs[..z];
let rhs = &rhs[..z];
let specialized = unsafe { reduce_sum_of_xy_v4_avx512fp16(lhs, rhs) };
let fallback = unsafe { reduce_sum_of_xy_fallback(lhs, rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
}
}
}

Expand Down Expand Up @@ -313,16 +323,22 @@ mod reduce_sum_of_xy {
#[cfg(all(target_arch = "x86_64", test))]
#[test]
fn reduce_sum_of_xy_v4_test() {
use rand::Rng;
const EPSILON: f32 = 2.0;
detect::init();
if !detect::v4::detect() {
println!("test {} ... skipped (v4)", module_path!());
return;
}
for _ in 0..300 {
let n = 4000;
let lhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let rhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let mut rng = rand::thread_rng();
for _ in 0..256 {
let n = 4016;
let lhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
let rhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
let specialized = unsafe { reduce_sum_of_xy_v4(&lhs, &rhs) };
let fallback = unsafe { reduce_sum_of_xy_fallback(&lhs, &rhs) };
assert!(
Expand Down Expand Up @@ -367,22 +383,32 @@ mod reduce_sum_of_xy {
#[cfg(all(target_arch = "x86_64", test))]
#[test]
fn reduce_sum_of_xy_v3_test() {
use rand::Rng;
const EPSILON: f32 = 2.0;
detect::init();
if !detect::v3::detect() {
println!("test {} ... skipped (v3)", module_path!());
return;
}
for _ in 0..300 {
let n = 4000;
let lhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let rhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let specialized = unsafe { reduce_sum_of_xy_v3(&lhs, &rhs) };
let fallback = unsafe { reduce_sum_of_xy_fallback(&lhs, &rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
let mut rng = rand::thread_rng();
for _ in 0..256 {
let n = 4016;
let lhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
let rhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
for z in 3984..4016 {
let lhs = &lhs[..z];
let rhs = &rhs[..z];
let specialized = unsafe { reduce_sum_of_xy_v3(lhs, rhs) };
let fallback = unsafe { reduce_sum_of_xy_fallback(lhs, rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
}
}
}

Expand Down Expand Up @@ -434,22 +460,32 @@ mod reduce_sum_of_d2 {
#[cfg(all(target_arch = "x86_64", test))]
#[test]
fn reduce_sum_of_d2_v4_avx512fp16_test() {
const EPSILON: f32 = 2.0;
use rand::Rng;
const EPSILON: f32 = 6.0;
detect::init();
if !detect::v4_avx512fp16::detect() {
println!("test {} ... skipped (v4_avx512fp16)", module_path!());
return;
}
for _ in 0..300 {
let n = 4000;
let lhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let rhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let specialized = unsafe { reduce_sum_of_d2_v4_avx512fp16(&lhs, &rhs) };
let fallback = unsafe { reduce_sum_of_d2_fallback(&lhs, &rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
let mut rng = rand::thread_rng();
for _ in 0..256 {
let n = 4016;
let lhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
let rhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
for z in 3984..4016 {
let lhs = &lhs[..z];
let rhs = &rhs[..z];
let specialized = unsafe { reduce_sum_of_d2_v4_avx512fp16(lhs, rhs) };
let fallback = unsafe { reduce_sum_of_d2_fallback(lhs, rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
}
}
}

Expand Down Expand Up @@ -486,22 +522,32 @@ mod reduce_sum_of_d2 {
#[cfg(all(target_arch = "x86_64", test))]
#[test]
fn reduce_sum_of_d2_v4_test() {
use rand::Rng;
const EPSILON: f32 = 2.0;
detect::init();
if !detect::v4::detect() {
println!("test {} ... skipped (v4)", module_path!());
return;
}
for _ in 0..300 {
let n = 4000;
let lhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let rhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let specialized = unsafe { reduce_sum_of_d2_v4(&lhs, &rhs) };
let fallback = unsafe { reduce_sum_of_d2_fallback(&lhs, &rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
let mut rng = rand::thread_rng();
for _ in 0..256 {
let n = 4016;
let lhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
let rhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
for z in 3984..4016 {
let lhs = &lhs[..z];
let rhs = &rhs[..z];
let specialized = unsafe { reduce_sum_of_d2_v4(lhs, rhs) };
let fallback = unsafe { reduce_sum_of_d2_fallback(lhs, rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
}
}
}

Expand Down Expand Up @@ -542,22 +588,32 @@ mod reduce_sum_of_d2 {
#[cfg(all(target_arch = "x86_64", test))]
#[test]
fn reduce_sum_of_d2_v3_test() {
use rand::Rng;
const EPSILON: f32 = 2.0;
detect::init();
if !detect::v3::detect() {
println!("test {} ... skipped (v3)", module_path!());
return;
}
for _ in 0..300 {
let n = 4000;
let lhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let rhs = (0..n).map(|_| rand::random::<_>()).collect::<Vec<_>>();
let specialized = unsafe { reduce_sum_of_d2_v3(&lhs, &rhs) };
let fallback = unsafe { reduce_sum_of_d2_fallback(&lhs, &rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
let mut rng = rand::thread_rng();
for _ in 0..256 {
let n = 4016;
let lhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
let rhs = (0..n)
.map(|_| f16::from_f32(rng.gen_range(-1.0..=1.0)))
.collect::<Vec<_>>();
for z in 3984..4016 {
let lhs = &lhs[..z];
let rhs = &rhs[..z];
let specialized = unsafe { reduce_sum_of_d2_v3(lhs, rhs) };
let fallback = unsafe { reduce_sum_of_d2_fallback(lhs, rhs) };
assert!(
(specialized - fallback).abs() < EPSILON,
"specialized = {specialized}, fallback = {fallback}."
);
}
}
}

Expand Down
Loading

0 comments on commit 86410d5

Please sign in to comment.