💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
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.mdindex 6e5d06c6..dbc617d3 100644--- a/docs/embeddings/configuration/index.md+++ b/docs/embeddings/configuration/index.md@@ -71,6 +71,13 @@ transform: functionWhen 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```yamlbatch: intdiff --git a/src/python/txtai/models/models.py b/src/python/txtai/models/models.pyindex bbc8c878..18edfa86 100644--- a/src/python/txtai/models/models.py+++ b/src/python/txtai/models/models.py@@ -64,15 +64,20 @@ class Models:@staticmethoddef 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/instanceReturns: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 unavailableif 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))@staticmethoddef reference(deviceid):@@ -113,7 +118,9 @@ class Models:"""return (- "cpu"+ deviceid+ if isinstance(deviceid, str)+ else "cpu"if deviceid < 0else f"cuda:{deviceid}"if torch.cuda.is_available()diff --git a/test/python/testmodels.py b/test/python/testmodels.pyindex edffa4ae..e04d3638 100644--- a/test/python/testmodels.py+++ b/test/python/testmodels.py@@ -6,6 +6,8 @@ import unittestfrom 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 = Falseself.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"))