-
-
Notifications
You must be signed in to change notification settings - Fork 17.3k
Update torch_utils.py #13682
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
base: master
Are you sure you want to change the base?
Update torch_utils.py #13682
Conversation
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]>
|
All Contributors have signed the CLA. ✅ |
|
👋 Hello @surely-you-cannot-be-sirius, thank you for submitting a To ensure a seamless integration of your work, please review the following checklist:
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! 🚀 |
UltralyticsAssistant
left a comment
There was a problem hiding this 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
| if cpu or mps: | ||
| os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # force torch.cuda.is_available() = False |
There was a problem hiding this comment.
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.
|
recheck |
|
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]>
|
I have read the CLA Document and I sign the CLA |
|
recheck |
|
Thanks for the updates and for signing the CLA, @surely-you-cannot-be-sirius; the change to avoid forcing |
Removed setting the environment variable
CUDA_VISIBLE_DEVICESto -1 when cpu is requested, as this sets a system-wide environment variable that causes issues with later calls totorch.cuda.is_available()anddevice_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 0This function is not called again when
cpuormpsis 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_devicebehavior by no longer forcingCUDA_VISIBLE_DEVICES=-1when using CPU or MPS, allowing more flexible CUDA visibility handling.📊 Key Changes
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'whendeviceiscpuormps.CUDA_VISIBLE_DEVICESis still set to the requested GPU indices.🎯 Purpose & Impact