Skip to content

[data][docs] Document Iteration APIs pipelining model - #67454

Merged
dstrodtman merged 9 commits into
ray-project:masterfrom
JasonLi1909:jason/docs-batch-iteration-internals
Oct 27, 2026
Merged

dstrodtman merged 9 commits into
ray-project:masterfrom
JasonLi1909:jason/docs-batch-iteration-internals

Conversation

@JasonLi1909

@JasonLi1909 JasonLi1909 commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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

Screenshot 2026-10-27 at 9 48 27 AM Screenshot 2026-10-27 at 9 47 25 AM

@JasonLi1909
JasonLi1909 requested review from a team as code owners October 26, 2026 23:27

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread doc/source/data/internals.md Resolved
Comment thread doc/source/data/internals.md Resolved
@ray-gardener ray-gardener Bot added docs An issue or change related to documentation data Ray Data-related issues labels Oct 27, 2026
@JasonLi1909
JasonLi1909 requested a review from a team as a code owner October 27, 2026 04:37
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]>
@JasonLi1909
JasonLi1909 force-pushed the jason/docs-batch-iteration-internals branch from 4aec342 to f773538 Compare October 27, 2026 15:53
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]>
@JasonLi1909 JasonLi1909 changed the title [data][docs] Document the Dataset iteration threading model [data][docs] Document Iteration APIs pipelining model Oct 27, 2026
…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 dstrodtman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

  1. Worker count. The text says the worker count is min(4, prefetch_batches). In iter_batches.py, the 4 is the default of RAY_DATA_MAX_FORMAT_THREADPOOL_NUM_WORKERS, and with prefetch_batches=0 the 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?
  2. 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_fn argument that iter_torch_batches sets internally.

No objection from the docs side once the author has looked at these.

Comment thread doc/source/data/internals.md Resolved
Comment thread doc/source/data/internals.md Resolved
Comment thread doc/source/data/internals.md Outdated
* - Collate
- Running your `collate_fn`.
* - Finalize
- Running your `finalize_fn`. For GPU training, this is the host-to-device transfer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch @dstrodtman, reworded "Running the finalize function. Only iter_torch_batches sets one, which moves the batch to the target device."

Comment thread doc/source/data/internals.md Resolved
Comment thread doc/source/data/internals.md Resolved
JasonLi1909 and others added 5 commits October 27, 2026 11:00
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]>
@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Oct 27, 2026
@dstrodtman
dstrodtman merged commit 34b6176 into ray-project:master Oct 27, 2026
7 checks passed
justinyeh1995 pushed a commit to justinyeh1995/ray that referenced this pull request Oct 28, 2026
)

## 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]>
dstrodtman added a commit that referenced this pull request Oct 30, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues docs An issue or change related to documentation go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants