Migrate disk PQ flat scan to flat API - #1341
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
a802e20 to
4d78a62
Compare
There was a problem hiding this comment.
Pull request overview
This PR migrates the disk PQ “flat scan” path onto the shared diskann::flat API, introducing a dedicated disk PQ FlatSearchStrategy + visitor that scans PQ-compressed rows and then reuses the existing full-precision reranking + filtering pipeline. It also factors PQ query preprocessing into a reusable owned query-computer (TransposedQueryComputer) so both graph and flat PQ search can share the same preprocessing approach.
Changes:
- Update
FlatIndex::knn_searchto return a lifetime-boundSendFutureso it can borrow strategy/context/output across.await. - Add
TransposedQueryComputer(+ error type) to build per-query PQ lookup tables for transposed PQ tables. - Route disk flat scan through
FlatIndexusing a new disk-specific flat strategy/visitor, and remove now-unused PQ scratch batching API.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| diskann/src/flat/index.rs | Adjusts knn_search signature/lifetimes to support borrowed-provider flat search entrypoints. |
| diskann-quantization/src/product/tables/transposed/query.rs | Introduces an owned PQ query computer for transposed tables (L2/IP), with unit tests. |
| diskann-quantization/src/product/tables/transposed/mod.rs | Wires the new transposed query module into the transposed table submodule exports. |
| diskann-quantization/src/product/tables/mod.rs | Re-exports the new transposed query computer + error at the tables module boundary. |
| diskann-quantization/src/product/mod.rs | Re-exports the new transposed query types at the product module boundary. |
| diskann-disk/src/search/provider/disk_provider.rs | Implements disk PQ flat scan via diskann::flat (DiskFlatProvider/DiskFlatSearchStrategy/DiskFlatVisitor) while preserving scan-time filtering and rerank behavior. |
| diskann-disk/src/search/pq/pq_scratch.rs | Removes PQScratch::max_vectors and updates tests accordingly (no longer needed after migration). |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Aditya Krishnan (@arkrishn94) I ended up making a few design changes beyond the
One related detail: filtering happens in These were the main areas where the migration required broader architectural choices, so feedback on them would be helpful before finalizing the approach. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1341 +/- ##
==========================================
- Coverage 91.55% 91.55% -0.01%
==========================================
Files 521 521
Lines 100371 100516 +145
==========================================
+ Hits 91899 92027 +128
- Misses 8472 8489 +17
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Mark Hildebrand (hildebrandmw)
left a comment
There was a problem hiding this comment.
As usual, I will defer to the maintainers of diskann-disk to make the judgement calls here, but what immediately stands out to me is that trying to fit the flat scan into the diskann flat-scan API is essentially recreating the custom flat-scan implementation but with significantly more code. That is, this appears to be working hard to fit the API (and indeed changing the API in diskann) without materially benefiting from doing so.
To me, this indicates two things:
- There is an ergonomic gap in the flat API that needs to be fixed. For example - it requires a
QueryComputerwhich is causing some of the churn in this PR [1]. I don't think that's a good direction since it separates the compute engine from the internal of theFlatAccessor, when closer coupling (e.g. howSearchAccessorworks now for the graph index) allows for safer optimization. - We're missing even lower-level infrastructure (e.g. generic batch PQ computation independent of
diskann-disk) that would help with reusability. Think: a more generally reuseable version ofcompute_pq_distance.
There are parts that look good. Extracting rerank_and_filter to a synchronous function (instead of the current unfortunate bounce through async) is a good improvement. Simplifying PQ scratch initialization is good - though I might suggest keeping it in DiskSearchScratch fusing it with the DiskSearchScratch's pooled API to avoid the multi-stage initialization that is currently done.
[1] The graph portion of diskann used to work this way and it turns out to be way better for a huge number of reasons to not.
|
I agree with your assessment. This migration exposed a limitation in the current flat API: separating the visitor from I also agree that the better direction is to improve the flat API itself. Following the principle established in PR #1067, I propose making flat visitors query-aware and responsible for producing distances. Proposed APIpub trait DistancesUnordered: HasId + Send + Sync {
type Error: ToRanked + Debug + Send + Sync + 'static;
fn distances_unordered<F>(
&mut self,
f: F,
) -> impl SendFuture<Result<(), Self::Error>>
where
F: Send + FnMut(Self::Id, f32);
}
pub trait SearchStrategy<'a, P, T>: Send + Sync
where
P: DataProvider,
{
type Visitor: DistancesUnordered<Id = P::InternalId>;
type Error: StandardError;
fn create_visitor(
&'a self,
provider: &'a P,
context: &'a P::Context,
query: T,
) -> Result<Self::Visitor, Self::Error>;
}The generic flat-search flow becomes: let mut visitor = strategy.create_visitor(provider, context, query)?;
visitor.distances_unordered(callback).await?;
processor.post_process(&mut visitor, query, candidates, output).await?;The responsibility boundary would be:
For disk PQ, graph and flat search can then use one pooled The main advantages are:
The main trade-off is a public flat-trait change. To limit migration cost, the existing trait and method names remain. I also searched for visible consumers and did not find an independent public implementation outside DiskANN itself, forks, and vendored copies. I have tried this proposal in the latest revision of the PR so that the design can be reviewed through a concrete implementation:
I also agree that a generic batch PQ primitive independent of I would appreciate your review of both the proposed API direction and the implementation in this revision. Does this align with what you had in mind? Mark Hildebrand (@hildebrandmw) Aditya Krishnan (@arkrishn94) |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Mark Hildebrand (hildebrandmw)
left a comment
There was a problem hiding this comment.
Thanks - reworking the flat API to resemble the graph API and merging the compute into DiskSearchScratch is much cleaner.
However, my larger concern still applies. I do not see what the flat API is enabling in diskann-disk to justify the increased complexity. Aditya Krishnan (@arkrishn94) - can you weigh in?
Aditya Krishnan (arkrishn94)
left a comment
There was a problem hiding this comment.
Thanks Junkui. Firstly, I apologize for the severely delayed review for this PR.
I'm largely on board with the direction of these changes. I like the simplification of the flat search API and might even suggest getting rid of the FlatIndex entirely. I guess if we go down this path, it might make sense to open a pre-cursor PR to this with just the changes to the flat API.
The simplification to DiskSearchScratch looks good, although as Mark said it would be nice to consolidate the initialization for it.
The one comment I had about the complexity of introducing the flat API here is- can we get rid of the FlatVisitor struct entirely and just work over the DiskAccessor?
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Reference Issues/PRs
Depends on #1359. After #1359 merges, this PR will be rebased onto
mainso its diff contains only the disk PQ migration.What does this implement/fix? Briefly explain your changes.
DiskAccessorand one pooledDiskSearchScratchacross graph and flat PQ search.The generic query-aware flat API redesign is reviewed independently in #1359. This PR focuses on adapting the disk backend to that API without maintaining a separate flat visitor or duplicate scratch initialization path.
Any other comments?
Until #1359 merges, this diff temporarily includes its prerequisite API changes. Those changes will disappear from this PR after it is rebased onto the updated
mainbranch.Architecture simplification
Before this change, graph search and flat search used separate query-state owners.
DiskAccessorandFlatVisitorduplicated the provider reference, pooled scratch handle, cache configuration, constructor logic, query initialization, and post-processing integration.flowchart TB subgraph Before["Before: parallel query-state owners"] direction LR G1["Graph search"] --> DA1["DiskAccessor"] --> S1["DiskSearchScratch"] F1["Flat search"] --> FV["FlatVisitor"] --> S2["DiskSearchScratch"] end DUP["Duplicated ownership and lifecycle:<br/>provider · pooled scratch · cache flag<br/>constructor · query initialization · post-process adapter"] DA1 -.-> DUP FV -.-> DUP subgraph After["After: one query-aware accessor"] direction LR G2["Graph search"] --> DA2["DiskAccessor"] F2["Flat search"] --> DA2 DA2 --> API["SearchAccessor<br/>+ DistancesUnordered"] API --> S3["DiskSearchScratch<br/>one pooled-query lifecycle"] endFlatVisitoris removed.DiskAccessornow supports both graph traversal and unordered flat scanning, so both paths share one query-state model and one scratch initialization path while preserving their different filtering stages.