Skip to content

Conversation

@surely-you-cannot-be-sirius
Copy link

@surely-you-cannot-be-sirius surely-you-cannot-be-sirius commented Nov 14, 2025

Removed setting the environment variable CUDA_VISIBLE_DEVICES to -1 when cpu is requested, as this sets a system-wide environment variable that causes issues with later calls to torch.cuda.is_available() and device_count().

E.g. I noticed this issue when running some model loading tests to different devices - if I set the device to 'cpu' first, even when this model goes out of scope the environment variable still persists so on a later test, when I checked to see that the cuda device was available then torch.cuda.device_count() is 0

This function is not called again when cpu or mps is set so will not cause errors later on in the function - there is no need to set this envvar.

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Refines select_device behavior by no longer forcing CUDA_VISIBLE_DEVICES=-1 when using CPU or MPS, allowing more flexible CUDA visibility handling.

📊 Key Changes

  • Removed the logic that set os.environ['CUDA_VISIBLE_DEVICES'] = '-1' when device is cpu or mps.
  • Retained existing behavior for explicit non-CPU devices, where CUDA_VISIBLE_DEVICES is still set to the requested GPU indices.

🎯 Purpose & Impact

  • 🧠 Allows users to select CPU or MPS without globally disabling CUDA visibility via environment variables.
  • 🧪 Reduces side effects for mixed environments (e.g., systems with both CUDA and MPS) where CUDA availability checks are still desired.
  • ⚙️ Makes device selection behavior more transparent and predictable for downstream tools and scripts.

Removed setting the environment variable CUDA_VISIBLE_DEVICES to -1 when cpu is requested, as this sets a system-wide environment variable that causes issues with later calls to torch.cuda.is_available().

This function is not called again when cpu or mps is set so will not cause errors later on - there is no need to set this envvar

Signed-off-by: James Heaton SI <[email protected]>
@github-actions
Copy link
Contributor

github-actions bot commented Nov 14, 2025

All Contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@UltralyticsAssistant UltralyticsAssistant added bug Something isn't working python Pull requests that update python code labels Nov 14, 2025
@UltralyticsAssistant
Copy link
Member

👋 Hello @surely-you-cannot-be-sirius, thank you for submitting a ultralytics/yolov5 🚀 PR! This is an automated review assistant, and a human engineer will be by shortly to help further.

To ensure a seamless integration of your work, please review the following checklist:

  • Define a Purpose: Clearly explain the purpose of your fix or feature in your PR description, and link to any relevant issues. Ensure your commit messages are clear, concise, and adhere to the project's conventions.
  • Synchronize with Source: Confirm your PR is synchronized with the ultralytics/yolov5 main branch. If it's behind, update it by clicking the 'Update branch' button or by running git pull and git merge main locally.
  • Ensure CI Checks Pass: Verify all Ultralytics Continuous Integration (CI) checks are passing. If any checks fail, please address the issues.
  • Update Documentation: Update the relevant documentation for any new or modified features.
  • Add Tests: If applicable, include or update tests to cover your changes, and confirm that all tests are passing.
  • Sign the CLA: Please ensure you have signed our Contributor License Agreement if this is your first Ultralytics PR by writing "I have read the CLA Document and I sign the CLA" in a new message.
  • Minimize Changes: Limit your changes to the minimum necessary for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." — Bruce Lee

For more guidance, please refer to our Contributing Guide. Don't hesitate to leave a comment if you have any questions. Thank you for contributing to Ultralytics! 🚀

Copy link
Member

@UltralyticsAssistant UltralyticsAssistant left a comment

Choose a reason for hiding this comment

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

🔍 PR Review

Made with ❤️ by Ultralytics Actions

The change simplifies select_device by no longer forcing CUDA_VISIBLE_DEVICES='-1' when cpu or mps is selected, but it also changes the function’s behavioral contract. Please verify that no callers depend on select_device('cpu'/'mps') disabling CUDA, and update documentation/tests to reflect the new behavior so that downstream expectations remain clear.

💬 Posted 1 inline comment

Comment on lines -118 to -119
if cpu or mps:
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # force torch.cuda.is_available() = False

Choose a reason for hiding this comment

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

💡 MEDIUM: Removing the CUDA_VISIBLE_DEVICES='-1' assignment for cpu/mps makes select_device('cpu') and select_device('mps') no longer guarantee that CUDA is effectively disabled for the current process. Any existing callers that relied on select_device('cpu') to force torch.cuda.is_available() to be False (or to prevent accidental CUDA usage by downstream libs) will now see different behavior. It would be good to double‑check call sites, update any docstrings/comments that describe this behavior, and consider adding a small test to lock in the new contract so this change is intentional and well‑documented.

@surely-you-cannot-be-sirius
Copy link
Author

recheck

@surely-you-cannot-be-sirius
Copy link
Author

Yeah I have missed something here, I will make some changes

Doesn't check if cuda is available when non-cpu device requested

Signed-off-by: James Heaton SI <[email protected]>
@surely-you-cannot-be-sirius
Copy link
Author

I have read the CLA Document and I sign the CLA

@surely-you-cannot-be-sirius
Copy link
Author

recheck

@pderrenger
Copy link
Member

Thanks for the updates and for signing the CLA, @surely-you-cannot-be-sirius; the change to avoid forcing CUDA_VISIBLE_DEVICES=-1 for CPU/MPS makes sense and should prevent the stale CUDA visibility you observed. Please confirm you’ve tested switching devices within a single process (CPU → CUDA and back) and behavior when CUDA_VISIBLE_DEVICES is already set externally; once you’ve verified that and CI is green, this looks good to move forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants