Skip to content

[core] Not overriding accelerator id env vars when num_accelerators is 0 or not set - #54928

Merged
jjyao merged 14 commits into
ray-project:masterfrom
Qiaolin-Yu:cuda_device
Aug 18, 2025
Merged

jjyao merged 14 commits into
ray-project:masterfrom
Qiaolin-Yu:cuda_device

Conversation

@Qiaolin-Yu

@Qiaolin-Yu Qiaolin-Yu commented Jul 25, 2025 •

Copy link
Copy Markdown
Member

Why are these changes needed?

This PR aims to avoid overriding the accelerator ids environment variables when the number of accelerators is zero. For example, when this environment variable is set, if a user sets num_gpus=0 in the ray.init() call, the environment variable CUDA_VISIBLE_DEVICES will not be set to an empty string.

Note that this logic is controlled by a new env var RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO. We make RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=1 by default to avoid a breaking change. To enable the new behavior, it needs to setRAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0.

With this PR, ray.init() will output the following message.

python /ray-workspace/temp.py
2025-08-18 18:26:21,621 INFO worker.py:1943 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265 
2025-08-18 18:26:53,873 WARNING worker.py:1991 -- Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0

Related issue number

Close #54868

Checks

@Qiaolin-Yu
Qiaolin-Yu requested a review from a team as a code owner July 25, 2025 20:54

@gemini-code-assist gemini-code-assist Bot 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.

Summary of Changes

Hello @Qiaolin-Yu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines how Ray manages accelerator visibility environment variables (such as CUDA_VISIBLE_DEVICES). The primary goal is to prevent unintended modifications to these variables when no accelerators are requested and to ensure that any necessary modifications are properly reset after a task completes. This improves the predictability and isolation of task execution, especially in environments with diverse hardware configurations.

Highlights

  • Granular Control for Accelerator Visibility: The set_visible_accelerator_ids function has been enhanced to prevent overriding accelerator environment variables (like CUDA_VISIBLE_DEVICES) if no accelerator IDs are assigned for a given resource (e.g., when num_gpus=0). This avoids unnecessary modifications to the environment.
  • Environment Variable Restoration: A new mechanism is introduced to capture the original state of accelerator-related environment variables before they are modified. After a non-actor task completes, these variables are now explicitly reset to their original values, ensuring a clean environment for subsequent tasks and preventing unintended side effects.
  • Actor Task Exception: The new environment variable management and restoration logic is intentionally skipped for actor tasks. This allows actors to maintain their own accelerator visibility configurations without interference from Ray's automatic environment variable cleanup.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request addresses an issue where CUDA_VISIBLE_DEVICES was being overridden even when num_gpus was not set. The changes correctly skip this override and also introduce a mechanism to reset any modified accelerator-related environment variables after a task completes. Overall, this is a solid improvement to resource management for accelerators.

Comment thread python/ray/_private/utils.py Resolved
@Qiaolin-Yu
Qiaolin-Yu marked this pull request as draft July 25, 2025 22:36
@Qiaolin-Yu
Qiaolin-Yu marked this pull request as ready for review August 4, 2025 18:29
@Qiaolin-Yu Qiaolin-Yu added core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests labels Aug 4, 2025
Comment thread python/ray/_raylet.pyx Resolved
@ruisearch42

Copy link
Copy Markdown
Contributor

@Qiaolin-Yu , we will need to do some testing for vLLM before rolling this out. Let me know when the PR is ready.

@Qiaolin-Yu

Copy link
Copy Markdown
Member Author

@Qiaolin-Yu , we will need to do some testing for vLLM before rolling this out. Let me know when the PR is ready.

I think the functionality is basically ready, you can try it 👀.

@jjyao
jjyao requested a review from edoakes August 5, 2025 23:47
@jjyao jjyao assigned edoakes and unassigned jjyao Aug 5, 2025
@guapisolo

guapisolo commented Aug 10, 2025 •

Copy link
Copy Markdown

@Qiaolin-Yu , a PR for slime may use this feature. Let me know when the PR is ready.

@Qiaolin-Yu

Qiaolin-Yu commented Aug 12, 2025 •

Copy link
Copy Markdown
Member Author

@Qiaolin-Yu , a PR for slime may use this feature. Let me know when the PR is ready.

@guapisolo After offline discussion, we believe that a temporary solution is to add an additional environment variable to control this logic. So it will not be a break change.

@guapisolo

Copy link
Copy Markdown

@Qiaolin-Yu , a PR for slime may use this feature. Let me know when the PR is ready.

@guapisolo After offline discussion, we believe that a temporary solution is to add an additional environment variable to control this logic. So it will not be a break change.

Got it. Thanks!

@Qiaolin-Yu Qiaolin-Yu changed the title [core] Not overriding CUDA_VISIBLE_DEVICES in the case where num_gpus is not set [core] Not overriding accelerator id env vars when num_accelerators is 0 or not set Aug 12, 2025

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

LG

@Qiaolin-Yu
Qiaolin-Yu requested a review from jjyao August 14, 2025 20:29
Comment thread python/ray/_private/worker.py Resolved
Comment thread python/ray/_private/worker.py Resolved
@Qiaolin-Yu
Qiaolin-Yu requested a review from richardliaw August 18, 2025 18:29
@jjyao
jjyao merged commit e9160b7 into ray-project:master Aug 18, 2025
dioptre pushed a commit to sourcetable/ray that referenced this pull request Aug 20, 2025
edoakes pushed a commit that referenced this pull request Aug 20, 2025
…ng (#55791)

Before this pr:
```
python /ray-workspace/temp.py
2025-08-18 18:26:21,621 INFO worker.py:1943 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265 
2025-08-18 18:26:53,873 WARNING worker.py:1991 -- Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
```

After this pr:
```
2025-08-20 19:08:41,923 INFO worker.py:1939 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265 
/sgl-workspace/ray/python/ray/_private/worker.py:1987: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
  warnings.warn(
```

#54928
jugalshah291 pushed a commit to jugalshah291/ray_fork that referenced this pull request Sep 11, 2025
jugalshah291 pushed a commit to jugalshah291/ray_fork that referenced this pull request Sep 11, 2025
…ng (ray-project#55791)

Before this pr:
```
python /ray-workspace/temp.py
2025-08-18 18:26:21,621 INFO worker.py:1943 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265
2025-08-18 18:26:53,873 WARNING worker.py:1991 -- Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
```

After this pr:
```
2025-08-20 19:08:41,923 INFO worker.py:1939 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265
/sgl-workspace/ray/python/ray/_private/worker.py:1987: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
  warnings.warn(
```

ray-project#54928

Signed-off-by: jugalshah291 <[email protected]>
dstrodtman pushed a commit that referenced this pull request Oct 6, 2025
dstrodtman pushed a commit to dstrodtman/ray that referenced this pull request Oct 6, 2025
…ng (ray-project#55791)

Before this pr:
```
python /ray-workspace/temp.py
2025-08-18 18:26:21,621 INFO worker.py:1943 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265
2025-08-18 18:26:53,873 WARNING worker.py:1991 -- Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
```

After this pr:
```
2025-08-20 19:08:41,923 INFO worker.py:1939 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265
/sgl-workspace/ray/python/ray/_private/worker.py:1987: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
  warnings.warn(
```

ray-project#54928

Signed-off-by: Douglas Strodtman <[email protected]>
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
landscapepainter pushed a commit to landscapepainter/ray that referenced this pull request Nov 17, 2025
…ng (ray-project#55791)

Before this pr:
```
python /ray-workspace/temp.py
2025-08-18 18:26:21,621 INFO worker.py:1943 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265 
2025-08-18 18:26:53,873 WARNING worker.py:1991 -- Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
```

After this pr:
```
2025-08-20 19:08:41,923 INFO worker.py:1939 -- Started a local Ray instance. View the dashboard at http://127.0.0.1:8265 
/sgl-workspace/ray/python/ray/_private/worker.py:1987: FutureWarning: Tip: In future versions of Ray, Ray will no longer override accelerator visible devices env var if num_gpus=0 or num_gpus=None (default). To enable this behavior and turn off this error message, set RAY_ACCEL_ENV_VAR_OVERRIDE_ON_ZERO=0
  warnings.warn(
```

ray-project#54928
edoakes added a commit that referenced this pull request Apr 13, 2026
#62492)

Following up on #54928 where we originally introduced a feature flag to
give users the option to not set CUDA_VISIBLE_DEVICES when num_gpus=0 or
None. We also output an error informing users that the default behavior
will be changed in a future ray version. Since it's been around 8 months
since we introduced this feature flag and the error is a bit
distracting, we're now setting this as the default behavior meaning we
will no longer override CUDA_VISIBLE_DEVICES when num_gpus = 0 or None.

---------

Signed-off-by: Joshua Lee <[email protected]>
Co-authored-by: Edward Oakes <[email protected]>
doanxem99 pushed a commit to doanxem99/ray that referenced this pull request Apr 15, 2026
ray-project#62492)

Following up on ray-project#54928 where we originally introduced a feature flag to
give users the option to not set CUDA_VISIBLE_DEVICES when num_gpus=0 or
None. We also output an error informing users that the default behavior
will be changed in a future ray version. Since it's been around 8 months
since we introduced this feature flag and the error is a bit
distracting, we're now setting this as the default behavior meaning we
will no longer override CUDA_VISIBLE_DEVICES when num_gpus = 0 or None.

---------

Signed-off-by: Joshua Lee <[email protected]>
Co-authored-by: Edward Oakes <[email protected]>
Signed-off-by: doanxem99 <[email protected]>
HLDKNotFound pushed a commit to chichic21039/ray that referenced this pull request Apr 22, 2026
ray-project#62492)

Following up on ray-project#54928 where we originally introduced a feature flag to
give users the option to not set CUDA_VISIBLE_DEVICES when num_gpus=0 or
None. We also output an error informing users that the default behavior
will be changed in a future ray version. Since it's been around 8 months
since we introduced this feature flag and the error is a bit
distracting, we're now setting this as the default behavior meaning we
will no longer override CUDA_VISIBLE_DEVICES when num_gpus = 0 or None.

---------

Signed-off-by: Joshua Lee <[email protected]>
Co-authored-by: Edward Oakes <[email protected]>
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
ray-project#62492)

Following up on ray-project#54928 where we originally introduced a feature flag to
give users the option to not set CUDA_VISIBLE_DEVICES when num_gpus=0 or
None. We also output an error informing users that the default behavior
will be changed in a future ray version. Since it's been around 8 months
since we introduced this feature flag and the error is a bit
distracting, we're now setting this as the default behavior meaning we
will no longer override CUDA_VISIBLE_DEVICES when num_gpus = 0 or None.

---------

Signed-off-by: Joshua Lee <[email protected]>
Co-authored-by: Edward Oakes <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] Not setting CUDA_VISIBLE_DEVICES in the case where num_gpus is not set

6 participants