mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 20:44:00 +00:00
feat(python): add two-phase shuffled reads (2PSR) to elastic dataloader (#5066)
Supersedes #4263, which I can no longer push to. This PR continues the same branch, so the review history is there. ## Summary Adds two-phase shuffled reads (2PSR) to `StreamingDataset`, enabled with `block_size`: 1. **Block shuffle:** contiguous blocks of `block_size` live rows are shuffled and assigned to splits. This replaces 1-phase's per-row take with mostly contiguous range scans, which are much cheaper on object storage. 2. **Bounded row shuffle:** each split shuffles rows over a rolling window of `window_blocks` blocks. `max_shuffle_distance` bounds how far a row can move. The feature also supports `filter` (sparse takes of live rows), `reshuffle_blocks_per_epoch`, resume and checkpointing, background block prefetch, and multi-worker DataLoaders. 2PSR stays opt-in: `block_size=None` keeps today's 1-phase behavior. Since the last review round: - **Fixed worker startup with small blocks.** The pickled dataset no longer grows with the number of blocks; the block order is rebuilt in `__setstate__`. Once that pickle passed the 64 KiB pipe buffer, spawned DataLoader workers started one at a time (~280 s with 128 workers at ≤2,048-row blocks). - **Fixed per-call block copies.** Each loaded block is combined into a single chunk. Before, `take` on a multi-chunk block concatenated the whole block on every call. - **Fixed early eviction.** Blocks are evicted only behind the contiguous prefix of rows already served. With `io_queue_depth > 1`, calls can arrive out of order and previously evicted blocks that were still needed, causing 5–40% extra reads. - **New default:** `window_blocks` now defaults to 8, and the docstring recommends `block_size=512` with DataLoader workers. None of these fixes changes which rows are read or their order. A check across 12 config, epoch and seed combinations on 8 ranks gave identical per-rank order before and after. ## Benchmarks Throughput was measured on EC2 i4i.32xlarge in us-west-2, reading a FineWeb-Edu table of 4,096 B rows. Each number is the median of 3 trials, and each trial is a full pass. 2PSR used 512-row blocks, 256 splits, 128 workers and `io_queue_depth=4`. 1-phase used 32 splits, 8 workers and `io_queue_depth=32`. | Storage | Rows | 2PSR | 1-phase | Speedup | |---|---|---|---|---| | S3 | 8.4M (34 GB) | 16.9 s | 230 s | 13.6× | | S3 | 33.6M (137 GB) | 45.6 s | 781 s | 17.1× | | S3 | 134M (550 GB) | 168 s | 3,098 s | 18.4× | | Local NVMe | 8.4M | 16.4 s | 116 s | 7.1× | On the 8.4M-row S3 table, 2PSR reads 1.5–2.0 GB/s at every block size from 128 to 65,536 rows. A fit across the three S3 sizes gives 2PSR about 7 s fixed cost plus 3.4 GB/s, and 1-phase about 39 s plus 0.18 GB/s. That puts the large-table limit near 19×. Quality was measured by pretraining GPT-2 124M on 2^21 FineWeb-Edu docs for 16 epochs on 8×H100. The table shows final validation loss, mean ± SD over 3 seeds: | Reader | Final val loss | |---|---| | 1-phase | 2.8464 ± 0.0006 | | 2PSR, `block_size=128`, msd 4,096 | 2.8457 ± 0.0011 | | 2PSR, `block_size=512`, msd 4,096 | 2.8457 ± 0.0007 | | 2PSR, `block_size=2048`, msd 32,768 | 2.8466 ± 0.0009 | | No shuffle (1 seed) | 2.8606 | Every 2PSR config is within seed noise of 1-phase. The 95% confidence intervals of the difference all include zero and are at most 13% of the no-shuffle penalty. Training throughput was the same for every reader (3.15–3.18M tok/s, GPU-bound). ## Testing - `test_elastic_dataloader.py` and `test_permutation.py`: all 324 tests pass after merging `main`. The new tests cover: - pickle size independent of block count; - single-chunk blocks; - no re-reads under out-of-order calls; - window defaults; - resume, filter, worker and checkpoint behavior. - `ruff check` and `ruff format --check` are clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
co-authored by
Claude Opus 5
Xuanwo
parent
4d9fd358f8
commit
147fdb25a0
@@ -54,7 +54,7 @@ ds = StreamingDataset(
|
||||
|
||||
## Shuffling Rows {#shuffling-rows}
|
||||
|
||||
In model training, model quality can be hurt by unintentionally learning patterns in the ordering of the source data. Shuffling the dataset before training alleviates this problem. However, fetching rows with scattered sparse reads across object storage can be dramatically slower than fetching contiguous batches. `StreamingDataset` shuffles at one of two granularities, selected by the `shuffle_clump_size` parameter.
|
||||
In model training, model quality can be hurt by unintentionally learning patterns in the ordering of the source data. Shuffling the dataset before training alleviates this problem. However, fetching rows with scattered sparse reads across object storage can be dramatically slower than fetching contiguous batches. `StreamingDataset` shuffles rows individually by default; `shuffle_clump_size` and `block_size` each trade some randomness for more contiguous reads.
|
||||
|
||||
<Note>
|
||||
`StreamingDataset` shuffle seeds combine `shuffle_seed` with `epoch`; each epoch produces a different permutation while runs
|
||||
@@ -109,6 +109,34 @@ for sample in eval_ds:
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
### Block Shuffle {#block-shuffle}
|
||||
|
||||
Setting `block_size` enables a two-phase shuffled read. First, contiguous blocks of `block_size` rows are shuffled and read with range scans. Then each split shuffles rows across a rolling window of `window_blocks` blocks (default `8`). Unlike a clump, a block does not stay together: a row can move up to `max_shuffle_distance` positions (default `window_blocks * block_size`) from its place in the block order. Every split serves the same number of rows, so a split can start or end partway through a block, and fewer than `num_splits` rows are left out per epoch.
|
||||
|
||||
`block_size=512` is a good starting point. Use [DataLoader workers](#dataloader-workers), because throughput comes from reading many splits in parallel; `num_splits` must be divisible by `world_size * num_workers`. Keep in mind:
|
||||
|
||||
- The table needs at least `num_splits * block_size` live rows (after `filter`), so that each split gets at least one full block. The example below needs at least 32,768.
|
||||
- `block_size` cannot be combined with `shuffle_clump_size` or `split_strategy`; passing both raises `ValueError`.
|
||||
- On LanceDB Embedded, reading on-disk tables requires the optional `pylance` package (`pip install "lancedb[pylance]"`).
|
||||
|
||||
<CodeGroup>
|
||||
```py Python icon=Python
|
||||
# Block shuffle: shuffle contiguous blocks, then rows within a bounded window.
|
||||
for epoch in range(num_epochs):
|
||||
ds = StreamingDataset(
|
||||
table,
|
||||
shuffle_seed=42,
|
||||
epoch=epoch,
|
||||
num_splits=64,
|
||||
block_size=512, # rows per contiguous block read
|
||||
window_blocks=8, # blocks per split whose rows are mixed together
|
||||
)
|
||||
loader = StreamingDataLoader(ds, batch_size=32, num_workers=16)
|
||||
for batch in loader:
|
||||
train_step(batch)
|
||||
```
|
||||
</CodeGroup>
|
||||
|
||||
## Fetching {#fetching}
|
||||
|
||||
Traditional datasets have often been designed to yield a single sample at a time upon iteration. However, in modern use cases, accessing data on object storage one row at a time introduces too much per-call overhead. Thus, `StreamingDataset` fetches data in read batches, whose size is controlled by the `read_batch_size` parameter (defaults = `64`).
|
||||
|
||||
Reference in New Issue
Block a user