From 3df7c36c883fcd503d52b0182fd8d03e5e707fbd Mon Sep 17 00:00:00 2001 From: Ivan Pleshkov Date: Wed, 16 Mar 2022 12:25:02 +0400 Subject: [PATCH] remove avx512 --- lib/segment/src/lib.rs | 2 - lib/segment/src/spaces/simple.rs | 31 ------- lib/segment/src/spaces/simple_avx512.rs | 103 ------------------------ 3 files changed, 136 deletions(-) delete mode 100644 lib/segment/src/spaces/simple_avx512.rs diff --git a/lib/segment/src/lib.rs b/lib/segment/src/lib.rs index 1a8e2aee54..c20604f38b 100644 --- a/lib/segment/src/lib.rs +++ b/lib/segment/src/lib.rs @@ -1,5 +1,3 @@ -#![cfg_attr(target_feature = "avx512f", feature(stdsimd))] - mod common; pub mod entry; pub mod fixtures; diff --git a/lib/segment/src/spaces/simple.rs b/lib/segment/src/spaces/simple.rs index 738f088718..7f90b04898 100644 --- a/lib/segment/src/spaces/simple.rs +++ b/lib/segment/src/spaces/simple.rs @@ -8,9 +8,6 @@ use super::simple_sse::*; #[cfg(target_arch = "x86_64")] use super::simple_avx::*; -#[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))] -use super::simple_avx512::*; - #[cfg(all(target_arch = "aarch64", target_feature = "neon"))] use super::simple_neon::*; @@ -29,13 +26,6 @@ impl Metric for EuclidMetric { } fn similarity(&self, v1: &[VectorElementType], v2: &[VectorElementType]) -> ScoreType { - #[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))] - { - if is_x86_feature_detected!("avx512f") { - return unsafe { euclid_similarity_avx512f(v1, v2) }; - } - } - #[cfg(target_arch = "x86_64")] { if is_x86_feature_detected!("avx") && is_x86_feature_detected!("fma") { @@ -71,13 +61,6 @@ impl Metric for DotProductMetric { } fn similarity(&self, v1: &[VectorElementType], v2: &[VectorElementType]) -> ScoreType { - #[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))] - { - if is_x86_feature_detected!("avx512f") { - return unsafe { dot_similarity_avx512f(v1, v2) }; - } - } - #[cfg(target_arch = "x86_64")] { if is_x86_feature_detected!("avx") && is_x86_feature_detected!("fma") { @@ -113,13 +96,6 @@ impl Metric for CosineMetric { } fn similarity(&self, v1: &[VectorElementType], v2: &[VectorElementType]) -> ScoreType { - #[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))] - { - if is_x86_feature_detected!("avx512f") { - return unsafe { dot_similarity_avx512f(v1, v2) }; - } - } - #[cfg(target_arch = "x86_64")] { if is_x86_feature_detected!("avx") && is_x86_feature_detected!("fma") { @@ -145,13 +121,6 @@ impl Metric for CosineMetric { } fn preprocess(&self, vector: &[VectorElementType]) -> Option> { - #[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))] - { - if is_x86_feature_detected!("avx512f") { - return Some(unsafe { cosine_preprocess_avx512f(vector) }); - } - } - #[cfg(target_arch = "x86_64")] { if is_x86_feature_detected!("avx") && is_x86_feature_detected!("fma") { diff --git a/lib/segment/src/spaces/simple_avx512.rs b/lib/segment/src/spaces/simple_avx512.rs deleted file mode 100644 index 9f47d6fffb..0000000000 --- a/lib/segment/src/spaces/simple_avx512.rs +++ /dev/null @@ -1,103 +0,0 @@ -#[cfg(target_feature = "avx512f")] -use crate::types::{ScoreType, VectorElementType}; - -#[cfg(target_feature = "avx512f")] -use std::arch::x86_64::*; - -#[cfg(target_feature = "avx512f")] -pub unsafe fn euclid_similarity_avx512f( - v1: &[VectorElementType], - v2: &[VectorElementType], -) -> ScoreType { - let n = v1.len(); - let m = n - (n % 16); - let mut sum512: __m512 = _mm512_setzero_ps(); - for i in (0..m).step_by(16) { - let sub512: __m512 = _mm512_sub_ps(_mm512_loadu_ps(&v1[i]), _mm512_loadu_ps(&v2[i])); - sum512 = _mm512_fmadd_ps(sub512, sub512, sum512); - } - let mut res = _mm512_mask_reduce_add_ps(u16::MAX, sum512); - for i in m..n { - res += (v1[i] - v2[i]).powi(2); - } - -res.sqrt() -} - -#[cfg(target_feature = "avx512f")] -pub unsafe fn cosine_preprocess_avx512f(vector: &[VectorElementType]) -> Vec { - let n = vector.len(); - let m = n - (n % 16); - let mut sum512: __m512 = _mm512_setzero_ps(); - for i in (0..m).step_by(16) { - sum512 = _mm512_fmadd_ps( - _mm512_loadu_ps(&vector[i]), - _mm512_loadu_ps(&vector[i]), - sum512, - ); - } - let mut length = _mm512_mask_reduce_add_ps(u16::MAX, sum512); - for v in vector.iter().take(n).skip(m) { - length += v.powi(2); - } - length = length.sqrt(); - vector.iter().map(|x| x / length).collect() -} - -#[cfg(target_feature = "avx512f")] -pub unsafe fn dot_similarity_avx512f( - v1: &[VectorElementType], - v2: &[VectorElementType], -) -> ScoreType { - let n = v1.len(); - let m = n - (n % 16); - let mut sum512: __m512 = _mm512_setzero_ps(); - for i in (0..m).step_by(16) { - sum512 = _mm512_fmadd_ps(_mm512_loadu_ps(&v1[i]), _mm512_loadu_ps(&v2[i]), sum512); - } - let mut res = _mm512_mask_reduce_add_ps(u16::MAX, sum512); - for i in m..n { - res += v1[i] * v2[i]; - } - res -} - -#[cfg(test)] -mod tests { - #[cfg(target_feature = "avx512f")] - #[test] - fn test_spaces_avx512() { - use super::*; - use crate::spaces::simple::*; - - if is_x86_feature_detected!("avx512f") { - let v1: Vec = vec![ - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., - 26., 27., 28., 29., 30., 31., - ]; - let v2: Vec = vec![ - 40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54., 55., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., - 10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25., - 56., 57., 58., 59., 60., 61., - ]; - - let euclid_simd = unsafe { euclid_similarity_avx512f(&v1, &v2) }; - let euclid = euclid_similarity(&v1, &v2); - assert_eq!(euclid_simd, euclid); - - let dot_simd = unsafe { dot_similarity_avx512f(&v1, &v2) }; - let dot = dot_similarity(&v1, &v2); - assert_eq!(dot_simd, dot); - - let cosine_simd = unsafe { cosine_preprocess_avx512f(&v1) }; - let cosine = cosine_preprocess(&v1); - assert_eq!(cosine_simd, cosine); - } else { - println!("avx512 test skipped"); - } - } -}