Skip to content

[cgraph] Avoid depending on torch CPU module for CPU-only actor #53849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 18, 2025

Conversation

hipudding
Copy link
Contributor

@hipudding hipudding commented Jun 16, 2025

Why are these changes needed?

In cases where the actor involved in the Compiled Graph exclusively uses the CPU, AcceleratorContext unnecessarily loads torch’s CPU module, resulting in an avoidable dependency.
This PR eliminates that dependency by avoiding the torch module loading for CPU-only actors.

Related issue number

Closes #53716

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • 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 :(

Signed-off-by: huafengchun <huafengchun@gmail.com>
@hipudding hipudding marked this pull request as ready for review June 17, 2025 00:25
@Copilot Copilot AI review requested due to automatic review settings June 17, 2025 00:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates AcceleratorContext to skip importing the torch CPU backend when the device is CPU-only, removing an unnecessary dependency on torch.cpu.

  • Conditional import of torch.<backend> only for non-CPU devices
  • Eliminates unconditional torch.cpu import for CPU-only actors
Comments suppressed due to low confidence (3)

python/ray/experimental/channel/accelerator_context.py:40

  • The comment should mention how the CPU case is handled (e.g., that no torch backend is loaded or that self._torch_mod will be None). This will clarify expected behavior for maintainers.
# Import the torch backend module (e.g., torch.cuda) if the device is not 'cpu'.

python/ray/experimental/channel/accelerator_context.py:1

  • Add a unit test for the CPU-only path to verify that AcceleratorContext initializes correctly without importing a torch backend and does not raise errors when accessing self._torch_mod.
def __init__(self, torch_module_name: str, communicator_cls: Type[Communicator])

python/ray/experimental/channel/accelerator_context.py:41

  • When torch_module_name is "cpu", self._torch_mod is never defined, which may lead to an AttributeError later. Consider adding an else block to define self._torch_mod (e.g., None or the base torch module) or refactor usage to handle a missing backend module.
if torch_module_name != "cpu":

@hipudding
Copy link
Contributor Author

@ruisearch42 @kevin85421 Could you please review this bug fix? Thanks.

Copy link
Contributor

@ruisearch42 ruisearch42 left a comment

Choose a reason for hiding this comment

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

Thanks. Let me trigger the tests.

@ruisearch42 ruisearch42 added the go add ONLY when ready to merge, run all tests label Jun 17, 2025
@ruisearch42
Copy link
Contributor

@jjyao
Copy link
Collaborator

jjyao commented Jun 18, 2025

@jjyao jjyao changed the title CPU device within AcceleratorContext independently of torch. [cgraph] CPU device within AcceleratorContext independently of torch. Jun 18, 2025
@jjyao jjyao changed the title [cgraph] CPU device within AcceleratorContext independently of torch. [cgraph] Avoid depending on torch CPU module for CPU-only actor Jun 18, 2025
@jjyao jjyao merged commit 61f7057 into ray-project:master Jun 18, 2025
5 checks passed
rebel-scottlee pushed a commit to rebellions-sw/ray that referenced this pull request Jun 21, 2025
…project#53849)

Signed-off-by: huafengchun <huafengchun@gmail.com>
Signed-off-by: Scott Lee <scott.lee@rebellions.ai>
minerharry pushed a commit to minerharry/ray that referenced this pull request Jun 27, 2025
elliot-barn pushed a commit that referenced this pull request Jul 2, 2025
Signed-off-by: huafengchun <huafengchun@gmail.com>
Signed-off-by: elliot-barn <elliot.barnwell@anyscale.com>
goutamvenkat-anyscale pushed a commit to goutamvenkat-anyscale/ray that referenced this pull request Jul 4, 2025
…project#53849)

Signed-off-by: huafengchun <huafengchun@gmail.com>
Signed-off-by: Goutam V <goutam@anyscale.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Release test compiled_graphs failed
3 participants