add bf_tree benchmark infrastructure#1106
Conversation
4201a33 to
1661985
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #1106 +/- ##
==========================================
- Coverage 89.37% 89.34% -0.04%
==========================================
Files 484 484
Lines 91331 91452 +121
==========================================
+ Hits 81630 81704 +74
- Misses 9701 9748 +47
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
2981ffc to
c36e2a5
Compare
There was a problem hiding this comment.
Pull request overview
Adds bf_tree-backed benchmark support to diskann-benchmark (full-precision + spherical quantization; static + streaming variants) and updates diskann-bftree to address inplace-delete behavior and reduce per-call allocations in neighbor serialization.
Changes:
- Add a new
bftreeinput schema + backend benchmarks (static build/search and streaming runbook modes, including spherical quantization bit-width dispatch). - Add delete-time vector caching in
diskann-bftreeto support inplace-delete pruning after the underlying storage entries are removed. - Rework neighbor list serialization to avoid per-call heap allocations (stack buffer fast path).
Reviewed changes
Copilot reviewed 18 out of 19 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| diskann-bftree/src/quant.rs | Makes get_vector_sync available outside tests for delete-time caching. |
| diskann-bftree/src/provider.rs | Adds delete caches + accessor fallbacks for inplace-delete pruning; exposes clear_delete_caches(). |
| diskann-bftree/src/neighbors.rs | Switches neighbor serialization to a stack buffer fast path. |
| diskann-benchmark/src/inputs/mod.rs | Centralizes PRINT_WIDTH + write_field! for consistent summaries. |
| diskann-benchmark/src/inputs/graph_index.rs | Exposes IndexBuild helpers for reuse by bf_tree inputs. |
| diskann-benchmark/src/inputs/bftree.rs | New bf_tree input schemas (FP + spherical; static + streaming) and display/validation. |
| diskann-benchmark/src/backend/index/streaming/stats.rs | Adds a bf_tree-only GenericStats::noop() helper for skipped maintenance steps. |
| diskann-benchmark/src/backend/index/mod.rs | Registers bf_tree benchmarks behind the bftree feature. |
| diskann-benchmark/src/backend/index/bftree/mod.rs | New bf_tree benchmark module + shared streaming runner. |
| diskann-benchmark/src/backend/index/bftree/full_precision.rs | Implements bf_tree full-precision static + streaming benchmarks. |
| diskann-benchmark/src/backend/index/bftree/spherical.rs | Implements bf_tree spherical quantization static + streaming benchmarks. |
| diskann-benchmark/example/graph-index-bftree.json | Example config for bf_tree full-precision static build/search. |
| diskann-benchmark/example/graph-index-bftree-stream.json | Example config for bf_tree full-precision streaming runbook. |
| diskann-benchmark/example/graph-index-bftree-spherical.json | Example config for bf_tree spherical quantization static build/search. |
| diskann-benchmark/example/graph-index-bftree-spherical-stream.json | Example config for bf_tree spherical quantization streaming runbook. |
| diskann-benchmark/Cargo.toml | Adds bf_tree dependency + feature flag. |
| Cargo.toml | Adds diskann-bftree to workspace dependencies. |
| Cargo.lock | Locks in the new dependency edge. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
b8cdc3a to
8b0469c
Compare
|
@copilot re-evaluate please |
Re-evaluated: you’re right, those |
0461814 to
cfddda1
Compare
hildebrandmw
left a comment
There was a problem hiding this comment.
Thanks Jordan, this is looking good!
| // During `inplace_delete`, the algorithm first removes vectors from storage via | ||
| // `Delete::delete`, then prunes the neighbors of the deleted node which requires | ||
| // reading the deleted vector back through the accessor. We cache vectors here | ||
| // before deletion so the accessor can fall back to the cache on a `Transient` error. |
There was a problem hiding this comment.
I don't think is the right way to handle this. The issue is multi-insert wanting to access the adjacency list, right? I think there are several better options:
- In
bf-tree, we can delete the data, but not the adjacency lists. That way inplace delete will continue to work. This is a local fix. - Adjust the semantics of inplace delete or how it's called in benchmarks to actually delete the vector only after inplace delete runs so the algorithm still has adjacency list information?
In general, if we see a mismatch like this, we should focus on not working around the problem.
| /// Stack buffer size for neighbor serialization. Supports up to 512 neighbors | ||
| /// with u32 IDs (512 * 4 = 2048 bytes). Any practical ANN workload uses | ||
| /// max_degree well below this limit. | ||
| const MAX_NEIGHBOR_BUF_BYTES: usize = 2048; |
There was a problem hiding this comment.
This is a huge stack allocation. Is this showing up in profiles?
We already create auxiliary accessors for neighbor accesses - can't we use a Vec inside those as the scratch space? The accessors are already reused across calls and so we can amortize the Vec allocation.
There was a problem hiding this comment.
This was something that I was experimenting with and forgot to revert. I'll use the reusable vec buffer though.
| write_field!(f, "cb_size_byte", self.cb_size_byte)?; | ||
| write_field!(f, "leaf_page_size", self.leaf_page_size)?; | ||
| if let Some(v) = self.cb_max_record_size { | ||
| write_field!(f, "cb_max_record_size", v)?; |
There was a problem hiding this comment.
Along with my other comment, we should at least record what the default values are, even if they aren't explicitly set by the user (though they probably should be).
| build: IndexBuild, | ||
| search_phase: SearchPhase, | ||
| #[serde(default)] | ||
| vector_store_config: Option<BfTreeStoreConfig>, |
There was a problem hiding this comment.
I really think it's a good idea to not use defaults here. It's more verbose, but also means we have more information.
| ))?); | ||
|
|
||
| let max_points = | ||
| ((max_points_arg as f32) * (1.0 + 2.0 * consolidate_threshold)).ceil() as usize; |
There was a problem hiding this comment.
I believe this exists to work around limitations in delete tracking for the inmem index. Since bf-tree is using "hard deletes" instead, I think we can potentially avoid needing this extra overhead. Happy to talk about it offline.
Add benchmark support for the BfTreeProvider backend, including: - Full-precision static and streaming benchmarks - Spherical quantization (1/2/4-bit) static and streaming benchmarks - Delete cache in BfTreeProvider for inplace_delete graph repair - Stack buffer optimization for neighbor serialization - Early validation of max_degree vs buffer capacity - Runtime num_bits validation (1/2/4) for spherical inputs - ADDING_PROVIDERS.md guide for wiring new backends Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1170fe8 to
3bf48a1
Compare
|
I'm not sure I fully understand the bug in deletion. Why do we need to keep the deleted vector around for pruning its neighbors? If it's in someone's neighbor list after deletion, it should be ignored during pruning. Is there some difference in how bf-tree handles this versus how in-mem providers handle this that meant deletes needed to be handled differently? |
| "search_phase": { | ||
| "search-type": "topk", | ||
| "queries": "query100K.fbin", | ||
| "groundtruth": "msturing-gt-1M", |
There was a problem hiding this comment.
For the sake of keeping the examples directory a bit tighter, maybe we don't need an example for streaming + spherical + bf-tree, since we already have separate examples of streaming and spherical.
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| pub(crate) struct BfTreeStoreConfig { | ||
| /// Size of the circular buffer (in-memory write cache) in bytes. | ||
| pub(crate) cb_size_byte: usize, |
There was a problem hiding this comment.
iirc bf-tree has a minimum circular buffer size of 8192 bytes. Should we enforce that here?
There was a problem hiding this comment.
i can add that check.
|
|
||
| registry.register(SQ_NAME, spherical::BfTreeSpherical::new().search(Topk))?; | ||
|
|
||
| const SQ_STREAM: &str = "graph-index-stream-bftree-spherical-quantization"; |
There was a problem hiding this comment.
Do we need to create separate name variables to pass clippy or something?
There was a problem hiding this comment.
Nah, I'll remove these and just put them inline.
| mod spherical; | ||
| mod spherical_streaming; | ||
|
|
||
| /// Run a streaming benchmark using the given runbook parameters. |
There was a problem hiding this comment.
I'm not sure this function belongs in mod.rs, can we move it to its own file with a more descriptive name, like streaming_utils or similar?
There was a problem hiding this comment.
a streaming_utils would work. I'll shuffle it over.
|
I think posting some experiments with a more complicated runbook would be a good idea, it looks like all the experimental results here are from |
| /// Dispatches `num_bits` at runtime rather than using const generics to reduce | ||
| /// monomorphization. | ||
| pub(super) struct BfTreeSpherical { | ||
| search: Plugins<BfTreeSQProvider, SearchPhase, Strategy<Quantized>>, | ||
| } | ||
|
|
||
| impl BfTreeSpherical { | ||
| pub(super) fn new() -> Self { | ||
| Self { | ||
| search: Plugins::new(), | ||
| } |
There was a problem hiding this comment.
Can some of these general helpers and structs be moved to their own file? Then we could have one file for spherical utils etc., one for generic search, and one for streaming. Would make it easier to understand the structure and navigate
There was a problem hiding this comment.
I've already largely refactored all of this out in a follow-up pr. I just didn't want to overwhelm this pr with changes.
There was a problem hiding this comment.
I left some cosmetic comments but overall the structure and code look good to me. For the streaming I have a couple of concerns:
- I would like to see experiments with a more complicated runbook to make sure we have caught any perf or correctness issues. I would especially want to make sure we've tested
replaceadequately. - I am not sure I understand why encountering deleted nodes during pruning and throwing a
Transienterror is a bug. This seems like intended behavior and the transient error can be appropriately ignored. If we instead leave the deleted vectors in the graph until the end of deletion and take them into account during pruning of other nodes, we're just wasting work by computing distances to them anyway. Can you explain the reasoning here if I am wrong?
Thanks!
| Ok(results) | ||
| } | ||
|
|
||
| pub(crate) fn register_benchmarks(registry: &mut Registry) -> anyhow::Result<()> { |
There was a problem hiding this comment.
Can we model the tag names based off
DiskANN/diskann-benchmark/src/backend/index/benchmarks.rs
Lines 95 to 111 in 68cc3c4
There was a problem hiding this comment.
I'll rename these all in a follow-up pr to align with each other
In-mem marks the vectors as deleted but keeps them around to be used during the inplace delete as the query point for finding replacement neighbors. bf_tree's design decision to hard delete would break this flow, which is why storing them temporarily is necessary. The bug I'm addressing was introduced during the bftree migration out of providers and into its own crate and we opted to go for the hard deletes. |
hildebrandmw
left a comment
There was a problem hiding this comment.
Thanks - I think this is revealing issues with our inplace_delete API that need to get resolved. I'd say stick with hard deletes and avoid the inplace delete method that requires soft-deletes for now.
| @@ -0,0 +1,143 @@ | |||
| use std::{io::Write, sync::Arc}; | |||
There was a problem hiding this comment.
Don't forget your header. We should add a CI lint for this.
| pub(crate) graph_params: Option<GraphParams>, | ||
|
|
||
| // Nodes marked for deletion. Vector data is removed in `flush_deletes()`. | ||
| pending_deletes: Mutex<Vec<u32>>, |
There was a problem hiding this comment.
I'm still trying to understand this. I think we need to fix inplace_delete instead of hack around. I'm not sure how inplace_delete is even supposed to work at all since it calls delete and then immediately accesses the deleted element. For now, using the onehop or onehop_and_twohop should avoid fetching the element we just deleted.
TLDR: I propose we don't bend over to make soft-deletes work and instead use the inplace-delete methods that don't require soft deletes. Then we can work on getting rid of the whole concept of a soft delete.
| let id_size = std::mem::size_of::<I>(); | ||
| let total_len = self.dim * id_size; | ||
|
|
||
| let mut buf = self.buf.lock().unwrap(); |
There was a problem hiding this comment.
When I was referring to scratch allocation, I meant passing in this scratch buffer as an argument. The various accessors can then allocate the buffer and pass it into these calls.
Having a central buffer behind a lock introduces a serialization bottleneck that isn't needed.
|
|
||
| pub(crate) fn data_type(&self) -> DataType { | ||
| self.data_type | ||
| } |
There was a problem hiding this comment.
I agree in principle with moving these behind accessors. However, the larger goal I think should be towards not interacting with inputs piecemeal and instead having inputs do coarser grained things, like return a whole diskann::graph::Config.
The reasoning is that these piecemeal type operations can very easily lead to benchmarks trying to reuse inputs from other benchmarks and just "forgetting" about arguments, leading them to accept inputs they don't honor.
I'm not really sure what the right solution is outside of engineering discipline.
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| drop_deleted: Option<GenericStats>, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| release: Option<GenericStats>, |
There was a problem hiding this comment.
Making these optional is not the move. It forces us into an awkward situation of "knowing" that "None" means bf-tree, but even in that case, the time spent in bf-tree's flushing isn't accounted for. Either make this an enum that is more explicit about the backend, or duplicate the struct to match what's more precisely needed.
| None => diskann_quantization::spherical::PreScale::None, | ||
| }; | ||
|
|
||
| let quantizer = diskann_quantization::spherical::SphericalQuantizer::train( |
There was a problem hiding this comment.
Maybe not necessary for this PR, but sequences like this are what I imagine we can use the inputs more directly for: Instead of pulling out all the configuration from the inputs, we can instead provide the input with raw data and let it construct the quantizer. This makes it much more difficult to forget about fields. Anyways, food for thought.
Doubly so that this is basically reimplemented in the static benchmarks.
Adds benchmark support for the bf_tree storage backend — both full-precision and spherical-quantized, in static (build-once) and streaming (insert/delete/search) modes.
What's included
Benchmark variants (4 tags):
graph-index-bftree-full-precision-f32— static build + searchgraph-index-bftree-stream-full-precision-f32— streaming with runbookgraph-index-build-bftree-spherical-quantization— static spherical (1/2/4-bit)graph-index-stream-bftree-spherical-quantization— streaming spherical (1/2/4-bit)Bug fixes in diskann-bftree:
inplace_deleteremoves a vector viaDelete::delete, then prunes the deleted node's neighbors — which requires reading the deleted vector back for distance computations. Previously the vector was immediately removed from bf-tree storage, causingTransienterrors during pruning. Fix:delete()now defers actual vector removal by recording the ID inpending_deletes. Vectors remain readable throughout pruning.flush_deletes()performs the actual bf-tree removal and is called frommaintain()after each inplace_delete batch completes.append_vectorwould resize the neighbor list toself.dimand write the count atdim - 1, butset_neighborswrote a variable-length buffer. This inconsistency caused bf-tree page fragmentation and potential read errors when the two code paths produced different value sizes for the same key. Fix: both paths now write a fixed-size dim-element buffer using the same format (|neighbors|padding|count|).set_neighborsandappend_vectorusedVec::concat()/Vec::resize()on every call. Replaced with a singleMutex<Vec<u8>>buffer allocated once inNeighborProvider::new(), eliminating per-call heap allocations.Benchmark infrastructure:
bftree_parameters_from(),run_streaming()flush_deletes()to perform deferred vector removal after maintenance passesQuantDeletetrait — no longer needed with deferred deletion approachGenericStats::emptyreplaced withOption<GenericStats>for maintain stats (bf_tree has no separate drop_deleted/release phases)Example configs
See
diskann-benchmark/example/graph-index-bftree*.jsonfor ready-to-run configurations targeting MSTuring-1M.Tests on CosmosDBDevBox VM (16 vCPU 64GB RAM 2,048GB SSD)
Results from MSTuring-1M
Full Precision streaming:
Spherical 2-bit streaming:
Spherical 4-bit streaming:
Comparison across bit widths at L=200:
The configuration used in the above tests was enough to keep all of the data in memory and didn't overflow to disk. I also tested full-precision using 32mb of cb size and experienced a 2x performance degradation due to the disk lookups.
CI reports an increase in IR size of 11%, which is over the 5% allowed in the CI file. This is largely unavoidable given the amount of code added here, so that CI gate should be ignored for this PR.