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

Add mps support. #75

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Empty file added lang_sam/models/__init__.py
Empty file.
9 changes: 7 additions & 2 deletions lang_sam/models/gdino.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import torch
from PIL import Image
from transformers import AutoModelForZeroShotObjectDetection, AutoProcessor
from lang_sam.models.utils import get_device_type

device_type = get_device_type()
DEVICE = torch.device(device_type)

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
if torch.cuda.get_device_properties(0).major >= 8:
Expand All @@ -18,7 +21,9 @@ def __init__(self):
def build_model(self, ckpt_path: str | None = None):
model_id = "IDEA-Research/grounding-dino-base"
self.processor = AutoProcessor.from_pretrained(model_id)
self.model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(DEVICE)
self.model = AutoModelForZeroShotObjectDetection.from_pretrained(model_id).to(
luca-medeiros marked this conversation as resolved.
Show resolved Hide resolved
DEVICE
)

def predict(
self,
Expand Down
12 changes: 12 additions & 0 deletions lang_sam/models/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import logging
import torch


def get_device_type() -> str:
if torch.backends.mps.is_available():
return "mps"
elif torch.cuda.is_available():
return "cuda"
else:
logging.warning("No GPU found, using CPU instead")
return "cpu"