[MOD-17102] Add cosine-family support for COSINE_SIMILARITY#997
Open
rbs333 wants to merge 1 commit into
Open
Conversation
|
|
rbs333
marked this pull request as ready for review
July 20, 2026 20:13
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #997 +/- ##
==========================================
- Coverage 97.14% 97.12% -0.03%
==========================================
Files 141 141
Lines 8245 8250 +5
==========================================
+ Hits 8010 8013 +3
- Misses 235 237 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Describe the changes in the pull request
Adds
COSINE_SIMILARITYas a fourth vector metric (VecSimMetric_CosineSimilarity). It reuses the existing cosine-distance execution path end-to-end — no new spaces functions, preprocessors, ordering, heap, or comparator logic. AVecSimMetric_IsCosineFamily()helper is introduced so every place that special-casesVecSimMetric_Cosine(preprocessing/normalization, SVS factory, quant preprocessor, stored/query blob sizing, dist-func selection) treats the new metric identically. The public metric name and the exposed score/range semantics (similarity = 1 - distance) are translated at the API boundary in the RediSearch layer; this repo only adds the metric and routes it through the cosine internals.Non-breaking:
L2,IP, andCOSINEbehavior is unchanged, and the new enum value is appended last so existing serialized indexes keep their metric ordinals (L2=0, IP=1, Cosine=2).Which issues this PR fixes
Main objects this PR modified
VecSimMetricenum + newVecSimMetric_IsCosineFamily()helper (vec_sim_common.h)spaces.cpp(dist-func selection, all types incl. SQ8_FP16),preprocessors_factory.h,preprocessors.h(QuantPreprocessor),svs_factory.cpp,svs_utils.hvec_utils.cpp(VecSimMetric_ToString,VecSimParams_GetStoredDataSize),vec_sim.cpp(VecSimParams_GetQueryBlobSize)bindings.cpp)test_spaces.cpp,test_components.cppMark if applicable
Note
Medium Risk
Touches metric enum and broad routing (dist funcs, preprocessing, SVS); behavior for existing L2/IP/Cosine should be unchanged but wrong routing would affect all cosine-family searches.
Overview
Adds
VecSimMetric_CosineSimilarityas a fourth distance metric (appended after existing enum values) andVecSimMetric_IsCosineFamily()so cosine-specific behavior applies to both cosine metrics without duplicating checks.VecSimMetric_CosineSimilarityfollows the same internal path asVecSimMetric_Cosine: cosine preprocessors, normalization/IP shortcuts when vectors are already normalized, integral-vector norm padding in blob sizing,GetDistFuncdispatch (including SQ8 variants), SVSDistanceIPindexing, andQuantPreprocessorsupport. Stringify exposesCOSINE_SIMILARITY; Python bindings export the new enum value. SVS drops a separateDistanceCosineSimilaritydistance conversion template in favor of the shared IP conversion used for cosine metrics.Unit tests cover the new metric in quant preprocessor suites and assert
CosineSimilarityselects the same distance functions asCosine.Reviewed by Cursor Bugbot for commit b3bb365. Bugbot is set up for automated code reviews on this repo. Configure here.