-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh: Add device selection mechanism (#60)
* enh: Add device selection mechanism * Fix bug in util * remove prefix in patch
- Loading branch information
1 parent
78fce2e
commit bf12b49
Showing
3 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from logging import getLogger | ||
|
||
import torch | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
def select_optimal_device(device: str | None) -> str: | ||
""" | ||
Guess what your optimal device should be based on backend availability. | ||
If you pass a device, we just pass it through. | ||
:param device: The device to use. If this is not None you get back what you passed. | ||
:return: The selected device. | ||
""" | ||
if device is None: | ||
if torch.cuda.is_available(): | ||
device = "cuda" | ||
elif torch.backends.mps.is_available(): | ||
device = "mps" | ||
else: | ||
device = "cpu" | ||
logger.info(f"Automatically selected device: {device}") | ||
|
||
return device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters