Skip to content

[Data] Make all map ops zero-copy by default #58285

Merged
alexeykudinkin merged 6 commits intoray-project:masterfrom
alexeykudinkin:ak/map-btch-zc-fix
Oct 30, 2025
Merged

[Data] Make all map ops zero-copy by default #58285
alexeykudinkin merged 6 commits intoray-project:masterfrom
alexeykudinkin:ak/map-btch-zc-fix

Conversation

@alexeykudinkin
Copy link
Contributor

Description

Historically, the intention was to avoid failures upon attempts to modify provided batch in-place when, for ex, using Torch tensors.

However, that is unjustifiably penalizing 99.9% of use-cases for 0.1% of scenarios. As such, we're flipping this setting to be zero_copy_batch=True by default.

Related issues

Link related issues: "Fixes #1234", "Closes #1234", or "Related to #1234".

Additional information

Optional: Add implementation details, API changes, usage examples, screenshots, etc.

Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
@alexeykudinkin alexeykudinkin requested a review from a team as a code owner October 29, 2025 19:53
Copy link
Contributor

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

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 changes the default value of zero_copy_batch to True for map operations, which is a sensible change to optimize for the common use case and improve performance. My review focuses on the correctness and documentation impact of this change.

I've identified a critical issue in Dataset.add_column where zero_copy_batch is incorrectly set to True. Since the add_column operation mutates the batch, this will lead to runtime errors. I've suggested reverting this specific change.

Additionally, I've pointed out that the docstring for Dataset.map_batches needs to be updated to reflect the new default value of zero_copy_batch and to provide clearer guidance to users.

Overall, the direction of the PR is good, but the identified issues should be addressed before merging.

compute=compute,
concurrency=concurrency,
zero_copy_batch=False,
zero_copy_batch=True,
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The add_column UDF mutates the batch in-place (e.g., batch.loc[:, col] = column). Setting zero_copy_batch=True will provide a read-only batch, which will cause a runtime error when the UDF attempts to modify it. This should be set to False to ensure a writable copy of the batch is provided.

Suggested change
zero_copy_batch=True,
zero_copy_batch=False,

compute: Optional[ComputeStrategy] = None,
batch_format: Optional[str] = "default",
zero_copy_batch: bool = False,
zero_copy_batch: bool = True,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

With this change, the default for zero_copy_batch is now True. The docstring should be updated to reflect this.

  1. On line 614, Default is ``False`` should be changed to Default is ``True`` .
  2. The tip on lines 485-487 is now outdated. It suggests setting zero_copy_batch=True for performance, but this is now the default. It would be more helpful to rephrase it to warn users about in-place mutation, for example: "By default, map_batches uses zero-copy batching to maximize performance. If your function mutates the batch, you must set zero_copy_batch=False to avoid errors."

cursor[bot]

This comment was marked as outdated.

Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
@alexeykudinkin alexeykudinkin added the go add ONLY when ready to merge, run all tests label Oct 29, 2025
Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
@raulchen
Copy link
Contributor

Can you add a test where the UDF modifies data while zero_copy is True? Make sure the error message clearly tells users to tune the zero_copy flag.

@ray-gardener ray-gardener bot added the data Ray Data-related issues label Oct 30, 2025
Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
@alexeykudinkin
Copy link
Contributor Author

Can you add a test where the UDF modifies data while zero_copy is True? Make sure the error message clearly tells users to tune the zero_copy flag.

There's already such a test

https://github.com/search?q=repo%3Aray-project%2Fray%20%22tried%20to%20mutate%20a%20zero-copy%20read-only%20batch%22&type=code

@alexeykudinkin alexeykudinkin enabled auto-merge (squash) October 30, 2025 05:37
@alexeykudinkin alexeykudinkin merged commit f926999 into ray-project:master Oct 30, 2025
7 checks passed
YoussefEssDS pushed a commit to YoussefEssDS/ray that referenced this pull request Nov 8, 2025
## Description

Historically, the intention was to avoid failures upon attempts to
modify provided batch in-place when, for ex, using Torch tensors.

However, that is unjustifiably penalizing 99.9% of use-cases for 0.1% of
scenarios. As such, we're flipping this setting to be
`zero_copy_batch=True` by default.

## Related issues
> Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to
ray-project#1234".

## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

---------

Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
## Description

Historically, the intention was to avoid failures upon attempts to
modify provided batch in-place when, for ex, using Torch tensors.

However, that is unjustifiably penalizing 99.9% of use-cases for 0.1% of
scenarios. As such, we're flipping this setting to be
`zero_copy_batch=True` by default.

## Related issues
> Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to
ray-project#1234".

## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

---------

Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
Aydin-ab pushed a commit to Aydin-ab/ray-aydin that referenced this pull request Nov 19, 2025
## Description

Historically, the intention was to avoid failures upon attempts to
modify provided batch in-place when, for ex, using Torch tensors.

However, that is unjustifiably penalizing 99.9% of use-cases for 0.1% of
scenarios. As such, we're flipping this setting to be
`zero_copy_batch=True` by default.

## Related issues
> Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to
ray-project#1234".

## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

---------

Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
Signed-off-by: Aydin Abiar <aydin@anyscale.com>
Future-Outlier pushed a commit to Future-Outlier/ray that referenced this pull request Dec 7, 2025
## Description

Historically, the intention was to avoid failures upon attempts to
modify provided batch in-place when, for ex, using Torch tensors.

However, that is unjustifiably penalizing 99.9% of use-cases for 0.1% of
scenarios. As such, we're flipping this setting to be
`zero_copy_batch=True` by default.

## Related issues
> Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to
ray-project#1234".

## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

---------

Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
Signed-off-by: Future-Outlier <eric901201@gmail.com>
peterxcli pushed a commit to peterxcli/ray that referenced this pull request Feb 25, 2026
## Description

Historically, the intention was to avoid failures upon attempts to
modify provided batch in-place when, for ex, using Torch tensors.

However, that is unjustifiably penalizing 99.9% of use-cases for 0.1% of
scenarios. As such, we're flipping this setting to be
`zero_copy_batch=True` by default.

## Related issues
> Link related issues: "Fixes ray-project#1234", "Closes ray-project#1234", or "Related to
ray-project#1234".

## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

---------

Signed-off-by: Alexey Kudinkin <ak@anyscale.com>
Signed-off-by: peterxcli <peterxcli@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Ray fails to serialize self-reference objects

2 participants