Normalize to speed up distance calculation #1
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.
This pull request introduces several optimizations and enhancements to the vector similarity calculations. The most important changes include adding a new dot product function, normalizing vectors before calculations, and updating benchmarks and tests to reflect these changes.
Optimizations and Enhancements:
internal/cosine/simd/simd.go
: Introduced theDotProduct
function to calculate the dot product between two vectors, addedgenericDotProduct
for non-hardware implementations, and updated theCosine
function to usegenericCosine
for non-hardware cases. [1] [2]bruteforce.go
: Added vector normalization in theAdd
andSearch
methods to ensure vectors are unit vectors, allowing the use of dot product for cosine similarity calculations. [1] [2] [3]Performance Improvements:
internal/cosine/cosine_apple.c
,internal/cosine/cosine_avx.c
,internal/cosine/cosine_neon.c
: Added thef32_dot_product
function to each SIMD implementation to support efficient dot product calculations. [1] [2] [3]Benchmark and Test Updates:
internal/cosine/simd/simd_test.go
: Updated benchmarks to include dot product performance and added tests for the newDotProduct
function. [1] [2]Miscellaneous:
bruteforce_test.go
: Updated benchmark results to reflect the performance improvements from using dot product and vector normalization.