bugmakerrrrrr opened a new issue, #13185:
URL: https://github.com/apache/lucene/issues/13185

   ### Description
   
   Currently, in the KNN retrieval process, we use 
`VectorSimilarityFunction#compare` to calculate the score between the target 
vector and the current vector. This method requires recalculating the norm of 
the target vector each time. To avoid this repetition, we can pass the square 
norm of the target vector to score method. I suggest modifying the relevant 
interface as follows:
           ```
   @functionalInterface
   public interface ByteVectorScorer {
       float score(byte[] vector);
   }
   
   @functionalInterface
   public interface FloatVectorScorer {
       float score(float[] vector);
   }
   
   public enum VectorSimilarityFunction {
   
       EUCLIDEAN {
           @Override
           public ByteVectorScorer getVectorScorer(byte[] target) {
               return vector -> 1 / (1f + squareDistance(target, vector));
           }
   
           @Override
           public FloatVectorScorer getVectorScorer(float[] target) {
               return vector -> 1 / (1 + squareDistance(target, vector));
           }
       },
   
       COSINE {
           @Override
           public ByteVectorScorer getVectorScorer(byte[] target) {
               int squareNorm = dotProduct(target, target);
               return vector -> (1 + cosine(target, vector, squareNorm)) / 2;
           }
   
           @Override
           public FloatVectorScorer getVectorScorer(float[] target) {
               double squareNorm = dotProduct(target, target);
               return vector -> Math.max((1 + cosine(target, vector, 
squareNorm)) / 2, 0);
           }
       };
   
       public abstract ByteVectorScorer getVectorScorer(byte[] target);
   
       public abstract FloatVectorScorer getVectorScorer(float[] target);
   }
   ```
   
   Any thoughts?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to