diskann-providers 0.51.0

DiskANN is a fast approximate nearest neighbor search library for high dimensional data
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

use std::{num::NonZeroUsize, sync::Arc};

use diskann::{
    ANNError, ANNResult,
    graph::{
        self, ConsolidateKind, InplaceDeleteMethod,
        glue::{
            Batch, DefaultSearchStrategy, InplaceDeleteStrategy, InsertStrategy,
            MultiInsertStrategy, PruneStrategy, SearchStrategy,
        },
        index::{DegreeStats, PagedSearchState, PartitionedNeighbors, SearchState},
        search_output_buffer,
    },
    neighbor::Neighbor,
    provider::{AsNeighbor, AsNeighborMut, DataProvider, Delete, SetElement},
    utils::ONE,
};

use crate::storage::{LoadWith, StorageReadProvider};

/// Synchronous wrapper around [`graph::DiskANNIndex`] that owns or borrows a tokio runtime.
pub struct DiskANNIndex<DP: DataProvider> {
    /// The underlying async DiskANNIndex.
    pub inner: Arc<graph::DiskANNIndex<DP>>,
    /// Keeps the runtime alive when `Self` owns it; `None` when using an external handle.
    _runtime: Option<tokio::runtime::Runtime>,
    handle: tokio::runtime::Handle,
}

/// Create a multi-threaded tokio runtime and return it together with its handle.
fn create_multi_thread_runtime() -> (tokio::runtime::Runtime, tokio::runtime::Handle) {
    #[allow(clippy::expect_used)]
    let rt = tokio::runtime::Builder::new_multi_thread()
        .build()
        .expect("failed to create tokio runtime");
    let handle = rt.handle().clone();
    (rt, handle)
}

/// Create a current-thread tokio runtime and return it together with its handle.
fn create_current_thread_runtime() -> (tokio::runtime::Runtime, tokio::runtime::Handle) {
    #[allow(clippy::expect_used)]
    let rt = tokio::runtime::Builder::new_current_thread()
        .build()
        .expect("failed to create tokio runtime");
    let handle = rt.handle().clone();
    (rt, handle)
}

impl<DP> DiskANNIndex<DP>
where
    DP: DataProvider,
{
    /// Construct a synchronous `DiskANNIndex` with its own multi-threaded `tokio::runtime::Runtime`.
    ///
    /// A default multi-threaded runtime will be created and owned by `Self`. For a single-threaded
    /// runtime use [`new_with_current_thread_runtime`](Self::new_with_current_thread_runtime), or
    /// to supply an external runtime handle use [`new_with_handle`](Self::new_with_handle).
    pub fn new_with_multi_thread_runtime(config: graph::Config, data_provider: DP) -> Self {
        let (rt, handle) = create_multi_thread_runtime();
        Self::new_internal(config, data_provider, Some(rt), handle, Some(ONE))
    }

    /// Construct a synchronous `DiskANNIndex` with its own single-threaded `tokio::runtime::Runtime`.
    ///
    /// A default current-thread runtime will be created and owned by `Self`. For a multi-threaded
    /// runtime use [`new_with_multi_thread_runtime`](Self::new_with_multi_thread_runtime), or
    /// to supply an external runtime handle use [`new_with_handle`](Self::new_with_handle).
    pub fn new_with_current_thread_runtime(config: graph::Config, data_provider: DP) -> Self {
        let (rt, handle) = create_current_thread_runtime();
        Self::new_internal(config, data_provider, Some(rt), handle, Some(ONE))
    }

    /// Construct a synchronous `DiskANNIndex` that uses a provided `tokio::runtime::Handle`.
    ///
    /// The `tokio::runtime::Runtime` is owned externally and we just keep a `Handle` to it.
    /// `thread_hint` is forwarded to [`graph::DiskANNIndex::new`] to size internal thread pools;
    /// pass `None` to let it choose a default.
    pub fn new_with_handle(
        config: graph::Config,
        data_provider: DP,
        handle: tokio::runtime::Handle,
        thread_hint: Option<NonZeroUsize>,
    ) -> Self {
        Self::new_internal(config, data_provider, None, handle, thread_hint)
    }

    fn new_internal(
        config: graph::Config,
        data_provider: DP,
        runtime: Option<tokio::runtime::Runtime>,
        handle: tokio::runtime::Handle,
        thread_hint: Option<NonZeroUsize>,
    ) -> Self {
        let inner = Arc::new(graph::DiskANNIndex::new(config, data_provider, thread_hint));
        Self {
            inner,
            _runtime: runtime,
            handle,
        }
    }

    /// Run an arbitrary async operation against the underlying
    /// [`graph::DiskANNIndex`] using this wrapper's tokio runtime.
    ///
    /// This is a catch-all escape hatch for async methods on the inner index
    /// that do not (yet) have a dedicated synchronous wrapper. The closure
    /// receives an `&Arc<graph::DiskANNIndex<DP>>` and should return a future.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let stats = index.run(|inner| inner.some_async_method(&ctx))?;
    /// ```
    pub fn run<F, Fut, R>(&self, f: F) -> R
    where
        F: FnOnce(&Arc<graph::DiskANNIndex<DP>>) -> Fut,
        Fut: core::future::Future<Output = R>,
    {
        self.handle.block_on(f(&self.inner))
    }

    /// Load a prebuilt index from storage with its own multi-threaded `tokio::runtime::Runtime`.
    ///
    /// This is the synchronous equivalent of
    /// [`LoadWith::load_with`](crate::storage::LoadWith::load_with).
    /// A default multi-threaded runtime is created and owned by `Self`.
    /// For a single-threaded runtime use [`load_with_current_thread_runtime`](Self::load_with_current_thread_runtime),
    /// or to supply an external runtime handle use [`load_with_handle`](Self::load_with_handle).
    pub fn load_with_multi_thread_runtime<T, P>(provider: &P, auxiliary: &T) -> ANNResult<Self>
    where
        graph::DiskANNIndex<DP>: LoadWith<T, Error = ANNError>,
        P: StorageReadProvider,
    {
        let (rt, handle) = create_multi_thread_runtime();
        let inner = handle.block_on(graph::DiskANNIndex::<DP>::load_with(provider, auxiliary))?;
        Ok(Self {
            inner: Arc::new(inner),
            _runtime: Some(rt),
            handle,
        })
    }

    /// Load a prebuilt index from storage with its own single-threaded `tokio::runtime::Runtime`.
    ///
    /// This is the synchronous equivalent of
    /// [`LoadWith::load_with`](crate::storage::LoadWith::load_with).
    /// A default current-thread runtime is created and owned by `Self`.
    /// For a multi-threaded runtime use [`load_with_multi_thread_runtime`](Self::load_with_multi_thread_runtime),
    /// or to supply an external runtime handle use [`load_with_handle`](Self::load_with_handle).
    pub fn load_with_current_thread_runtime<T, P>(provider: &P, auxiliary: &T) -> ANNResult<Self>
    where
        graph::DiskANNIndex<DP>: LoadWith<T, Error = ANNError>,
        P: StorageReadProvider,
    {
        let (rt, handle) = create_current_thread_runtime();
        let inner = handle.block_on(graph::DiskANNIndex::<DP>::load_with(provider, auxiliary))?;
        Ok(Self {
            inner: Arc::new(inner),
            _runtime: Some(rt),
            handle,
        })
    }

    /// Load a prebuilt index from storage using a provided `tokio::runtime::Handle`.
    ///
    /// This is the synchronous equivalent of
    /// [`LoadWith::load_with`](crate::storage::LoadWith::load_with).
    /// The `tokio::runtime::Runtime` is owned externally and we just keep a `Handle` to it.
    /// For an owned runtime use [`load_with_multi_thread_runtime`](Self::load_with_multi_thread_runtime)
    /// or [`load_with_current_thread_runtime`](Self::load_with_current_thread_runtime).
    pub fn load_with_handle<T, P>(
        provider: &P,
        auxiliary: &T,
        handle: tokio::runtime::Handle,
    ) -> ANNResult<Self>
    where
        graph::DiskANNIndex<DP>: LoadWith<T, Error = ANNError>,
        P: StorageReadProvider,
    {
        let inner = handle.block_on(graph::DiskANNIndex::<DP>::load_with(provider, auxiliary))?;
        Ok(Self {
            inner: Arc::new(inner),
            _runtime: None,
            handle,
        })
    }

    pub fn insert<S, T>(
        &self,
        strategy: S,
        context: &DP::Context,
        id: &DP::ExternalId,
        vector: T,
    ) -> ANNResult<()>
    where
        S: InsertStrategy<DP, T>,
        DP: SetElement<T>,
        T: Copy + Send,
    {
        self.handle
            .block_on(self.inner.insert(strategy, context, id, vector))
    }

    pub fn multi_insert<S, B>(
        &self,
        strategy: S,
        context: &DP::Context,
        vectors: Arc<B>,
        ids: Arc<[DP::ExternalId]>,
    ) -> ANNResult<()>
    where
        Self: 'static,
        S: MultiInsertStrategy<DP, B>,
        B: Batch,
        DP: for<'a> SetElement<B::Element<'a>>,
    {
        self.handle
            .block_on(self.inner.multi_insert(strategy, context, vectors, ids))
    }

    pub fn is_any_neighbor_deleted<NA>(
        &self,
        context: &DP::Context,
        accessor: &mut NA,
        vector_id: DP::InternalId,
    ) -> ANNResult<bool>
    where
        DP: Delete,
        NA: AsNeighbor<Id = DP::InternalId>,
    {
        self.handle.block_on(
            self.inner
                .is_any_neighbor_deleted(context, accessor, vector_id),
        )
    }

    pub fn drop_adj_list<NA>(&self, accessor: &mut NA, vector_id: DP::InternalId) -> ANNResult<()>
    where
        NA: AsNeighborMut<Id = DP::InternalId>,
    {
        self.handle
            .block_on(self.inner.drop_adj_list(accessor, vector_id))
    }

    #[allow(clippy::type_complexity)]
    pub fn get_undeleted_neighbors<NA>(
        &self,
        context: &DP::Context,
        accessor: &mut NA,
        vector_id: DP::InternalId,
    ) -> ANNResult<PartitionedNeighbors<DP::InternalId>>
    where
        DP: Delete,
        NA: AsNeighbor<Id = DP::InternalId>,
    {
        self.handle.block_on(
            self.inner
                .get_undeleted_neighbors(context, accessor, vector_id),
        )
    }

    pub fn inplace_delete<S>(
        &self,
        strategy: S,
        context: &DP::Context,
        id: &DP::ExternalId,
        num_to_replace: usize,
        inplace_delete_method: InplaceDeleteMethod,
    ) -> ANNResult<()>
    where
        S: InplaceDeleteStrategy<DP> + Sync + Clone,
        DP: Delete,
    {
        self.handle.block_on(self.inner.inplace_delete(
            strategy,
            context,
            id,
            num_to_replace,
            inplace_delete_method,
        ))
    }

    pub fn drop_deleted_neighbors<NA>(
        &self,
        context: &DP::Context,
        accessor: &mut NA,
        vector_id: DP::InternalId,
        only_orphans: bool,
    ) -> ANNResult<ConsolidateKind>
    where
        DP: Delete,
        NA: AsNeighborMut<Id = DP::InternalId>,
    {
        self.handle.block_on(self.inner.drop_deleted_neighbors(
            context,
            accessor,
            vector_id,
            only_orphans,
        ))
    }

    pub fn consolidate_vector<S>(
        &self,
        strategy: &S,
        context: &DP::Context,
        vector_id: DP::InternalId,
    ) -> ANNResult<ConsolidateKind>
    where
        DP: Delete,
        S: PruneStrategy<DP>,
    {
        self.handle
            .block_on(self.inner.consolidate_vector(strategy, context, vector_id))
    }

    pub fn search<S, T, O, OB, P>(
        &self,
        search_params: P,
        strategy: &S,
        context: &DP::Context,
        query: T,
        output: &mut OB,
    ) -> ANNResult<P::Output>
    where
        P: graph::search::Search<DP, S, T>,
        S: DefaultSearchStrategy<DP, T, O>,
        O: Send,
        OB: search_output_buffer::SearchOutputBuffer<O> + Send + ?Sized,
    {
        self.handle.block_on(
            self.inner
                .search(search_params, strategy, context, query, output),
        )
    }

    #[allow(clippy::type_complexity)]
    pub fn start_paged_search<S, T>(
        &self,
        strategy: S,
        context: &DP::Context,
        query: T,
        l_value: usize,
    ) -> ANNResult<PagedSearchState<DP, S, S::QueryComputer>>
    where
        S: SearchStrategy<DP, T> + 'static,
        T: Copy + Send,
    {
        self.handle.block_on(
            self.inner
                .start_paged_search(strategy, context, query, l_value),
        )
    }

    #[allow(clippy::type_complexity)]
    pub fn start_paged_search_with_init_ids<S, T>(
        &self,
        strategy: S,
        context: &DP::Context,
        query: T,
        l_value: usize,
        init_ids: Option<&[DP::InternalId]>,
    ) -> ANNResult<PagedSearchState<DP, S, S::QueryComputer>>
    where
        S: SearchStrategy<DP, T> + 'static,
        T: Copy + Send,
    {
        self.handle.block_on(
            self.inner
                .start_paged_search_with_init_ids(strategy, context, query, l_value, init_ids),
        )
    }

    pub fn next_search_results<S, T>(
        &self,
        context: &DP::Context,
        search_state: &mut SearchState<DP::InternalId, (S, S::QueryComputer)>,
        k: usize,
        result_output: &mut [Neighbor<DP::InternalId>],
    ) -> ANNResult<usize>
    where
        S: SearchStrategy<DP, T>,
    {
        self.handle.block_on(self.inner.next_search_results(
            context,
            search_state,
            k,
            result_output,
        ))
    }

    pub fn count_reachable_nodes<NA>(
        &self,
        start_points: &[DP::InternalId],
        accessor: &mut NA,
    ) -> ANNResult<usize>
    where
        NA: AsNeighbor<Id = DP::InternalId>,
    {
        self.handle
            .block_on(self.inner.count_reachable_nodes(start_points, accessor))
    }

    pub fn get_degree_stats<NA>(&self, accessor: &mut NA) -> ANNResult<DegreeStats>
    where
        for<'a> &'a DP: IntoIterator<Item = DP::InternalId, IntoIter: Send>,
        NA: AsNeighbor<Id = DP::InternalId>,
    {
        self.handle.block_on(self.inner.get_degree_stats(accessor))
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use diskann::{
        graph::{self, search_output_buffer},
        provider::DefaultContext,
        utils::ONE,
    };
    use diskann_utils::test_data_root;
    use diskann_vector::distance::Metric;

    use super::DiskANNIndex;
    use crate::{
        index::diskann_async,
        model::{
            configuration::IndexConfiguration,
            graph::provider::async_::{
                common::{FullPrecision, TableBasedDeletes},
                inmem::{self, CreateFullPrecision, DefaultProvider},
            },
        },
        storage::{AsyncIndexMetadata, SaveWith, StorageReadProvider, VirtualStorageProvider},
        utils::create_rnd_from_seed_in_tests,
    };

    #[test]
    fn test_save_then_sync_load_round_trip() {
        // -- Build an index in async context and save it -----------------------
        let save_path = "/index";
        let file_path = "/sift/siftsmall_learn_256pts.fbin";

        let train_data = {
            let storage = VirtualStorageProvider::new_overlay(test_data_root());
            let mut reader = storage.open_reader(file_path).unwrap();
            diskann_utils::io::read_bin::<f32>(&mut reader).unwrap()
        };

        let pq_bytes = 8;
        let pq_table = diskann_async::train_pq(
            train_data.as_view(),
            pq_bytes,
            &mut create_rnd_from_seed_in_tests(0xe3c52ef001bc7ade),
            crate::utils::create_thread_pool(2).unwrap().as_ref(),
        )
        .unwrap();

        let (build_config, parameters) = diskann_async::simplified_builder(
            20,
            32,
            Metric::L2,
            train_data.ncols(),
            train_data.nrows(),
            |_| {},
        )
        .unwrap();

        let fp_precursor =
            CreateFullPrecision::new(parameters.dim, parameters.prefetch_cache_line_level);
        let data_provider =
            DefaultProvider::new_empty(parameters, fp_precursor, pq_table, TableBasedDeletes)
                .unwrap();

        let index =
            DiskANNIndex::new_with_current_thread_runtime(build_config.clone(), data_provider);

        let storage = VirtualStorageProvider::new_memory();
        let ctx = DefaultContext;
        for (i, v) in train_data.row_iter().enumerate() {
            index.insert(FullPrecision, &ctx, &(i as u32), v).unwrap();
        }

        let save_metadata = AsyncIndexMetadata::new(save_path.to_string());
        let storage_ref = &storage;
        let metadata_ref = &save_metadata;
        index
            .run(|inner| {
                let inner = Arc::clone(inner);
                async move { inner.save_with(storage_ref, metadata_ref).await }
            })
            .unwrap();

        // -- Reload via the synchronous wrapped_async API ----------------------
        let load_config = IndexConfiguration::new(
            Metric::L2,
            train_data.ncols(),
            train_data.nrows(),
            ONE,
            1,
            build_config,
        );

        type TestProvider = inmem::FullPrecisionProvider<
            f32,
            crate::model::graph::provider::async_::FastMemoryQuantVectorProviderAsync,
            crate::model::graph::provider::async_::TableDeleteProviderAsync,
        >;

        let loaded: DiskANNIndex<TestProvider> =
            DiskANNIndex::load_with_current_thread_runtime(&storage, &(save_path, load_config))
                .unwrap();

        // -- Verify the loaded index is functional -----------------------------
        // A single search call is enough to confirm the sync wrapper loaded a
        // working index. Exhaustive search-correctness is tested elsewhere.
        let top_k = 5;
        let search_l = 20;
        let mut ids = vec![0u32; top_k];
        let mut distances = vec![0.0f32; top_k];
        let mut output = search_output_buffer::IdDistance::new(&mut ids, &mut distances);

        let query = train_data.row(0);
        let kind = graph::search::Knn::new_default(top_k, search_l).unwrap();
        let stats = loaded
            .search(kind, &FullPrecision, &DefaultContext, query, &mut output)
            .unwrap();

        assert_eq!(stats.result_count, top_k as u32);
        // The query is itself in the dataset, so the nearest neighbor must be at distance 0.
        assert_eq!(ids[0], 0);
        assert_eq!(distances[0], 0.0);
    }
}