Elffuss BraidHub

kikocis/lancedb public

fix: hnsw default partitions (#1667)

PR fixes #1662

---------

Co-authored-by: Will Jones <willjones127@gmail.com>
Changesete822b4e34fb724b18fb5fd77f67e5d31b3c72576a277ef74b28c1a00a85f3360
AuthorGagan Bhullar <g.deepsingh1@gmail.com> · 2024-09-25 10:16 -06:00
CommitterGitHub <noreply@github.com> · 2024-09-25 09:16 -07:00
Parentsffc27a8ff454

Browse the files at this changeset

2 files changed

modified rust/lancedb/src/index/vector.rs +5 −0
@@ -214,6 +214,11 @@
214214 max(1, num_partitions)
215215 }
216216
217+pub(crate) fn suggested_num_partitions_for_hnsw(rows: usize, dim: u32) -> u32 {
218+ let num_partitions = (((rows as u64) * (dim as u64)) / (256 * 5_000_000)) as u32;
219+ max(1, num_partitions)
220+}
221+
217222 pub(crate) fn suggested_num_sub_vectors(dim: u32) -> u32 {
218223 if dim % 16 == 0 {
219224 // Should be more aggressive than this default.
modified rust/lancedb/src/table.rs +21 −5
@@ -54,7 +54,8 @@
5454 use crate::error::{Error, Result};
5555 use crate::index::scalar::FtsIndexBuilder;
5656 use crate::index::vector::{
57- IvfHnswPqIndexBuilder, IvfHnswSqIndexBuilder, IvfPqIndexBuilder, VectorIndex,
57+ suggested_num_partitions_for_hnsw, IvfHnswPqIndexBuilder, IvfHnswSqIndexBuilder,
58+ IvfPqIndexBuilder, VectorIndex,
5859 };
5960 use crate::index::IndexConfig;
6061 use crate::index::IndexStatistics;
@@ -1440,11 +1441,19 @@
14401441 });
14411442 }
14421443
1443- let num_partitions = if let Some(n) = index.num_partitions {
1444+ let num_partitions: u32 = if let Some(n) = index.num_partitions {
14441445 n
14451446 } else {
1446- suggested_num_partitions(self.count_rows(None).await?)
1447+ match field.data_type() {
1448+ arrow_schema::DataType::FixedSizeList(_, n) => Ok::<u32, Error>(
1449+ suggested_num_partitions_for_hnsw(self.count_rows(None).await?, *n as u32),
1450+ ),
1451+ _ => Err(Error::Schema {
1452+ message: format!("Column '{}' is not a FixedSizeList", field.name()),
1453+ }),
1454+ }?
14471455 };
1456+
14481457 let num_sub_vectors: u32 = if let Some(n) = index.num_sub_vectors {
14491458 n
14501459 } else {
@@ -1503,10 +1512,17 @@
15031512 });
15041513 }
15051514
1506- let num_partitions = if let Some(n) = index.num_partitions {
1515+ let num_partitions: u32 = if let Some(n) = index.num_partitions {
15071516 n
15081517 } else {
1509- suggested_num_partitions(self.count_rows(None).await?)
1518+ match field.data_type() {
1519+ arrow_schema::DataType::FixedSizeList(_, n) => Ok::<u32, Error>(
1520+ suggested_num_partitions_for_hnsw(self.count_rows(None).await?, *n as u32),
1521+ ),
1522+ _ => Err(Error::Schema {
1523+ message: format!("Column '{}' is not a FixedSizeList", field.name()),
1524+ }),
1525+ }?
15101526 };
15111527
15121528 let mut dataset = self.dataset.get_mut().await?;