-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description of changes *Summarize the changes made by this PR.* - Improvements & Bug fixes - Operator for querying nearest heads for a query - New functionality - ... ## Test plan *How are these changes tested?* - [x] Tests pass locally with `pytest` for python, `yarn test` for js, `cargo test` for rust ## Documentation Changes None
- Loading branch information
1 parent
65b5e01
commit c8aa427
Showing
5 changed files
with
186 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
rust/worker/src/execution/operators/spann_centers_search.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use chroma_distance::DistanceFunction; | ||
use chroma_error::{ChromaError, ErrorCodes}; | ||
use chroma_index::spann::utils::rng_query; | ||
use thiserror::Error; | ||
use tonic::async_trait; | ||
|
||
use crate::{ | ||
execution::operator::Operator, | ||
segment::spann_segment::{SpannSegmentReader, SpannSegmentReaderContext}, | ||
}; | ||
|
||
#[derive(Debug)] | ||
pub struct SpannCentersSearchInput { | ||
// TODO(Sanket): Ship the reader instead of constructing here. | ||
reader_context: SpannSegmentReaderContext, | ||
// Assumes that query is already normalized in case of cosine. | ||
query: Vec<f32>, | ||
k: usize, | ||
rng_epsilon: f32, | ||
rng_factor: f32, | ||
distance_function: DistanceFunction, | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug)] | ||
pub struct SpannCentersSearchOutput { | ||
center_ids: Vec<usize>, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum SpannCentersSearchError { | ||
#[error("Error creating spann segment reader")] | ||
SpannSegmentReaderCreationError, | ||
#[error("Error querying RNG")] | ||
RngQueryError, | ||
} | ||
|
||
impl ChromaError for SpannCentersSearchError { | ||
fn code(&self) -> ErrorCodes { | ||
match self { | ||
Self::SpannSegmentReaderCreationError => ErrorCodes::Internal, | ||
Self::RngQueryError => ErrorCodes::Internal, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct SpannCentersSearchOperator {} | ||
|
||
impl SpannCentersSearchOperator { | ||
#[allow(dead_code)] | ||
pub fn new() -> Box<Self> { | ||
Box::new(SpannCentersSearchOperator {}) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Operator<SpannCentersSearchInput, SpannCentersSearchOutput> for SpannCentersSearchOperator { | ||
type Error = SpannCentersSearchError; | ||
|
||
async fn run( | ||
&self, | ||
input: &SpannCentersSearchInput, | ||
) -> Result<SpannCentersSearchOutput, SpannCentersSearchError> { | ||
let spann_reader = SpannSegmentReader::from_segment( | ||
&input.reader_context.segment, | ||
&input.reader_context.blockfile_provider, | ||
&input.reader_context.hnsw_provider, | ||
input.reader_context.dimension, | ||
) | ||
.await | ||
.map_err(|_| SpannCentersSearchError::SpannSegmentReaderCreationError)?; | ||
// RNG Query. | ||
let res = rng_query( | ||
&input.query, | ||
spann_reader.index_reader.hnsw_index.clone(), | ||
input.k, | ||
input.rng_epsilon, | ||
input.rng_factor, | ||
input.distance_function.clone(), | ||
) | ||
.await | ||
.map_err(|_| SpannCentersSearchError::RngQueryError)?; | ||
Ok(SpannCentersSearchOutput { center_ids: res.0 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters