fix: compute SQ dot distance from dequantized values#7355
Open
cwj0bzxg wants to merge 1 commit into
Open
Conversation
Xuanwo
reviewed
Jun 19, 2026
| lower_bound: f32, | ||
| ) -> f32 { | ||
| let code_dot = dot_u8(sq_code, query_sq_code) as f32; | ||
| let dot = step * step * code_dot |
Collaborator
There was a problem hiding this comment.
This expanded affine dot calculation is performed in f32, and the large offset terms can cancel in high-dimensional near-zero vectors enough to flip SQ Dot rankings.
| ], | ||
| ); | ||
| let storage = | ||
| ScalarQuantizationStorage::try_new(8, DistanceType::Dot, -10.0..245.0, [batch], None) |
Collaborator
There was a problem hiding this comment.
The new coverage only exercises unit-step and constant bounds, so regressions in the step and step * step terms can pass while arbitrary-range SQ Dot distances remain wrong.
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.
This PR fixes Dot distance computation for scalar-quantized vectors. #7352
Previously, the SQ Dot path computed the dot product directly from
u8quantized codes and only applied a scale factor. This is incorrect when SQ uses a non-zerolower_bound, because each code represents an offset value:As a result, the old Dot path missed the offset-related terms and could produce a very different ranking from the actual vector values. This caused severe recall degradation for SQ indexes with
metric="dot".The fix computes SQ Dot using the full expansion:
Equivalently:
distance = 1 - dotis then used for Dot distance, matching the existing Dot distance convention.Changes
lower_boundoffset terms.Validation
I rebuilt the Python extension with this patch and reran MSMARCO WebSearch 1M Dot benchmarks.
Dataset:
Before this fix,
IVF_HNSW_SQrecall@10 was only around0.0250to0.0684acrossef=20..640, while theIVF_HNSW_FLATbaseline reached0.5179to0.9377.After this fix:
IVF_HNSW_SQ
IVF_SQ
IVF_HNSW_FLAT (baseline)
With the corrected distance formula, SQ Dot recall is restored to the same range as the Flat baseline.
IVF_HNSW_SQis close toIVF_HNSW_FLATat the sameef, while generally providing higher QPS. The remaining recall gap at highefis expected from quantization loss rather than an incorrect distance formula.