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

perf: various fixes to improve shuffling performance at high scales #2710

Merged
merged 6 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 6 additions & 4 deletions python/python/benchmarks/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,13 @@ def test_transform_vectors_with_precomputed_parts(

@pytest.mark.benchmark(group="shuffle_vectors")
def test_shuffle_vectors(test_large_dataset, tmpdir, benchmark):
ivf = rand_ivf(test_dataset)
pq = rand_pq(test_dataset, ivf)
builder = IndicesBuilder(test_dataset, "vector")
ivf = rand_ivf(test_large_dataset)
pq = rand_pq(test_large_dataset, ivf)
builder = IndicesBuilder(test_large_dataset, "vector")
transformed_uri = str(tmpdir / "output.lance")
builder.transform_vectors(ivf, pq, transformed_uri)
part_ids_path = str(tmpdir / "part_ids")
gen_rand_part_ids(test_large_dataset, part_ids_path)
builder.transform_vectors(ivf, pq, transformed_uri, None, part_ids_path)
shuffle_out = str(tmpdir)
benchmark.pedantic(
builder.shuffle_transformed_vectors,
Expand Down
4 changes: 2 additions & 2 deletions python/src/indices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use arrow_array::{Array, FixedSizeListArray};
use arrow_data::ArrayData;
use lance::index::vector::ivf::builder::write_vector_storage;
use lance::io::ObjectStore;
use lance_index::vector::ivf::shuffler::{load_partitioned_shuffles, shuffle_vectors};
use lance_index::vector::ivf::shuffler::{shuffle_vectors, IvfShuffler};
use lance_index::vector::{
ivf::{storage::IvfModel, IvfBuildParams},
pq::{PQBuildParams, ProductQuantizer},
Expand Down Expand Up @@ -300,7 +300,7 @@ async fn do_load_shuffled_vectors(
pq_model: ProductQuantizer,
) -> PyResult<()> {
let (_, path) = object_store_from_uri_or_path(dir_path).await?;
let streams = load_partitioned_shuffles(path.clone(), filenames)
let streams = IvfShuffler::load_partitioned_shuffles(&path, filenames)
.await
.infer_error()?;

Expand Down
46 changes: 27 additions & 19 deletions rust/lance-file/src/v2/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use prost::Message;
use prost_types::Any;
use snafu::{location, Location};
use tokio::io::AsyncWriteExt;
use tracing::instrument;

use crate::datatypes::FieldsWithMeta;
use crate::format::pb;
Expand Down Expand Up @@ -175,6 +176,7 @@ impl FileWriter {
Ok(())
}

#[instrument(skip_all, level = "debug")]
async fn write_pages(
&mut self,
mut encoding_tasks: FuturesUnordered<EncodeTask>,
Expand Down Expand Up @@ -263,6 +265,30 @@ impl FileWriter {
Ok(self.schema.as_ref().unwrap())
}

#[instrument(skip_all, level = "debug")]
fn encode_batch(&mut self, batch: &RecordBatch) -> Result<Vec<Vec<EncodeTask>>> {
self.schema
.as_ref()
.unwrap()
.fields
.iter()
.zip(self.column_writers.iter_mut())
.map(|(field, column_writer)| {
let array = batch
.column_by_name(&field.name)
.ok_or(Error::InvalidInput {
source: format!(
"Cannot write batch. The batch was missing the column `{}`",
field.name
)
.into(),
location: location!(),
})?;
column_writer.maybe_encode(array.clone())
})
.collect::<Result<Vec<_>>>()
}

/// Schedule a batch of data to be written to the file
///
/// Note: the future returned by this method may complete before the data has been fully
Expand All @@ -273,7 +299,6 @@ impl FileWriter {
batch.get_array_memory_size()
);
self.ensure_initialized(batch)?;
let schema = self.schema.as_ref().unwrap();
let num_rows = batch.num_rows() as u64;
if num_rows == 0 {
return Ok(());
Expand All @@ -292,24 +317,7 @@ impl FileWriter {
};
// First we push each array into its column writer. This may or may not generate enough
// data to trigger an encoding task. We collect any encoding tasks into a queue.
let encoding_tasks = schema
.fields
.iter()
.zip(self.column_writers.iter_mut())
.map(|(field, column_writer)| {
let array = batch
.column_by_name(&field.name)
.ok_or(Error::InvalidInput {
source: format!(
"Cannot write batch. The batch was missing the column `{}`",
field.name
)
.into(),
location: location!(),
})?;
column_writer.maybe_encode(array.clone())
})
.collect::<Result<Vec<_>>>()?;
let encoding_tasks = self.encode_batch(batch)?;
let encoding_tasks = encoding_tasks
.into_iter()
.flatten()
Expand Down
1 change: 1 addition & 0 deletions rust/lance-index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ criterion.workspace = true
lance-datagen.workspace = true
lance-testing.workspace = true
tempfile.workspace = true
test-log.workspace = true
datafusion-sql.workspace = true
random_word = { version = "0.4.3", features = ["en"] }

Expand Down
Loading
Loading