Skip to content

feat: support multi-query batch distance calculation in CalDistanceById#2422

Open
LHT129 wants to merge 1 commit into
antgroup:mainfrom
LHT129:feature-batch-cal-distance
Open

feat: support multi-query batch distance calculation in CalDistanceById#2422
LHT129 wants to merge 1 commit into
antgroup:mainfrom
LHT129:feature-batch-cal-distance

Conversation

@LHT129

@LHT129 LHT129 commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

Extend CalDistanceById(const DatasetPtr&, ids, count, bool, topk=-1) to accept DatasetPtr with NumElements > 1, computing distances for each query against its own row of IDs. The ids input is a row-major matrix with num_queries * count entries, where count is the number of IDs per query. The result distances buffer has the same row-major layout: distances[i * count + j] = distance from query i to ids[i * count + j]. -1 indicates an invalid ID.

Closes #2421

Changes

  • include/vsag/index.h: Updated API documentation for CalDistanceById(DatasetPtr, ...) to describe multi-query behavior
  • include/vsag/index_features.h: Added SUPPORT_BATCH_CAL_DISTANCE_BY_ID feature flag
  • src/algorithm/inner_index_interface.h/.cpp: Implemented default multi-query loop in CalDistanceById(DatasetPtr, ...). When NumElements > 1, constructs per-query sub-DatasetPtrs and iterates over each query row with its corresponding row of IDs
  • Feature flag registration: Set SUPPORT_BATCH_CAL_DISTANCE_BY_ID for indexes that support batch distance behavior
  • DiskANN/HNSW: Updated public batch method signatures to match the virtual API; they keep the existing full-distance behavior for topk == -1 and explicitly reject topk != -1 because top-k output is not supported there

Backward Compatibility

Default runtime behavior is unchanged when topk == -1 and NumElements == 1: callers receive the full unsorted distance array without returned IDs. topk != -1 enables sorted top-k (id, distance) output. The public virtual batch method signature changes by adding the final topk parameter, so out-of-tree subclasses must update their overrides.

Scope

  • Supported when the feature flag is advertised by the index implementation
  • DiskANN/HNSW: batch distance remains available for topk == -1; topk != -1 returns unsupported

Copilot AI review requested due to automatic review settings July 7, 2026 02:46
@LHT129 LHT129 self-assigned this Jul 7, 2026
@vsag-bot

vsag-bot commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

/label S-waiting-on-review
/waiting-on reviewer
/request-review @jiaweizone
/request-review @inabao
/request-review @vsag-bot

@LHT129 LHT129 added kind/improvement Optimizations, UX polish, or minor improvements 性能优化、体验打磨或细节改良 version/1.0 labels Jul 7, 2026
@mergify mergify Bot added the module/api label Jul 7, 2026
@mergify

mergify Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 All 2 merge protections satisfied — ready to merge.

Show 2 satisfied protections

🟢 Require kind label

  • label~=^kind/

🟢 Require version label

  • label~=^version/

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Extends the Index::CalDistanceById(const DatasetPtr&, ids, count, bool) API to support multi-query inputs (query->GetNumElements() > 1) and introduces a capability flag to advertise this behavior across selected index implementations.

Changes:

  • Updated API documentation to define the multi-query N × count row-major distances layout and invalid-ID sentinel behavior.
  • Added IndexFeature::SUPPORT_BATCH_CAL_DISTANCE_BY_ID and registered it for several in-scope algorithms.
  • Implemented a default multi-query loop in InnerIndexInterface::CalDistanceById(const DatasetPtr&, ...).

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
include/vsag/index.h Documents multi-query behavior and output layout for CalDistanceById(DatasetPtr, ...).
include/vsag/index_features.h Adds SUPPORT_BATCH_CAL_DISTANCE_BY_ID feature flag.
src/algorithm/inner_index_interface.h Updates interface docs for multi-query batch distance semantics.
src/algorithm/inner_index_interface.cpp Adds default multi-query implementation for CalDistanceById(DatasetPtr, ...).
src/algorithm/hgraph/hgraph_build.cpp Registers the new batch-distance feature for HGraph.
src/algorithm/ivf/ivf.cpp Registers the new batch-distance feature for IVF (fp32 path).
src/algorithm/bruteforce/bruteforce.cpp Registers the new batch-distance feature for BruteForce.
src/algorithm/pyramid/pyramid.cpp Registers the new batch-distance feature for Pyramid.
src/algorithm/lazy_hgraph/lazy_hgraph.cpp Registers the new batch-distance feature for LazyHGraph.
src/algorithm/sindi/sindi.cpp Registers the new batch-distance feature for SINDI.

Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread include/vsag/index_features.h Outdated
@LHT129
LHT129 force-pushed the feature-batch-cal-distance branch from ce4e4d8 to 4b0203f Compare July 7, 2026 03:45
@pull-request-size pull-request-size Bot added size/L and removed size/M labels Jul 7, 2026
Copilot AI review requested due to automatic review settings July 7, 2026 06:20
@LHT129
LHT129 force-pushed the feature-batch-cal-distance branch from 4b0203f to c655622 Compare July 7, 2026 06:20
@LHT129
LHT129 force-pushed the feature-batch-cal-distance branch from c655622 to d2d5afa Compare July 7, 2026 06:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 6 comments.

Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread tests/test_index/test_index_calc.cpp Outdated
Copilot AI review requested due to automatic review settings July 7, 2026 06:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 6 comments.

Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread tests/test_index/test_index_calc.cpp Outdated
Comment thread tests/test_index/test_index_calc.cpp Outdated
Comment thread include/vsag/index_features.h Outdated
@LHT129
LHT129 force-pushed the feature-batch-cal-distance branch from d2d5afa to ecc9190 Compare July 7, 2026 07:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (877 changed lines across 30 files).
Submitted 4 inline comments.

Reviewed commit 6e40881.

Comment thread src/algorithm/hgraph/hgraph.cpp
Comment thread src/algorithm/hgraph/hgraph.cpp Outdated
Comment thread src/algorithm/inner_index_interface.cpp
Comment thread src/algorithm/ivf/ivf.cpp Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (922 changed lines across 30 files).
Submitted 6 inline comments.

Reviewed commit d33b2c2.

Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/inner_index_interface.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread include/vsag/index.h Outdated
Comment thread include/vsag/index.h

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (964 changed lines across 30 files).
Submitted 2 inline comments.

Reviewed commit db1e930.

Comment thread src/algorithm/sindi/sindi.cpp
Comment thread include/vsag/index.h

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (965 changed lines across 30 files).
Submitted 1 inline comment.

Reviewed commit 3bb1437.

Comment thread src/algorithm/sindi/sindi.cpp Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (985 changed lines across 30 files).
Submitted 3 inline comments.

Reviewed commit 4b867ec.

Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread docs/docs/en/src/advanced/calc_distance_by_id.md Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (985 changed lines across 30 files).
Submitted 3 inline comments.

Reviewed commit 3a145f5.

Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread src/algorithm/lazy_hgraph/lazy_hgraph.cpp
Comment thread src/algorithm/inner_index_interface.cpp

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (992 changed lines across 30 files).
Submitted 3 inline comments.

Reviewed commit 977edd0.

Comment thread src/algorithm/sindi/sindi.cpp Outdated
Comment thread src/algorithm/inner_index_interface.cpp
Comment thread src/algorithm/bruteforce/bruteforce.cpp

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (989 changed lines across 30 files).
Submitted 5 inline comments.

Reviewed commit 3769288.

Comment thread src/algorithm/bruteforce/bruteforce.cpp
Comment thread src/algorithm/inner_index_interface.cpp
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread include/vsag/index.h

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (989 changed lines across 30 files).
Submitted 6 inline comments.

Reviewed commit 0646c48.

Comment thread src/algorithm/inner_index_interface.cpp
Comment thread src/algorithm/bruteforce/bruteforce.cpp
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread include/vsag/index.h
Comment thread docs/docs/en/src/api/index_class.md Outdated
Comment thread include/vsag/index_features.h Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (993 changed lines across 30 files).
Submitted 3 inline comments.

Reviewed commit 56e5501.

Comment thread src/algorithm/inner_index_interface.cpp
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread include/vsag/index.h

@wxyucs wxyucs 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

@mergify

mergify Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@vsag-bot vsag-bot 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.

Automated inline review completed.

Review effort: high (993 changed lines across 30 files).
Submitted 5 inline comments.

Reviewed commit 00b2f43.

Comment thread src/algorithm/inner_index_interface.cpp
Comment thread src/algorithm/bruteforce/bruteforce.cpp
Comment thread src/algorithm/sindi/sindi.cpp
Comment thread include/vsag/index.h
* If positive, returns the top-k smallest distances in ascending order along with
* matching IDs. The result Dim is min(topk, count). Invalid IDs (missing labels) are
* ranked last and only appear if there are fewer than topk valid IDs.
* @return result is valid distance of input ids. If topk == -1, Dim() is count and the

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.

[P3] Do not promise metadata omitted by legacy raw overrides

This says topk == -1 returns Dim() == count, but direct raw-pointer calls to HGraph and Pyramid leave both shape fields at zero, while DiskANN and HNSW also leave Dim() unset. Callers following the new contract can treat a nonempty distance buffer as empty; populate consistent metadata in those implementations or qualify the contract.

Comment thread include/vsag/index.h
* Callers should check SUPPORT_BATCH_CALC_DISTANCE_BY_ID before relying on
* multi-query behavior; unsupported indexes return an error. Both the input
* IDs and result distances use row-major layout with count IDs per query:
* ids[i * count + j] is the j-th ID for query i, and distances[i * count + j]

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.

[P3] Use the reduced top-k row stride in the layout description

This unconditionally describes result rows with stride count, but positive top-k results are packed with stride min(topk, count). For the second and later queries, distances[i * count + j] can therefore read past the returned allocation. Qualify this formula as the topk == -1 layout and direct top-k callers to GetDim().

- Add multi-query support to CalDistanceById batch interfaces
- Add topk parameter for returning top-k smallest distances with IDs
- Support HGraph, IVF, BruteForce, Pyramid, LazyHGraph, SINDI
- DiskANN/HNSW reject topk != -1
- Row-major layout for multi-query IDs and distances
- Invalid IDs ranked last with distance -1
- Feature flag SUPPORT_BATCH_CALC_DISTANCE_BY_ID

Signed-off-by: LHT129 <tianlan.lht@antgroup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in-review kind/improvement Optimizations, UX polish, or minor improvements 性能优化、体验打磨或细节改良 module/api module/docs module/index module/testing size/XL version/1.1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support multi-query batch distance calculation in CalDistanceById

4 participants