[RFC] Bring your own type erasure - #1068
Merged
Merged
Conversation
added 2 commits
May 14, 2026 10:51
added 2 commits
May 14, 2026 10:56
Mark Hildebrand (hildebrandmw)
marked this pull request as ready for review
May 14, 2026 17:57
Aditya Krishnan (arkrishn94)
approved these changes
May 14, 2026
Aditya Krishnan (arkrishn94)
left a comment
Contributor
There was a problem hiding this comment.
Thanks Mark, this is super helpful.
Copilot started reviewing on behalf of
Mark Hildebrand (hildebrandmw)
May 14, 2026 18:01
View session
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an RFC documenting a “bring your own type erasure” pattern intended to reduce nested dispatch in distance-computation composition.
Changes:
- Adds a new RFC describing the motivation, example pattern, generated-code comparison, and trade-offs.
- Identifies potential application areas such as
DistanceProvider, spherical quantization kernels, and multi-vector backends.
Comments suppressed due to low confidence (3)
rfcs/01068-byo-type-erasure.md:31
- Grammar: the article and plural noun do not agree here.
The combination of unwrapping + delegation is used to create another trait object, leading to an unavoidable situations where we have at least two levels of dynamic dispatch.
rfcs/01068-byo-type-erasure.md:19
- Grammar: this compound modifier should be hyphenated before the noun.
Lower level APIs in our library use various flavors of type-erasure to enable polymorphism over metric, micro-architecture, and length specialization.
rfcs/01068-byo-type-erasure.md:44
- Grammar: this compound modifier should be hyphenated before the noun.
How can we redesign our lower level APIs to allow composition of distance computations?
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1068 +/- ##
==========================================
+ Coverage 89.95% 90.99% +1.04%
==========================================
Files 489 489
Lines 93127 93127
==========================================
+ Hits 83773 84744 +971
+ Misses 9354 8383 -971
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
Jordan Maples (JordanMaples)
approved these changes
May 14, 2026
Suryansh Gupta (suri-kumkaran)
approved these changes
May 15, 2026
Suryansh Gupta (suri-kumkaran)
added a commit
that referenced
this pull request
May 30, 2026
Adds the multi-vector MaxSim distance benchmark to `diskann-benchmark`
behind a `multi-vector` Cargo feature (off by default). Public library
API stays minimal: internals (`Kernel<A>`, `tiled_reduce`, layouts,
`TileBudget`) remain private; the only external surface is the generic
`build_max_sim<T>` factory (gated by the sealed `MaxSimElement` trait)
plus the `MaxSimKernel<T>` / `Erase<T>` traits.
The factory follows the BYOTE ("Bring your own type erasure") pattern
from RFC #1068.
`MaxSim::new` is now infallible (`-> Self`, no longer `-> Result`) and
`MaxSimError::BufferLengthIsZero` is removed. With the kernel boundary
enforcing the only meaningful precondition (`scores.len() ==
nrows()`), an empty `scores` buffer is a defined no-op — the standalone
"can't be empty" rule was defensive code that caught nothing the size
check doesn't already catch.
## JSON shape
```json
{
"type": "multi-vector-op",
"content": {
"element_type": "float32",
"isa": "x86-64-v3",
"runs": [ { "num_query_vectors": 8, "num_doc_vectors": 32, "dim": 128, "loops_per_measurement": 200, "num_measurements": 50 } ]
}
}
```
## Suggested review order
1. **`distance/isa.rs`** — `MaxSimIsa`, `NotSupported`,
`is_available()`.
2. **`distance/kernel.rs`** — `MaxSimKernel<T>` (`Result`-returning),
`Erase<T>`, `BoxErase`.
3. **`distance/factory.rs`** — sealed `MaxSimElement` + `build_max_sim`;
`Prepared` (SIMD) and `ReferenceKernel`; parity, size-mismatch,
zero-doc, and zero-query tests.
4. **`distance/max_sim.rs`** — `MaxSim::new` infallible;
`BufferLengthIsZero` removed.
5. **`multi_vector/{mod,distance/mod}.rs`** — public re-exports; verify
`QueryComputer` is gone.
6. **`benchmark/inputs/multi_vector.rs`** — `MultiVectorOp`, `BenchIsa`
shadow enum, `From<BenchIsa> for MaxSimIsa`.
7. **`benchmark/backend/multi_vector/{mod,driver,kernels}.rs`** —
`cfg_if!` gate; `Kernel<T>` carrier registered per `MaxSimElement`;
`try_match` rejects unavailable ISAs at job selection.
8. **Glue + tests** — `backend/mod.rs`, `inputs/mod.rs`, `Cargo.toml`
feature, `main.rs` integration tests, JSON fixtures.
---------
Co-authored-by: Suryansh Gupta <suryangupta@microsoft.com>
Harsha Simhadri (harsha-simhadri)
approved these changes
Jul 2, 2026
weiyaoluo (SeliMeli)
pushed a commit
to SeliMeli/DiskANN
that referenced
this pull request
Jul 22, 2026
Adds the multi-vector MaxSim distance benchmark to `diskann-benchmark`
behind a `multi-vector` Cargo feature (off by default). Public library
API stays minimal: internals (`Kernel<A>`, `tiled_reduce`, layouts,
`TileBudget`) remain private; the only external surface is the generic
`build_max_sim<T>` factory (gated by the sealed `MaxSimElement` trait)
plus the `MaxSimKernel<T>` / `Erase<T>` traits.
The factory follows the BYOTE ("Bring your own type erasure") pattern
from RFC microsoft#1068.
`MaxSim::new` is now infallible (`-> Self`, no longer `-> Result`) and
`MaxSimError::BufferLengthIsZero` is removed. With the kernel boundary
enforcing the only meaningful precondition (`scores.len() ==
nrows()`), an empty `scores` buffer is a defined no-op — the standalone
"can't be empty" rule was defensive code that caught nothing the size
check doesn't already catch.
## JSON shape
```json
{
"type": "multi-vector-op",
"content": {
"element_type": "float32",
"isa": "x86-64-v3",
"runs": [ { "num_query_vectors": 8, "num_doc_vectors": 32, "dim": 128, "loops_per_measurement": 200, "num_measurements": 50 } ]
}
}
```
## Suggested review order
1. **`distance/isa.rs`** — `MaxSimIsa`, `NotSupported`,
`is_available()`.
2. **`distance/kernel.rs`** — `MaxSimKernel<T>` (`Result`-returning),
`Erase<T>`, `BoxErase`.
3. **`distance/factory.rs`** — sealed `MaxSimElement` + `build_max_sim`;
`Prepared` (SIMD) and `ReferenceKernel`; parity, size-mismatch,
zero-doc, and zero-query tests.
4. **`distance/max_sim.rs`** — `MaxSim::new` infallible;
`BufferLengthIsZero` removed.
5. **`multi_vector/{mod,distance/mod}.rs`** — public re-exports; verify
`QueryComputer` is gone.
6. **`benchmark/inputs/multi_vector.rs`** — `MultiVectorOp`, `BenchIsa`
shadow enum, `From<BenchIsa> for MaxSimIsa`.
7. **`benchmark/backend/multi_vector/{mod,driver,kernels}.rs`** —
`cfg_if!` gate; `Kernel<T>` carrier registered per `MaxSimElement`;
`try_match` rejects unavailable ISAs at job selection.
8. **Glue + tests** — `backend/mod.rs`, `inputs/mod.rs`, `Cargo.toml`
feature, `main.rs` integration tests, JSON fixtures.
---------
Co-authored-by: Suryansh Gupta <suryangupta@microsoft.com>
weiyaoluo (SeliMeli)
pushed a commit
to SeliMeli/DiskANN
that referenced
this pull request
Jul 22, 2026
A small RFC describing a pattern we can use to reduce levels indirection in distance function composition. No true action is needed in the codebase, I'm just writing down what I've recommended to several people for an issue I'm seeing occur more frequently.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A small RFC describing a pattern we can use to reduce levels indirection in distance function composition. No true action is needed in the codebase, I'm just writing down what I've recommended to several people for an issue I'm seeing occur more frequently.
Rendered