Repository navigation
[data][docs] Document Iteration APIs pipelining model - #67454
dstrodtman merged 9 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds a new 'Dataset iteration' section to the Ray Data internals documentation, detailing how iteration APIs consume blocks across different thread tiers (prepare, process, and finalize) and explaining the various iteration stages. It also introduces a supporting SVG diagram to visualize these threads. The review feedback suggests minor grammatical corrections in the newly added text to improve readability.
Add a section to the Ray Data internals page covering the thread tiers behind the iteration APIs, which step of work runs on each tier, and what each pipeline stage does. Link to it from the Ray Train guide on debugging data loading bottlenecks, which uses the stage names it defines. Signed-off-by: JasonLi1909 <[email protected]>
4aec342 to
f773538
Compare
Use "use" instead of "leverage", move the API list out of a parenthetical, add alt text to the diagram, drop filler, address the reader in the second person, and replace passive voice and nominalizations. Signed-off-by: JasonLi1909 <[email protected]>
Demote the section to a subsection of "Key concepts" so it sits alongside the production-side subsections, and move the Ray Train note to the end of the section, where the surrounding prose is about data loading latency. Signed-off-by: JasonLi1909 <[email protected]>
…ta page Point readers of the batch iteration section at the internals page that explains how Ray Data produces those batches. Signed-off-by: JasonLi1909 <[email protected]>
dstrodtman
left a comment
There was a problem hiding this comment.
Docs-side style review from Douglas Strodtman (Anyscale docs team). Claude Code assisted with this pass. I've read every comment below and stand behind them.
Scope is style, grammar, and Ray's writing style guide only. The Ray Data and Ray Train maintainers own whether the described behavior is accurate and have final say on anything technical. Nice addition overall: the thread diagram plus the step and stage tables make the iterator pipeline easy to follow, and both earlier bot suggestions are already applied at this head.
The inline suggestions cover a long sentence with passive voice, a parenthetical in a table header, a table cell that doesn't answer its column's question, and a redundant admonition.
Questions for the Data maintainers
These are questions, not requests. I checked python/ray/data/_internal/block_batching/ on master but defer to you on what's worth documenting.
- Worker count. The text says the worker count is
min(4, prefetch_batches). Initer_batches.py, the 4 is the default ofRAY_DATA_MAX_FORMAT_THREADPOOL_NUM_WORKERS, and withprefetch_batches=0the pool has zero workers, so format and collate run inline rather than in worker threads. Is either case worth a sentence, or is the default-path description intentional? finalize_fn. See the inline comment on the Finalize row. It reads as a user-supplied function, but on master it's the private_finalize_fnargument thatiter_torch_batchessets internally.
No objection from the docs side once the author has looked at these.
| * - Collate | ||
| - Running your `collate_fn`. | ||
| * - Finalize | ||
| - Running your `finalize_fn`. For GPU training, this is the host-to-device transfer. |
There was a problem hiding this comment.
Question for the Data maintainers, not a style change: "your finalize_fn" reads as something the user passes in. On master, DataIterator._iter_batches takes it as the private _finalize_fn, and iter_torch_batches sets it through _get_finalize_fn(device=...). I don't see a public finalize_fn parameter. If that's right, something like "Running the finalize function. For iter_torch_batches, this moves the batch to the target device." might be more accurate. I'm deferring to you on the wording.
There was a problem hiding this comment.
Good catch @dstrodtman, reworded "Running the finalize function. Only iter_torch_batches sets one, which moves the batch to the target device."
Co-authored-by: Douglas Strodtman <[email protected]> Signed-off-by: Jason Li <[email protected]>
Co-authored-by: Douglas Strodtman <[email protected]> Signed-off-by: Jason Li <[email protected]>
Co-authored-by: Douglas Strodtman <[email protected]> Signed-off-by: Jason Li <[email protected]>
Co-authored-by: Douglas Strodtman <[email protected]> Signed-off-by: Jason Li <[email protected]>
finalize_fn isn't a user-facing parameter. Only iter_torch_batches sets one, and it moves the batch to the target device, which isn't always a host-to-device transfer. Signed-off-by: JasonLi1909 <[email protected]>
) ## Overview Today, there are no docs detailing the internal dataset iterating mechanism for iteration APIs like `iter_batches`, `iter_rows`, `to_tf`, etc. This is a knowledge gap for users that care about data loading latency, particularly for model training. This PR adds a section to the Ray Data internals page that describes the underlying pipelining mechanism for dataset iteration APIs. This includes: - A diagram outlining the pipelining mechanism - Detail on each of the iteration stages (production wait, data transfer, batching, format, collate, finalize) - Cross-links with Ray Train's data loading debugging docs, and a link in from the Iterating over data guide ### Screenshots <img width="1396" height="731" alt="Screenshot 2026-10-27 at 9 48 27 AM" src="https://github.com/user-attachments/assets/a0e45062-8b02-4772-9d7b-0d26edebea30" /> <img width="1406" height="519" alt="Screenshot 2026-10-27 at 9 47 25 AM" src="https://github.com/user-attachments/assets/8ba12102-353c-4206-8ab6-4b646133d337" /> --------- Signed-off-by: JasonLi1909 <[email protected]> Signed-off-by: Jason Li <[email protected]> Co-authored-by: Douglas Strodtman <[email protected]>
…strings on iteration APIs (#67486) ## Summary Corrects the `prefetch_batches` and `local_shuffle_buffer_size` argument docstrings that are copied across the `Dataset` and `DataIterator` iteration APIs, so they describe what `BatchIterator` and `ShufflingBatcher` actually do. This is a docstring-only change. Behavior is unchanged. This came up while reviewing #67454, which documents the iteration pipeline on the Ray Data internals page. The API reference pages that section links to contradicted its diagram. ## Before ### `prefetch_batches` The shared text said: > The number of batches to fetch ahead of the current batch to fetch. If set to greater than 0, a separate threadpool will be used to fetch the objects to the local node, format the batches, and apply the collate_fn. Defaults to 1. That doesn't match `python/ray/data/_internal/block_batching/iter_batches.py`: - Iteration always runs in a background thread. `_iter_batches` wraps the pipeline in `iter_threaded(...)` whatever the value of `prefetch_batches`. - The format and collate pool has `min(RAY_DATA_MAX_FORMAT_THREADPOOL_NUM_WORKERS, prefetch_batches)` workers, and that environment variable defaults to 4. Setting `prefetch_batches` above 4 fetches further ahead but adds no threads, and the docstring didn't say so. - With `prefetch_batches=0`, there's no pool. `prefetch_batches_locally` yields block refs without prefetching, and format and collate run inline. - `iter_batches`, `iter_tf_batches`, `to_tf`, and `to_torch` don't accept a `collate_fn`, but their docstrings mentioned applying one. - The first sentence repeated "to fetch". `iter_jax_batches` had a different one-line version. ### `local_shuffle_buffer_size` The docstrings disagreed with each other and with `ShufflingBatcher` in `python/ray/data/_internal/batcher.py`: - The `iter_torch_batches`, `iter_tf_batches`, `to_torch`, and `to_tf` docstrings said the buffer size "must be greater than or equal to `batch_size`." The code doesn't reject a smaller value. It rounds it up to `batch_size`. - `ShufflingBatcher` raises `ValueError` when `batch_size` is `None`. Only some docstrings said `batch_size` is required, and `iter_batches`, which accepts `batch_size=None`, didn't. - `DataIterator.iter_jax_batches` had a one-line version, "Minimum rows for local in-memory shuffle." - None of them stated the trade-off that `ShufflingBatcher`'s own docstring gives: a larger buffer is more random but adds latency to the first batch. ## After ### `prefetch_batches` Every `prefetch_batches` docstring now uses the same text. Methods that accept `collate_fn` (`iter_torch_batches`, `iter_jax_batches`) mention collation, and the others don't: > The number of batches to fetch ahead of the current batch. Ray Data prepares batches in background threads while your code consumes the current one. If greater than 0, a thread pool formats the batches. The pool has `prefetch_batches` threads, up to a limit of 4. To change the limit, set the `RAY_DATA_MAX_FORMAT_THREADPOOL_NUM_WORKERS` environment variable. If 0, Ray Data doesn't prefetch blocks and formats each batch without the thread pool. Defaults to 1. ### `local_shuffle_buffer_size` Every docstring now uses the same text: > If not `None`, Ray Data randomly shuffles the data using a local in-memory shuffle buffer, and this value serves as the minimum number of rows that must be in the buffer to yield a batch. When there are no more rows to add to the buffer, Ray Data drains the remaining rows. A larger buffer makes the shuffle more random but might increase the latency to the first batch. If this value is smaller than `batch_size`, Ray Data rounds it up to `batch_size`. Local shuffling raises a `ValueError` if `batch_size` is `None`. I kept the "minimum number of rows" sentence's meaning as it was. It's an approximation of the compaction watermarks in `ShufflingBatcher.has_batch()`, and I've asked in a comment below whether you want it more precise. Updated methods, for both arguments: - `DataIterator`: `iter_batches`, `iter_torch_batches`, `iter_tf_batches`, `iter_jax_batches`, `to_torch`, and `to_tf`. - `Dataset`: `iter_batches`, `iter_torch_batches`, `iter_tf_batches`, `iter_jax_batches`, and `to_tf`. ## Subsystems API (docstrings only) ## Not a duplicate I searched open PRs for `prefetch_batches` and iterator docstring changes. #67355 touches `iter_batches.py` and `util.py` to stop the block prefetcher early, but it doesn't change these docstrings, the thread-pool sizing, or `ShufflingBatcher`. ## Testing - `pre-commit run --files python/ray/data/iterator.py python/ray/data/dataset.py` passes, including ruff, black, pydoclint, and the Ray docstyle check. - I checked each claim against `iter_batches.py` (`_format_batches`, `_iter_batches`, `prefetch_batches_locally`), `util.py` (`iter_threaded`), and `batcher.py` (`ShufflingBatcher.__init__` and `has_batch`) on master. - I didn't run a local Sphinx build. The change is plain prose plus inline literals. The `docstring-only` label is applied to skip the heavy Ray Data test suites. I wrote this with AI assistance (Claude Code). I reviewed every changed line and can defend the change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
Overview
Today, there are no docs detailing the internal dataset iterating mechanism for iteration APIs like
iter_batches,iter_rows,to_tf, etc. This is a knowledge gap for users that care about data loading latency, particularly for model training.This PR adds a section to the Ray Data internals page that describes the underlying pipelining mechanism for dataset iteration APIs. This includes:
Screenshots