Skip to content

armgit / txtai

Public Imported from GitHub
0

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

fix: address centering review feedback

morgan-codedce11b08ab18d
diff --git a/docs/embeddings/configuration/vectors.md b/docs/embeddings/configuration/vectors.md
index e9dd56f1..a2629ae0 100644
--- a/docs/embeddings/configuration/vectors.md
+++ b/docs/embeddings/configuration/vectors.md
@@ -151,7 +151,7 @@ center: boolean | dict
```
Centers normalized late-interaction token vectors and normalizes them again before MUVERA or LEMUR encoding. Centering is applied to
-both query and document vectors. `true` uses document scope and `false` disables centering. When omitted, centering defaults to document
+both query and document vectors. `true` uses batch scope and `false` disables centering. When omitted, centering defaults to batch
scope only for late-interaction models that load more than one `torch.nn.Linear` layer; models with zero or one loaded linear layer keep
their previous output.
@@ -164,7 +164,7 @@ center:
`document` subtracts the mean of each document or query independently. `batch` subtracts the mean of all real token rows in the current
model batch. `collection` requires a caller-provided, one-dimensional mean vector through either `mean` (an array-like value) or `path`
-(a NumPy `.npy` file), but not both. Zero-padding rows are never centered and remain zero.
+(a safetensors file containing a `center.mean` tensor), but not both. Zero-padding rows are never centered and remain zero.
### muvera
```yaml
diff --git a/src/python/txtai/models/pooling/late.py b/src/python/txtai/models/pooling/late.py
index b37f5420..00e8cfb0 100644
--- a/src/python/txtai/models/pooling/late.py
+++ b/src/python/txtai/models/pooling/late.py
@@ -179,12 +179,12 @@ class LatePooling(Pooling):
resolved token centering configuration
"""
- # Enable document centering by default for multi-linear models
+ # Enable batch centering by default for multi-linear models
if not configured:
center = sum(isinstance(module, torch.nn.Linear) for module in linear.modules()) > 1
if isinstance(center, bool):
- return {"scope": "document"} if center else None
+ return {"scope": "batch"} if center else None
if not isinstance(center, dict):
raise ValueError("center must be a boolean or dictionary")
@@ -206,7 +206,12 @@ class LatePooling(Pooling):
if (mean is None) == (path is None):
raise ValueError("collection center scope requires exactly one of mean or path")
- mean = np.load(path, allow_pickle=False) if path is not None else np.asarray(mean)
+ if path is not None:
+ with safetensors.safe_open(path, framework="np") as source:
+ mean = source.get_tensor("center.mean")
+ else:
+ mean = np.asarray(mean)
+
if mean.ndim != 1 or not np.isfinite(mean).all():
raise ValueError("collection center mean must be a finite one-dimensional array")
@@ -242,18 +247,16 @@ class LatePooling(Pooling):
means = [self.center["mean"]] * len(vectors)
for x, (value, mask) in enumerate(zip(vectors, masks)):
- if not mask.any():
- continue
-
- current = value[mask]
- average = current.mean(axis=0) if scope == "document" else means[x]
- if average is not None and average.shape != (current.shape[1],):
- raise ValueError("center mean dimension must match token vector dimension")
-
- current = current - average
- norms = np.linalg.norm(current, axis=1, keepdims=True)
- current = np.divide(current, norms, out=np.zeros_like(current), where=norms != 0)
- vectors[x][mask] = current
+ if mask.any():
+ current = value[mask]
+ average = current.mean(axis=0) if scope == "document" else means[x]
+ if average is not None and average.shape != (current.shape[1],):
+ raise ValueError("center mean dimension must match token vector dimension")
+
+ current = current - average
+ norms = np.linalg.norm(current, axis=1, keepdims=True)
+ current = np.divide(current, norms, out=np.zeros_like(current), where=norms != 0)
+ vectors[x][mask] = current
return np.asarray(vectors) if array else vectors
diff --git a/test/python/testmodels/testpooling.py b/test/python/testmodels/testpooling.py
index 0fd11e7a..bfbd35fe 100644
--- a/test/python/testmodels/testpooling.py
+++ b/test/python/testmodels/testpooling.py
@@ -10,6 +10,7 @@ import tempfile
import unittest
import numpy as np
+from safetensors.numpy import save_file
import torch
from txtai.models import Models, ClsPooling, LastPooling, LatePooling, Lemur, MeanPooling, PoolingFactory
@@ -124,10 +125,11 @@ class TestPooling(unittest.TestCase):
self.assertIsNone(LatePooling.centersettings(None, empty, False))
self.assertIsNone(LatePooling.centersettings(None, single, False))
- self.assertEqual(LatePooling.centersettings(None, multiple, False), {"scope": "document"})
+ self.assertEqual(LatePooling.centersettings(None, multiple, False), {"scope": "batch"})
self.assertIsNone(LatePooling.centersettings(False, multiple, True))
- self.assertEqual(LatePooling.centersettings(True, empty, True), {"scope": "document"})
+ self.assertEqual(LatePooling.centersettings(True, empty, True), {"scope": "batch"})
self.assertEqual(LatePooling.centersettings({"scope": "batch"}, empty, True), {"scope": "batch"})
+ self.assertEqual(LatePooling.centersettings({"scope": "document"}, empty, True), {"scope": "document"})
def testLateCenterSettings(self):
"""
@@ -140,8 +142,8 @@ class TestPooling(unittest.TestCase):
np.testing.assert_array_equal(settings["mean"], mean)
with tempfile.TemporaryDirectory() as output:
- path = os.path.join(output, "mean.npy")
- np.save(path, mean)
+ path = os.path.join(output, "mean.safetensors")
+ save_file({"center.mean": mean}, path)
settings = LatePooling.centersettings({"scope": "collection", "path": path}, linear, True)
np.testing.assert_array_equal(settings["mean"], mean)
@@ -149,7 +151,7 @@ class TestPooling(unittest.TestCase):
(None, "center must be a boolean or dictionary"),
({"scope": "invalid"}, "center scope must be one of"),
({"scope": "collection"}, "requires exactly one"),
- ({"scope": "collection", "mean": mean, "path": "mean.npy"}, "requires exactly one"),
+ ({"scope": "collection", "mean": mean, "path": "mean.safetensors"}, "requires exactly one"),
({"scope": "document", "mean": mean}, "only valid with collection scope"),
({"scope": "document", "invalid": True}, "unknown center setting"),
({"scope": "collection", "mean": [[0.0, 1.0]]}, "finite one-dimensional array"),
@@ -223,15 +225,17 @@ class TestPooling(unittest.TestCase):
centered = PoolingFactory.create({"path": "neuml/colbert-bert-tiny", "device": self.device, "modelargs": {"muvera": None, "center": True}})
texts = ["Short text.", "A considerably longer text exercises padding behavior."]
- separate = centered.encode(texts, batch=1, category="data")
+ self.assertEqual(centered.center, {"scope": "batch"})
together = centered.encode(texts, batch=2, category="data")
- np.testing.assert_allclose(separate, together, rtol=1e-5, atol=1e-6)
+ repeated = centered.encode(texts, batch=2, category="data")
+ np.testing.assert_array_equal(together, repeated)
+ self.assertEqual(centered.batches, [2])
self.assertTrue(np.any(np.all(together == 0.0, axis=2)))
- centered.center = {"scope": "batch"}
- batch = centered.encode(texts, batch=1, category="data")
- self.assertEqual(centered.batches, [1, 1])
- np.testing.assert_allclose(batch, separate, rtol=1e-5, atol=1e-6)
+ centered.center = {"scope": "document"}
+ separate = centered.encode(texts, batch=1, category="data")
+ document = centered.encode(texts, batch=2, category="data")
+ np.testing.assert_allclose(document, separate, rtol=1e-5, atol=1e-6)
def testLemur(self):
"""
@@ -286,6 +290,55 @@ class TestPooling(unittest.TestCase):
pooling = PoolingFactory.create({"path": model, "device": self.device})
self.assertEqual(pooling.encode(["test"], category="query").shape, (1, 10240))
+ def testLemurCenter(self):
+ """
+ Test late pooling with centered LEMUR fixed dimensional encoding
+ """
+
+ corpus = [
+ "Machine learning models retrieve relevant passages.",
+ "Late interaction compares token embeddings.",
+ "Dense indexes search fixed dimensional vectors.",
+ "A query encoder produces contextual token vectors.",
+ "Document encoders represent passages for retrieval.",
+ "Maximum similarity aggregates token matches.",
+ "LEMUR learns a corpus specific reduction.",
+ "MUVERA uses randomized fixed dimensional encodings.",
+ "The trainer stores reusable pooling artifacts.",
+ "New documents can be encoded after training.",
+ "Short text.",
+ "A considerably longer synthetic document exercises padding behavior.",
+ ] * 2
+ texts = ["Short text.", "A considerably longer synthetic document exercises padding behavior."]
+
+ for model in ["neuml/colbert-bert-tiny", "neuml/pylate-bert-tiny"]:
+ with tempfile.TemporaryDirectory() as output:
+ LemurTrainer()(
+ model,
+ corpus,
+ output,
+ gpu=False,
+ epochs=0,
+ final_hidden_dim=128,
+ train_subset_size=24,
+ learn_subset_size=256,
+ ols_sample_size=128,
+ seed=42,
+ )
+
+ pooling = PoolingFactory.create({"path": model, "device": self.device, "modelargs": {"lemur": {"path": output}, "center": True}})
+ self.assertEqual(pooling.center, {"scope": "batch"})
+
+ queries = pooling.encode(texts, batch=2, category="query")
+ documents = pooling.encode(texts, batch=2, category="data")
+ self.assertEqual(queries.shape, (2, 128))
+ self.assertEqual(documents.shape, (2, 128))
+ self.assertTrue(np.isfinite(queries).all())
+ self.assertTrue(np.isfinite(documents).all())
+
+ np.testing.assert_array_equal(pooling.encode(texts, batch=2, category="query"), queries)
+ np.testing.assert_array_equal(pooling.encode(texts, batch=2, category="data"), documents)
+
def testLemurRoundTrip(self):
"""
Test an ordinary LEMUR artifact save/load round-trip