Skip to content
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

Compatibility with Apple Silicon GPUs #82

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.8.MAC'
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():
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
Loading