Skip to content

Fix sq internal vector encoding#6763

Merged
generall merged 3 commits intodevfrom
Fix-SQ-internal-vector-encoding
Jun 26, 2025
Merged

Fix sq internal vector encoding#6763
generall merged 3 commits intodevfrom
Fix-SQ-internal-vector-encoding

Conversation

@IvanPleshkov
Copy link
Contributor

@IvanPleshkov IvanPleshkov commented Jun 26, 2025

This PR fixes SQ accuracy lost after #6729.

The problem was with the calculation of the vector's offset value (what is the offset value for an SQ vector? You can find it here: https://qdrant.tech/articles/scalar-quantization/).

This value is different between vector and query. For query (in Dot metric) it's

values.iter().cloned().sum::<f32>()
    * self.metadata.alpha
    * self.metadata.offset

For the vector it's

dim as f32 * offset * offset + values.iter().cloned().sum::<f32>()
    * self.metadata.alpha
    * self.metadata.offset

This PR fixes difference in query internal method. Test included.

All Submissions:

  • Contributions should target the dev branch. Did you create your branch from dev?
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you formatted your code locally using cargo +nightly fmt --all command prior to submission?
  3. Have you checked your code using cargo clippy --all --all-features command?

Changes to Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@coderabbitai

This comment was marked as resolved.

Comment on lines +513 to +519
DistanceType::Dot => {
self.metadata.actual_dim as f32 * self.metadata.offset * self.metadata.offset
}
DistanceType::L1 => 0.0,
DistanceType::L2 => {
self.metadata.actual_dim as f32 * self.metadata.offset * self.metadata.offset
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the same. We can merge them if we like:

Suggested change
DistanceType::Dot => {
self.metadata.actual_dim as f32 * self.metadata.offset * self.metadata.offset
}
DistanceType::L1 => 0.0,
DistanceType::L2 => {
self.metadata.actual_dim as f32 * self.metadata.offset * self.metadata.offset
}
DistanceType::Dot | DistanceType::L2 => {
self.metadata.actual_dim as f32 * self.metadata.offset * self.metadata.offset
}
DistanceType::L1 => 0.0,

@generall generall merged commit 5895877 into dev Jun 26, 2025
20 checks passed
@generall generall deleted the Fix-SQ-internal-vector-encoding branch June 26, 2025 13:08
generall pushed a commit that referenced this pull request Jul 17, 2025
* Fix SQ internal vector encoding

* add test

* clippy warning
@coderabbitai coderabbitai bot mentioned this pull request Jul 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants