-
Notifications
You must be signed in to change notification settings - Fork 379
Add API to load pre-built index in wrapped_async #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,11 @@ | |
| * Licensed under the MIT license. | ||
| */ | ||
|
|
||
| use futures_executor::block_on; | ||
| use std::{num::NonZeroUsize, sync::Arc}; | ||
|
|
||
| use diskann::{ | ||
| ANNResult, | ||
| ANNError, ANNResult, | ||
| graph::{ | ||
| self, ConsolidateKind, InplaceDeleteMethod, SearchParams, | ||
| glue::{ | ||
|
|
@@ -20,6 +21,14 @@ use diskann::{ | |
| utils::{ONE, async_tools::VectorIdBoxSlice}, | ||
| }; | ||
|
|
||
| use crate::{ | ||
| model::IndexConfiguration, | ||
| storage::{ | ||
| AsyncIndexMetadata, AsyncQuantLoadContext, LoadWith, StorageReadProvider, | ||
| file_storage_provider::FileStorageProvider, | ||
| }, | ||
| }; | ||
|
|
||
| pub struct DiskANNIndex<DP: DataProvider> { | ||
| /// The underlying async DiskANNIndex. | ||
| pub inner: Arc<graph::DiskANNIndex<DP>>, | ||
|
|
@@ -317,3 +326,50 @@ where | |
| self.handle.block_on(self.inner.get_degree_stats(accessor)) | ||
| } | ||
| } | ||
|
|
||
| pub trait SyncLoadWith<DP>: Sized { | ||
| fn load_with(path: &str, index_config: IndexConfiguration) -> ANNResult<Self>; | ||
| } | ||
|
|
||
| // Load static memory index from pre-built files synchronously | ||
| impl<DP> SyncLoadWith<DP> for DiskANNIndex<DP> | ||
| where | ||
| DP: DataProvider<InternalId = u32> + LoadWith<AsyncQuantLoadContext, Error = ANNError>, | ||
| { | ||
| fn load_with(path: &str, index_config: IndexConfiguration) -> ANNResult<DiskANNIndex<DP>> { | ||
| let storage = FileStorageProvider; | ||
| let data_provider = create_data_provider(&storage, path, &index_config); | ||
|
|
||
| match data_provider { | ||
| Ok(dp) => { | ||
| let index = | ||
| DiskANNIndex::<DP>::new_with_current_thread_runtime(index_config.config, dp); | ||
| Ok(index) | ||
| } | ||
| Err(e) => Err(e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn create_data_provider<'a, P, DP>( | ||
| provider: &P, | ||
| path: &'a str, | ||
| index_config: &'a IndexConfiguration, | ||
| ) -> ANNResult<DP> | ||
| where | ||
| P: StorageReadProvider, | ||
| DP: DataProvider + LoadWith<AsyncQuantLoadContext, Error = ANNError>, | ||
| { | ||
| let pq_context = AsyncQuantLoadContext { | ||
| metadata: AsyncIndexMetadata::new(path), | ||
| num_frozen_points: index_config.num_frozen_pts, | ||
| metric: index_config.dist_metric, | ||
| prefetch_lookahead: index_config.prefetch_lookahead.map(|x| x.get()), | ||
| is_disk_index: false, // only support in-memory index loading here | ||
| prefetch_cache_line_level: index_config.prefetch_cache_line_level, | ||
| }; | ||
|
Comment on lines
+363
to
+370
|
||
|
|
||
| let data_provider = DP::load_with(provider, &pq_context); | ||
|
|
||
| block_on(data_provider) | ||
| } | ||
|
Comment on lines
372
to
375
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New dependency is added with an inline version (
futures-executor = "0.3") instead of using[workspace.dependencies]like most shared third-party crates in this repo. Consider adding it to the workspace dependency list and referencing it viaworkspace = trueto keep versions consistent across crates.