Skip to content

Optimize PyArrow block shuffling with dynamic rebatched chunk interleaving - #8407

Merged
lhoestq merged 7 commits into
huggingface:mainfrom
yuxin00j:optimize-pyarrow-block-shuffle-v1
Sep 7, 2026
Merged

lhoestq merged 7 commits into
huggingface:mainfrom
yuxin00j:optimize-pyarrow-block-shuffle-v1

Conversation

@yuxin00j

@yuxin00j yuxin00j commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

When streaming PyArrow datasets with shuffling enabled (buffer_size > 0), the BufferShuffledExamplesIterable._iter_arrow() method historically relied on a Python-level list to maintain the shuffle buffer, replacing elements one-by-one. In a multi-worker DataLoader environment, this per-row generator approach caused severe GIL contention, high generator state-transition overhead, and inefficient memory interleaving.
This PR introduces Amortized Native C++ Block Shuffling for PyArrow streaming. It completely replaces the Python 1-row exchange loop with vectorized block shuffling:

  • Incoming PyArrow tables are accumulated in memory until the combined size reaches 2 * self.buffer_size.
  • The buffered tables are concatenated via pyarrow.concat_tables(tables).
  • The entire block is shuffled in a single vectorized C++ call using pyarrow.compute.take(buffer_table, indices).
  • The first current_len - self.buffer_size rows are yielded immediately, while the remaining self.buffer_size rows are retained as the seed buffer for the next shuffle block.
    By eliminating the 1-row yield loop, we drastically drop CPU overhead and unlock the full IO throughput potential of PyArrow when combined with split_dataset_by_node and multiple dataloader workers.

Impact

We benchmarked streaming shuffled reads over 3 epochs on a 5.24 GB Parquet dataset (1.63M rows across 20 shards, 9 row groups/file) stored in a Google Cloud Storage Rapid bucket (c4-highmem-96, 96 vCPUs):

import datasets
from datasets.distributed import split_dataset_by_node
from torch.utils.data import DataLoader

ds = datasets.load_dataset(
    "parquet",
    data_files="gs://your-bucket/benchmark_data/shard_*.parquet",
    split="train",
    streaming=True,
)
ds = ds.shuffle(seed=0, buffer_size=10000, max_buffer_input_shards=10)
ds = split_dataset_by_node(ds, rank=rank, world_size=8)
ds = ds.with_format("torch")

loader = DataLoader(
    ds,
    batch_size=8,
    num_workers=4,
    prefetch_factor=2,
    persistent_workers=True,
)

for epoch in range(3):
    ds.set_epoch(epoch)
    for batch in loader:
        pass

After #8387 and this PR:

  • Mean epoch duration: Reduced from 268.96 s to 36.08 s (-86.6%).
  • Sample throughput: Improved from 6,054 samples/s to 45,227 samples/s (7.47x).

@Yonghui-Lee

Copy link
Copy Markdown
Contributor

The PR changes the shuffle results for a given seed, seems to cause test_iterable_dataset_shuffle_buffer_uses_multiple_input_shards to fail.

Comment thread src/datasets/iterable_dataset.py Resolved
@yuxin00j

Copy link
Copy Markdown
Contributor Author

The PR changes the shuffle results for a given seed, seems to cause test_iterable_dataset_shuffle_buffer_uses_multiple_input_shards to fail.

Updated the test.

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@lhoestq lhoestq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement ! I think it would make sense to also update the __iter__ method as well so both __iter__ and _iter_arrow return examples in the same order - otherwise it can be confusing for users IMO

… randomization

Update the randomization logic of BufferShuffledExamplesIterable.__iter__ to match the rolling amortized block-shuffling algorithm used in _iter_arrow.
@yuxin00j
yuxin00j force-pushed the optimize-pyarrow-block-shuffle-v1 branch from 5552280 to c9dc3f4 Compare September 7, 2026 02:14
@yuxin00j

yuxin00j commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Nice improvement ! I think it would make sense to also update the __iter__ method as well so both __iter__ and _iter_arrow return examples in the same order - otherwise it can be confusing for users IMO

@lhoestq sure, I have updated the __iter__ method.

@yuxin00j
yuxin00j requested a review from lhoestq September 7, 2026 02:39

@lhoestq lhoestq left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm ! I could even get a x150 speed boost with a local dataset !

@lhoestq
lhoestq merged commit d03ede1 into huggingface:main Sep 7, 2026
12 of 14 checks passed
@lhoestq

lhoestq commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

This is really, really useful btw ! Would it be ok if I share this improvement online and mention you and the GCS team ?

edit: I couldn't wait lol: https://x.com/lhoestq/status/2096966828547277195?s=20

@yuxin00j

yuxin00j commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

This is really, really useful btw ! Would it be ok if I share this improvement online and mention you and the GCS team ?

edit: I couldn't wait lol: https://x.com/lhoestq/status/2096966828547277195?s=20

@lhoestq Yes, thanks for the recognition!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants