From 147fdb25a0e449ce278e7ecff7da24c16b80215d Mon Sep 17 00:00:00 2001 From: Jason Zheng Date: Wed, 25 Nov 2026 09:08:39 -0800 Subject: [PATCH] feat(python): add two-phase shuffled reads (2PSR) to elastic dataloader (#5066) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Xuanwo --- docs/web/training/data-loading.mdx | 30 +- python/python/lancedb/streaming.py | 1115 +++++++++++++++-- python/python/lancedb/table.py | 44 +- .../python/tests/test_elastic_dataloader.py | 827 ++++++++++++ python/python/tests/utils.py | 40 + 5 files changed, 1922 insertions(+), 134 deletions(-) diff --git a/docs/web/training/data-loading.mdx b/docs/web/training/data-loading.mdx index 2a3250105..1db37189c 100644 --- a/docs/web/training/data-loading.mdx +++ b/docs/web/training/data-loading.mdx @@ -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. `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: ``` +### 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]"`). + + +```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) +``` + + ## 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`). diff --git a/python/python/lancedb/streaming.py b/python/python/lancedb/streaming.py index 23a81c135..f568b7c7c 100644 --- a/python/python/lancedb/streaming.py +++ b/python/python/lancedb/streaming.py @@ -28,11 +28,12 @@ import threading import time import warnings from collections import deque -from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import Future, ThreadPoolExecutor from copy import deepcopy from multiprocessing import RawArray, get_all_start_methods, get_context from typing import Any, Callable, cast, Iterator, Literal, NamedTuple, Optional, Union +import numpy as np import pyarrow as pa import pyarrow.compute as pc import torch @@ -47,6 +48,7 @@ from .permutation import ( _table_from_pickle_state, _table_to_pickle_state, ) +from .table import LanceTable logger = logging.getLogger(__name__) @@ -55,9 +57,303 @@ logger = logging.getLogger(__name__) # distinct seeds for any practically encountered epoch count. _EPOCH_PRIME = 100003 +# Offsets added when seeding each split's 2-phase bounded shuffle +# permutation (distinct from _EPOCH_PRIME and from each other so they +# can't collide). +_SIGMA_PRIME = 1000003 +_SIGMA_SPLIT_PRIME = 3000017 + DEFAULT_READ_BATCH_SIZE = 64 DEFAULT_PREFETCH_BATCHES = 4 +# 8 blocks of the recommended block_size=512 give max_shuffle_distance 4,096, +# which matched 1-phase validation loss in GPT-2 pretraining; S3 throughput +# was flat across window sizes. +DEFAULT_WINDOW_BLOCKS = 8 _UNSET = object() +# Checkpoint keys pinning the 2-phase read order; absent in 1-phase checkpoints. +# block_size comes first so a 1-phase/2-phase mix reports the mode setting. +_TWO_PHASE_CHECKPOINT_KEYS = ( + "block_size", + "shuffle", + "max_shuffle_distance", + "reshuffle_blocks_per_epoch", +) + + +def _build_bounded_permutation(n: int, max_distance: int, seed: int) -> np.ndarray: + """Build a permutation of ``[0, n)`` with ``|sigma(k) - k| < max_distance``. + + Partitions ``[0, n)`` into consecutive chunks of ``max_distance`` entries + (the last chunk may be shorter) and fully shuffles each chunk + independently. Since ``k`` and ``sigma(k)`` always fall in the same + chunk, this trivially satisfies the displacement bound. Vectorized + (one random key matrix, sorted per row) so it stays cheap even sized to + an entire split's live-row count. + """ + if n == 0: + return np.array([], dtype=np.int64) + rng = np.random.default_rng(seed) + sigma = np.arange(n, dtype=np.int64) + num_full = n // max_distance + full_len = num_full * max_distance + if num_full: + # Sorting i.i.d. random keys within each row of a + # (num_full, max_distance) matrix gives, per row, a uniformly + # random permutation of that row's column indices -- i.e. one + # independent shuffled chunk per row, all at once. + keys = rng.random((num_full, max_distance)) + chunk_perms = np.argsort(keys, axis=1) + bases = (np.arange(num_full, dtype=np.int64) * max_distance)[:, None] + sigma[:full_len] = (chunk_perms + bases).reshape(-1) + if full_len < n: + tail = np.arange(full_len, n, dtype=np.int64) + tail_perm = np.argsort(rng.random(n - full_len)) + sigma[full_len:] = tail[tail_perm] + return sigma + + +class _TwoPhaseSplitReader: + """Sequential-only stand-in for ``Permutation`` driving 2-phase reads. + + Duck-types ``Permutation.__getitems__`` so 2-phase's shuffled reads flow + through the same stage-1 I/O machinery (``io_queue_depth`` prefetch, + ``Future``-based fetches) used by 1-phase, instead of a separate ad hoc + path. + + This split's assigned blocks, concatenated in order and restricted to + live rows, give every row a dense natural rank in ``[0, num_rows)``. A + single permutation ``sigma`` of that rank space -- built once, with + ``|sigma(rank) - rank| < max_shuffle_distance`` for every rank -- decides + each row's output position. A row may move anywhere within that bound, + including into a different block's range entirely; the only restriction + is the distance from its own natural rank, not which block it started + in. A block is evicted, and the next block assigned to this split read + in to replace it, as soon as every one of its rows has been read past + in the output order -- which happens naturally once ``sigma`` has been + applied, without any extra bookkeeping about "generations" or "slots". + + Only one ``__getitems__`` call executes its own gather/evict logic at a + time (serialized by ``_lock``): the block cache and read plan are + shared, mutable state, and letting ``io_queue_depth`` submit multiple + concurrent calls for the same split would race on them. The actual + block *fetches* do run concurrently with each other and with whichever + call holds ``_lock``, though: each call, after satisfying its own rows, + schedules background prefetches (on a small private executor, sized by + ``prefetch_depth``) for the next few not-yet-loaded blocks the read + plan is about to need, up to ``prefetch_depth`` blocks in flight or + already fetched but not yet consumed at once. By the time a later call + actually needs one of those blocks, its fetch has often already + completed, so the call just picks up the finished result instead of + blocking on storage -- overlapping this split's own I/O with whatever + is consuming its previous blocks, the same way ``io_queue_depth`` + overlaps I/O for 1-phase. + """ + + def __init__( + self, + block_ids: list[Any], + block_live_counts: list[int], + shuffle: bool, + max_shuffle_distance: int, + seed: int, + read_block_fn: Callable[[Any], pa.Table], + columns: Optional[list[str]], + skip: int = 0, + prefetch_depth: int = 1, + ): + self._block_ids = block_ids + self._read_block_fn = read_block_fn + self._columns = columns + self._lock = threading.Lock() + self._loaded: dict[int, pa.Table] = {} + self._pending: dict[int, Future] = {} + self._prefetch_depth = max(1, prefetch_depth) + + counts = np.asarray(block_live_counts, dtype=np.int64) + total_rows = int(counts.sum()) + num_blocks = len(block_ids) + if not 0 <= skip <= total_rows: + raise ValueError(f"skip ({skip}) out of range for {total_rows} row(s)") + + # Natural (block, then row) order: block_pos[k]/local_offset[k] + # describe the row whose dense natural rank is exactly k. These two + # arrays are retained for the reader's whole lifetime, one entry per + # row in the *entire* split (not just the resident window -- see the + # whole-split-memory review thread, still open) -- so their dtype is + # the full per-row memory cost, not a rounding choice. block_pos + # values are block indices (bounded by num_blocks) and local_offset + # values are in-block row offsets (bounded by this split's largest + # block), both far smaller than the row-rank range sigma/order below + # need int64 for, so int32 halves that cost in the realistic case; + # falls back to int64 only if a single split's block count or block + # size itself exceeds what int32 can represent. + max_count = int(counts.max()) if num_blocks else 0 + pos_dtype = np.int32 if num_blocks < 2**31 else np.int64 + local_dtype = np.int32 if max_count < 2**31 else np.int64 + block_pos = np.repeat(np.arange(num_blocks, dtype=pos_dtype), counts) + local_offset = ( + np.concatenate([np.arange(c, dtype=local_dtype) for c in counts]) + if num_blocks + else np.array([], dtype=local_dtype) + ) + + if shuffle and total_rows: + sigma = _build_bounded_permutation(total_rows, max_shuffle_distance, seed) + else: + sigma = np.arange(total_rows, dtype=np.int64) + + # sigma is itself a permutation of dense ranks, so inverting it + # (argsort) directly gives, for each output position, which + # natural rank -- and thus which (block, local offset) -- to draw. + # Slicing off the first `skip` entries resumes at a saved absolute + # position: those rows were already consumed in an earlier run, so + # blocks that only appear before `skip` are simply never + # referenced and never loaded. + order = np.argsort(sigma, kind="stable")[skip:] + self._plan_block_pos = block_pos[order] + self._plan_local_offset = local_offset[order] + self.num_rows = total_rows - skip + + # Last plan position (inclusive) at which each block is still + # needed, so it can be evicted as soon as we've read past it. + # Computed with numpy (first occurrence in the reversed plan) rather + # than a per-row Python loop: materializing the whole split's plan + # as a Python list briefly costs about twice the plan arrays' memory. + plan_len = len(self._plan_block_pos) + if plan_len: + visited, rev_first = np.unique( + self._plan_block_pos[::-1], return_index=True + ) + last_pos = plan_len - 1 - rev_first + self._block_last_pos: dict[int, int] = dict( + zip(visited.tolist(), last_pos.tolist()) + ) + else: + self._block_last_pos = {} + + # Distinct blocks in the order the plan first needs them. Prefetch + # walks this (one entry per block) instead of rescanning raw plan + # positions, which costs one Python step per *row* scanned and + # dominates read time once max_shuffle_distance is large. + if plan_len: + _, first_pos = np.unique(self._plan_block_pos, return_index=True) + self._block_visit_order = self._plan_block_pos[np.sort(first_pos)].tolist() + else: + self._block_visit_order = [] + # Prefetch's starting point in _block_visit_order. Every entry + # before it is a block whose last use is already behind the read + # watermark, so rescanning that ever-growing prefix on each top-up + # would make prefetch cost quadratic in the split's block count. + self._visit_cursor = 0 + # io_queue_depth submits several calls at once and they may take + # _lock out of order, so a later call's max index is not a safe + # eviction point: an earlier, still-waiting call can need a block + # whose last use precedes it, and evicting it forces a re-read. + # Evict only behind _served_prefix, the first plan position not yet + # returned by any call. + self._served = np.zeros(self.num_rows, dtype=bool) + self._served_prefix = 0 + # Created last so a failed construction leaves no threads behind. + self._executor = ThreadPoolExecutor(max_workers=self._prefetch_depth) + + def _load_block(self, block_id: Any) -> pa.Table: + # A block read can arrive as several record batches. Table.take on a + # multi-chunk column concatenates the whole column first, so every + # later gather from this block would copy all of it; combining once + # here pays that copy a single time per block instead of per call. + return self._read_block_fn(block_id).combine_chunks() + + def _advance_served_prefix(self, idx_arr: np.ndarray) -> int: + """Mark ``idx_arr`` served; return the last position of the served prefix.""" + self._served[idx_arr] = True + n = self.num_rows + while self._served_prefix < n: + window = self._served[self._served_prefix : self._served_prefix + 4096] + if window.all(): + self._served_prefix += len(window) + else: + self._served_prefix += int(window.argmin()) + break + return self._served_prefix - 1 + + def __getitems__(self, indices) -> pa.RecordBatch: + # Fancy-indexed (not sliced) so this also supports non-contiguous + # index lists, not just the contiguous range(start, start + fetch) + # that the main I/O pipeline requests. + idx_arr = np.asarray(indices, dtype=np.int64) + with self._lock: + block_pos_slice = self._plan_block_pos[idx_arr] + local_off_slice = self._plan_local_offset[idx_arr] + for bp in np.unique(block_pos_slice).tolist(): + if bp not in self._loaded: + if bp in self._pending: + # Prefetched by an earlier call: block on it only if + # it hasn't finished yet -- often it already has. + self._loaded[bp] = self._pending.pop(bp).result() + else: + # read_block_fn already returns only this block's + # live rows, in natural (ascending) order -- no + # further filtering or reordering needed here. + self._loaded[bp] = self._load_block(self._block_ids[bp]) + + # Group by source block (stable, so each block's rows keep + # their relative order), gather from each, then invert the + # grouping to restore this chunk's actual priority order. + group_order = np.argsort(block_pos_slice, kind="stable") + grouped_block_pos = block_pos_slice[group_order] + grouped_local_off = local_off_slice[group_order] + run_starts = np.flatnonzero(np.diff(grouped_block_pos)) + 1 + bounds = [0, *run_starts.tolist(), len(grouped_block_pos)] + tables = [ + self._loaded[int(grouped_block_pos[start])].take( + grouped_local_off[start:end] + ) + for start, end in zip(bounds[:-1], bounds[1:]) + ] + combined = pa.concat_tables(tables) + inverse = np.argsort(group_order) + result = combined.take(inverse) + + watermark = self._advance_served_prefix(idx_arr) + for bp in list(self._loaded): + if self._block_last_pos.get(bp, -1) <= watermark: + del self._loaded[bp] + + # Top up the prefetch queue: scan forward from this call's end + # for blocks the plan is about to need that aren't already + # loaded or already being fetched, and hand them to the + # background executor so a later call can often just pick up + # the (by then finished) result above instead of blocking. + room = self._prefetch_depth - len(self._pending) + if room > 0: + visit_order = self._block_visit_order + while ( + self._visit_cursor < len(visit_order) + and self._block_last_pos[visit_order[self._visit_cursor]] + <= watermark + ): + self._visit_cursor += 1 + for i in range(self._visit_cursor, len(visit_order)): + if room == 0: + break + bp = visit_order[i] + if bp in self._loaded or bp in self._pending: + continue + if self._block_last_pos.get(bp, -1) <= watermark: + # Already read past: never needed again. + continue + self._pending[bp] = self._executor.submit( + self._load_block, self._block_ids[bp] + ) + room -= 1 + + if self._columns is not None: + result = result.select(self._columns) + return result.combine_chunks().to_batches()[0] + + def close(self) -> None: + """Release this reader's background prefetch executor.""" + self._executor.shutdown(wait=False, cancel_futures=True) def _default_worker_start_method() -> str: @@ -67,6 +363,14 @@ def _default_worker_start_method() -> str: return "spawn" +class _BlockSlice(NamedTuple): + """Rows ``[start, stop)`` of one 2-phase block, by position within the block.""" + + block_id: int + start: int + stop: int + + class _WorkerSample(NamedTuple): data: Any dataset: "StreamingDataset" @@ -268,6 +572,7 @@ class StreamingDataset(IterableDataset): ``split_strategy=lambda b: b.bootstrap_sample(10_000, fixed=num_splits, seed=42)`` draws 10,000 samples with replacement, divided evenly across ``num_splits`` splits. + Cannot be combined with ``block_size``. shuffle_seed: Base seed for the random permutation. Combined with ``epoch`` so each epoch produces a different ordering. Pass ``None`` to generate @@ -293,7 +598,8 @@ class StreamingDataset(IterableDataset): once this many batches are buffered, applying backpressure when the transform stage (or ``transform_queue_depth``) is the bottleneck. Must be greater than zero. Defaults to ``DEFAULT_PREFETCH_BATCHES`` - (4). + (4). In 2-phase mode (``block_size`` set) this also sizes each split's + background block-prefetch depth. columns: Optional list of column names to read. When set, only those columns are fetched from storage; all others are omitted. ``None`` (the @@ -302,7 +608,50 @@ class StreamingDataset(IterableDataset): When set, rows are shuffled in contiguous groups of this size rather than individually. Larger clumps improve I/O locality (important on object storage) at the cost of reduced randomness. ``None`` (the - default) shuffles rows individually. + default) shuffles rows individually. Cannot be combined with + ``block_size`` (2-phase mode has its own locality/randomness knobs, + below). + block_size: + Enables 2-phase shuffled reads: rows are shuffled at the granularity + of contiguous blocks of exactly this many live rows, and each split + reads a rolling window of ``window_blocks`` blocks at a time instead + of taking individual rows by row-id. This trades some randomness + for I/O that is mostly contiguous range scans (or, when ``filter`` + is set, sparse takes of just the live rows) rather than one row-id + take per row, which is far cheaper on object storage. ``None`` (the + default) uses 1-phase shuffling, which permutes every row + individually up front. 512 is a good starting point: 128-2,048 + matched 1-phase validation loss in GPT-2 pretraining, and S3 + throughput was flat from 128 to 65,536. Throughput comes from + reading many splits in parallel, so use DataLoader workers (and + ``num_splits`` of at least the worker count); with no workers, + 2-phase can be slower than 1-phase. Each split serves the same + number of (live) rows, so a split may start or end partway through + a block; as in 1-phase, fewer than ``num_splits`` rows are dropped + per epoch. + window_blocks: + Number of blocks kept in RAM per split at once in 2-phase mode. + Larger windows shuffle across more rows at the cost of more memory; + must be at least 1. Requires ``block_size``. Defaults to + ``DEFAULT_WINDOW_BLOCKS`` (8). + max_shuffle_distance: + Bounds how far, in rows, 2-phase shuffling may displace a row from + its position in unshuffled (block, then row) order. Requires + ``block_size``. Smaller values shuffle less but let a block's RAM + be reclaimed and refilled with the next block sooner; larger values + shuffle more but hold blocks in RAM longer. Defaults to + ``window_blocks * block_size``, so the default window actually + holds ``window_blocks`` blocks in RAM at once; pass this explicitly + to decouple the shuffle bound from ``window_blocks``. + reshuffle_blocks_per_epoch: + Whether ``epoch`` feeds into which blocks are assigned to which + split, in addition to the row-level shuffle within each split's + blocks (which always varies by epoch). Requires ``block_size``. + ``True`` (the default) reassigns blocks every epoch, matching + 1-phase's per-epoch row reassignment. ``False`` keeps each + split's blocks fixed across epochs -- only the row order within + them changes -- which keeps a block's on-disk location, and thus + its read locality, stable across an entire training run. filter: Optional SQL filter expression (e.g. ``"label = 'dog'"``). Only rows that satisfy the predicate are included in the permutation. The filter @@ -455,6 +804,10 @@ class StreamingDataset(IterableDataset): io_queue_depth: int = DEFAULT_PREFETCH_BATCHES, columns: Optional[list[str]] = None, shuffle_clump_size: Optional[int] = None, + block_size: Optional[int] = None, + window_blocks: Optional[int] = None, + max_shuffle_distance: Optional[int] = None, + reshuffle_blocks_per_epoch: Optional[bool] = None, filter: Optional[str] = None, transform: Optional[Callable] = None, transform_parallelism: Optional[int] = None, @@ -489,6 +842,30 @@ class StreamingDataset(IterableDataset): ) if io_queue_depth <= 0: raise ValueError("io_queue_depth must be greater than 0") + if block_size is not None and block_size <= 1: + raise ValueError("block_size must be greater than 1") + if block_size is None: + if window_blocks is not None: + raise ValueError("window_blocks requires block_size to be set") + if max_shuffle_distance is not None: + raise ValueError("max_shuffle_distance requires block_size to be set") + if reshuffle_blocks_per_epoch is not None: + raise ValueError( + "reshuffle_blocks_per_epoch requires block_size to be set" + ) + else: + if window_blocks is not None and window_blocks < 1: + raise ValueError("window_blocks must be at least 1") + if max_shuffle_distance is not None and max_shuffle_distance <= 0: + raise ValueError("max_shuffle_distance must be greater than 0") + if split_strategy is not None: + raise ValueError("split_strategy cannot be combined with block_size") + if shuffle_clump_size is not None: + raise ValueError( + "shuffle_clump_size cannot be combined with block_size " + "(2-phase mode uses window_blocks/max_shuffle_distance " + "instead); remove one of them" + ) if transform_parallelism is not None and transform_parallelism <= 0: raise ValueError("transform_parallelism must be greater than 0") if pack_sequences is not None: @@ -568,6 +945,7 @@ class StreamingDataset(IterableDataset): self._io_queue_depth = io_queue_depth self._columns = columns self._shuffle_clump_size = shuffle_clump_size + self._block_size = block_size self._filter = filter self._transform = transform self._transform_parallelism = transform_parallelism @@ -647,74 +1025,185 @@ class StreamingDataset(IterableDataset): # this instance has never iterated have no entry. self._resume_positions: dict[int, int] = {} - # Build the permutation table once, deterministically. - builder = permutation_builder(table) - if filter is not None: - builder = builder.filter(filter) - if split_strategy is not None: - # Escape hatch: lets any PermutationBuilder split strategy drive this - # dataset (split_hash, split_calculated, bootstrap_sample, or any future - # strategy) without StreamingDataset needing a bespoke parameter for each - # one. shuffle/shuffle_seed/epoch/shuffle_clump_size are ignored in this - # path -- the caller's split_strategy is solely responsible for ordering. - self._perm_table = split_strategy(builder).execute() - # _rank_splits below assumes split ids are exactly 0..num_splits-1 with no - # gaps (every other path guarantees this by always building with - # fixed=num_splits) -- a split_strategy callable has no such guarantee, and - # a mismatch would otherwise silently drop rows (ids >= num_splits) or leave - # some ranks reading an empty split (missing ids < num_splits). - split_value_counts = ( - self._perm_table.to_arrow().column("split_id").value_counts() - ) - counts_by_split = dict( - zip( - split_value_counts.field("values").to_pylist(), - split_value_counts.field("counts").to_pylist(), - ) - ) - expected_splits = set(range(num_splits)) - if set(counts_by_split) != expected_splits: + if block_size is not None: + # 2-phase shuffled read: the source dataset is never given a + # row-level permutation. Instead we only shuffle at the + # granularity of contiguous row blocks; _perm_table (and its + # row-by-row index mapping) is not built at all in this mode. + # + # block_size counts LIVE rows (rows passing `filter`), not raw + # rows. + # + # Pin the exact version/branch the block plan below is computed + # against, so a write to the table between construction and a + # later iteration (or between epochs) cannot shift which rows a + # block refers to. Read paths in _read_block always reopen a + # private handle checked out to this pin (see + # _open_pinned_table) rather than reading the table's live, + # possibly-since-mutated state. Branch pinning isn't + # implemented yet -- the reopened handle always lands on the + # default branch (see _table_from_pickle_state) -- so a table + # checked out to a non-default branch is rejected below rather + # than silently pinning the wrong branch. + self._pinned_branch = table.current_branch() + if self._pinned_branch is not None: raise ValueError( - f"split_strategy produced split ids {sorted(counts_by_split)}, " - f"but num_splits={num_splits} requires exactly " - f"{{0, ..., {num_splits - 1}}}. Pass num_splits matching the " - "strategy's own split count (e.g. `fixed=num_splits` on " - "bootstrap_sample/split_random/etc.)." + "block_size does not yet support a table checked out to " + f"a non-default branch ({self._pinned_branch!r}); check " + "out the default branch before constructing StreamingDataset" ) - # The round-robin iteration below (and in _iter_owned) assumes every - # split has the same row count -- it stops as soon as any one owned - # split is exhausted (see its own comment: "all splits exhaust - # simultaneously"). Every other path guarantees this by always building - # with fixed=num_splits; a split_strategy using ratios/counts/split_hash - # weights has no such guarantee, and unequal sizes would otherwise - # silently drop rows from the larger splits -- or, with world_size > 1 - # where each rank owns different splits, end ranks after different row - # counts and desync or hang a distributed run's end-of-epoch collectives. - if len(set(counts_by_split.values())) > 1: + # An RTE write spec means some acknowledged writes only exist in + # the MemWAL, not yet flushed into the base table that the native + # scanner/take paths below read; a version pin can't see them, so + # reads here would silently omit them with no error. 1-phase + # already refuses this for the same reason (unflushed rows have + # no row id -- see PermutationBuilder); block_size mode has no + # row ids in its read path either, but reads positionally, so it + # fails differently (stale values, not a missing-row-id error) + # and needs its own explicit check. + if table.get_rte_spec() is not None: raise ValueError( - f"split_strategy produced unequal split sizes " - f"{counts_by_split} (split id -> row count). Every split must " - "have the same row count. Use `fixed=num_splits` rather than " - "`ratios`/`counts`/weighted `split_hash`, which can produce " - "unequal sizes, or split unevenly-sized groups into separate " - "StreamingDatasets instead." + "block_size does not yet support a table with an RTE " + "write spec set; unflushed MemWAL rows are not visible " + "to a version-pinned read, so block reads could " + "silently miss acknowledged writes. Call " + "table.checkpoint_rte() and table.disable_rte() " + "first, or construct StreamingDataset without block_size" ) - elif shuffle: - perm_seed = shuffle_seed + epoch * _EPOCH_PRIME - self._perm_table = builder.split_random( - fixed=num_splits, seed=perm_seed, clump_size=shuffle_clump_size - ).execute() - else: - self._perm_table = builder.split_sequential(fixed=num_splits).execute() - # Only the split_strategy path needs a permutation fingerprint in the - # checkpoint (see `_checkpoint_snapshot`): split_random/split_sequential are - # already fully pinned by shuffle_seed/num_splits, and a fingerprint over raw - # row ids would spuriously reject an otherwise-valid resume after those row - # ids get renumbered by compaction (row ids are not stable by default). - self._uses_split_strategy = split_strategy is not None - # Lazily computed and cached by `_perm_fingerprint` -- `_perm_table` never - # changes after construction, so this is correct to compute once. - self._perm_fingerprint_cache: Optional[str] = None + self._pinned_version = table.version + # Captured once, now, rather than recomputed on every + # _open_pinned_table() call: for a memory table, the "pickle + # state" below IS the row data (to_arrow()), not just reopen + # coordinates, so recomputing it later would silently capture + # whatever the live table looks like *then* instead of the + # snapshot this pin is supposed to mean. Not used for a + # RemoteTable -- see _open_pinned_table, which deep-copies it + # directly instead. + from .remote.table import RemoteTable as _RemoteTable + + self._pinned_table_state: Optional[dict] = ( + _table_to_pickle_state(table) + if self._connection_factory is None + and not isinstance(table, _RemoteTable) + else None + ) + + pinned_for_setup = self._open_pinned_table() + # Remote block reads go through take_offsets, which doesn't + # guarantee its output order, so they additionally select + # "_rowoffset" and sort locally by it (see _read_block); when the + # caller didn't narrow columns, that means explicitly listing + # every column, hence caching the full list once here. + self._all_columns: Optional[list[str]] = ( + pinned_for_setup.schema.names if columns is None else None + ) + self._live_offsets: Optional[np.ndarray] = None + if filter is None: + num_rows = pinned_for_setup.count_rows() + else: + self._live_offsets = self._scan_live_offsets(pinned_for_setup) + num_rows = int(self._live_offsets.size) + + # Every block holds exactly block_size live rows except a + # trailing runt, so no per-block range or count list is kept: + # anything sized by the block count also inflates the pickle + # shipped to each DataLoader worker (see __getstate__). + self._num_rows = num_rows + self._num_blocks = -(-num_rows // block_size) + full_blocks = num_rows // block_size + if full_blocks < num_splits: + raise ValueError( + f"block_size={block_size} yields only {full_blocks} " + f"block(s) from {num_rows} row(s), fewer than num_splits " + f"({num_splits}); use a smaller block_size or fewer splits" + ) + # Whether the epoch feeds into the block shuffle (so which + # blocks a split owns changes every epoch, same as 1-phase's + # row assignment) or only into the row-level shuffle within + # each split's fixed blocks (so blocks stay pinned to the same + # split/location across epochs, and only their row order + # varies). Defaults to True: matches the pre-existing + # behavior of using epoch for both. + self._reshuffle_blocks_per_epoch = ( + reshuffle_blocks_per_epoch + if reshuffle_blocks_per_epoch is not None + else True + ) + # Kept as a plain Python list for simplicity, unlike the + # 1-phase row mapping below, which is Arrow-backed because it + # is built and consumed through the Arrow-based permutation + # table machinery; a block permutation has no such need. + self._block_perm: Optional[list[int]] = self._shuffled_block_order() + self._perm_table = None + # Block mode has no PermutationBuilder output to fingerprint. + # These fields still drive common checkpoint code. + self._uses_split_strategy = False + self._perm_fingerprint_cache: Optional[str] = None + + # window_blocks is the primary residency knob: by default, + # max_shuffle_distance (the displacement bound that actually + # drives which blocks stay resident -- see + # _TwoPhaseSplitReader) is derived from it so that a split + # holds roughly window_blocks blocks in RAM at once. Passing + # max_shuffle_distance explicitly overrides that derivation. + self._window_blocks = ( + window_blocks if window_blocks is not None else DEFAULT_WINDOW_BLOCKS + ) + self._max_shuffle_distance = ( + max_shuffle_distance + if max_shuffle_distance is not None + else self._window_blocks * block_size + ) + else: # 1-phase shuffled read + # Build the permutation table once, deterministically. + builder = permutation_builder(table) + if filter is not None: + builder = builder.filter(filter) + if split_strategy is not None: + # Escape hatch: lets any PermutationBuilder split strategy drive + # this dataset without a bespoke parameter for each strategy. + self._perm_table = split_strategy(builder).execute() + # _rank_splits assumes split ids are exactly 0..num_splits-1. + split_value_counts = ( + self._perm_table.to_arrow().column("split_id").value_counts() + ) + counts_by_split = dict( + zip( + split_value_counts.field("values").to_pylist(), + split_value_counts.field("counts").to_pylist(), + ) + ) + expected_splits = set(range(num_splits)) + if set(counts_by_split) != expected_splits: + raise ValueError( + f"split_strategy produced split ids {sorted(counts_by_split)}, " + f"but num_splits={num_splits} requires exactly " + f"{{0, ..., {num_splits - 1}}}. Pass num_splits matching the " + "strategy's own split count (e.g. `fixed=num_splits` on " + "bootstrap_sample/split_random/etc.)." + ) + # Round-robin iteration assumes every split has the same row count. + if len(set(counts_by_split.values())) > 1: + raise ValueError( + f"split_strategy produced unequal split sizes " + f"{counts_by_split} (split id -> row count). Every split must " + "have the same row count. Use `fixed=num_splits` rather than " + "`ratios`/`counts`/weighted `split_hash`, which can produce " + "unequal sizes, or split unevenly-sized groups into separate " + "StreamingDatasets instead." + ) + elif shuffle: + perm_seed = shuffle_seed + epoch * _EPOCH_PRIME + self._perm_table = builder.split_random( + fixed=num_splits, seed=perm_seed, clump_size=shuffle_clump_size + ).execute() + else: + self._perm_table = builder.split_sequential(fixed=num_splits).execute() + # Only split_strategy needs a fingerprint: the built-in strategies + # are pinned by shuffle_seed/num_splits, while row ids can change + # across compaction. + self._uses_split_strategy = split_strategy is not None + self._perm_fingerprint_cache: Optional[str] = None if self._blocks_per_epoch == "auto": self._blocks_per_epoch = self._estimate_blocks_per_epoch() @@ -726,6 +1215,236 @@ class StreamingDataset(IterableDataset): range(rank_start, rank_start + splits_per_rank) ) + def _open_pinned_table(self): + """Open a private table handle checked out to this dataset's pin. + + A fresh handle so checking it out to the pinned version never + mutates ``self._table``, which the caller may still be using + elsewhere. Goes through ``Table.checkout``/``take_offsets``/ + ``search`` -- the same backend-agnostic interface ``RemoteTable`` + implements -- instead of pylance's ``to_lance()``, which only + works against a local table. + + Three cases, each independent of ``self._table`` by construction: + + - ``connection_factory`` set: call it fresh every time (the + documented way to get an unrelated handle; e.g. a worker + reconnect). + - A ``RemoteTable``: ``_table_to_pickle_state``/ + ``_table_from_pickle_state`` (used below for the other two cases) + special-case "remote" by returning ``self._table`` itself -- + correct for their original purpose (cross-process pickling, where + the unpickled copy necessarily lives in a different process), but + wrong here, since this runs in the *same* process: calling + ``checkout()`` on that result would pin (and, since LanceDB Cloud + rejects writes through a version-pinned handle, effectively make + read-only) the caller's own table object. ``deepcopy`` instead + round-trips through ``RemoteTable.__getstate__``/``__setstate__``, + which carries only reopen coordinates (connection state, branch, + checkout version) and clears the live connection handle, so the + copy reconnects independently on first real use. + - Local or memory: ``self._pinned_table_state`` (captured once at + construction -- see ``__init__``) is replayed here instead of + recomputing ``_table_to_pickle_state(self._table)`` fresh each + call. For a local (on-disk) table this would barely matter (the + state is just reopen coordinates, not data), but for a memory + table the "pickle state" the helper returns *is* the row data + (``to_arrow()``); recomputing it on every call would silently + capture whatever the live table looks like at that later moment + instead of the snapshot construction actually pinned. A reopened + memory table is always a fresh version-1 table, so there is + nothing meaningful to additionally ``checkout()`` -- it already + is exactly the pinned snapshot by construction. + """ + if self._connection_factory is not None: + # The factory may hand back a cached, caller-owned handle, and + # checkout() pins whatever handle it is called on, so pin a copy. + # Copy over the live connection rather than through pickle state: + # the factory exists so workers can hold unpicklable connections + # (e.g. with a Session), and an in-memory table must keep its + # version history for the checkout to find the snapshot. + handle = self._connection_factory(self._table.name) + if isinstance(handle, LanceTable): + pinned = handle._private_copy() + else: + pinned = deepcopy(handle) + pinned.checkout(self._pinned_version) + return pinned + + if self._pinned_table_state is None: + # Only a RemoteTable skips capturing _pinned_table_state (see + # __init__) -- anything else reaching here is a construction + # bug, not a runtime condition callers can hit. + pinned = deepcopy(self._table) + pinned.checkout(self._pinned_version) + return pinned + + pinned = _table_from_pickle_state(self._pinned_table_state) + if self._pinned_table_state["kind"] != "memory": + pinned.checkout(self._pinned_version) + return pinned + + def _pinned_lance_dataset(self, pinned_table): + """Native pylance handle for ``pinned_table``, if it's an on-disk table. + + ``to_lance()`` raises ``NotImplementedError`` on ``RemoteTable``, and + -- for a different reason -- also fails on a reopened ``memory://`` + table: ``_open_pinned_table`` rebuilds it as an isolated store (see + its docstring), which pylance's raw ``lance.dataset()`` open (what + ``to_lance()`` uses) cannot resolve; only the table's own connection + handle can see its data. Both cases are excluded here up front + rather than only via the ``except``, since the memory failure is a + ``ValueError`` indistinguishable from a real error. Cached per + dataset instance (keyed by nothing -- there's only ever one pin per + instance) so the conversion runs at most once rather than once per + block read. ``None`` means callers must use the backend-agnostic + query builder instead. + """ + if not hasattr(self, "_pinned_lance_ds_cache"): + conn_uri = getattr(getattr(pinned_table, "_conn", None), "uri", "") + if conn_uri.startswith("memory://"): + self._pinned_lance_ds_cache = None + else: + try: + self._pinned_lance_ds_cache = pinned_table.to_lance() + except NotImplementedError: + self._pinned_lance_ds_cache = None + return self._pinned_lance_ds_cache + + def _scan_live_offsets(self, pinned_table) -> np.ndarray: + """Ascending ``_rowoffset`` of every row matching ``filter``. + + Lance evaluates the filter natively and returns only each live row's + dataset-wide position, without materializing column data. Blocks are + contiguous chunks of this array, so a block read is a sparse take of + exactly its live rows. Goes through the table's own query builder + rather than pylance, so this also works against a remote table. + """ + live_offsets = ( + pinned_table.search() + .where(self._filter) + .select(["_rowoffset"]) + .to_arrow() + .column("_rowoffset") + .to_numpy() + ) + # Block and row semantics assume ascending order, which scan + # execution order doesn't guarantee. + if live_offsets.size and not np.all(live_offsets[:-1] <= live_offsets[1:]): + live_offsets = np.sort(live_offsets) + return live_offsets + + def _read_block( + self, pinned_table, block: _BlockSlice, columns: Optional[list[str]] + ) -> pa.Table: + """Read one 2-phase block slice's live rows, in natural (ascending) order.""" + lance_ds = self._pinned_lance_dataset(pinned_table) + block_start = block.block_id * self._block_size + row_start = block_start + block.start + row_end = block_start + block.stop + if self._filter is None: + if lance_ds is not None: + # Local table: pylance's positional range scan returns rows in + # storage order and is much faster than a take. + return lance_ds.scanner( + offset=row_start, limit=row_end - row_start, columns=columns + ).to_table() + # Remote offset/limit pages don't guarantee a consistent row order + # across requests, so independent pages can overlap or skip rows; + # explicit offsets name exactly this block's rows. + offsets = np.arange(row_start, row_end, dtype=np.int64) + else: + offsets = self._live_offsets[row_start:row_end] + if lance_ds is not None: + # Local table: pylance's native take() preserves the requested + # offset order already, so no sort-and-rebuild pass is needed. + return lance_ds.take(offsets.tolist(), columns=columns) + + # take_offsets makes no ordering guarantee for an arbitrary index + # list, so "_rowoffset" is always also selected and used to restore + # the request's ascending order locally. + select_cols = (columns if columns is not None else self._all_columns) + [ + "_rowoffset" + ] + table = ( + pinned_table.take_offsets(offsets.tolist()).select(select_cols).to_arrow() + ) + order = pc.sort_indices(table.column("_rowoffset")) + return table.take(order).drop_columns(["_rowoffset"]) + + def _shuffled_block_order(self) -> list[int]: + """The epoch's block permutation, derived only from scalar config.""" + block_order = list(range(self._num_blocks)) + if self._shuffle: + block_seed = self._shuffle_seed + if self._reshuffle_blocks_per_epoch: + block_seed += self._epoch * _EPOCH_PRIME + random.Random(block_seed).shuffle(block_order) + return block_order + + def _split_blocks(self, split_idx: int) -> list[_BlockSlice]: + """Block slices assigned to one split, in the split's read order. + + The epoch's shuffled blocks, laid end to end, form one row stream; + split k owns rows ``[k * n, (k + 1) * n)`` of it, where + ``n = num_rows // num_splits``. Cutting by rows rather than whole + blocks drops fewer than ``num_splits`` rows per epoch, like 1-phase, + so a split's first and last blocks may be partial. + """ + block_perm = np.asarray(self._block_perm, dtype=np.int64) + block_rows = np.full(len(block_perm), self._block_size, dtype=np.int64) + runt = self._num_rows - (self._num_blocks - 1) * self._block_size + block_rows[block_perm == self._num_blocks - 1] = runt + block_ends = np.cumsum(block_rows) + + rows_per_split = self._num_rows // self._num_splits + split_start = split_idx * rows_per_split + split_end = split_start + rows_per_split + first = int(np.searchsorted(block_ends, split_start, side="right")) + last = int(np.searchsorted(block_ends, split_end, side="left")) + blocks = [] + for pos in range(first, last + 1): + block_start = int(block_ends[pos] - block_rows[pos]) + blocks.append( + _BlockSlice( + block_id=int(block_perm[pos]), + start=max(split_start, block_start) - block_start, + stop=min(split_end, int(block_ends[pos])) - block_start, + ) + ) + return blocks + + def _make_two_phase_reader( + self, + split_idx: int, + pinned_table, + columns: Optional[list[str]], + skip: int = 0, + ) -> "_TwoPhaseSplitReader": + """Build the 2-phase reader for one split's assigned blocks. + + ``skip`` resumes from a saved absolute position within the split's + own deterministic read order (see ``_TwoPhaseSplitReader``). + """ + blocks = self._split_blocks(split_idx) + sigma_seed = ( + self._shuffle_seed + + self._epoch * _EPOCH_PRIME + + _SIGMA_PRIME + + split_idx * _SIGMA_SPLIT_PRIME + ) + return _TwoPhaseSplitReader( + block_ids=blocks, + block_live_counts=[block.stop - block.start for block in blocks], + shuffle=self._shuffle, + max_shuffle_distance=self._max_shuffle_distance, + seed=sigma_seed, + read_block_fn=lambda block: self._read_block(pinned_table, block, columns), + columns=columns, + skip=skip, + prefetch_depth=self._io_queue_depth, + ) + def _estimate_blocks_per_epoch(self) -> int: """Estimate a fixed packed-block budget from a bounded token sample.""" # TODO: Replace this fallback with Lance's dedicated exact token-count @@ -748,37 +1467,64 @@ class StreamingDataset(IterableDataset): "pass an explicit value for exact epoch sizing", ) - for split in range(self._num_splits): - permutation = Permutation.from_tables( - self._table, self._perm_table, split=split - ) - permutation = permutation.select_columns([token_column]) - permutation = permutation.with_transform(Transforms.arrow2arrow) - split_rows = permutation.num_rows - if split_rows == 0: - raise ValueError( - "blocks_per_epoch='auto' cannot estimate an empty dataset" - ) + def _sampled_token_count(column: Union[pa.Array, pa.ChunkedArray]) -> int: + lengths = pc.list_value_length(column) + if lengths.null_count: + raise ValueError("pack_sequences does not support null token lists") + return int(pc.sum(lengths).as_py()) + def _sample_size(split_rows: int) -> int: # Sample roughly 1% from each logical split, with at least one row # per split and a global target cap of 100,000 rows. - sample_rows = min( + return min( split_rows, max(1, min((split_rows + 99) // 100, sample_cap_per_split)), ) - sample_offsets = sorted(rng.sample(range(split_rows), sample_rows)) - sample_batch_size = max(1, self._read_batch_size) - for start in range(0, sample_rows, sample_batch_size): - batch = permutation.__getitems__( - sample_offsets[start : start + sample_batch_size] - ) - lengths = pc.list_value_length(batch.column(0)) - if lengths.null_count: - raise ValueError("pack_sequences does not support null token lists") - sampled_tokens += int(pc.sum(lengths).as_py()) - total_sampled += sample_rows - total_rows += split_rows + if self._block_size is not None: + # Sample whole blocks rather than scattered rows: a row sample + # spread over a split touches nearly every block, so it would + # read the entire token column just to estimate it. Only a + # split's first and last blocks can be partial, so a uniform + # sample of the split's own blocks still gives a near-unbiased + # per-row mean over exactly the rows that split will serve. + pinned_table = self._open_pinned_table() + for split in range(self._num_splits): + split_blocks = self._split_blocks(split) + split_rows = sum(block.stop - block.start for block in split_blocks) + num_sample_blocks = min( + len(split_blocks), + -(-_sample_size(split_rows) // self._block_size), + ) + for block in rng.sample(split_blocks, num_sample_blocks): + table = self._read_block(pinned_table, block, [token_column]) + sampled_tokens += _sampled_token_count(table.column(0)) + total_sampled += table.num_rows + total_rows += split_rows + else: + for split in range(self._num_splits): + permutation = Permutation.from_tables( + self._table, self._perm_table, split=split + ) + permutation = permutation.select_columns([token_column]) + permutation = permutation.with_transform(Transforms.arrow2arrow) + split_rows = permutation.num_rows + if split_rows == 0: + raise ValueError( + "blocks_per_epoch='auto' cannot estimate an empty dataset" + ) + + sample_rows = _sample_size(split_rows) + sample_offsets = sorted(rng.sample(range(split_rows), sample_rows)) + sample_batch_size = max(1, self._read_batch_size) + for start in range(0, sample_rows, sample_batch_size): + batch = permutation.__getitems__( + sample_offsets[start : start + sample_batch_size] + ) + sampled_tokens += _sampled_token_count(batch.column(0)) + + total_sampled += sample_rows + total_rows += split_rows # Pool the samples into one global average. Each document contributes # one EOS token. @@ -865,34 +1611,83 @@ class StreamingDataset(IterableDataset): if not my_splits: return - # Set identity transform on each Permutation so __getitems__ returns - # the raw RecordBatch. Stage 2 applies the real transform. - permutations: list[Permutation] = [] - initial_samples: list[int] = [] - initial_positions: list[int] = [] - for split_idx in my_splits: - perm = Permutation.from_tables( - self._table, self._perm_table, split=split_idx - ) - if self._columns is not None: - perm = perm.select_columns(self._columns) - perm = perm.with_transform(Transforms.arrow2arrow) - sample_count = self._resume_samples.get(split_idx, self._resume_offset) - # Both modes resume from absolute permutation positions. Packing - # stores them separately because it also checkpoints partial blocks. - start_pos = ( - self._pack_consumed[split_idx] - if self._pack_sequences is not None - else self._resume_positions.get(split_idx, sample_count) - ) - if start_pos > 0: - perm = perm.with_skip(start_pos) - initial_samples.append(sample_count) - initial_positions.append(start_pos) - permutations.append(perm) + if self._block_size is None: + # 1-phase: set identity transform on each Permutation so + # __getitems__ returns the raw RecordBatch. Stage 2 applies the + # real transform. + permutations: list[Permutation] = [] + initial_samples: list[int] = [] + initial_positions: list[int] = [] + for split_idx in my_splits: + perm = Permutation.from_tables( + self._table, self._perm_table, split=split_idx + ) + if self._columns is not None: + perm = perm.select_columns(self._columns) + perm = perm.with_transform(Transforms.arrow2arrow) + sample_count = self._resume_samples.get(split_idx, self._resume_offset) + # Both modes resume from absolute permutation positions. Packing + # stores them separately because it also checkpoints partial blocks. + start_pos = ( + self._pack_consumed[split_idx] + if self._pack_sequences is not None + else self._resume_positions.get(split_idx, sample_count) + ) + if start_pos > 0: + perm = perm.with_skip(start_pos) + initial_samples.append(sample_count) + initial_positions.append(start_pos) + permutations.append(perm) - n = len(permutations) - split_sizes = [perm.num_rows for perm in permutations] + n = len(permutations) + split_sizes = [perm.num_rows for perm in permutations] + else: + # 2-phase: each split owns an equal, contiguous run of rows from + # the epoch's shuffled block order (see _split_blocks); the + # fewer than num_splits leftover rows are dropped, as in 1-phase. + # + # Each split's blocks are wrapped in a _TwoPhaseSplitReader, + # which duck-types Permutation.__getitems__ -- so from here on, + # 2-phase flows through the exact same stage-1 I/O machinery + # (_fill_io/_submit_io/_drain_io, io_queue_depth prefetch) as + # 1-phase. No 2-phase-specific code exists past this point. + pinned_table = self._open_pinned_table() + + permutations: list[_TwoPhaseSplitReader] = [] + initial_samples = [] + initial_positions = [] + for split_idx in my_splits: + # Same absolute-position resume convention as 1-phase: a + # loaded checkpoint's positions_consumed_per_split (falling + # back to the sample count, then to the global offset) is + # this split's saved absolute position in its own + # deterministic read order. Packing stores its own cursor + # separately (it also checkpoints partial blocks), so it + # must be consulted here too, exactly as the 1-phase branch + # above does -- otherwise a packed resume silently ignores + # _pack_consumed and replays already-consumed documents. + sample_count = self._resume_samples.get(split_idx, self._resume_offset) + start_pos = ( + self._pack_consumed[split_idx] + if self._pack_sequences is not None + else self._resume_positions.get(split_idx, sample_count) + ) + try: + reader = self._make_two_phase_reader( + split_idx, pinned_table, self._columns, skip=start_pos + ) + except BaseException: + # The consumer loop's finally, which closes the readers' + # prefetch executors, hasn't been entered yet. + for built in permutations: + built.close() + raise + permutations.append(reader) + initial_samples.append(sample_count) + initial_positions.append(start_pos) + + n = len(permutations) + split_sizes = [reader.num_rows for reader in permutations] local_consumed = [0] * n # Permutation position each split has consumed through (absolute, # i.e. counted from the start of the unskipped split). Runs ahead of @@ -1332,6 +2127,13 @@ class StreamingDataset(IterableDataset): self._fetch_head_ref = None self._split_sizes_ref = None self._local_consumed_ref = None + if self._block_size is not None: + # Each _TwoPhaseSplitReader owns a private prefetch + # executor; a fresh set is built on every + # _iter_owned call, so these must be shut down here + # or their threads leak across epochs/iterations. + for reader in permutations: + reader.close() @property def bytes_loaded(self) -> int: @@ -1462,9 +2264,11 @@ class StreamingDataset(IterableDataset): state["_table"] = _table_to_pickle_state(self._table) # _perm_table: always in-memory; serialise as Arrow data (mirrors # how Permutation.__getstate__ handles its permutation_table). + # None in 2-phase mode, which never builds one. state["_perm_table"] = ( - self._perm_table.name, - self._perm_table.to_arrow(), + (self._perm_table.name, self._perm_table.to_arrow()) + if self._perm_table is not None + else None ) for key in ( "_raw_batches_ref", @@ -1475,6 +2279,22 @@ class StreamingDataset(IterableDataset): ): state[key] = None state["_consumer_iterator_lock"] = None + # Rebuilt in __setstate__. Spawn/forkserver workers receive this + # pickle through a pipe; once it outgrows the 64 KiB pipe buffer, + # each Popen blocks until its child has imported torch and read it, + # so a payload that scales with the block count serializes worker + # startup (minutes at 128 workers with small blocks). + if state.get("_block_perm") is not None: + state["_block_perm"] = None + # One entry per live row, so it would grow the pickle the same way; + # each worker rescans it from the pinned snapshot in __setstate__. + if state.get("_live_offsets") is not None: + state["_live_offsets"] = None + # Non-picklable Rust-backed handle (see _pinned_lance_dataset); + # dropped rather than set to None so the worker's first _read_block + # call recomputes it fresh against its own reconnected table instead + # of permanently misreading an absent cache as "confirmed remote". + state.pop("_pinned_lance_ds_cache", None) return state def __setstate__(self, state): @@ -1483,17 +2303,27 @@ class StreamingDataset(IterableDataset): table_name = state.pop("_table_name") table_state = state.pop("_table") - perm_name, perm_data = state.pop("_perm_table") + perm_table_state = state.pop("_perm_table") self.__dict__.update(state) self._consumer_iterator_lock = threading.Lock() + if "_block_perm" in state: + self._block_perm = self._shuffled_block_order() if self._connection_factory is not None: self._table = self._connection_factory(table_name) else: self._table = _table_from_pickle_state(table_state) - if table_state["kind"] == "memory": + if table_state["kind"] == "memory" and perm_table_state is not None: # Rebuilt from Arrow, so the recorded pin cannot resolve on it. - perm_data = _drop_base_version(perm_data) - self._perm_table = _connect("memory://").create_table(perm_name, perm_data) + perm_name, perm_data = perm_table_state + perm_table_state = (perm_name, _drop_base_version(perm_data)) + if perm_table_state is not None: + perm_name, perm_data = perm_table_state + self._perm_table = _connect("memory://").create_table(perm_name, perm_data) + else: + # 2-phase mode never builds a permutation table. + self._perm_table = None + if self._block_size is not None and self._filter is not None: + self._live_offsets = self._scan_live_offsets(self._open_pinned_table()) def state_dict(self) -> dict: """Snapshot the dataset's consumption state. @@ -1584,6 +2414,22 @@ class StreamingDataset(IterableDataset): self._perm_fingerprint_cache = hasher.hexdigest() return self._perm_fingerprint_cache + def _two_phase_checkpoint_config(self) -> dict: + """The 2-phase settings a saved position is only meaningful under.""" + if self._block_size is None: + return {} + return {key: getattr(self, f"_{key}") for key in _TWO_PHASE_CHECKPOINT_KEYS} + + def _check_two_phase_checkpoint_config(self, state: dict, source: str) -> None: + ours = self._two_phase_checkpoint_config() + for key in _TWO_PHASE_CHECKPOINT_KEYS: + if state.get(key) != ours.get(key): + raise ValueError( + f"{key} mismatch: {source} has {state.get(key)}, " + f"current dataset has {ours.get(key)}; saved positions " + "index a different 2-phase read order" + ) + def _checkpoint_snapshot(self) -> dict: # Only recorded for split_strategy: split_random/split_sequential are # already fully pinned by shuffle_seed/num_splits below, and row ids are not @@ -1599,6 +2445,7 @@ class StreamingDataset(IterableDataset): "shuffle_seed": self._shuffle_seed, "num_splits": self._num_splits, **perm_fingerprint, + **self._two_phase_checkpoint_config(), "epoch": self._epoch, "pack_sequences": self._pack_sequences, "eos_id": self._eos_id, @@ -1620,6 +2467,7 @@ class StreamingDataset(IterableDataset): "shuffle_seed": self._shuffle_seed, "num_splits": self._num_splits, **perm_fingerprint, + **self._two_phase_checkpoint_config(), "epoch": self._epoch, "samples_consumed_per_split": samples, "positions_consumed_per_split": positions, @@ -1690,6 +2538,7 @@ class StreamingDataset(IterableDataset): f"{key} mismatch in worker checkpoint: " f"{state.get(key)} != {expected}" ) + self._check_two_phase_checkpoint_config(state, "worker checkpoint") packed = "pack_buffers" in state if packed != (self._pack_sequences is not None): raise ValueError("worker checkpoint mode does not match the dataset") @@ -1759,7 +2608,10 @@ class StreamingDataset(IterableDataset): across compaction, so it would reject an otherwise-valid resume), and continue to rely on ``shuffle_seed``/``num_splits`` alone as before. Packed checkpoints pin ``pack_sequences``, ``eos_id``, ``pad_id``, - ``blocks_per_epoch``, and ``epoch``. + ``blocks_per_epoch``, and ``epoch``. Checkpoints from a ``block_size`` + dataset pin ``shuffle``, ``block_size``, ``max_shuffle_distance`` and + ``reshuffle_blocks_per_epoch``, and are rejected by a 1-phase dataset + and vice versa. """ if state["num_splits"] != self._num_splits: raise ValueError( @@ -1772,6 +2624,11 @@ class StreamingDataset(IterableDataset): f"current dataset has {self._shuffle_seed}" ) checkpoint_fingerprint = state.get("perm_fingerprint") + if checkpoint_fingerprint is not None and self._perm_table is None: + raise ValueError( + "permutation mismatch: the checkpoint records a split_strategy " + "fingerprint, but this block_size dataset has no permutation table" + ) if ( checkpoint_fingerprint is not None and checkpoint_fingerprint != self._perm_fingerprint @@ -1784,6 +2641,7 @@ class StreamingDataset(IterableDataset): "Resuming would apply saved per-split positions to the wrong " "rows." ) + self._check_two_phase_checkpoint_config(state, "checkpoint") self._consumer_checkpoint_requires_uniform = False if "pack_buffers" in state or self._pack_sequences is not None: @@ -1897,6 +2755,13 @@ class StreamingDataset(IterableDataset): for state in states[1:]: if ("pack_buffers" in state) != packed: raise ValueError("cannot merge packed and unpacked state dicts") + # Absent on purpose in 1-phase states, so compare presence too. + for key in _TWO_PHASE_CHECKPOINT_KEYS: + if state.get(key) != first.get(key): + raise ValueError( + f"{key} mismatch across state dicts: " + f"{state.get(key)} != {first.get(key)}" + ) for key in config_keys: if key not in state: continue diff --git a/python/python/lancedb/table.py b/python/python/lancedb/table.py index 688a241f5..cbd423d76 100644 --- a/python/python/lancedb/table.py +++ b/python/python/lancedb/table.py @@ -4433,17 +4433,22 @@ class LanceTable(Table): ) return connection.serialize() - def _namespace_client_for_pickle(self) -> Optional[Any]: - """Keep a table-level namespace client only when pickle can carry it.""" + def _table_namespace_client(self) -> Optional[Any]: + """The namespace client set on this table rather than its connection.""" namespace_client = self._namespace_client - if namespace_client is None: - return None if namespace_client is getattr(self._conn, "_namespace_client", None): - # The serialized connection rebuilds its own namespace client. + # The connection rebuilds its own namespace client. return None if namespace_client is getattr(self._conn, "_cached_namespace_client", None): # Local manifest-mode connections cache their namespace client here. return None + return namespace_client + + def _namespace_client_for_pickle(self) -> Optional[Any]: + """Keep a table-level namespace client only when pickle can carry it.""" + namespace_client = self._table_namespace_client() + if namespace_client is None: + return None import pickle @@ -4512,13 +4517,20 @@ class LanceTable(Table): # Keep the established behavior of exporting the visible table. return {"kind": "memory", "name": self.name, "data": self.to_arrow()} + return { + "kind": "lance", + "connection_state": self._pickle_connection_state(), + "namespace_client": self._namespace_client_for_pickle(), + **self._reopen_state(), + } + + def _reopen_state(self) -> dict[str, Any]: + """Arguments for ``_open_from_pickle_state``, minus the connection.""" # The native handle is authoritative: a tag checkout can move this # wrapper onto another branch, so never trust the branch captured at # construction time. self._branch = self.current_branch() return { - "kind": "lance", - "connection_state": self._pickle_connection_state(), "name": self.name, "namespace_path": list(self._namespace_path), # Only caller-supplied overrides are safe to replay. Namespace-provided @@ -4527,7 +4539,6 @@ class LanceTable(Table): "storage_options": self._storage_options, "index_cache_size": self._index_cache_size, "location": self._location, - "namespace_client": self._namespace_client_for_pickle(), "managed_versioning": self._managed_versioning, "pushdown_operations": set(self._pushdown_operations), "route_pushdown_to_rust": self._route_pushdown_to_rust, @@ -4535,6 +4546,23 @@ class LanceTable(Table): "checkout_version": self._checkout_version, } + def _private_copy(self) -> "LanceTable": + """A new handle on the same table, branch and checkout, over this + handle's own connection. + + ``checkout()`` on the copy leaves this handle untouched. Unlike a + pickle round trip, the connection need not be serializable (for + example, one created with a ``Session``), and an in-memory table keeps + its version history. + """ + return type(self)._open_from_pickle_state( + self._conn, + { + **self._reopen_state(), + "namespace_client": self._table_namespace_client(), + }, + ) + def __setstate__(self, state: dict[str, Any]) -> None: """Rebuild a table from ``__getstate__`` in the current process.""" if state.get("kind") == "memory": diff --git a/python/python/tests/test_elastic_dataloader.py b/python/python/tests/test_elastic_dataloader.py index 6e6ab45d1..6a70ddba3 100644 --- a/python/python/tests/test_elastic_dataloader.py +++ b/python/python/tests/test_elastic_dataloader.py @@ -31,12 +31,15 @@ Parameters used throughout: """ import dataclasses +import functools import logging import multiprocessing as mp +import pickle import threading from unittest.mock import patch import lancedb +import numpy as np import pyarrow as pa import pytest from utils import ( @@ -1179,6 +1182,74 @@ def test_streaming_dataloader_commits_only_consumed_worker_batches(tmp_path): assert remaining == uninterrupted == [[3, 4], [30, 40]] +def test_block_size_dataloader_workers_reconnect(tmp_path): + """Block-mode datasets pickle with no permutation table and reopen per worker.""" + db = lancedb.connect(tmp_path) + table = db.create_table("block_workers", pa.table({"id": list(range(8))})) + dataset = StreamingDataset( + table, + num_splits=2, + shuffle=False, + block_size=2, + ) + loader = StreamingDataLoader( + dataset, + batch_size=2, + num_workers=2, + multiprocessing_context="spawn", + ) + iterator = iter(loader) + try: + batches = [batch["id"].tolist() for batch in iterator] + finally: + iterator._shutdown_workers() + + assert sorted(row for batch in batches for row in batch) == list(range(8)) + + +@pytest.mark.parametrize("filter", [None, "id % 2 = 0"]) +@pytest.mark.parametrize("reshuffle_blocks_per_epoch", [True, False]) +def test_block_size_pickle_does_not_grow_with_block_count( + tmp_path, reshuffle_blocks_per_epoch, filter +): + """Spawned workers start serially once the pickle outgrows a pipe buffer.""" + db = lancedb.connect(tmp_path) + table = db.create_table("block_pickle", pa.table({"id": list(range(1 << 16))})) + + def make(block_size): + return StreamingDataset( + table, + num_splits=2, + shuffle_seed=7, + epoch=3, + block_size=block_size, + reshuffle_blocks_per_epoch=reshuffle_blocks_per_epoch, + filter=filter, + ) + + def pickled_state(dataset): + # The fixed-size shared-memory counters only pickle while spawning a + # child, and their size does not depend on the block count. + state = dataset.__getstate__() + for key in ("_worker_stats", "_untracked_worker_iteration", "_filled_count"): + del state[key] + return pickle.dumps(state) + + few_blocks, many_blocks = make(1 << 12), make(4) + few_size = len(pickled_state(few_blocks)) + many_size = len(pickled_state(many_blocks)) + assert many_size - few_size < 1024, (few_size, many_size) + # A filter's per-live-row offsets must not ride along either. + assert many_size < 64 * 1024, many_size + + restored = StreamingDataset.__new__(StreamingDataset) + restored.__setstate__(pickle.loads(pickled_state(many_blocks))) + assert restored._block_perm == many_blocks._block_perm + assert restored._block_perm != sorted(restored._block_perm) + if filter is not None: + np.testing.assert_array_equal(restored._live_offsets, many_blocks._live_offsets) + + def test_distributed_checkpoint_uses_rank_local_worker_boundary(tmp_path): db = lancedb.connect(tmp_path) table = db.create_table("rank_boundary", pa.table({"id": list(range(8))})) @@ -3093,6 +3164,36 @@ def _packed_dataset(table, pack_sequences, *, blocks_per_epoch, pad_id=0, **kwar ) +def test_pack_sequences_checkpoint_with_block_size(tmp_path): + table = _create_token_table( + tmp_path, + [[1], [2], [10, 11, 12, 13, 14, 15, 16, 17], [20]], + ) + kwargs = dict( + pack_sequences=5, + blocks_per_epoch=4, + num_splits=2, + block_size=2, + ) + reference = [ + block["input_ids"].tolist() for block in _packed_dataset(table, **kwargs) + ] + + dataset = _packed_dataset(table, **kwargs) + iterator = iter(dataset) + first_cycle = [next(iterator), next(iterator)] + checkpoint = dataset.state_dict() + iterator.close() + + resumed = _packed_dataset(table, **kwargs) + resumed.load_state_dict(checkpoint) + remaining = [block["input_ids"].tolist() for block in resumed] + + assert [block["input_ids"].tolist() for block in first_cycle] == reference[:2] + assert checkpoint["blocks_emitted_per_split"] == [1, 1] + assert remaining == reference[2:] + + def test_pack_sequences_emits_blocks_and_pads_final_tail(tmp_path): table = _create_token_table(tmp_path, [[1, 2], [3, 4], [5]]) dataset = _packed_dataset(table, 6, blocks_per_epoch=2) @@ -3572,6 +3673,732 @@ def test_streaming_dataset_over_remote_table(): assert_server_safe_row_id_requests(server) +class _ReorderingOffsetScanServer(MockPermutationServer): + """Answers offset/limit pages as if the table's two fragments complete in a + different order on each request, which remote scans are allowed to do.""" + + def _query(self, request, body): + if "offset" in body and not body.get("filter"): + rows = list(range(self.num_rows)) + half = self.num_rows // 2 + if body["offset"] >= half: + rows = rows[half:] + rows[:half] + start = body["offset"] + return self._arrow( + request, pa.table({"id": rows[start : start + body["k"]]}) + ) + return super()._query(request, body) + + +def test_two_phase_over_remote_table(): + """Unfiltered 2-phase reads of a remote table name each block's rows by + _rowoffset: offset/limit pages have no cross-request order guarantee.""" + server = _ReorderingOffsetScanServer(num_rows=16) + + with mock_remote_table(server) as table: + ds = StreamingDataset(table, num_splits=2, block_size=4, shuffle=False) + ids = [row["id"] for row in ds] + + assert sorted(ids) == list(range(server.num_rows)), ( + "Every row of the remote table must be yielded exactly once" + ) + + +def test_two_phase_over_remote_table_filtered_and_shuffled(): + """2-phase's filtered live-offset precompute and take_offsets-based + block reads both work against a remote table, including restoring + take_offsets' unspecified order via the _rowoffset it also selects.""" + server = MockPermutationServer(num_rows=20) + live_ids = list(range(16)) # 16 live rows -> 4 blocks, divides num_splits=2 + + with mock_remote_table(server) as table: + ds = StreamingDataset( + table, + num_splits=2, + block_size=4, + shuffle=True, + shuffle_seed=SHUFFLE_SEED, + filter="id IN (" + ",".join(str(i) for i in live_ids) + ")", + columns=["id"], + ) + ids = [row["id"] for row in ds] + + assert sorted(ids) == live_ids, "only live rows, each exactly once" + + +def test_two_phase_over_remote_table_does_not_pin_callers_handle(): + """_open_pinned_table() must not mutate the caller's own RemoteTable. + + Without a connection_factory, the reopen helpers used for local/memory + tables return the *same* RemoteTable object for the remote case (correct + for their original cross-process pickling purpose, wrong for same-process + reuse here). Checking that object out to the block-mode pin would + therefore pin -- and, since checkout_version != None marks a handle + version-pinned -- effectively make read-only the caller's own live table, + not an unrelated copy. + """ + server = MockPermutationServer(num_rows=8) + + with mock_remote_table(server) as table: + assert table._checkout_version is None, "caller's table starts unpinned" + ds = StreamingDataset(table, num_splits=1, block_size=4, shuffle=False) + list(ds) # drive at least one _open_pinned_table() call + assert table._checkout_version is None, ( + "iterating a 2-phase dataset must not pin the caller's own " + "table handle to a fixed version" + ) + + +@functools.lru_cache +def _cached_open_table(uri, name): + return lancedb.connect(uri).open_table(name) + + +@functools.lru_cache +def _cached_open_table_with_session(uri, name): + return lancedb.connect(uri, session=lancedb.Session()).open_table(name) + + +class _ReturnsTable: + def __init__(self, table): + self.table = table + + def __call__(self, name): + return self.table + + +@pytest.mark.parametrize("connection", ["local", "session", "memory"]) +def test_two_phase_does_not_pin_cached_factory_handle(tmp_path, connection): + """A connection_factory may return a cached, caller-owned handle: pinning + the dataset's snapshot must leave that handle writable, must not need a + picklable connection, and must keep reading the construction snapshot + after the handle's table changes.""" + if connection == "memory": + table = lancedb.connect("memory://").create_table( + "t", pa.table({"id": list(range(8))}) + ) + table.add(pa.table({"id": [100]})) + table.delete("id = 100") # the snapshot is at a version above 1 + factory = _ReturnsTable(table) + else: + lancedb.connect(tmp_path).create_table("t", pa.table({"id": list(range(8))})) + open_table = ( + _cached_open_table_with_session + if connection == "session" + else _cached_open_table + ) + factory = functools.partial(open_table, str(tmp_path)) + table = factory("t") + + ds = StreamingDataset( + table, block_size=4, shuffle=False, connection_factory=factory + ) + table.delete("id = 0") + table.add(pa.table({"id": [8]})) + + assert table.count_rows() == 8 + assert [row["id"] for row in ds] == list(range(8)) + + +# --------------------------------------------------------------------------- +# 2-phase shuffled reads (block_size) +# --------------------------------------------------------------------------- +# block_size switches StreamingDataset from 1-phase (per-row permutation) to +# 2-phase (block-granularity shuffle + a row-level shuffle bounded by +# max_shuffle_distance). NUM_ROWS=120 with block_size=10 gives 12 blocks, +# which divides NUM_SPLITS=12 evenly, so most tests below reuse lance_table +# and those constants without the runt blocks and mid-block split boundaries +# covered explicitly by their own tests. + + +def test_two_phase_full_coverage_no_duplicates(lance_table): + """Every sample ID appears exactly once across a full 2-phase epoch.""" + ds = StreamingDataset( + lance_table, + num_splits=NUM_SPLITS, + shuffle_seed=SHUFFLE_SEED, + block_size=10, + ) + ids = [s["id"] for s in ds] + assert len(ids) == NUM_ROWS + assert sorted(ids) == list(range(NUM_ROWS)), "every row must appear exactly once" + + +def test_two_phase_reads_runt_block(lance_table): + """A trailing partial block (block_size doesn't evenly divide the row + count) is read like any other block.""" + ds = StreamingDataset( + lance_table, + num_splits=1, + shuffle_seed=SHUFFLE_SEED, + block_size=13, # NUM_ROWS=120 -> 9 full blocks and a 3-row runt + shuffle=False, + ) + ids = [s["id"] for s in ds] + assert ids == list(range(NUM_ROWS)) + + +@pytest.mark.parametrize("reshuffle_blocks_per_epoch", [True, False]) +@pytest.mark.parametrize( + "block_size,num_splits,filter", + [ + # 12 blocks over 5 splits: split boundaries fall mid-block. + pytest.param(10, 5, None, id="whole_blocks_uneven_splits"), + # 9 full blocks plus a 3-row runt over 7 splits: 120 // 7 = 17 rows + # per split, so exactly 1 row is dropped. + pytest.param(13, 7, None, id="runt_and_uneven_splits"), + # 60 live rows in 9 blocks of 7 (the last a 4-row runt) over 4 splits. + pytest.param(7, 4, "id % 2 = 0", id="filtered"), + ], +) +def test_two_phase_drops_fewer_rows_than_splits( + lance_table, block_size, num_splits, filter, reshuffle_blocks_per_epoch +): + """Splits are cut by rows, not whole blocks, so each epoch drops fewer than + num_splits rows (as 1-phase does) and every split serves the same count.""" + live_ids = set(range(NUM_ROWS)) if filter is None else set(range(0, NUM_ROWS, 2)) + rows_per_split = len(live_ids) // num_splits + for epoch in range(2): + ds = StreamingDataset( + lance_table, + num_splits=num_splits, + shuffle_seed=SHUFFLE_SEED, + epoch=epoch, + block_size=block_size, + filter=filter, + reshuffle_blocks_per_epoch=reshuffle_blocks_per_epoch, + ) + for split in range(num_splits): + assert sum(b.stop - b.start for b in ds._split_blocks(split)) == ( + rows_per_split + ) + ids = [s["id"] for s in ds] + assert len(ids) == len(set(ids)) == rows_per_split * num_splits + assert set(ids) <= live_ids + assert len(live_ids) - len(ids) < num_splits + + +def test_two_phase_no_shuffle_preserves_order(lance_table): + """shuffle=False yields rows in exact original storage order.""" + ds = StreamingDataset( + lance_table, + num_splits=1, + shuffle_seed=SHUFFLE_SEED, + block_size=10, + shuffle=False, + ) + ids = [s["id"] for s in ds] + assert ids == list(range(NUM_ROWS)), ( + "shuffle=False must preserve original row order block-by-block" + ) + + +def test_two_phase_respects_max_shuffle_distance(lance_table): + """No row moves farther than max_shuffle_distance from its natural + (block, then row) position within its split's own block assignment. + + White-box: reconstructs the expected natural-rank order directly from + ds._block_perm (the shuffled block order), since that's the reference + frame the displacement bound is defined against, not raw table order. + """ + block_size = 10 # NUM_ROWS=120 -> 12 blocks + max_shuffle_distance = 25 + + ds = StreamingDataset( + lance_table, + num_splits=1, + shuffle_seed=SHUFFLE_SEED, + block_size=block_size, + max_shuffle_distance=max_shuffle_distance, + ) + ids = [s["id"] for s in ds] + + natural_order = [ + row_id + for bp in ds._block_perm + for row_id in range(bp * block_size, (bp + 1) * block_size) + ] + assert sorted(ids) == list(range(NUM_ROWS)) + assert sorted(natural_order) == list(range(NUM_ROWS)) + + natural_rank = {row_id: rank for rank, row_id in enumerate(natural_order)} + for output_pos, row_id in enumerate(ids): + displacement = abs(output_pos - natural_rank[row_id]) + assert displacement < max_shuffle_distance, ( + f"row {row_id} displaced {displacement} positions from its natural " + f"rank, exceeding max_shuffle_distance={max_shuffle_distance}" + ) + + +@pytest.mark.parametrize( + "kwargs,expected_window,expected_distance", + [ + pytest.param({}, 8, 80, id="defaults"), + pytest.param({"window_blocks": 2}, 2, 20, id="window_override"), + pytest.param({"max_shuffle_distance": 15}, 8, 15, id="distance_override"), + ], +) +def test_two_phase_window_defaults( + lance_table, kwargs, expected_window, expected_distance +): + ds = StreamingDataset( + lance_table, num_splits=1, shuffle_seed=SHUFFLE_SEED, block_size=10, **kwargs + ) + assert ds._window_blocks == expected_window + assert ds._max_shuffle_distance == expected_distance + + +def test_two_phase_filter_counts_live_rows(tmp_path): + """block_size counts only rows passing filter; dead rows never appear.""" + db = lancedb.connect(tmp_path) + table = db.create_table( + "t", + pa.table( + { + "id": list(range(NUM_ROWS)), + "category": ["train"] * 60 + ["val"] * 60, + } + ), + ) + ds = StreamingDataset( + table, + num_splits=1, + shuffle_seed=SHUFFLE_SEED, + block_size=10, # 60 live (train) rows -> 6 blocks exactly, no runt + filter="category = 'train'", + ) + ids = [s["id"] for s in ds] + assert all(i < 60 for i in ids), "only live (train) rows should appear" + assert sorted(ids) == list(range(60)) + + +def test_two_phase_resume_checkpoint_round_trip(lance_table): + """state_dict/load_state_dict resumes a 2-phase dataset without gaps or + repeats, the same guarantee test_doc_example_checkpoint verifies for + 1-phase.""" + steps_before_checkpoint = 5 + block_size = 10 # NUM_ROWS=120 -> 12 blocks, divides NUM_SPLITS=12 evenly + + ds = StreamingDataset( + lance_table, + num_splits=NUM_SPLITS, + shuffle_seed=SHUFFLE_SEED, + block_size=block_size, + ) + it = iter(ds) + consumed = [next(it)["id"] for _ in range(steps_before_checkpoint * NUM_SPLITS)] + checkpoint = ds.state_dict() + remaining_original = [s["id"] for s in it] + + ds_resumed = StreamingDataset( + lance_table, + num_splits=NUM_SPLITS, + shuffle_seed=SHUFFLE_SEED, + block_size=block_size, + ) + ds_resumed.load_state_dict(checkpoint) + remaining_resumed = [s["id"] for s in ds_resumed] + + assert remaining_original == remaining_resumed, ( + "Resumed 2-phase dataset must continue from exactly the same position" + ) + assert sorted(consumed + remaining_original) == list(range(NUM_ROWS)), ( + "Consumed + remaining must cover every row exactly once" + ) + + +@pytest.mark.parametrize( + "saved,resumed,mismatched_key", + [ + pytest.param( + {"block_size": 10}, {"block_size": 5}, "block_size", id="block_size" + ), + pytest.param( + {"block_size": 10}, {}, "block_size", id="two_phase_into_one_phase" + ), + pytest.param( + {}, {"block_size": 10}, "block_size", id="one_phase_into_two_phase" + ), + pytest.param( + {"block_size": 10}, + {"block_size": 10, "max_shuffle_distance": 15}, + "max_shuffle_distance", + id="max_shuffle_distance", + ), + pytest.param( + {"block_size": 10}, + {"block_size": 10, "reshuffle_blocks_per_epoch": False}, + "reshuffle_blocks_per_epoch", + id="reshuffle_blocks_per_epoch", + ), + pytest.param( + {"block_size": 10}, + {"block_size": 10, "shuffle": False}, + "shuffle", + id="shuffle", + ), + ], +) +def test_two_phase_checkpoint_rejects_different_read_order( + lance_table, saved, resumed, mismatched_key +): + """Saved positions index one 2-phase read order; resuming them under + another would replay consumed rows and skip unread ones. The error names + the setting to change.""" + + def make(options): + return StreamingDataset( + lance_table, num_splits=NUM_SPLITS, shuffle_seed=SHUFFLE_SEED, **options + ) + + source = make(saved) + it = iter(source) + for _ in range(2 * NUM_SPLITS): + next(it) + checkpoint = source.state_dict() + + with pytest.raises(ValueError, match=f"^{mismatched_key} mismatch: checkpoint"): + make(resumed).load_state_dict(checkpoint) + with pytest.raises(ValueError, match=f"^{mismatched_key} mismatch across"): + StreamingDataset.merge_state_dicts([checkpoint, make(resumed).state_dict()]) + + +def test_two_phase_failed_reader_build_closes_built_readers(lance_table, monkeypatch): + """A resume position past a split's end fails while building readers; the + readers built before it must still release their prefetch executors.""" + ds = StreamingDataset( + lance_table, num_splits=2, shuffle_seed=SHUFFLE_SEED, block_size=10 + ) + checkpoint = ds.state_dict() + checkpoint["positions_consumed_per_split"] = [0, NUM_ROWS] + ds.load_state_dict(checkpoint) + + closed = [] + close = streaming._TwoPhaseSplitReader.close + + def counting_close(reader): + closed.append(reader) + close(reader) + + monkeypatch.setattr(streaming._TwoPhaseSplitReader, "close", counting_close) + with pytest.raises(ValueError, match="out of range"): + list(ds) + assert len(closed) == 1 + + +def test_two_phase_pinned_version_survives_concurrent_write(tmp_path): + """The block plan is pinned to the table version at construction; a + write between construction and iteration must not change what an + in-progress epoch reads.""" + db = lancedb.connect(tmp_path) + table = db.create_table("t", pa.table({"id": list(range(8))})) + + ds = StreamingDataset(table, num_splits=1, block_size=4, shuffle=False) + table.delete("id = 0") # mutate after construction, before iteration + + ids = [s["id"] for s in ds] + assert ids == list(range(8)), ( + "must read the version pinned at construction, not the live, since-mutated one" + ) + + +def test_two_phase_pinned_memory_table_survives_concurrent_write(): + """Same guarantee as the on-disk test above, but for a memory:// table. + + A memory table's "pickle state" is its row data (to_arrow()), not just + reopen coordinates, so it must be captured once at construction -- not + recomputed from the live table on every _open_pinned_table() call -- + or a write made after construction would leak into every later read. + """ + db = lancedb.connect("memory://") + table = db.create_table("t", pa.table({"id": list(range(8))})) + + ds = StreamingDataset(table, num_splits=1, block_size=4, shuffle=False) + table.delete("id = 0") + table.add(pa.table({"id": [8]})) + + ids = [s["id"] for s in ds] + assert ids == list(range(8)), ( + "must read the snapshot captured at construction, not the live, " + "since-mutated one" + ) + + +def test_two_phase_memory_table_already_at_a_later_version(): + """Constructing over a memory table already past version 1 must not + fail: the pin is the snapshot's data, not a version number to replay + against a freshly rebuilt (always version-1) in-memory table.""" + db = lancedb.connect("memory://") + table = db.create_table("t", pa.table({"id": list(range(4))})) + table.add(pa.table({"id": [4, 5, 6, 7]})) # now at version 2, 8 live rows + + ds = StreamingDataset(table, num_splits=1, block_size=4, shuffle=False) + ids = [s["id"] for s in ds] + assert sorted(ids) == list(range(8)) + + +def test_two_phase_reader_plan_arrays_are_int32(lance_table): + """_plan_block_pos/_plan_local_offset are retained for the reader's + whole lifetime, one entry per row in the entire split (the open + whole-split-memory review thread) -- so their dtype is a real, constant + memory-footprint decision, not an implementation detail. Pinned here at + int32 (half of int64) so a future change can't silently regress it.""" + ds = StreamingDataset( + lance_table, num_splits=1, shuffle_seed=SHUFFLE_SEED, block_size=10 + ) + reader = ds._make_two_phase_reader(0, ds._open_pinned_table(), None) + assert reader._plan_block_pos.dtype == np.int32 + assert reader._plan_local_offset.dtype == np.int32 + # Values must still be correct, not just narrower -- e.g. a block index + # silently wrapping around int32 would be a far worse regression than + # the memory this dtype choice saves. + assert reader._plan_block_pos.max() < len(ds._split_blocks(0)) + assert reader._plan_local_offset.max() < 10 # block_size + + +class _LookupCountingDict(dict): + def __init__(self, *args): + super().__init__(*args) + self.lookups = 0 + + def __getitem__(self, key): + self.lookups += 1 + return super().__getitem__(key) + + def get(self, key, default=None): + self.lookups += 1 + return super().get(key, default) + + +@pytest.mark.parametrize( + "skip,swap_calls", + [ + pytest.param(0, False, id="fresh"), + pytest.param(1500, False, id="resumed"), + # io_queue_depth lets calls take the reader's lock out of order. + pytest.param(0, True, id="out_of_order"), + ], +) +def test_two_phase_reader_sequential_read_is_linear_in_blocks(skip, swap_calls): + """A sequential read must fetch each block once and keep per-call + bookkeeping bounded by the resident window, not by how many blocks the + split has already passed -- otherwise prefetch cost grows quadratically + with split size.""" + num_blocks, block_size, batch = 400, 8, 8 + block_ids = list(range(1000, 1000 + num_blocks)) + reads = [] + + def read_block(block_id): + reads.append(block_id) + # Two record batches per block, as a multi-batch scan returns. + half = block_size // 2 + return pa.concat_tables( + pa.table({"block": [block_id] * half, "row": range(lo, lo + half)}) + for lo in (0, half) + ) + + reader = streaming._TwoPhaseSplitReader( + block_ids=block_ids, + block_live_counts=[block_size] * num_blocks, + shuffle=True, + max_shuffle_distance=4 * block_size, + seed=SHUFFLE_SEED, + read_block_fn=read_block, + columns=None, + skip=skip, + prefetch_depth=2, + ) + try: + # Later positions overwrite earlier ones, leaving each block's last. + expected_last_pos = { + bp: pos for pos, bp in enumerate(reader._plan_block_pos.tolist()) + } + assert reader._block_last_pos == expected_last_pos + + reader._block_last_pos = _LookupCountingDict(reader._block_last_pos) + starts = list(range(0, reader.num_rows, batch)) + if swap_calls: + starts[0::2], starts[1::2] = starts[1::2], starts[0::2] + outputs = {} + for start in starts: + out = reader.__getitems__(range(start, min(start + batch, reader.num_rows))) + outputs[start] = list( + zip(out.column("block").to_pylist(), out.column("row").to_pylist()) + ) + assert all( + t.column("block").num_chunks == 1 for t in reader._loaded.values() + ) + rows = [row for start in sorted(outputs) for row in outputs[start]] + num_calls = len(starts) + finally: + reader.close() + + expected_rows = [ + (block_ids[bp], off) + for bp, off in zip( + reader._plan_block_pos.tolist(), reader._plan_local_offset.tolist() + ) + ] + assert rows == expected_rows + if skip == 0: + assert sorted(rows) == [ + (bid, off) for bid in block_ids for off in range(block_size) + ] + needed_blocks = {block_ids[bp] for bp in expected_last_pos} + assert sorted(reads) == sorted(needed_blocks) + # Rescanning every already-passed block on each prefetch top-up grows + # quadratically (~29k lookups fresh here); a bounded walk stays linear + # (~2k). + assert reader._block_last_pos.lookups < 10 * (num_calls + num_blocks) + + +def test_two_phase_auto_blocks_per_epoch_reads_only_sampled_blocks( + tmp_path, monkeypatch +): + """blocks_per_epoch='auto' must sample whole blocks: a scattered row + sample touches nearly every block and so reads the whole token column.""" + table = _create_token_table(tmp_path, [[1, 2, 3]] * 1000) + reads = [] + read_block = StreamingDataset._read_block + + def counting_read_block(self, pinned_table, block, columns): + reads.append(block) + return read_block(self, pinned_table, block, columns) + + monkeypatch.setattr(StreamingDataset, "_read_block", counting_read_block) + with pytest.warns(UserWarning, match="approximate token-count sample"): + dataset = _packed_dataset( + table, 4, blocks_per_epoch="auto", num_splits=2, block_size=10 + ) + + # 1% of each 500-row split rounds up to a single 10-row block per split. + assert len(reads) == 2 + assert reads[0] in dataset._split_blocks(0) + assert reads[1] in dataset._split_blocks(1) + # Every document is 3 tokens plus EOS, so the sample is exact: 4000 / 4. + assert dataset.state_dict()["blocks_per_epoch"] == 1000 + + +def test_two_phase_rejects_rte_write_spec_table(tmp_path): + """An RTE write spec means some acknowledged writes live only in the + MemWAL, invisible to a version-pinned read; block_size must refuse the + table at construction rather than silently reading stale base-table + values (mirroring the row-id rejection 1-phase already applies).""" + from lancedb import RTEWriteSpec + + db = lancedb.connect(tmp_path) + schema = pa.schema( + [ + pa.field("id", pa.int64(), nullable=False), + pa.field("value", pa.int64(), nullable=False), + ] + ) + table = db.create_table( + "t", + pa.Table.from_pydict({"id": list(range(8)), "value": [0] * 8}, schema=schema), + ) + table.set_unenforced_primary_key("id") + table.enable_rte(RTEWriteSpec.bucket("id", 1)) + table.merge_insert( + "id" + ).when_matched_update_all().when_not_matched_insert_all().execute( + pa.Table.from_pydict({"id": list(range(8)), "value": [99] * 8}, schema=schema) + ) + + with pytest.raises(ValueError, match="RTE write spec"): + StreamingDataset(table, num_splits=1, block_size=4, shuffle=False) + + +def test_two_phase_rejects_fingerprinted_checkpoint(lance_table): + """A checkpoint tied to a split_strategy permutation cannot resume block mode.""" + source = StreamingDataset( + lance_table, + split_strategy=lambda b: b.bootstrap_sample( + 2 * NUM_ROWS, fixed=NUM_SPLITS, seed=SHUFFLE_SEED + ), + num_splits=NUM_SPLITS, + ) + checkpoint = source.state_dict() + assert "perm_fingerprint" in checkpoint + + target = StreamingDataset( + lance_table, + num_splits=NUM_SPLITS, + shuffle_seed=checkpoint["shuffle_seed"], + block_size=10, + ) + with pytest.raises(ValueError, match="no permutation table"): + target.load_state_dict(checkpoint) + + +def test_two_phase_reshuffle_blocks_per_epoch_toggle(lance_table): + """reshuffle_blocks_per_epoch=False keeps each split's blocks fixed + across epochs; the default (True) reassigns them every epoch.""" + block_size = 10 # NUM_ROWS=120 -> 12 blocks + + def block_perm_for(epoch, reshuffle): + ds = StreamingDataset( + lance_table, + num_splits=1, + shuffle_seed=SHUFFLE_SEED, + block_size=block_size, + epoch=epoch, + reshuffle_blocks_per_epoch=reshuffle, + ) + return list(ds._block_perm) + + assert block_perm_for(0, False) == block_perm_for(1, False), ( + "blocks must stay pinned to the same split across epochs when disabled" + ) + assert block_perm_for(0, True) != block_perm_for(1, True), ( + "blocks should be reassigned across epochs by default" + ) + + +def test_two_phase_too_few_blocks_for_num_splits_raises(tmp_path): + """Fewer blocks than num_splits would leave some splits with no data.""" + db = lancedb.connect(tmp_path) + table = db.create_table("t", pa.table({"id": list(range(20))})) # 2 blocks + with pytest.raises(ValueError): + StreamingDataset(table, num_splits=4, shuffle_seed=SHUFFLE_SEED, block_size=10) + + +@pytest.mark.parametrize( + "extra_kwargs", + [ + pytest.param( + {"block_size": 10, "shuffle_clump_size": 5}, + id="shuffle_clump_size_with_block_size", + ), + pytest.param( + { + "block_size": 10, + "split_strategy": lambda b: b.split_sequential(fixed=NUM_SPLITS), + }, + id="split_strategy_with_block_size", + ), + pytest.param({"block_size": 1}, id="block_size_too_small"), + pytest.param({"window_blocks": 2}, id="window_blocks_without_block_size"), + pytest.param( + {"max_shuffle_distance": 5}, id="max_shuffle_distance_without_block_size" + ), + pytest.param( + {"reshuffle_blocks_per_epoch": True}, + id="reshuffle_blocks_per_epoch_without_block_size", + ), + ], +) +def test_two_phase_constructor_validation_raises(lance_table, extra_kwargs): + """Invalid 2-phase parameter combinations raise ValueError at construction.""" + with pytest.raises(ValueError): + StreamingDataset( + lance_table, + num_splits=NUM_SPLITS, + shuffle_seed=SHUFFLE_SEED, + **extra_kwargs, + ) + + # --------------------------------------------------------------------------- # split_strategy escape hatch # --------------------------------------------------------------------------- diff --git a/python/python/tests/utils.py b/python/python/tests/utils.py index 90ad26463..cbc0ba02a 100644 --- a/python/python/tests/utils.py +++ b/python/python/tests/utils.py @@ -36,6 +36,15 @@ def is_row_id_take(body) -> bool: return "_rowid" in (body.get("filter") or "") +def is_row_offset_take(body) -> bool: + """True when a query body fetches specific rows by ``_rowoffset``. + + This is the request shape ``Table.take_offsets`` sends on the remote + backend, distinct from ``is_row_id_take``'s ``_rowid IN (...)``. + """ + return "_rowoffset" in (body.get("filter") or "") + + def arrow_file_bytes(table: pa.Table) -> bytes: """Serialize to the Arrow IPC *file* framing the /query/ route answers with.""" sink = pa.BufferOutputStream() @@ -175,6 +184,37 @@ class MockPermutationServer: pa.table({"_rowid": pa.array(row_ids, pa.uint64())}), ) + if is_row_offset_take(body): + # A _rowoffset take (2-phase's filtered block reads): this + # table's rows are never deleted/reordered, so id == _rowoffset + # for every row, same invariant as id == _rowid above. + row_offsets = sorted(parse_in_list(body["filter"])) + columns = body.get("columns") or ["id", "_rowoffset"] + values = {"id": row_offsets, "_rowoffset": row_offsets} + return self._arrow( + request, + pa.table( + { + col: pa.array( + values[col], + pa.uint64() if col == "_rowoffset" else pa.int64(), + ) + for col in columns + } + ), + ) + + if body.get("columns") == ["_rowoffset"] and "IN" in (body.get("filter") or ""): + # 2-phase's live-offset precompute scan: a user filter over + # "_rowoffset" alone. This mock only understands ` IN + # (...)` filters (not arbitrary SQL), and since id == _rowoffset + # for every row here, reuses parse_in_list regardless of which + # column the filter actually names. + row_offsets = sorted(parse_in_list(body["filter"])) + return self._arrow( + request, pa.table({"_rowoffset": pa.array(row_offsets, pa.uint64())}) + ) + # The schema probe: filtered to nothing, so it carries schema and no rows. return self._arrow(request, pa.table({"id": pa.array([], pa.int64())}))