Skip to content
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

TraversalContext can be reused for IVF #189

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions rs/index/src/hnsw/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,12 @@ use quantization::typing::VectorOps;
use rand::Rng;
use utils::distance::l2::L2DistanceCalculatorImpl::StreamingSIMD;

use super::utils::{GraphTraversal, TraversalContext};
use super::utils::GraphTraversal;
use crate::hnsw::writer::Header;
use crate::index::Index;
use crate::utils::{IdWithScore, SearchContext};
use crate::utils::{IdWithScore, SearchContext, TraversalContext};
use crate::vector::fixed_file::FixedFileVectorStorage;

impl TraversalContext for SearchContext {
fn visited(&self, i: u32) -> bool {
self.visited.contains(i)
}

fn set_visited(&mut self, i: u32) {
self.visited.insert(i);
}

fn should_record_pages(&self) -> bool {
self.record_pages
}

fn record_pages(&mut self, page_id: String) {
match &mut self.visited_pages {
Some(visited_pages) => {
visited_pages.insert(page_id);
}
None => {}
}
}
}

pub struct Hnsw<Q: Quantizer> {
// Need this for mmap
#[allow(dead_code)]
Expand Down
8 changes: 1 addition & 7 deletions rs/index/src/hnsw/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bit_vec::BitVec;
use ordered_float::NotNan;
use quantization::quantization::Quantizer;

use crate::utils::TraversalContext;
pub struct BuilderContext {
visited: BitVec,
}
Expand Down Expand Up @@ -39,13 +40,6 @@ pub struct PointAndDistance {
pub distance: NotNan<f32>,
}

pub trait TraversalContext {
fn visited(&self, i: u32) -> bool;
fn set_visited(&mut self, i: u32);
fn should_record_pages(&self) -> bool;
fn record_pages(&mut self, page_id: String);
}

/// Move the traversal logic out, since it's used in both indexing and query path
pub trait GraphTraversal<Q: Quantizer> {
type ContextT: TraversalContext;
Expand Down
30 changes: 30 additions & 0 deletions rs/index/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ impl SearchContext {
}
}

pub trait TraversalContext {
fn visited(&self, i: u32) -> bool;
fn set_visited(&mut self, i: u32);
fn should_record_pages(&self) -> bool;
fn record_pages(&mut self, page_id: String);
}

impl TraversalContext for SearchContext {
fn visited(&self, i: u32) -> bool {
self.visited.contains(i)
}

fn set_visited(&mut self, i: u32) {
self.visited.insert(i);
}

fn should_record_pages(&self) -> bool {
self.record_pages
}

fn record_pages(&mut self, page_id: String) {
match &mut self.visited_pages {
Some(visited_pages) => {
visited_pages.insert(page_id);
}
None => {}
}
}
}

#[derive(Debug)]
pub struct IdWithScore {
pub id: u64,
Expand Down
3 changes: 1 addition & 2 deletions rs/index/src/vector/fixed_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use memmap2::Mmap;
use num_traits::ToBytes;
use utils::mem::transmute_u8_to_slice;

use crate::hnsw::utils::TraversalContext;
use crate::utils::SearchContext;
use crate::utils::{SearchContext, TraversalContext};

pub struct FixedFileVectorStorage<T> {
_marker: PhantomData<T>,
Expand Down
Loading