-
Notifications
You must be signed in to change notification settings - Fork 376
Experimental Multi-Vector Chamfer Distance with SIMD & BLAS Optimizations #730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
suri-kumkaran
wants to merge
4
commits into
main
Choose a base branch
from
users/suryangupta/multivector-bench
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT license. | ||
| [package] | ||
| name = "experimental-multi-vector-bench" | ||
| edition.workspace = true | ||
| version.workspace = true | ||
| authors.workspace = true | ||
| description = "Experimental multi-vector benchmarking support for DiskANN" | ||
| documentation.workspace = true | ||
| license.workspace = true | ||
|
|
||
| [[bin]] | ||
| name = "multivec-bench" | ||
| path = "src/bin/multivec_bench.rs" | ||
|
|
||
| [dependencies] | ||
| diskann-linalg.workspace = true | ||
| diskann-utils.workspace = true | ||
| diskann-quantization.workspace = true | ||
| diskann-vector.workspace = true | ||
| diskann-wide.workspace = true | ||
|
|
||
| # Benchmark dependencies | ||
| anyhow.workspace = true | ||
| diskann-benchmark-runner.workspace = true | ||
| rand.workspace = true | ||
| serde = { workspace = true, features = ["derive"] } | ||
| serde_json.workspace = true | ||
| thiserror.workspace = true | ||
|
|
||
| [dev-dependencies] | ||
| tempfile.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # experimental-multi-vector-bench | ||
|
|
||
| Experimental multi-vector benchmarking support for DiskANN, enabling late interaction retrieval with token-level embeddings. | ||
|
|
||
| ## Scope & Goals | ||
|
|
||
| This crate is an **experimental workspace** focused on: | ||
|
|
||
| 1. **Fast Chamfer distance implementation for `f32`** - Develop and benchmark high-performance implementations of the Chamfer distance function for multi-vector representations using 32-bit floating point values. | ||
|
|
||
| 2. **Multiple computation approaches** - Compare naive scalar, SIMD-accelerated, transposed, tiling, and SGEMM implementations to quantify performance gains. | ||
|
|
||
| 3. **Benchmarking infrastructure** - Provide tooling to measure and compare different implementation strategies. | ||
|
|
||
| ## Current Status | ||
|
|
||
| - ✅ `MultiVector` type alias for `Mat<Standard<f32>>` (row-major storage from diskann-quantization) | ||
| - ✅ `TransposedMultiVector` type for block-transposed storage (SIMD-optimized) | ||
| - ✅ `Chamfer<Approach>` - Generic distance calculator using Inner Product similarity | ||
| - ✅ `Chamfer<NaiveApproach>` - Scalar baseline implementation | ||
| - ✅ `Chamfer<SimdApproach>` - SIMD-accelerated implementation | ||
| - ✅ `Chamfer<TransposedApproach>` - Block-transposed SIMD with transposed documents | ||
| - ✅ `Chamfer<TransposedWithTilingApproach>` - Block-transposed SIMD with query pair tiling | ||
| - ✅ `Chamfer<QueryTransposedWithTilingApproach>` - Transposed query with doc pair tiling | ||
| - ✅ `Chamfer<SgemmApproach>` - BLAS SGEMM + SIMD row-max | ||
| - ✅ Implements `diskann_vector::DistanceFunction` trait for ecosystem compatibility | ||
| - ✅ Benchmark utility integrated with diskann-benchmark-runner | ||
|
|
||
| ## Usage | ||
|
|
||
| ```rust | ||
| use experimental_multi_vector_bench::{ | ||
| Chamfer, SimdApproach, TransposedWithTilingApproach, QueryTransposedWithTilingApproach, | ||
| MultiVector, TransposedMultiVector, Standard, | ||
| }; | ||
| use diskann_vector::DistanceFunction; | ||
|
|
||
| // Create a multi-vector (3 vectors of dimension 4) | ||
| let mv = MultiVector::new(Standard::new(3, 4), 0.0f32).unwrap(); | ||
|
|
||
| // Basic usage with row-major vectors (NaiveApproach or SimdApproach) | ||
| let chamfer = Chamfer::<SimdApproach>::new(); | ||
| let distance = chamfer.evaluate_similarity(&query, &document); | ||
|
|
||
| // Optimized for few query tokens (≤8): transpose documents | ||
| let chamfer = Chamfer::<TransposedWithTilingApproach>::new(); | ||
| let transposed_doc = TransposedMultiVector::from(&document); | ||
| let distance = chamfer.evaluate_similarity(&query, &transposed_doc); | ||
|
|
||
| // Optimized for many query tokens (≥16): transpose query instead | ||
| let chamfer = Chamfer::<QueryTransposedWithTilingApproach>::new(); | ||
| let transposed_query = TransposedMultiVector::from(&query); | ||
| let distance = chamfer.evaluate_similarity(&transposed_query, &document); | ||
|
|
||
| // For large Q×D: use SGEMM | ||
| use experimental_multi_vector_bench::{SgemmApproach, SgemmScratch}; | ||
| let chamfer = Chamfer::<SgemmApproach>::new(); | ||
| let mut scratch = SgemmScratch::new(); | ||
| let distance = chamfer.evaluate_similarity_with_scratch(&query, &document, &mut scratch); | ||
| ``` | ||
|
|
||
| ## Type Aliases | ||
|
|
||
| This crate uses shared types from `diskann-quantization` for multi-vector representation: | ||
|
|
||
| ```rust | ||
| // Row-major owning matrix | ||
| pub type MultiVector = Mat<Standard<f32>>; | ||
|
|
||
| // Immutable view | ||
| pub type MultiVectorRef<'a> = MatRef<'a, Standard<f32>>; | ||
| ``` | ||
|
|
||
| The `Standard<f32>` representation provides: | ||
|
|
||
| - Contiguous row-major storage | ||
| - Direct `as_slice()` access for BLAS operations | ||
| - Zero-copy views via `MatRef` | ||
|
|
||
| ## Future Work | ||
|
|
||
| - [ ] Add RFC based on findings for DiskANN integration | ||
| - [ ] Additional similarity measures (Cosine, SquaredL2) | ||
| - [ ] Support for additional element types (`f16`, `u8` quantized, etc.) | ||
|
|
||
| ## Running Benchmarks | ||
|
|
||
| ```bash | ||
| # Run benchmarks with example configuration | ||
| cargo run --release -p experimental-multi-vector-bench --bin multivec-bench -- run \ | ||
| --input-file experimental-multi-vector-bench/examples/bench.json \ | ||
| --output-file results.json | ||
|
|
||
| # Verify correctness (all approaches should produce same checksum) | ||
| cargo run --release -p experimental-multi-vector-bench --bin multivec-bench -- run \ | ||
| --input-file experimental-multi-vector-bench/examples/verify.json \ | ||
| --output-file verify_results.json | ||
| ``` | ||
|
|
||
| See [examples/bench.json](examples/bench.json) for benchmark configuration format. | ||
|
|
||
| ### Benchmark Configuration | ||
|
|
||
| The benchmark supports six approaches via the `approach` field: | ||
|
|
||
| - `"naive"` - Scalar baseline | ||
| - `"simd"` - SIMD-accelerated | ||
| - `"transposed_simd"` - Block-transposed SIMD | ||
| - `"transposed_with_tiling"` - Block-transposed SIMD with query pair tiling | ||
| - `"query_transposed_with_tiling"` - Transposed query with doc pair tiling | ||
| - `"sgemm"` - BLAS SGEMM + SIMD row-max | ||
|
|
||
| ## Module Structure | ||
|
|
||
| ```text | ||
| src/ | ||
| ├── lib.rs # Crate root with re-exports and type aliases | ||
| ├── multi_vector.rs # TransposedMultiVector type (block-transposed storage) | ||
| ├── distance/ | ||
| │ ├── mod.rs # Chamfer<Approach> generic struct | ||
| │ ├── naive.rs # Scalar implementation (NaiveApproach) | ||
| │ ├── simd.rs # SIMD-accelerated (SimdApproach) | ||
| │ ├── transposed.rs # Transposed docs (TransposedApproach) | ||
| │ ├── transposed_tiling.rs # Transposed docs + query tiling (TransposedWithTilingApproach) | ||
| │ ├── query_transposed_tiling.rs # Transposed query + doc tiling (QueryTransposedWithTilingApproach) | ||
| │ └── sgemm.rs # BLAS SGEMM + row-max (SgemmApproach) | ||
| └── bench/ | ||
| ├── mod.rs # Benchmark registration and dispatch | ||
| ├── input.rs # Benchmark input types | ||
| └── runner.rs # Benchmark execution logic | ||
| ``` | ||
|
|
||
| ## Contributing | ||
|
|
||
| This work is experimental and will be submitted as separate PRs. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| { | ||
| "search_directories": [], | ||
| "jobs": [ | ||
| { | ||
| "type": "multivec-op", | ||
| "content": { | ||
| "approach": "simd", | ||
| "runs": [ | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 16 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 } | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "type": "multivec-op", | ||
| "content": { | ||
| "approach": "transposed_simd", | ||
| "runs": [ | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 16 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 } | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "type": "multivec-op", | ||
| "content": { | ||
| "approach": "transposed_with_tiling", | ||
| "runs": [ | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 16 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 } | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "type": "multivec-op", | ||
| "content": { | ||
| "approach": "query_transposed_with_tiling", | ||
| "runs": [ | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 16 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 } | ||
| ] | ||
| } | ||
| }, | ||
| { | ||
| "type": "multivec-op", | ||
| "content": { | ||
| "approach": "sgemm", | ||
| "runs": [ | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 128, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 }, | ||
| { "dim": 256, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 16 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 8, "num_doc_token": 32 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 16, "num_doc_token": 64 }, | ||
| { "dim": 384, "num_points": 100, "loops_per_measurement": 10, "num_measurements": 50, "num_query_token": 32, "num_doc_token": 128 } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to think about: the creep of
Standardspecific methods is not something I think we should lean into - especially if we want this to replacediskann_utils::views::Matrixand friends.I've found myself needing something like this for some other multi-vector related work and I think it makes sense to have something like
This way, MinMax, transposed, blocked etc. can all opt-in to this as well. That said, I feel that the lack of ability to add inherent methods to be a little unfortunate.