Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Find appended best matches when IVFSparse probes only some blocks

morgan-coded3e82a68cb4c9
diff --git a/src/python/txtai/ann/sparse/ivfsparse.py b/src/python/txtai/ann/sparse/ivfsparse.py
index bbe4eb2a..06571b13 100644
--- a/src/python/txtai/ann/sparse/ivfsparse.py
+++ b/src/python/txtai/ann/sparse/ivfsparse.py
@@ -109,13 +109,27 @@ class IVFSparse(ANN):
offset = self.size()
# Sort into clusters and merge
- for cluster, ids in self.aggregate(embeddings).items():
+ clusters = self.aggregate(embeddings)
+ for cluster, ids in clusters.items():
# Add new ids
self.ids[cluster].extend([x + offset for x in ids])
# Add new data
self.blocks[cluster] = vstack([self.blocks[cluster], embeddings[ids]])
+ # Update block max summary vectors for clusters with new data
+ if self.centroids is not None:
+ rows, start = [], 0
+ for cluster, ids in sorted(clusters.items()):
+ if start < cluster:
+ rows.append(self.centroids[start:cluster])
+ maximum = csr_matrix(embeddings[ids].max(axis=0))
+ rows.append(self.centroids[cluster].maximum(maximum))
+ start = cluster + 1
+ if start < self.centroids.shape[0]:
+ rows.append(self.centroids[start:])
+ self.centroids = vstack(rows, format="csr")
+
# Update id offset and index metadata
self.config["offset"] += embeddings.shape[0]
self.metadata()
diff --git a/test/python/testann/testsparse/testivfsparse.py b/test/python/testann/testsparse/testivfsparse.py
index 6e247e42..9dacc110 100644
--- a/test/python/testann/testsparse/testivfsparse.py
+++ b/test/python/testann/testsparse/testivfsparse.py
@@ -192,3 +192,28 @@ class TestIVFSparse(SparseTest):
self.assertEqual(results, [(0, 1.0), (2, 0.5), (1, 0.0)])
ann.close()
+
+ def testIVFSparseAppendMaxima(self):
+ """
+ Test appended rows update block maxima before query probing
+ """
+
+ # Build two distinct blocks and preserve their initial summaries
+ ann = SparseANNFactory.create({"backend": "ivfsparse", "ivfsparse": {"nlist": 2, "nprobe": 1}})
+ ann.index(csr_matrix([[1, 0, 0]] * 40 + [[0, 1, 0]] * 40, dtype=np.float64))
+ before = ann.centroids.copy()
+
+ # Append a row whose maximum differs from the full block maximum
+ ann.append(csr_matrix([[0.5, 0, 2]], dtype=np.float64))
+ self.assertEqual(ann.search(csr_matrix([[0, 0.1, 1]], dtype=np.float64), 1)[0], [(80, 2.0)])
+ cluster = next(key for key, ids in ann.ids.items() if 80 in ids)
+ np.testing.assert_array_equal(ann.centroids[cluster].toarray(), [[1, 0, 2]])
+ np.testing.assert_array_equal(ann.centroids[1 - cluster].toarray(), before[1 - cluster].toarray())
+
+ # Touch the other block while retaining the previous maximum
+ ann.append(csr_matrix([[0, 2, 0]], dtype=np.float64))
+ for key, block in ann.blocks.items():
+ np.testing.assert_array_equal(ann.centroids[key].toarray(), block.max(axis=0).toarray())
+ self.assertEqual(ann.centroids.format, "csr")
+ self.assertEqual(ann.centroids.shape, (2, 3))
+ ann.close()