💡 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
Handle IVFSparse searches with mixed float dtypes (#1484)
Morgan Carr73a7706d2c00
diff --git a/src/python/txtai/ann/sparse/ivfsparse.py b/src/python/txtai/ann/sparse/ivfsparse.pyindex b3e9b53a..bbe4eb2a 100644--- a/src/python/txtai/ann/sparse/ivfsparse.py+++ b/src/python/txtai/ann/sparse/ivfsparse.py@@ -57,6 +57,9 @@ class IVFSparse(ANN):self.deletes = Nonedef index(self, embeddings):+ # Store data as float32+ embeddings = embeddings.astype(np.float32, copy=False)+# Compute model training sizetrain, sample = embeddings, self.setting("sample")if sample:@@ -99,6 +102,9 @@ class IVFSparse(ANN):self.metadata({"clusters": len(self.blocks)})def append(self, embeddings):+ # Store data as float32+ embeddings = embeddings.astype(np.float32, copy=False)+# Get offsetoffset = self.size()@@ -123,6 +129,9 @@ class IVFSparse(ANN):self.deletes.append(int(x))def search(self, queries, limit):+ # Search with float32 queries+ queries = queries.astype(np.float32, copy=False)+results = []# Calculate number of threads using a thread batch size of 32@@ -154,7 +163,7 @@ class IVFSparse(ANN):header = next(unpacker)# Read cluster centroids, if available- self.centroids = SparseArray().load(f) if header["centroids"] else None+ self.centroids = SparseArray().load(f).astype(np.float32, copy=False) if header["centroids"] else None# Read cluster idsself.ids = dict(next(unpacker))@@ -162,7 +171,7 @@ class IVFSparse(ANN):# Read cluster data blocksself.blocks = {}for key in self.ids:- self.blocks[key] = SparseArray().load(f)+ self.blocks[key] = SparseArray().load(f).astype(np.float32, copy=False)# Read deletesself.deletes = next(unpacker)diff --git a/test/python/testann/testsparse/testivfsparse.py b/test/python/testann/testsparse/testivfsparse.pyindex ec0879f7..6e247e42 100644--- a/test/python/testann/testsparse/testivfsparse.py+++ b/test/python/testann/testsparse/testivfsparse.py@@ -9,6 +9,7 @@ from unittest.mock import patchimport numpy as np+from scipy.sparse import csr_matrixfrom sklearn.cluster import MiniBatchKMeansfrom txtai.ann import SparseANNFactory@@ -174,3 +175,20 @@ class TestIVFSparse(SparseTest):ann.close()self.assertEqual(features, [100, 10])++ def testIVFSparseMixedDtypes(self):+ """+ Test float32 and float64 queries after appending float64 rows+ """++ # Index float32 rows and append a float64 row+ ann = SparseANNFactory.create({"backend": "ivfsparse"})+ ann.index(csr_matrix([[1, 0], [0, 1]], dtype=np.float32))+ ann.append(csr_matrix([[0.5, 0.5]], dtype=np.float64))++ # Validate scores for both query types without losing the existing rows+ for dtype in [np.float32, np.float64]:+ results = ann.search(csr_matrix([[1, 0]], dtype=dtype), 3)[0]+ self.assertEqual(results, [(0, 1.0), (2, 0.5), (1, 0.0)])++ ann.close()