Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add support for alternative acceleration devices, closes #489

davidmezzettie2c9ca46992d
diff --git a/setup.py b/setup.py
index d752be80..4bd34cba 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ with open("README.md", "r", encoding="utf-8") as f:
DESCRIPTION = "".join([line for line in f if "gh-dark-mode-only" not in line])
# Required dependencies
-install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "pyyaml>=5.3", "torch>=1.6.0", "transformers>=4.22.0"]
+install = ["faiss-cpu>=1.7.1.post2", "numpy>=1.18.4", "pyyaml>=5.3", "torch>=1.13.0", "transformers>=4.22.0"]
# Optional dependencies
extras = {}
diff --git a/src/python/txtai/models/models.py b/src/python/txtai/models/models.py
index c43a772c..bbc8c878 100644
--- a/src/python/txtai/models/models.py
+++ b/src/python/txtai/models/models.py
@@ -73,8 +73,8 @@ class Models:
device id
"""
- # Always return -1 if gpu is None or CUDA is unavailable
- if gpu is None or not torch.cuda.is_available():
+ # Always return -1 if gpu is None or an accelerator device is unavailable
+ if gpu is None or not Models.hasaccelerator():
return -1
# Default to device 0 if gpu is True and not otherwise specified
@@ -112,7 +112,37 @@ class Models:
device reference
"""
- return "cpu" if deviceid < 0 else f"cuda:{deviceid}"
+ return (
+ "cpu"
+ if deviceid < 0
+ else f"cuda:{deviceid}"
+ if torch.cuda.is_available()
+ else "mps"
+ if torch.backends.mps.is_available()
+ else Models.finddevice()
+ )
+
+ @staticmethod
+ def hasaccelerator():
+ """
+ Checks if there is an accelerator device available.
+
+ Returns:
+ True if an accelerator device is available, False otherwise
+ """
+
+ return torch.cuda.is_available() or torch.backends.mps.is_available() or bool(Models.finddevice())
+
+ @staticmethod
+ def finddevice():
+ """
+ Attempts to find an alternative accelerator device.
+
+ Returns:
+ name of first alternative accelerator available or None if not found
+ """
+
+ return next((device for device in ["xpu"] if hasattr(torch, device) and getattr(torch, device).is_available()), None)
@staticmethod
def load(path, config=None, task="default"):