-
Notifications
You must be signed in to change notification settings - Fork 436
Remove k_value from Knn
#1151
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?
Remove k_value from Knn
#1151
Changes from all commits
97b36ef
07b3671
eac3ffb
17780f8
43eb517
1d3a52b
17eac62
0ab4baa
4ddca60
54ee01b
9e1743f
93504e6
b7c27ce
acd1f8c
0202348
a7b696f
0191fee
2f28f50
7ee7938
10940ad
e0ca6f6
d664e44
dc0145e
a5fa47a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,11 @@ | |
|
|
||
| //! A built-in helper for benchmarking K-nearest neighbors. | ||
|
|
||
| use std::sync::Arc; | ||
| use std::{num::NonZeroUsize, sync::Arc}; | ||
| use thiserror::Error; | ||
|
|
||
| use diskann::{ | ||
| ANNResult, | ||
| ANNError, ANNResult, | ||
| graph::{self, glue}, | ||
| provider, | ||
| }; | ||
|
|
@@ -30,7 +31,7 @@ use crate::{ | |
| /// the latter. Result aggregation for [`search::search_all`] is provided | ||
| /// by the [`Aggregator`] type. | ||
| /// | ||
| /// The provided implementation of [`Search`] accepts [`graph::search::Knn`] | ||
| /// The provided implementation of [`Search`] accepts [`KnnParams`] | ||
| /// and returns [`Metrics`] as additional output. | ||
| /// | ||
| /// # Type Parameters | ||
|
|
@@ -194,7 +195,7 @@ where | |
| T: AsyncFriendly + Clone, | ||
| { | ||
| type Id = DP::ExternalId; | ||
| type Parameters = graph::search::Knn; | ||
| type Parameters = KnnParams; | ||
| type Output = Metrics; | ||
|
|
||
| fn num_queries(&self) -> usize { | ||
|
|
@@ -215,7 +216,7 @@ where | |
| O: graph::SearchOutputBuffer<DP::ExternalId> + Send, | ||
| { | ||
| let context = DP::Context::default(); | ||
| let knn_search = *parameters; | ||
| let knn_search = parameters.knn; | ||
| let strategy = self.strategy.get(index)?; | ||
| let processor = self.post_processor.as_post_processor(strategy); | ||
|
|
||
|
|
@@ -238,6 +239,51 @@ where | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum KnnParamsError { | ||
| #[error("k_value cannot be zero")] | ||
| KZero, | ||
| #[error("l_value ({l_value}) must be at least k_value ({k_value})")] | ||
| LLessThanK { l_value: usize, k_value: usize }, | ||
| #[error("invalid KNN parameters")] | ||
| InvalidKnnParameters, | ||
| } | ||
|
|
||
| impl From<KnnParamsError> for ANNError { | ||
| #[track_caller] | ||
| fn from(err: KnnParamsError) -> Self { | ||
| ANNError::opaque(err) | ||
| } | ||
| } | ||
|
|
||
| /// A wrapper for the [`graph::search::Knn`] struct that also includes the `k` value. | ||
| #[derive(Debug, Copy, Clone)] | ||
| pub struct KnnParams { | ||
| k_value: NonZeroUsize, | ||
| pub knn: graph::search::Knn, | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change this to pub struct KnnParams {
num_neighbors: NonZeroUsize,
knn: graph::search::Knn,
}
impl KnnParams {
pub fn new(num_neighbors: NonZeroUsize, knn: graph::search::Knn) -> Result<Self, Error>;
pub fn num_neighbors(&self) -> NonZeroUsize;
pub fn knn(&self) -> &graph::search::Knn; Reasons:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, the name |
||
|
|
||
| impl KnnParams { | ||
| /// Construct a new [`KnnParams`]. | ||
| pub fn new(k_value: usize, l_value: usize) -> Result<Self, KnnParamsError> { | ||
| let k_value = NonZeroUsize::new(k_value).ok_or(KnnParamsError::KZero)?; | ||
| if l_value < k_value.get() { | ||
| return Err(KnnParamsError::LLessThanK { | ||
| l_value, | ||
| k_value: k_value.get(), | ||
| }); | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| let knn = graph::search::Knn::new(l_value, None) | ||
| .map_err(|_| KnnParamsError::InvalidKnnParameters)?; | ||
| Ok(Self { k_value, knn }) | ||
| } | ||
|
|
||
| pub fn k_value(&self) -> NonZeroUsize { | ||
| self.k_value | ||
| } | ||
| } | ||
|
|
||
| /// An [`search::Aggregate`]d summary of multiple [`KNN`] search runs | ||
| /// returned by the provided [`Aggregator`]. | ||
| /// | ||
|
|
@@ -249,7 +295,7 @@ pub struct Summary { | |
| pub setup: search::Setup, | ||
|
|
||
| /// The [`Search::Parameters`] used for the batch of runs. | ||
| pub parameters: graph::search::Knn, | ||
| pub parameters: KnnParams, | ||
|
|
||
| /// The end-to-end latency for each repetition in the batch. | ||
| pub end_to_end_latencies: Vec<MicroSeconds>, | ||
|
|
@@ -317,15 +363,15 @@ impl<'a, I> Aggregator<'a, I> { | |
| } | ||
| } | ||
|
|
||
| impl<I> search::Aggregate<graph::search::Knn, I, Metrics> for Aggregator<'_, I> | ||
| impl<I> search::Aggregate<KnnParams, I, Metrics> for Aggregator<'_, I> | ||
| where | ||
| I: crate::recall::RecallCompatible, | ||
| { | ||
| type Output = Summary; | ||
|
|
||
| fn aggregate( | ||
| &mut self, | ||
| run: search::Run<graph::search::Knn>, | ||
| run: search::Run<KnnParams>, | ||
| mut results: Vec<search::SearchResults<I, Metrics>>, | ||
| ) -> anyhow::Result<Summary> { | ||
| // Compute the recall using just the first result. | ||
|
|
@@ -422,7 +468,7 @@ mod tests { | |
| let rt = crate::tokio::runtime(2).unwrap(); | ||
| let results = search::search( | ||
| knn.clone(), | ||
| graph::search::Knn::new(nearest_neighbors, 10, None).unwrap(), | ||
| KnnParams::new(nearest_neighbors, 10).unwrap(), | ||
| NonZeroUsize::new(2).unwrap(), | ||
| &rt, | ||
| ) | ||
|
|
@@ -446,11 +492,11 @@ mod tests { | |
| // Try the aggregated strategy. | ||
| let parameters = [ | ||
| search::Run::new( | ||
| graph::search::Knn::new(nearest_neighbors, 10, None).unwrap(), | ||
| KnnParams::new(nearest_neighbors, 10).unwrap(), | ||
| setup.clone(), | ||
| ), | ||
| search::Run::new( | ||
| graph::search::Knn::new(nearest_neighbors, 15, None).unwrap(), | ||
| KnnParams::new(nearest_neighbors, 15).unwrap(), | ||
| setup.clone(), | ||
| ), | ||
| ]; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.