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 a datatype setting to the zvec backends (#1493)

PerryLink23d2d518bdd7
diff --git a/docs/embeddings/configuration/ann.md b/docs/embeddings/configuration/ann.md
index 899fc351..cba4e0f1 100644
--- a/docs/embeddings/configuration/ann.md
+++ b/docs/embeddings/configuration/ann.md
@@ -142,9 +142,10 @@ zvec:
efconstruction: ef_construction param for HnswIndexParam (int) - defaults to 200
m: number of HNSW links per element (int) - defaults to 50
efsearch: ef search param for HnswQueryParam (int) - defaults to 300
+ datatype: vector data type (fp32|fp16|int8) - defaults to fp32
```
-The [zvec](https://github.com/alibaba/zvec) backend is an embedded, path-based vector index.
+The [zvec](https://github.com/alibaba/zvec) backend is an embedded, path-based vector index. `datatype` sets the vector data type; `int8` stores integer vectors, so the embeddings must already be integers. The sparse backend (`zvecsparse`) accepts `fp32` (default) and `fp16`.
### pgvector
```yaml
diff --git a/src/python/txtai/ann/dense/zvec.py b/src/python/txtai/ann/dense/zvec.py
index 46a8041f..b4a964fe 100644
--- a/src/python/txtai/ann/dense/zvec.py
+++ b/src/python/txtai/ann/dense/zvec.py
@@ -146,11 +146,35 @@ class Zvec(ANN):
"""
Gets the vector data type.
+ The ``datatype`` setting selects it: ``fp32`` (default), ``fp16`` or ``int8``.
+ ``int8`` stores integer vectors, so the embeddings handed to this backend must
+ already be integers. ``fp64`` is not offered because zvec 0.8.0 rejects
+ ``VECTOR_FP64`` for dense vector fields.
+
Returns:
zvec vector data type
"""
- return zvec.DataType.VECTOR_FP32
+ types = self.datatypes()
+ datatype = self.setting("datatype", "fp32")
+ if datatype not in types:
+ raise ValueError(f"Unsupported zvec datatype: {datatype} - options are {', '.join(types)}")
+
+ return types[datatype]
+
+ def datatypes(self):
+ """
+ Gets the supported vector data types.
+
+ Returns:
+ dict of datatype setting to zvec vector data type
+ """
+
+ return {
+ "fp32": zvec.DataType.VECTOR_FP32,
+ "fp16": zvec.DataType.VECTOR_FP16,
+ "int8": zvec.DataType.VECTOR_INT8,
+ }
def prepare(self, data):
"""
diff --git a/src/python/txtai/ann/sparse/zvecsparse.py b/src/python/txtai/ann/sparse/zvecsparse.py
index b08bd79a..ed1a59ea 100644
--- a/src/python/txtai/ann/sparse/zvecsparse.py
+++ b/src/python/txtai/ann/sparse/zvecsparse.py
@@ -28,8 +28,11 @@ class ZvecSparse(Zvec):
return results
- def datatype(self):
- return zvec.DataType.SPARSE_VECTOR_FP32
+ def datatypes(self):
+ return {
+ "fp32": zvec.DataType.SPARSE_VECTOR_FP32,
+ "fp16": zvec.DataType.SPARSE_VECTOR_FP16,
+ }
def prepare(self, data):
return {int(index): float(value) for index, value in zip(data.indices, data.data)}
diff --git a/test/python/testann/testdense/testzvec.py b/test/python/testann/testdense/testzvec.py
index c27e687c..fa4bebeb 100644
--- a/test/python/testann/testdense/testzvec.py
+++ b/test/python/testann/testdense/testzvec.py
@@ -2,6 +2,8 @@
Zvec ANN module tests
"""
+import numpy as np
+
from txtai.ann import ANNFactory
from .base import DenseTest
@@ -31,3 +33,28 @@ class TestZvec(DenseTest):
with self.assertRaises(FileNotFoundError):
ann = ANNFactory.create({"backend": "zvec"})
ann.load("non-exist-path")
+
+ def testZvecDatatype(self):
+ """
+ Test zvec backend vector data type setting
+ """
+
+ # fp32 is the default, fp16 is a supported alternative
+ for datatype, expected in (("fp32", "VECTOR_FP32"), ("fp16", "VECTOR_FP16")):
+ self.runTests("zvec", {"zvec": {"datatype": datatype}}, update=False)
+
+ ann = ANNFactory.create({"backend": "zvec", "dimensions": 8, "zvec": {"datatype": datatype}})
+ self.assertEqual(ann.datatype().name, expected)
+
+ # int8 stores integer vectors
+ data = np.random.randint(0, 100, (100, 8)).astype(np.int8)
+ ann = ANNFactory.create({"backend": "zvec", "dimensions": data.shape[1], "zvec": {"datatype": "int8"}})
+ ann.index(data)
+
+ self.assertEqual(ann.count(), 100)
+ self.assertEqual(len(ann.search(data[:1], 1)[0]), 1)
+
+ # Unsupported values are rejected with the supported options
+ ann = ANNFactory.create({"backend": "zvec", "dimensions": 8, "zvec": {"datatype": "fp64"}})
+ with self.assertRaises(ValueError):
+ ann.index(np.random.rand(4, 8).astype(np.float32))
diff --git a/test/python/testann/testsparse/testzvecsparse.py b/test/python/testann/testsparse/testzvecsparse.py
index 7084f875..f98b0168 100644
--- a/test/python/testann/testsparse/testzvecsparse.py
+++ b/test/python/testann/testsparse/testzvecsparse.py
@@ -48,6 +48,29 @@ class TestZvecSparse(SparseTest):
self.assertEqual(ann.count(), 1)
self.assertEqual(ann.search(data, 1)[0][0][0], 1)
+ def testZvecSparseDatatype(self):
+ """
+ Test Sparse zvec backend vector data type setting
+ """
+
+ data = self.generate(1, 30522)
+
+ for datatype, expected in (("fp32", "SPARSE_VECTOR_FP32"), ("fp16", "SPARSE_VECTOR_FP16")):
+ ann = SparseANNFactory.create({"backend": "zvecsparse", "dimensions": 30522, "zvecsparse": {"datatype": datatype}})
+ self.assertEqual(ann.datatype().name, expected)
+
+ ann.index(data)
+
+ # fp16 rounds the stored values, so the exact-match score is ~1.0 rather than 1.0
+ matches = ann.search(data, 1)[0]
+ self.assertEqual(matches[0][0], 0)
+ self.assertGreater(matches[0][1], 0.99)
+
+ # Unsupported values are rejected with the supported options
+ ann = SparseANNFactory.create({"backend": "zvecsparse", "dimensions": 30522, "zvecsparse": {"datatype": "int8"}})
+ with self.assertRaises(ValueError):
+ ann.index(data)
+
# Close ANN
ann.close()