Repository navigation
Compute the query magnitude once in similar_by_vector - #1905
Open
sebastianbreguel wants to merge 1 commit into
Open
sebastianbreguel wants to merge 1 commit into
sebastianbreguel wants to merge 1 commit into
Conversation
The cosine similarity recomputed the query vector's magnitude for every stored embedding. Compute it once per call; scores and ranking are unchanged.
Contributor
Author
This branch has not been deployed
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.
llm similarscores every stored embedding withllm.cosine_similarity(other_vector, vector), which recomputes the query vector's magnitude for each row. With 1536-dimension vectors that is about a quarter of the work. This computes the query magnitude once and keeps the rest of the arithmetic the same, sollm similargets about 24% faster and returns the same scores, bit for bit.Numbers
Time spent in
Collection.similar_by_id(..., number=10), median of 10 interleaved runs, 1536-dimension vectors:Whole-process wall time drops by 14% to 23%. Peak RSS does not change.
macOS arm64 native, Python 3.14. Linux arm64 in Docker on the same Mac (4 CPUs, 2 GiB, Python 3.13).
Same behavior
dot_product / (other_magnitude * query_magnitude), the same operations in the same order ascosine_similarity(a=other_vector, b=vector), so every score is identical, not just close.numberabove the row count) on main and on the branch for all four databases: identical.pytest: 1599 passed. ruff, black --check and mypy are clean.One difference:
similar_by_vectorno longer goes throughllm.cosine_similarity, so monkeypatching that function no longer changesllm similar. I did not find anything in the repo that relies on it.To revert, put back the
return llm.cosine_similarity(other_vector, vector)line and dropquery_magnitude.Benchmark script
The databases hold N random Gaussian vectors (seed 0) encoded with
llm.encode, in a collection created throughembeddings_migrations.