[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.15% 97.15%
=======================================
Files 141 141
Lines 8328 8334 +6
=======================================
+ Hits 8091 8097 +6
Misses 237 237 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
alonre24
reviewed
Jul 30, 2026
alonre24
left a comment
Collaborator
There was a problem hiding this comment.
LGTM - please fix formatting and static analysis errors that are failing the CI basic test flow
Also, please add an index-level equivalence test - a small FLAT/HNSW test that inserts the
same vectors and asserts Cosine and CosineSimilarity return identical query results with expected different scores
rbs333
force-pushed
the
MOD-17012/cosine_similarity_metric
branch
from
July 30, 2026 13:21
b3bb365 to
df7725a
Compare
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 core metric routing (preprocessing, distance kernels, SVS factory, blob sizing) across many types; behavior for existing L2/IP/Cosine should be unchanged but mis-routing would affect search correctness.
Overview
Introduces
VecSimMetric_CosineSimilarityas a new enum value (appended after existing metrics) andVecSimMetric_IsCosineFamily()so cosine-specific behavior applies to both cosine metrics without duplicating logic.Cosine-path routing is extended to the new metric: distance function selection in
spaces.cpp, preprocessor factory and QuantPreprocessor static checks, SVS index creation (DistanceIP+ forced preprocessing), integral-vector norm sizing for stored/query blobs, andVecSimMetric_ToString. SVS drops a redundantDistanceCosineSimilaritydistance conversion specialization in favor of the shared IP path.Bindings and tests: Python exposes the new enum; unit tests cover invalid-metric checks, SQ8 dist-func parity with cosine, and quant preprocessor parametrization for
CosineSimilarity.Reviewed by Cursor Bugbot for commit df7725a. Bugbot is set up for automated code reviews on this repo. Configure here.