Skip to content

Commit

Permalink
perf: remove the unnecessary selection while searching (#2004)
Browse files Browse the repository at this point in the history
  • Loading branch information
BubbleCal authored Feb 27, 2024
1 parent 6c7e46b commit 934b0fa
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions rust/lance-index/src/vector/hnsw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ pub struct HNSW {
metric_type: MetricType,
/// Entry point of the graph.
entry_point: u32,

#[allow(dead_code)]
/// Whether to use the heuristic to select neighbors (Algorithm 4 or 3 in the paper).
use_select_heuristic: bool,
}
Expand Down Expand Up @@ -292,28 +294,16 @@ impl HNSW {
pub fn search(&self, query: &[f32], k: usize, ef: usize) -> Result<Vec<(u32, f32)>> {
let mut ep = vec![self.entry_point];
let num_layers = self.levels.len();

for level in self.levels.iter().rev().take(num_layers - 1) {
let candidates = beam_search(level, &ep, query, 1)?;
ep = if self.use_select_heuristic {
select_neighbors_heuristic(level, query, &candidates, 1, false)
.map(|(_, id)| id)
.collect()
} else {
select_neighbors(&candidates, 1).map(|(_, id)| id).collect()
};
ep = select_neighbors(&candidates, 1).map(|(_, id)| id).collect();
}

let candidates = beam_search(&self.levels[0], &ep, query, ef)?;
if self.use_select_heuristic {
Ok(
select_neighbors_heuristic(&self.levels[0], query, &candidates, k, false)
.map(|(d, u)| (u, d.into()))
.collect(),
)
} else {
Ok(select_neighbors(&candidates, k)
.map(|(d, u)| (u, d.into()))
.collect())
}
Ok(select_neighbors(&candidates, k)
.map(|(d, u)| (u, d.into()))
.collect())
}

/// Write the HNSW graph to a Lance file.
Expand Down

0 comments on commit 934b0fa

Please sign in to comment.