Skip to content

[Data] Chunk DataFrame emission in WebDatasetDatasource._read_stream - #65394

Merged
richardliaw merged 3 commits into
ray-project:masterfrom
Hyunoh-Yeo:data-webdataset-chunked-emission
Aug 12, 2026
Merged

richardliaw merged 3 commits into
ray-project:masterfrom
Hyunoh-Yeo:data-webdataset-chunked-emission

Conversation

@Hyunoh-Yeo

Copy link
Copy Markdown
Contributor

Description

WebDatasetDatasource._read_stream emits one single-row DataFrame per sample, which means the output buffer runs PandasBlockAccessor.size_bytes once per sample. On large shards this dominates read-side CPU (~63% of the ReadWebDataset operator's CPU). This changes the behavior of readwebdataset and let it accumulates 512 samples per emitted DataFrame.

Related issues

Closes #65350

Additional information

Benchmark specs are specified in #65350

  • It was cross-validated with a reporter

Added a parametrized test (test_read_webdataset_chunked_samples) that verifies the output is identical to the original per-sample behavior (row count, values, and order)

  • test with different sample sizes (1 / 511 / 512 / 513 / 1000 samples).

Signed-off-by: Hyunoh-Yeo <[email protected]>
@Hyunoh-Yeo

Hyunoh-Yeo commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor Author

@Hyunoh-Yeo
Hyunoh-Yeo marked this pull request as ready for review August 11, 2026 20:36
@Hyunoh-Yeo
Hyunoh-Yeo requested a review from a team as a code owner August 11, 2026 20:36

@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 optimizes the WebDataset datasource by chunking sample emission into batches of 512 samples per DataFrame instead of yielding a DataFrame for every single sample, and adds corresponding unit tests. Feedback suggests using .convert_dtypes() on the generated DataFrames to prevent silent type promotion (e.g., converting integers to floats due to NaN values from missing fields in heterogeneous records), which can cause downstream schema instability in Ray Data.

Comment on lines +475 to +480
if len(rows) >= chunk_size:
yield pd.DataFrame.from_records(rows)
rows = []

if len(rows) > 0:
yield pd.DataFrame.from_records(rows)

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.

medium

When chunking samples into a single pd.DataFrame using pd.DataFrame.from_records(rows), if the samples in the chunk have heterogeneous keys (i.e., some samples are missing certain optional fields), Pandas will automatically introduce NaN values for the missing fields. This causes any integer or boolean columns with missing values to be cast to float64.

This silent type promotion can lead to schema/type instability across different blocks (e.g., some blocks having int64 and others having float64 for the same column), which can cause downstream failures in Ray Data during block concatenation or schema unification.

To prevent this, consider converting the resulting DataFrame using .convert_dtypes() to preserve nullable integer/boolean types, or ensure that missing values are handled safely.

Suggested change
if len(rows) >= chunk_size:
yield pd.DataFrame.from_records(rows)
rows = []
if len(rows) > 0:
yield pd.DataFrame.from_records(rows)
if len(rows) >= chunk_size:
yield pd.DataFrame.from_records(rows).convert_dtypes()
rows = []
if len(rows) > 0:
yield pd.DataFrame.from_records(rows).convert_dtypes()

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.

The behavior itself as not changed from original 1 row dataframe emission. Heterogenious promotion already happens in the current code. convert_dtypes() in fact introduces mutation in dtypes.

@Hyunoh-Yeo Hyunoh-Yeo changed the title [Data] read_webdataset emits one DataFrame per sample, causing per-sample size_bytes overhead in the output buffer [Data] Chunk DataFrame emission in WebDatasetDatasource._read_stream Aug 11, 2026
@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Aug 12, 2026
@Hyunoh-Yeo
Hyunoh-Yeo marked this pull request as draft August 12, 2026 01:40
Signed-off-by: Hyunoh-Yeo <[email protected]>
@Hyunoh-Yeo
Hyunoh-Yeo marked this pull request as ready for review August 12, 2026 19:01

@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 optimizes the WebDataset datasource by batching samples into chunks of 512 before yielding them as pandas DataFrames, rather than yielding a DataFrame for each individual sample. This change significantly improves performance. Tests have been added to verify chunked reading and handling of heterogeneous keys. The feedback suggests extracting the hardcoded chunk size of 512 into a module-level constant to improve code maintainability.

Signed-off-by: Hyunoh-Yeo <[email protected]>
)

# Number of samples accumulated into each emitted DataFrame block.
WEBDATASET_READ_CHUNK_SIZE = 512

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.

make this configurable via the API?

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.

I will do that right away. I wanted to keep the scope minimal for the first pass.

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.

not that important

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.

got it. Thank you!

@richardliaw richardliaw added the go add ONLY when ready to merge, run all tests label Aug 12, 2026
@richardliaw
richardliaw enabled auto-merge (squash) August 12, 2026 19:41
@richardliaw
richardliaw merged commit 9c6ffee into ray-project:master Aug 12, 2026
8 checks passed
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ay-project#65394)

## Description
`WebDatasetDatasource._read_stream` emits one single-row DataFrame per
sample, which means the output buffer runs
`PandasBlockAccessor.size_bytes` once per sample. On large shards this
dominates read-side CPU (~63% of the `ReadWebDataset` operator's CPU).
This changes the behavior of readwebdataset and let it accumulates 512
samples per emitted DataFrame.

## Related issues
Closes ray-project#65350

## Additional information
Benchmark specs are specified in ray-project#65350
- It was cross-validated with a reporter

Added a parametrized test (`test_read_webdataset_chunked_samples`) that
verifies the output is identical to the original per-sample behavior
(row count, values, and order)
- test with different sample sizes (1 / 511 / 512 / 513 / 1000 samples).

---------

Signed-off-by: Hyunoh-Yeo <[email protected]>
Signed-off-by: 400Ping <[email protected]>
richardliaw pushed a commit that referenced this pull request Aug 21, 2026
## Description
`read_webdataset` does not allow num_cpus, num_gpus, memory, or
ray_remote_args, so users must drop to `read_datasource` and build
`WebDatasetDatasource` directly to tune read task resources, while
readers of other formats already expose these. This allows users to pass
these arguments to `read_webdataset` for consistency and user
convenience, which matters when the read is I/O bound. In this case,
lowering num_cpus runs more concurrent read tasks for higher S3
throughput.

## Related issues
Related to #65394

---------

Signed-off-by: Hyunoh-Yeo <[email protected]>
Signed-off-by: Hyunoh Yeo <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…ay-project#65394)

## Description
`WebDatasetDatasource._read_stream` emits one single-row DataFrame per
sample, which means the output buffer runs
`PandasBlockAccessor.size_bytes` once per sample. On large shards this
dominates read-side CPU (~63% of the `ReadWebDataset` operator's CPU).
This changes the behavior of readwebdataset and let it accumulates 512
samples per emitted DataFrame.

## Related issues
Closes ray-project#65350

## Additional information
Benchmark specs are specified in ray-project#65350 
- It was cross-validated with a reporter

Added a parametrized test (`test_read_webdataset_chunked_samples`) that
verifies the output is identical to the original per-sample behavior
(row count, values, and order)
- test with different sample sizes (1 / 511 / 512 / 513 / 1000 samples).

---------

Signed-off-by: Hyunoh-Yeo <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…#65623)

## Description
`read_webdataset` does not allow num_cpus, num_gpus, memory, or
ray_remote_args, so users must drop to `read_datasource` and build
`WebDatasetDatasource` directly to tune read task resources, while
readers of other formats already expose these. This allows users to pass
these arguments to `read_webdataset` for consistency and user
convenience, which matters when the read is I/O bound. In this case,
lowering num_cpus runs more concurrent read tasks for higher S3
throughput.

## Related issues
Related to ray-project#65394

---------

Signed-off-by: Hyunoh-Yeo <[email protected]>
Signed-off-by: Hyunoh Yeo <[email protected]>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] read_webdataset emits one DataFrame per sample, causing per-sample size_bytes overhead in the output buffer

2 participants