[MOD-14955] Add DistanceCalculatorWithNorm#1002
Conversation
| * | ||
| * @return the distance between the vectors. | ||
| */ | ||
| DistType calcDistance(const void *vector_data1, const void *vector_data2) const { |
There was a problem hiding this comment.
This method calls indexCalculator->calcDistance, restoring the per-distance IndexCalculatorInterface vtable fallback removed by PR #937. DistanceCalculatorWithNorm always returns nullptr for the query kernel, so every visited candidate now takes a virtual call followed by the raw kernel call; existing calculators also pay the new branch. Cache a non-virtual state-aware callback or specialize the normalized dispatch outside the candidate loop, and benchmark against the #937 path before merging
f438a59 to
d37077d
Compare
4deae2b to
a0be70d
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a0be70d. Configure here.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dor-forer-MOD-14952-support-quant-normalization #1002 +/- ##
===================================================================================
+ Coverage 97.11% 97.12% +0.01%
===================================================================================
Files 141 141
Lines 8296 8326 +30
===================================================================================
+ Hits 8057 8087 +30
Misses 239 239 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|

Describe the changes in the pull request
This PR is stacked on #1000. Review only the incremental diff against
dor-forer-MOD-14952-support-quant-normalization.Cherry-picks the two commits from ARM-software/VectorSimilarity-for-Arm#3:
493ee7a8andc27401d6.This PR adds
DistanceCalculatorWithNorm, which invokes the selected SQ8 kernel and applies the mean-normalization correction terms needed to recover the distance between the original vectors. The follow-up commit makes metadata reads safe for unaligned storage.The calculator integrates with the construction-time dispatch introduced in #999:
mean_sum_squares.IndexCalculatorInterface::calcDistanceorcalcDistanceForQuery, so it does not restore the per-distance vtable lookup removed by MOD-14916 Devirtualize HNSW / brute-force search hot path #937.Tests cover FP32/FP16, L2/IP, symmetric/asymmetric, zero-mean, randomized-vector, and stateless/stateful dispatch behavior.
Which issues this PR fixes
Main objects this PR modified
DistanceCalculatorWithNormMark if applicable
Validation
make formatmake check-formattest_componentsandtest_hnswtest_components: 40/40 passedtest_hnswexcluding the two external serialization fixtures: 168/168 passedgit diff --check dor-forer-MOD-14952-support-quant-normalization...HEADDistanceDispatchaveraged 2.69 ns/call (no measurable regression within run-to-run noise)Note
Medium Risk
Changes core vector distance math for normalized SQ8 indices; incorrect formulas would skew search results, though coverage is strong and L2 symmetric path stays on the raw kernel.
Overview
Adds
DistanceCalculatorWithNorm, a distance calculator for mean-normalized SQ8 indices where stored vectors are quantized fromx' = x - mean. It runs the existing SQ8 kernels and applies analytical corrections usingx_mean_ip/y_mean_ipmetadata andmean_sum_squaresso L2/IP distances match the original (unshifted) vectors for symmetric SQ8–SQ8 and asymmetric SQ8–query paths.getDistanceDispatchkeeps a stateless fast path for stored-to-stored L2 (mean terms cancel); other modes use stateful callbacks with a cached context (kernels +mean_sum_squares) for the HNSW hot path. Metadata is read viaload_unaligned.Unit tests in
test_components.cppcover FP32/FP16, L2/IP, symmetric/asymmetric distances, zero-mean equivalence to raw kernels, random vectors vs brute force, and dispatch stateless vs stateful behavior.Reviewed by Cursor Bugbot for commit c559e31. Bugbot is set up for automated code reviews on this repo. Configure here.