You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "thecoop (via GitHub)" <gi...@apache.org> on 2025/11/05 10:28:29 UTC

[I] Lucene104ScalarQuantizedVectorFormat returns different docs and scores after merging [lucene]

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

   ### Description
   
   We've found a bug in `Lucene104ScalarQuantizedVectorFormat` where the scorer is returning different documents scores after merging segments together. The test case can be found [here](https://github.com/thecoop/lucene/blob/scalar-quantizer-merge-bug/lucene/core/src/test/org/apache/lucene/codecs/lucene104/TestLucene104ScalarQuantizedVectorsFormat.java#L79) (on `branch_10x`)
   
   Before merge, docs returned and scores are:
   
   `{1=6.082725012674928E-4, 2=1.0}`
   
   After merge, docs returned and scores are:
   
   `{0=3.581063720048405E-5, 1=6.144083454273641E-4}`
   
   Both the top docs and the scores returned are different.
   
   This only occurs with the `PACKED_NIBBLE` encoding.
   
   
   ### Version and environment details
   
   Reproduces on `main` and `branch_10x`


-- 
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-unsubscribe@lucene.apache.org.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492927976

   `score` as this point is the euclidean distance and I don't think it's ever valid for that value to be negative.
   
   For normalized dot product we should be able apply both a min and a max to keep the value in bounds, right?


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3498616617

   @mccullocht I think your fix works well. 
   
   One thing I noticed and we should consider with the tests, as the residual norm2 increases, the error will increase relative to the quantization bits allowed. 
   
   The manual testing values picked for the repeatable test are pretty...extreme. Their residual norms are pretty crazy given the vector we care about is very far from the single centroid.
   
   
   The fix you provided works fine for nibbles, int7 and int8 for the test data. 
   
   However, it doesn't work for single bit as the norm2 is so huge. I am not 100% sure how to address this directly for these unrealistic vectors. But really, anything where the residual norm2 is huge is likely not going to work very well with low bit values. 
   
   
   Regardless, I don't think this should block the fix. Mind opening a PR @mccullocht ?


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492853304

   Making the following change fixes it. Need to think through if its ever valid for `score` to be negative and then we scale it to `0` without scaling it eagerly to `0` before hand. 
   
   ```
         score =
             Math.max(
                 queryCorrections.additionalCorrection()
                     + indexCorrections.additionalCorrection()
                     - 2 * score,
                 0f);
         return 1f / (1f + score);
   ```


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different docs and scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3490862722

   I need to try and replicate, but when I tried before, there wasn't any bug. 
   
   I expect the scores to be different, but all my testing indicated they were within 0.002, no major 1.0 shifting.


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492668730

   I also experience failure with SINGLE_BIT_QUERY_NIBBLE but the quantization is lossier so I'm focusing on PACKED_NIBBLE. This also reproduces on `main` so I've been working there.
   
   Agreed that the exactly equal quantized vector thing is weird, I guess it's a result of quantization loss?
   
   I also get a failure for UNSIGNED_BYTE on the identical vector but it returns a value that is slightly out of bounds near 1 rather than a near-zero value.


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492289524

   I am able to reproduce so taking a look


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492937664

   > For normalized dot product we should be able apply both a min and a max to keep the value in bounds, right?
   
   Anything below `-1` indicates a rounding error that should be accounted for. Possibly it should have a maximum value of `1f` to prevent rounding errors in the other direction.
   
   It seems to me that applying the snapping earlier just fixes the issue for both.


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3493529397

   I tried `Math.clamp(score, -1f, 1f)` but this fails `TestLucene104HnswScalarQuantizedVectorsFormat.testSingleVectorCase`, but clamping the lower bound as you described passes. If the vectors were normalized and not centered then the `[-1,1]` bounds would be correct for sure but with the adjustments it's less obvious to me what the bounds should be.


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3495234817

   The issue with clamping for dot product was actually caused by the test -- it was not normalizing the input vectors 😬. In limited testing this seems to work for everything but maximum inner product.
   
   https://github.com/mccullocht/lucene/pull/new/mccullocht/quantized-score-bounding


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492680789

   OK, this looks like floating point arithmetic. Not 100% sure how to fix. But here are the values I see on this "nearly equivalent" doc:
   
   <img width="674" height="782" alt="Image" src="https://github.com/user-attachments/assets/8e3a691e-2d04-4296-916e-b3fa42365cf8" />
   
   The resulting score ends up being `-8`, which is REALLY close to the perfect score of `0` (given the magnitudes of the values). 
   
   Basically, ALL the stored values are correct, all the corrections & vector values themselves are exactly the same. However, somewhere along the line, we end up with a rounding error that causes the score to under-shoot by `-8.99`
   
   I do not think this is a format error. We just need to fix some math in the scorer to handle this "almost" equivalent case.


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different docs and scores after merging [lucene]

Posted by "thecoop (via GitHub)" <gi...@apache.org>.
thecoop commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3490801214

   After further testing, the incorrect scores are also output when the vectors are all written to the same segment in the first place.


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492864081

   Similarly, it makes me think we need to change how the scaling for normalized dot-product works and switch it to `return (1f + Math.max(score, -1f)) / 2f;`


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "mccullocht (via GitHub)" <gi...@apache.org>.
mccullocht commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3498861620

   Put up a PR for this @benwtrent. We may be able to do better than this but let's not let perfect be the enemy of good here.


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3492546808

   Some further testing I have done:
   
    - Unsigned byte still returns in the correct order, with its equivalent vector being ranked first, but its outside of 0.001 tolerance. Seven bit has correct order within that tolerance (so, more accurate than unsigned????)
    - PACKED_NIBBLE returns a score of `0` for the exactly equal vector after merge (really weird)
    - SINGLE_BIT_QUERY_NIBBLE: suffers similarly as packed_nibble, but doesn't return exactly zero.
   
   This is for euclidean, haven't tried inner product yet.
   
   This is the test I am running: https://github.com/apache/lucene/compare/branch_10x...benwtrent:lucene:trying-to-repeat-weirdness?expand=1


-- 
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-unsubscribe@lucene.apache.org

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


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


Re: [I] Lucene104ScalarQuantizedVectorFormat returns different scores after merging [lucene]

Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on issue #15408:
URL: https://github.com/apache/lucene/issues/15408#issuecomment-3491677688

   The key part is having a doc almost exactly the same as the query vector 
   
   Was able to replicate with that
   


-- 
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-unsubscribe@lucene.apache.org

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


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