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

Support pytorch acceleration on M1 Mac hardware #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 13 additions & 3 deletions empanada_napari/finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,19 @@ def main(config):
main_worker(config)

def main_worker(config):
config['device'] = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

if str(config['device']) == 'cpu':
if torch.cuda.is_available():
device = torch.device("cuda:0")
elif torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
config['device'] = device

if torch.device("cuda:0"):
print("Using GPU for training.")
elif str(config['device']) == "mps":
print("Using M1 Mac hardware for training.")
elif str(config['device']) == 'cpu':
print(f"Using CPU for training.")
else:
print(f"Using GPU for training.")
Expand Down
19 changes: 14 additions & 5 deletions empanada_napari/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,13 @@ def __init__(
use_gpu=True,
use_quantized=False
):
# check whether GPU is available
device = torch.device('cuda:0' if torch.cuda.is_available() and use_gpu else 'cpu')
# check whether GPU or M1 Mac hardware is available
if torch.cuda.is_available() and use_gpu:
device = torch.device('cuda:0')
elif torch.backends.mps.is_available():
device = torch.device('mps')
else:
device = torch.device('cpu')
if use_quantized and str(device) == 'cpu' and model_config.get('model_quantized') is not None:
model_url = model_config['model_quantized']
else:
Expand Down Expand Up @@ -340,9 +345,13 @@ def __init__(
store_url=None,
save_panoptic=False
):
# check whether GPU is available
# check whether GPU is available
device = torch.device('cuda:0' if torch.cuda.is_available() and use_gpu else 'cpu')
# check whether GPU or M1 Mac hardware is available
if torch.cuda.is_available() and use_gpu:
device = torch.device('cuda:0')
elif torch.backends.mps.is_available():
device = torch.device('mps')
else:
device = torch.device('cpu')
if use_quantized and str(device) == 'cpu' and model_config.get('model_quantized') is not None:
model_url = model_config['model_quantized']
else:
Expand Down
17 changes: 14 additions & 3 deletions empanada_napari/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,20 @@ def main(config):
return main_worker(config)

def main_worker(config):
config['device'] = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

if str(config['device']) == 'cpu':
# check whether GPU or M1 Mac hardware is available
if torch.cuda.is_available() and use_gpu:
device = torch.device('cuda:0')
elif torch.backends.mps.is_available():
device = torch.device('mps')
else:
device = torch.device('cpu')
config['device'] = device

if str(config['device']) == 'cuda:0':
print("Using GPU for training.")
elif str(config['device']) == 'mps':
print("Using M1 Mac hardware for traiing.")
elif str(config['device']) == 'cpu':
print(f"Using CPU for training.")
else:
print(f"Using GPU for training.")
Expand Down