-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor device determination to function; add MPS fallback
- Loading branch information
Showing
10 changed files
with
54 additions
and
14 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
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
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 |
---|---|---|
@@ -1,9 +1,38 @@ | ||
import functools | ||
import gc | ||
|
||
import torch | ||
|
||
try: | ||
HAS_CUDA = torch.cuda.is_available() | ||
except Exception: | ||
HAS_CUDA = False | ||
|
||
try: | ||
HAS_MPS = torch.backends.mps.is_available() | ||
except Exception: | ||
HAS_MPS = False | ||
|
||
|
||
def clean_memory(): | ||
if torch.cuda.is_available(): | ||
torch.cuda.empty_cache() | ||
gc.collect() | ||
if HAS_CUDA: | ||
torch.cuda.empty_cache() | ||
if HAS_MPS: | ||
torch.mps.empty_cache() | ||
|
||
|
||
@functools.lru_cache(maxsize=None) | ||
def get_preferred_device() -> torch.device: | ||
try: | ||
import accelerate | ||
device = accelerate.Accelerator().device | ||
except Exception: | ||
if HAS_CUDA: | ||
device = torch.device("cuda") | ||
elif HAS_MPS: | ||
device = torch.device("mps") | ||
else: | ||
device = torch.device("cpu") | ||
print(f"get_preferred_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
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
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