Skip to content
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

[ENH] SIMD vectorization for distance metrics #2084

Merged
merged 2 commits into from
May 1, 2024

Conversation

sanketkedia
Copy link
Contributor

Description of changes

Adds SIMD vectorization for euclidean, cosine and inner product for x86, x86_64 and arm. Instruction sets whose support has been added are SSE, AVX and NEON.

Test plan

  • [+] Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Documentation Changes

No

Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

@sanketkedia sanketkedia self-assigned this Apr 30, 2024
[[bench]]
name = "distance_metrics"
path = "src/benches/distance_metrics.rs"
harness = false
Copy link
Collaborator

@HammadB HammadB Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

learning: what does this do (harness)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

harness disables libtest benchmarking since I am using the criterion crate

@@ -47,6 +52,7 @@ proptest = "1.4.0"
proptest-state-machine = "0.1.0"
"rand" = "0.8.5"
rayon = "1.8.0"
criterion = "0.3"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity what alternatives did we evaluate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. By default, we have libtest using which we can do basic benchmarking. My main motive behind using criterion was to enable sophisticated benchmarking for the future. In particular criterion provides:

  1. Statistics: Statistical analysis detects if, and by how much, performance has changed since the last benchmark run
  2. Charts: Uses gnuplot to generate detailed graphs of benchmark results

These would be useful in future to run these benchmarks in our CI/CD pipeline or nightly/weekly (some cadence) to detect perf regressions for e.g.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thank you

all(target_feature = "avx", target_feature = "fma")
))]
{
if std::arch::is_x86_feature_detected!("avx")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add tests to validate that the simd impls (whatever can run on the target machine) match the base impl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a test already in types.rs - test_distance_function_l2sqr() which I was relying on. If that regresses then there is some bug in the SIMD impls. That test validates inner product and l2 norm. Since, cosine is the same as inner product for us, I am guessing that is not needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great good point

Copy link
Contributor

@codetheweb codetheweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is super cool, wonder if it'd be worth using an existing vectorized implementation like this one instead of implementing it ourselves?

@sanketkedia
Copy link
Contributor Author

@codetheweb the main issue with using a third-party is that our distance functions have slightly different definition. For e.g. cosine similarity assumes the vectors are normalized

Copy link

vercel bot commented May 1, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
chroma ❌ Failed (Inspect) May 1, 2024 8:21pm

@sanketkedia
Copy link
Contributor Author

Verified that there is about 10x perf improvement for both arm and x86_64. Going to merge this unless anyone else has any objections.

@sanketkedia sanketkedia enabled auto-merge (squash) May 1, 2024 21:38
@sanketkedia sanketkedia disabled auto-merge May 1, 2024 21:42
@sanketkedia sanketkedia merged commit e53ab49 into chroma-core:main May 1, 2024
123 of 124 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants