-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
Further ideas for optimizing vector functions. #46202
Comments
Pinging @elastic/es-search |
While working on #46294, I ran some experiments that might give some further ideas:
I don't have proper benchmark results, but in previous experiments I also tried using an optimized formula for L2 norm (1), taking shortcuts when only ranking is needed (2) and avoiding validation of the query vector length on every function call (4). I wasn't able to get major gains from these optimizations -- just wanted to make a note so we don't spend too much time on API discussion without first checking that the changes will help. |
@jtibshirani Thank you for the ideas. Indeed it makes sense first to run benchmarks before making changes to any APIs.
A user also reported 25% performance drop by moving from accessing doc values directly to script_score |
Closing this issue as the work on vectors is mostly done in Lucene for ANN search. |
Here are more ideas for optimizing brute-force computations in vectors that needs investigation. Some of these ideas come with the changes in APIs:
l2norm
-- euclidean distance:Instead of computing
l2norm
assqrt((d1-q1)^2 + (d2-q2)^2 ...)
, compute it assqrt(||d||^2 + ||q||^2 - 2dq)
, where:|||d||
— doc vector magnitude is precomputed during indexing (may be we can also precomputed squared version of it)||q||
— query vector magnitude is precomputed once during querydq
— dot product, we already have an optimized computation for it.We can use a
trank_scores
parameter of a search request (by default = true)This allows several optimizations during query:
l2norm
— we don't need to do the finalsqrt
. Also, if you use an optimized version ofl2norm
from Query DSL: Terms Filter #1 we can skip calculation of||q||^2
cosineSimilarity
— similarly, we can skip calculation of query vector magnitudeWe introduce a new parameter for
dense_vector
field:This allows:
cosineSimilarity
: we don't need to retrieve/compute doc vector magnitude, we don't need to computer query vector magnitude (as queries are also supposed to be normalized)l2norm
: if we usel2norm
from the Query DSL: Terms Filter #1, then the formula for normalized vectors will be converted tosqrt(2 - 2dq)
. If we also don't care about scores, the formula will be1 - dq
.Inquire from the painless team how to bring
DenseVectorScriptDocValues
to the constructor to make only once per query a check that the query's number of dims is equal to docs' number of dims.cc: @jtibshirani
The text was updated successfully, but these errors were encountered: