Commit e3139b4
[flat index] Flat Search Interface (#983)
This PR introduces a trait interface and a light index to support
brute-force search for providers that can be used as/are a flat-index.
There is an associated RFC that walks through the interface and
associated implementation in `diskann` as a new `flat` module.
Rendered RFC
[link](https://github.com/microsoft/DiskANN/blob/u/adkrishnan/flat-index/rfcs/00983-flat-search.md).
## Motivation
The repo has no first-class surface for brute-force search. This PR adds
a small trait hierarchy that gives flat search the same
provider-agnostic shape that graph search has, so any backend
(in-memory, quantized, disk, remote) can plug in once and reuse a shared
algorithm.
## Traits (`flat/strategy.rs`)
**`DistancesUnordered<C>`** — the single trait a backend must implement.
Fuses iteration and scoring into one method: the implementation drives a
full scan, scoring each element with a precomputed query computer `C`,
and invokes a callback with `(id, distance)` pairs. Key associated
types:
- `ElementRef<'a>` -- the reference shape `C` scores against.
- `Id` -- the id type yielded to the callback (decoupled from `HasId` so
visitors can yield any id shape).
- `C : for<'a> PreprocessedDistanceFunction<Self::ElementRef<'a>, f32>`
-- the precomputer query computer.
```rust
pub trait DistancesUnordered<C>: Send + Sync
where
C: for<'a> PreprocessedDistanceFunction<Self::ElementRef<'a>, f32>,
{
type ElementRef<'a>;
type Id;
type Error: ToRanked + Debug + Send + Sync + 'static;
fn distances_unordered<F>(
&mut self,
computer: &C,
f: F,
) -> impl SendFuture<Result<(), Self::Error>>
where
F: Send + FnMut(Self::Id, f32);
}
```
**`SearchStrategy<P, T>`** — factory that creates a `DistancesUnordered`
visitor from a provider + context, and builds the per-query computer.
Mirrors the graph-side strategy pattern. Two fallible methods:
- `create_visitor` — borrows provider + context, returns a `Visitor`
- `build_query_computer` — preprocesses the query `T` into a
`QueryComputer`
```rust
pub trait SearchStrategy<P, T>: Send + Sync
where
P: DataProvider,
{
type ElementRef<'a>;
type Id;
type QueryComputer: for<'a> PreprocessedDistanceFunction<Self::ElementRef<'a>, f32>;
type QueryComputerError: StandardError;
type Visitor<'a>: for<'b> DistancesUnordered<
Self::QueryComputer,
ElementRef<'b> = Self::ElementRef<'b>,
Id = Self::Id,
>;
type Error: StandardError;
fn create_visitor<'a>(&'a self, provider: &'a P, context: &'a P::Context)
-> Result<Self::Visitor<'a>, Self::Error>;
fn build_query_computer(&self, query: T)
-> Result<Self::QueryComputer, Self::QueryComputerError>;
}
```
## Index (`flat/index.rs`)
**`FlatIndex<P>`** — thin `'static` wrapper around a `DataProvider`.
Currently we have implemented the naive kNN search algorithm for the
flat index. `knn_search` asks the strategy for a visitor, builds the
query computer, drives `distances_unordered` through a priority queue,
and writes results via `SearchPostProcess`.
## Test infrastructure (`flat/test/`)
A self-contained test provider with dimension-validated `Strategy`,
transient-error injection, and a `KnnOracleRun` harness that compares
`knn_search` results against a brute-force reference with baseline
caching for regression detection.
## Future work
- **Post-processing** — `knn_search` currently uses the graph-side
`SearchPostProcess` trait to write results into the output buffer. #1067
will introduce a flat-specific post-processing step that decouples flat
search from the graph module's output machinery.
- **Vector ids** - The vector id over which `DistancesUnordered` acts is
an associated type, instead of the `InternalId` of the provider. This is
due to overly restrictive trait bounds on `VectorId` trait. We plan on
relaxing this allowing for more generic id types.
---------
Co-authored-by: Alex Razumov (from Dev Box) <alrazu@microsoft.com>1 parent 4f70a82 commit e3139b4
13 files changed
Lines changed: 2439 additions & 0 deletions
File tree
- diskann
- src
- flat
- test
- cases
- test/generated/flat/test/cases/flat_knn_search
- rfcs
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
0 commit comments