Skip to content

Conversation

KaisennHu
Copy link
Contributor

@KaisennHu KaisennHu commented Oct 11, 2025

Why are these changes needed?

Enable zero-copy serialization for all PyTorch tensors by setting RAY_ENABLE_ZERO_COPY_TORCH_TENSORS=1 to accelerate serialization.

Example test script:

import ray
import torch
from datetime import datetime
import os

os.environ["RAY_ENABLE_ZERO_COPY_TORCH_TENSORS"] = "1"

ray.init(runtime_env={"env_vars": {"RAY_ENABLE_ZERO_COPY_TORCH_TENSORS": "1"}})

@ray.remote
def process(tensor):
    return tensor.sum()

x = torch.ones(1024, 1024, 256)

start_time = datetime.now()
x_ref = process.remote(x)
result = ray.get(x_ref)
time_diff = datetime.now() - start_time

print(f"result      : {result}")
print(f"between time: {time_diff.total_seconds()}s")
print(f"result type : {type(result)}")

Below are the performance gains and validation results:
aaaa-opt-1

Related issue number

Closes #56740

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run pre-commit jobs to lint the changes in this PR. (pre-commit setup)
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@KaisennHu KaisennHu requested a review from a team as a code owner October 11, 2025 07:51
cursor[bot]

This comment was marked as outdated.

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 introduces an optimization for serializing PyTorch tensors by enabling zero-copy serialization. This is achieved by converting tensors to NumPy arrays, which can be handled efficiently by Ray's existing serialization mechanisms. The feature is opt-in via the RAY_ENABLE_ZERO_COPY_TORCH_TENSORS environment variable. The implementation adds recursive conversion functions to handle tensors nested within various data structures. My review focuses on improving code clarity, maintainability, and adherence to best practices by suggesting the use of constants and refactoring duplicated code.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@ray-gardener ray-gardener bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Oct 11, 2025
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 core Issues that should be addressed in Ray Core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] [Core] Support Zero-Copy Serialization for Read-Only Tensors

1 participant