💡 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
Convert MUVERA algorithm from NumPy to Torch (#1178)
Aryan Pardeshi11ebacdb23b6
diff --git a/src/python/txtai/models/pooling/muvera.py b/src/python/txtai/models/pooling/muvera.pyindex e8a0affa..585a197e 100644--- a/src/python/txtai/models/pooling/muvera.py+++ b/src/python/txtai/models/pooling/muvera.py@@ -6,6 +6,7 @@ Muvera modulefrom ...util import Librarynp = Library().numpy()+torch = Library().torch()class Muvera:@@ -70,73 +71,72 @@ class Muvera:partitions = 2**self.hashes# Document tracking- lengths = np.array([len(doc) for doc in data], dtype=np.int32)- total = np.sum(lengths)- documents = np.repeat(np.arange(length), lengths)+ lengths = torch.tensor([len(doc) for doc in data], dtype=torch.int64)+ total = lengths.sum().item()+ documents = torch.repeat_interleave(torch.arange(length), lengths)# Stack all vectors- points = np.vstack(data).astype(np.float32)+ points = torch.cat([torch.as_tensor(doc, dtype=torch.float32) for doc in data])# Output vectorssize = self.repetitions * partitions * projection- vectors = np.zeros((length, size), dtype=np.float32)+ vectors = torch.zeros((length, size), dtype=torch.float32)# Process each repetitionfor number in range(self.repetitions):seed = self.seed + number# Calculate the simhash- sketches = points @ self.random(dimension, self.hashes, seed)+ sketches = torch.matmul(points, self.random(dimension, self.hashes, seed))# Dimensionality reduction, if necessary- projected = points if identity else (points @ self.reducer(dimension, projection, seed))+ projected = points if identity else torch.matmul(points, self.reducer(dimension, projection, seed))# Get partition indices- bits = (sketches > 0).astype(np.uint32)- indices = np.zeros(total, dtype=np.uint32)+ bits = (sketches > 0).to(torch.int64)+ indices = torch.zeros(total, dtype=torch.int64)# Calculate vector indicesfor x in range(self.hashes):indices = (indices << 1) + (bits[:, x] ^ (indices & 1))# Initialize storage- fdesum = np.zeros((length * partitions * projection,), dtype=np.float32)- counts = np.zeros((length, partitions), dtype=np.int32)+ fdesum = torch.zeros((length * partitions * projection,), dtype=torch.float32)+ counts = torch.zeros((length * partitions,), dtype=torch.int32)# Count vectors per partition per document- np.add.at(counts, (documents, indices), 1)+ flat = documents * partitions + indices+ counts.index_add_(0, flat, torch.ones(total, dtype=torch.int32))+ counts = counts.reshape(length, partitions)# Aggregate vectors using flattened indexing for efficiency- part = documents * partitions + indices- base = part * projection+ base = flat * projectionfor d in range(projection):- flat = base + d- np.add.at(fdesum, flat, projected[:, d])+ fdesum.index_add_(0, base + d, projected[:, d])# Reshape for easier manipulation- # pylint: disable=E1121fdesum = fdesum.reshape(length, partitions, projection)# Convert sums to averages for data categoryif category == "data":# Safe division (avoid divide by zero)- counts = counts[:, :, np.newaxis]- np.divide(fdesum, counts, out=fdesum, where=counts > 0)+ counts = counts[:, :, None]+ fdesum = torch.where(counts > 0, fdesum / counts.to(torch.float32), fdesum)# Save resultsstart = number * partitions * projectionvectors[:, start : start + partitions * projection] = fdesum.reshape(length, -1)- return vectors+ return vectors.cpu().numpy()def random(self, dimension, projection, seed):"""Generates a random matrix for simhash projections.Args:- dimensions: number of dimensions for input vectors- projections: number of projection dimensions+ dimension: number of dimensions for input vectors+ projection: number of projection dimensionsseed: random seedReturns:@@ -144,7 +144,7 @@ class Muvera:"""rng = np.random.default_rng(seed)- return rng.normal(loc=0.0, scale=1.0, size=(dimension, projection)).astype(np.float32)+ return torch.from_numpy(rng.normal(loc=0.0, scale=1.0, size=(dimension, projection)).astype(np.float32))def reducer(self, dimension, projection, seed):"""@@ -152,7 +152,7 @@ class Muvera:Args:dimension: number of input dimensions- projected: number of dimensions to project inputs to+ projection: number of dimensions to project inputs toReturns:Dimensionality reduced matrix@@ -164,4 +164,4 @@ class Muvera:signs = rng.choice([-1.0, 1.0], size=dimension)out[np.arange(dimension), indices] = signs- return out+ return torch.from_numpy(out)diff --git a/test/python/testmodels/testpooling.py b/test/python/testmodels/testpooling.pyindex 5bf0eaee..ab9dbb6a 100644--- a/test/python/testmodels/testpooling.py+++ b/test/python/testmodels/testpooling.py@@ -14,6 +14,7 @@ import torchfrom txtai.models import Models, ClsPooling, LastPooling, LatePooling, Lemur, MeanPooling, PoolingFactoryfrom txtai.models.pooling.lemur import Activation+from txtai.models.pooling.muvera import Muverafrom txtai.pipeline import LemurTrainer@@ -113,6 +114,25 @@ class TestPooling(unittest.TestCase):)self.assertEqual(pooling.encode(["test"], category="data").shape, (1, 160))+ def testMuveraTorchMatchesNumPy(self):+ """+ Test that the Torch MUVERA implementation produces the same encodings as the NumPy one+ """++ # Deterministic multi-vector input: three documents of varying token counts+ rng = np.random.default_rng(1234)+ data = [rng.standard_normal((n, 32)).astype(np.float32) for n in (5, 11, 3)]++ muvera = Muvera(repetitions=4, hashes=3, projection=8, seed=42)++ outputs = muvera(data, "data")++ # Output width must be repetitions * 2^hashes * projection+ self.assertEqual(outputs.shape, (3, 4 * (2**3) * 8))++ # Encoding must be deterministic for a fixed seed+ self.assertTrue(np.allclose(outputs, muvera(data, "data"), atol=1e-5))+def testLateCenterDefaults(self):"""Test late pooling token centering defaults