Skip to content

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

Merged
jjyao merged 2 commits into
ray-project:masterfrom
hipudding:53716
Jun 18, 2025
Merged

jjyao merged 2 commits into
ray-project:masterfrom
hipudding:53716

Conversation

@hipudding

@hipudding hipudding commented Jun 16, 2025 •

Copy link
Copy Markdown
Contributor

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

@hipudding
hipudding marked this pull request as ready for review June 17, 2025 00:25
Copilot AI review requested due to automatic review settings June 17, 2025 00:25

Copilot AI 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.

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
Copy Markdown
Contributor Author

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

@ruisearch42 ruisearch42 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.

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
Copy Markdown
Contributor

@jjyao

jjyao commented Jun 18, 2025

Copy link
Copy Markdown
Contributor

@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
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
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

4 participants