Skip to content

Commit

Permalink
Merge pull request #82 from ahoust17/master
Browse files Browse the repository at this point in the history
Compatibility with Apple Silicon GPUs
  • Loading branch information
ziatdinovmax committed Dec 11, 2023
2 parents 5b8e57c + 2d77fbd commit e884ba5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion atomai/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.7.8'
version = '0.7.9'
9 changes: 8 additions & 1 deletion atomai/trainers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ class BaseTrainer:
"""
def __init__(self):
set_train_rng(1)
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
if torch.backends.mps.is_available():

This comment has been minimized.

Copy link
@markcoletti

markcoletti Dec 14, 2023

Except this breaks things for others if they do not have a version of pytorch that supports this. :(

E.g., I'm now getting this error: module 'torch.backends' has no attribute 'mps'

This comment has been minimized.

Copy link
@ziatdinovmax

ziatdinovmax Dec 14, 2023

Author Collaborator

@markcoletti - thanks for brining this up. It should be an easy fix: we can either condition it on pytorch version or simply check if torch.backends has mps attribute:

if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
    # ...

@ahoust17 can you please add this fix to ensure backward compatibility?

self.device = torch.device("mps") # backend for Apple silicon GPUs
elif torch.cuda.is_available():
self.device = 'cuda'
else:
self.device = 'cpu'
self.net = None
self.criterion = None
self.optimizer = None
Expand Down Expand Up @@ -362,6 +367,8 @@ def print_statistics(self, e: int, **kwargs) -> None:
accuracy_metrics = "Accuracy"
if torch.cuda.is_available():
gpu_usage = gpu_usage_map(torch.cuda.current_device())
elif torch.backends.mps.is_available():
gpu_usage = gpu_usage_map(torch.mps.current_device())
else:
gpu_usage = ['N/A ', ' N/A']
if self.compute_accuracy:
Expand Down
5 changes: 5 additions & 0 deletions atomai/utils/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def load_weights(model: Type[torch.nn.Module],
torch.manual_seed(0)
if torch.cuda.device_count() > 0:
checkpoint = torch.load(weights_path)
elif torch.backends.mps.is_available():
checkpoint = torch.load(weights_path, map_location='mps')
else:
checkpoint = torch.load(weights_path, map_location='cpu')
model.load_state_dict(checkpoint)
Expand Down Expand Up @@ -199,6 +201,9 @@ def mock_forward(model: Type[torch.nn.Module],
x = torch.randn(1, *dims)
if next(model.parameters()).is_cuda:
x = x.cuda()
elif next(model.parameters()).is_mps:
mps_device = torch.device("mps")
x = x.to(mps_device)
out = model(x)
return out

Expand Down

0 comments on commit e884ba5

Please sign in to comment.