Repository navigation
[Data] Chunk DataFrame emission in WebDatasetDatasource._read_stream - #65394
Conversation
Signed-off-by: Hyunoh-Yeo <[email protected]>
There was a problem hiding this comment.
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.
| if len(rows) >= chunk_size: | ||
| yield pd.DataFrame.from_records(rows) | ||
| rows = [] | ||
|
|
||
| if len(rows) > 0: | ||
| yield pd.DataFrame.from_records(rows) |
There was a problem hiding this comment.
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.
| 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() |
There was a problem hiding this comment.
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.
Signed-off-by: Hyunoh-Yeo <[email protected]>
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
make this configurable via the API?
There was a problem hiding this comment.
I will do that right away. I wanted to keep the scope minimal for the first pass.
There was a problem hiding this comment.
got it. Thank you!
…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]>
## 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>
…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]>
…#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>
Description
WebDatasetDatasource._read_streamemits one single-row DataFrame per sample, which means the output buffer runsPandasBlockAccessor.size_bytesonce per sample. On large shards this dominates read-side CPU (~63% of theReadWebDatasetoperator'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
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)