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

feat: add apple silicon GPU acceleration #6151

Merged
merged 24 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions e2e/modeling/test_dpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ def test_dpr_processor_save_load_non_bert_tokenizer(tmp_path: Path, query_and_pa

if torch.cuda.is_available():
device = torch.device("cuda")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
model = BiAdaptiveModel(
Expand Down Expand Up @@ -753,6 +755,8 @@ def test_dpr_processor_save_load_non_bert_tokenizer(tmp_path: Path, query_and_pa

if torch.cuda.is_available():
device = torch.device("cuda")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
loaded_model = BiAdaptiveModel(
Expand Down Expand Up @@ -879,6 +883,8 @@ def test_dpr_processor_save_load_non_bert_tokenizer(tmp_path: Path, query_and_pa

if torch.cuda.is_available():
device = torch.device("cuda")
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
device = torch.device("mps")
else:
device = torch.device("cpu")
model = BiAdaptiveModel(
Expand Down
3 changes: 2 additions & 1 deletion haystack/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ def collect_static_system_specs() -> Dict[str, Any]:
except ImportError:
specs["libraries.transformers"] = False

has_mps = hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
NripeshN marked this conversation as resolved.
Show resolved Hide resolved
try:
torch_import.check()
specs.update(
{
"libraries.torch": torch.__version__,
"libraries.cuda": torch.version.cuda if torch.cuda.is_available() else False,
"hardware.gpus": torch.cuda.device_count() if torch.cuda.is_available() else 0,
"hardware.gpus": torch.cuda.device_count() if torch.cuda.is_available() else 1 if has_mps else 0,
}
)
except ImportError:
Expand Down
3 changes: 3 additions & 0 deletions haystack/modeling/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def initialize_device_settings(
else:
devices_to_use = [torch.device("cuda:0")]
n_gpu = 1
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
devices_to_use = torch.device("mps")
n_gpu = 1
else:
devices_to_use = [torch.device("cpu")]
n_gpu = 0
Expand Down
2 changes: 2 additions & 0 deletions haystack/preview/components/readers/extractive.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def warm_up(self):
if self.model is None:
if torch.cuda.is_available():
self.device = self.device or "cuda:0"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
self.device = self.device or "mps:0"
else:
self.device = self.device or "cpu:0"
self.model = AutoModelForQuestionAnswering.from_pretrained(self.model_name_or_path, token=self.token).to(
Expand Down
3 changes: 2 additions & 1 deletion haystack/utils/experiment_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def get_or_create_env_meta_data() -> Dict[str, Any]:
from haystack.telemetry import HAYSTACK_EXECUTION_CONTEXT

global env_meta_data # pylint: disable=global-statement
has_mps = hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
if not env_meta_data:
env_meta_data = {
"os_version": platform.release(),
Expand All @@ -246,7 +247,7 @@ def get_or_create_env_meta_data() -> Dict[str, Any]:
"transformers_version": transformers.__version__,
"torch_version": torch.__version__,
"torch_cuda_version": torch.version.cuda if torch.cuda.is_available() else 0,
"n_gpu": torch.cuda.device_count() if torch.cuda.is_available() else 0,
"n_gpu": torch.cuda.device_count() if torch.cuda.is_available() else 1 if has_mps else 0,
"n_cpu": os.cpu_count(),
"context": os.environ.get(HAYSTACK_EXECUTION_CONTEXT),
"execution_env": _get_execution_environment(),
Expand Down
2 changes: 2 additions & 0 deletions haystack/utils/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ def get_devices(devices: Optional[List[Union[str, torch.device]]]) -> List[torch
return [torch.device(device) for device in devices]
elif torch.cuda.is_available():
return [torch.device(device) for device in range(torch.cuda.device_count())]
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return [torch.device("mps")]
return [torch.device("cpu")]