-
-
Notifications
You must be signed in to change notification settings - Fork 30
Implement distance metric selection #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pierd
wants to merge
3
commits into
djc:main
Choose a base branch
from
pierd:distance-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,135 @@ pub(crate) unsafe fn euclid_distance_avx(v1: &[f32], v2: &[f32]) -> f32 { | |
} | ||
result.abs().sqrt() | ||
} | ||
|
||
#[target_feature(enable = "avx")] | ||
#[target_feature(enable = "fma")] | ||
pub(crate) unsafe fn cosine_preprocess_avx(vector: &mut [f32]) { | ||
let n = vector.len(); | ||
let m = n - (n % 32); | ||
let mut ptr: *const f32 = vector.as_ptr(); | ||
let mut sum256_1: __m256 = _mm256_setzero_ps(); | ||
let mut sum256_2: __m256 = _mm256_setzero_ps(); | ||
let mut sum256_3: __m256 = _mm256_setzero_ps(); | ||
let mut sum256_4: __m256 = _mm256_setzero_ps(); | ||
let mut i: usize = 0; | ||
while i < m { | ||
let m256_1 = _mm256_loadu_ps(ptr); | ||
sum256_1 = _mm256_fmadd_ps(m256_1, m256_1, sum256_1); | ||
|
||
let m256_2 = _mm256_loadu_ps(ptr.add(8)); | ||
sum256_2 = _mm256_fmadd_ps(m256_2, m256_2, sum256_2); | ||
|
||
let m256_3 = _mm256_loadu_ps(ptr.add(16)); | ||
sum256_3 = _mm256_fmadd_ps(m256_3, m256_3, sum256_3); | ||
|
||
let m256_4 = _mm256_loadu_ps(ptr.add(24)); | ||
sum256_4 = _mm256_fmadd_ps(m256_4, m256_4, sum256_4); | ||
|
||
ptr = ptr.add(32); | ||
i += 32; | ||
} | ||
|
||
let mut length = hsum256_ps_avx(sum256_1) | ||
+ hsum256_ps_avx(sum256_2) | ||
+ hsum256_ps_avx(sum256_3) | ||
+ hsum256_ps_avx(sum256_4); | ||
for i in 0..n - m { | ||
length += (*ptr.add(i)).powi(2); | ||
} | ||
if length < f32::EPSILON { | ||
return; | ||
} | ||
length = length.sqrt(); | ||
for x in vector.iter_mut() { | ||
*x /= length; | ||
} | ||
} | ||
|
||
#[target_feature(enable = "avx")] | ||
#[target_feature(enable = "fma")] | ||
pub(crate) unsafe fn dot_similarity_avx(v1: &[f32], v2: &[f32]) -> f32 { | ||
let n = v1.len(); | ||
let m = n - (n % 32); | ||
let mut ptr1: *const f32 = v1.as_ptr(); | ||
let mut ptr2: *const f32 = v2.as_ptr(); | ||
let mut sum256_1: __m256 = _mm256_setzero_ps(); | ||
let mut sum256_2: __m256 = _mm256_setzero_ps(); | ||
let mut sum256_3: __m256 = _mm256_setzero_ps(); | ||
let mut sum256_4: __m256 = _mm256_setzero_ps(); | ||
let mut i: usize = 0; | ||
while i < m { | ||
sum256_1 = _mm256_fmadd_ps(_mm256_loadu_ps(ptr1), _mm256_loadu_ps(ptr2), sum256_1); | ||
sum256_2 = _mm256_fmadd_ps( | ||
_mm256_loadu_ps(ptr1.add(8)), | ||
_mm256_loadu_ps(ptr2.add(8)), | ||
sum256_2, | ||
); | ||
sum256_3 = _mm256_fmadd_ps( | ||
_mm256_loadu_ps(ptr1.add(16)), | ||
_mm256_loadu_ps(ptr2.add(16)), | ||
sum256_3, | ||
); | ||
sum256_4 = _mm256_fmadd_ps( | ||
_mm256_loadu_ps(ptr1.add(24)), | ||
_mm256_loadu_ps(ptr2.add(24)), | ||
sum256_4, | ||
); | ||
|
||
ptr1 = ptr1.add(32); | ||
ptr2 = ptr2.add(32); | ||
i += 32; | ||
} | ||
|
||
let mut result = hsum256_ps_avx(sum256_1) | ||
+ hsum256_ps_avx(sum256_2) | ||
+ hsum256_ps_avx(sum256_3) | ||
+ hsum256_ps_avx(sum256_4); | ||
|
||
for i in 0..n - m { | ||
result += (*ptr1.add(i)) * (*ptr2.add(i)); | ||
} | ||
result | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_spaces_avx() { | ||
use super::*; | ||
use crate::*; | ||
|
||
if is_x86_feature_detected!("avx") && is_x86_feature_detected!("fma") { | ||
let v1: Vec<f32> = 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<f32> = 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_distance_avx(&v1, &v2) }; | ||
let euclid = euclid_distance(&v1, &v2); | ||
assert_eq!(euclid_simd, euclid); | ||
|
||
let dot_simd = unsafe { dot_similarity_avx(&v1, &v2) }; | ||
let dot = dot_similarity(&v1, &v2); | ||
assert_eq!(dot_simd, dot); | ||
|
||
let mut v1 = v1; | ||
let mut v1_copy = v1.clone(); | ||
unsafe { cosine_preprocess_avx(&mut v1) }; | ||
cosine_preprocess(&mut v1_copy); | ||
assert_eq!(v1, v1_copy); | ||
} else { | ||
println!("avx test skipped"); | ||
Comment on lines
+146
to
+183
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it would be nice to split the addition of test code between the commits that add the test code (or alternatively, add it separately at the end). |
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be curious to see some benchmarking results in this PR for the legacy approach vs your new implementations!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The most recent results:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I think
legacy
is euclid except it doesn't bother taking the square root, right? If it's still 4ns faster thanEuclid
, we should maybe expose it under a name other thanEuclidMetric
, as I think we'd want to stick to it (since the difference between the square root and the squared value doesn't usually matter for our use case?).Did you run any tests to show that the platform-specific implementations have the same result (or close to it) as the non-SIMD implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the
legacy
is euclid but with square root. I believe we don't care about having squared values. If I drop the square root calculation then they are almost the same (2-5% difference):(the numbers are higher because I'm on battery and the CPU is throttled)
We could use that since it doesn't have dimensions limitation. Another idea would be to have the current metric (as for example
Euclid300
) for the use case of 300 dimensions.