-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[fix](function) Improve numerical robustness of cosine_distance / cosine_similarity #62840
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
Merged
yiguolei
merged 7 commits into
apache:master
from
yx-keith:Enhance-numerical-stability-for-cosine-distance/similarity
May 28, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b84e7a
[improvement](be) Optimize cosine array distance calculation
2a06e93
Merge branch 'master' into Enhance-numerical-stability-for-cosine-dis…
yx-keith 36289ef
[fix](be) Fix numerical instability in cosine distance/similarity
yx-keith e94725c
[fix](be) Fix clang-format violation in cosine similarity test
yx-keith 12b0ce0
[fix](regression) Update cosine distance/similarity expected values
yx-keith 74302aa
Merge remote-tracking branch 'apache/master' into Enhance-numerical-s…
yx-keith d650aba
[fix](test) Remove redundant assert_cast on get_null_map_column_ptr
yx-keith 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
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.
This still overflows before the new
doublenorm is computed becausedot_prod,squared_x, andsquared_yare accumulated asfloatand the products are evaluated asfloat. For a valid finite FLOAT input such ascosine_similarity([1e20], [1e20]),x[i] * x[i]andx[i] * y[i]become+inf; thendot_prod / normisinf / inf, producingNaN(std::clampdoes not sanitize NaN). The expected result for parallel vectors is still1.0similarity /0.0distance. Please widen the accumulators and the per-element products todoublebefore accumulation in both cosine functions, and add a test that covers this overflow-at-accumulation case.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.
Thanks for the catch. The per-element overflow only triggers when |x[i]| > sqrt(FLT_MAX) ≈ 1.84e19, which is even more pathological than the outer-product overflow this PR addresses. Widening accumulators to double would halve SIMD throughput (~5-20% end-to-end regression in vector search workloads) while only covering a stricter subset of overflow scenarios that have never been observed in real embeddings. We prefer to keep the current scope; happy to file a follow-up if a real-world case is reported.