Skip to content

[MOD-14953] Add calcDistanceForQuery to IndexCalculatorInterface#999

Merged
dor-forer merged 4 commits into
mainfrom
dor-forer-MOD-14953-calc-distance-for-query
Jul 23, 2026
Merged

[MOD-14953] Add calcDistanceForQuery to IndexCalculatorInterface#999
dor-forer merged 4 commits into
mainfrom
dor-forer-MOD-14953-calc-distance-for-query

Conversation

@dor-forer

@dor-forer dor-forer commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Describe the changes in the pull request

Cherry-picks ARM's ed1df68a4f1585747068a0560720fd76fb92a6b9 from ARM-software/VectorSimilarity-for-Arm#1.

This PR adds calcDistanceForQuery so stored candidates and query blobs can use asymmetric representations and operand ordering.

It also introduces a construction-time DistanceDispatch selected through getDistanceDispatch(DistanceMode). HNSW caches one dispatch for stored-to-stored distances and another for stored-to-query distances, avoiding calculator vtable lookups in the hot path.

The dispatch has separate optimized representations:

  • Stateless calculators cache the raw distance kernel directly, avoiding a context adapter or second indirect function call.
  • Stateful calculators cache a contextual callback and opaque context pointer, allowing later calculators to access index-level state without restoring virtual dispatch in HNSW.

HNSW search and batch-iterator paths use the stored-to-query dispatch. Index construction and maintenance continue using the stored-to-stored dispatch. Component tests cover symmetric defaults, asymmetric query functions, stateless dispatch, and contextual dispatch.

Which issues this PR fixes

  1. MOD-14953

Main objects this PR modified

  1. IndexCalculatorInterface, DistanceCalculatorInterface, and DistanceDispatch
  2. VecSimIndexAbstract
  3. HNSWIndex and HNSW_BatchIterator
  4. Component and tiered-HNSW unit tests

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Validation

  • make format
  • make check-format
  • Clean release build of test_components and test_hnsw
  • test_components: 28/28 passed
  • test_hnsw excluding the two external serialization fixtures: 168/168 passed
  • ASAN focused regression test: HNSWTest/0.hnsw_blob_sanity_test passed

@CLAassistant

CLAassistant commented Jul 22, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

* IndexCalculatorInterface change

Add calcDistanceForQuery() API and use it in HNSW index

* Improve tests

(cherry picked from commit ed1df68)
@dor-forer
dor-forer force-pushed the dor-forer-MOD-14953-calc-distance-for-query branch from 29ac23a to 2fe184d Compare July 22, 2026 09:02
@dor-forer
dor-forer marked this pull request as ready for review July 22, 2026 11:51
@dor-forer
dor-forer requested a review from ofiryanai July 23, 2026 08:44
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.74468% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.10%. Comparing base (dd6879e) to head (82f2462).

Files with missing lines Patch % Lines
src/VecSim/spaces/computer/calculator.h 88.88% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #999      +/-   ##
==========================================
- Coverage   97.14%   97.10%   -0.05%     
==========================================
  Files         141      141              
  Lines        8245     8268      +23     
==========================================
+ Hits         8010     8029      +19     
- Misses        235      239       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ofiryanai ofiryanai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

if (label == 1) { // the result in the hnsw index
expected_score = hnsw_index->calcDistance(norm_double_query, norm_double_vec);
expected_score =
hnsw_index->calcDistanceForQuery(norm_double_vec, norm_double_query);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AI suggest to increase coverage:

This HNSW instance defaults query_dist_func to dist_func, so it does not verify that HNSW selects the distinct StoredToQuery dispatch. Add an integration test that installs different stored↔stored and stored↔query functions

@dor-forer
dor-forer added this pull request to the merge queue Jul 23, 2026
Merged via the queue into main with commit 6e64d11 Jul 23, 2026
16 checks passed
@dor-forer
dor-forer deleted the dor-forer-MOD-14953-calc-distance-for-query branch July 23, 2026 15:18
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