apache / apache/lucene

Scalar quantization extreme edge case of uniform vector values

Open
#13,480 0 comments 0 reactions 0 assignees View on GitHub
type:bug vector-based-search
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

### Description

When quantizing vectors that have a uniform value, the quantiles can get really weird. Meaning both min and max quantiles are actually equivalent. Additionally, the scoring can be unexpected.

Here is a test indicating this weirdness:

```
public void testWeirdEdgeCase() throws IOException {
float[] uniformVector = new float[128];
for (int i = 0; i < uniformVector.length; i++) {
uniformVector[i] = 1;
}
float[] query = new float[128];
for (int i = 0; i < query.length; i++) {
query[i] = 2;
}
ScalarQuantizer scalarQuantizer = ScalarQuantizer.fromVectors(fromFloats(new float[][] {uniformVector}),
0.99f, 1, (byte) 7);
byte[] quantized = new byte[128];
float offset = scalarQuantizer.quantize(query, quantized, VectorSimilarityFunction.EUCLIDEAN);
byte[] quantizedVector = new byte[128];
float offsetVector = scalarQuantizer.quantize(uniformVector, quantizedVector, VectorSimilarityFunction.EUCLIDEAN);
ScalarQuantizedVectorSimilarity quantizedSimilarity =
ScalarQuantizedVectorSimilarity.fromVectorSimilarity(
VectorSimilarityFunction.EUCLIDEAN, scalarQuantizer.getConstantMultiplier(), scalarQuantizer.getBits());
float score1 = quantizedSimilarity.score(quantized, offset, quantizedVector, offsetVector);

for (int i = 0; i < query.length; i++) {
query[i] = 5;
}
offset = scalarQuantizer.quantize(query, quantized, VectorSimilarityFunction.EUCLIDEAN);
float score2 = quantizedSimilarity.score(quantized, offset, quantizedVector, offsetVector);
assertNotEquals(score1, score2);
}
```

### Version and environment details

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with ScalarQuantizer.fromVectors and its quantize path, then inspect ScalarQuantizedVectorSimilarity.score for the uniform-vector case shown in testWeirdEdgeCase. Run the supplied test with a vector of 128 ones and queries of 2 and 5; done should establish the intended behavior for the two resulting scores.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.