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 passing torch devices to embeddings and pipelines, closes #492

davidmezzettia833eeff5dd5
diff --git a/docs/embeddings/configuration/index.md b/docs/embeddings/configuration/index.md
index 6e5d06c6..dbc617d3 100644
--- a/docs/embeddings/configuration/index.md
+++ b/docs/embeddings/configuration/index.md
@@ -71,6 +71,13 @@ transform: function
When method is `external`, this function transforms input content into embeddings. The input to this function is a list of data. This method must return either a numpy array or list of numpy arrays.
+#### gpu
+```yaml
+gpu: boolean|int|string|device
+```
+
+Set the target device. Supports true/false, device id, device string and torch device instance. This is automatically derived if omitted.
+
## batch
```yaml
batch: int
diff --git a/src/python/txtai/models/models.py b/src/python/txtai/models/models.py
index bbc8c878..18edfa86 100644
--- a/src/python/txtai/models/models.py
+++ b/src/python/txtai/models/models.py
@@ -64,15 +64,20 @@ class Models:
@staticmethod
def deviceid(gpu):
"""
- Translates a gpu flag into a device id.
+ Translates input gpu argument into a device id.
Args:
- gpu: True/False if GPU should be enabled, also supports a GPU device id
+ gpu: True/False if GPU should be enabled, also supports a device id/string/instance
Returns:
device id
"""
+ # Return if this is already a torch device
+ # pylint: disable=E1101
+ if isinstance(gpu, torch.device):
+ return gpu
+
# Always return -1 if gpu is None or an accelerator device is unavailable
if gpu is None or not Models.hasaccelerator():
return -1
@@ -98,7 +103,7 @@ class Models:
# Torch device
# pylint: disable=E1101
- return torch.device(Models.reference(deviceid))
+ return deviceid if isinstance(deviceid, torch.device) else torch.device(Models.reference(deviceid))
@staticmethod
def reference(deviceid):
@@ -113,7 +118,9 @@ class Models:
"""
return (
- "cpu"
+ deviceid
+ if isinstance(deviceid, str)
+ else "cpu"
if deviceid < 0
else f"cuda:{deviceid}"
if torch.cuda.is_available()
diff --git a/test/python/testmodels.py b/test/python/testmodels.py
index edffa4ae..e04d3638 100644
--- a/test/python/testmodels.py
+++ b/test/python/testmodels.py
@@ -6,6 +6,8 @@ import unittest
from unittest.mock import patch
+import torch
+
from txtai.models import Models
@@ -26,8 +28,21 @@ class TestModels(unittest.TestCase):
self.assertEqual(Models.deviceid(0), 0)
self.assertEqual(Models.deviceid(1), 1)
+ # Test direct torch device
+ # pylint: disable=E1101
+ self.assertEqual(Models.deviceid(torch.device("cpu")), torch.device("cpu"))
+
cuda.return_value = False
self.assertEqual(Models.deviceid(True), -1)
self.assertEqual(Models.deviceid(False), -1)
self.assertEqual(Models.deviceid(0), -1)
self.assertEqual(Models.deviceid(1), -1)
+
+ def testDevice(self):
+ """
+ Tests the device method
+ """
+
+ # pylint: disable=E1101
+ self.assertEqual(Models.device("cpu"), torch.device("cpu"))
+ self.assertEqual(Models.device(torch.device("cpu")), torch.device("cpu"))