Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Cleanup of Pooling PRs

davidmezzetti47c05994bf01
diff --git a/docs/embeddings/configuration/vectors.md b/docs/embeddings/configuration/vectors.md
index 0c945b51..a5b4c72e 100644
--- a/docs/embeddings/configuration/vectors.md
+++ b/docs/embeddings/configuration/vectors.md
@@ -25,8 +25,7 @@ Embeddings method to use. If the method is not provided, it is inferred using th
Builds embeddings using a transformers model. While this can be any transformers model, it works best with
[models trained](https://huggingface.co/models?pipeline_tag=sentence-similarity) to build embeddings.
-`mean`, `cls`, `max`, `last` and `late` pooling are supported and automatically inferred from the model. The pooling method can be overwritten by changing the method
-from `transformers` to `meanpooling`, `clspooling`, `maxpooling`, `lastpooling` or `latepooling` respectively.
+`mean`, `cls`, `last`, `late` and `max` pooling are supported and automatically inferred from the model. The pooling method can be overwritten by changing the method from `transformers` to `meanpooling`, `clspooling`, `lastpooling`, `latepooling` or `maxpooling` respectively.
Setting `maxlength` to `True` enables truncating inputs to the `max_seq_length`. Setting `maxlength` to an integer will truncate inputs to that value. When omitted (default), the `maxlength` will be set to either the model or tokenizer maxlength.
diff --git a/src/python/txtai/models/pooling/factory.py b/src/python/txtai/models/pooling/factory.py
index df127d50..6b2a32dd 100644
--- a/src/python/txtai/models/pooling/factory.py
+++ b/src/python/txtai/models/pooling/factory.py
@@ -45,17 +45,13 @@ class PoolingFactory:
return Pooling(path, device, tokenizer, maxlength, loadprompts, modelargs)
# Derive pooling method if it's not specified and path is a string
- if (not method or method not in ("clspooling", "meanpooling", "maxpooling", "lastpooling", "latepooling")) and isinstance(path, str):
+ if (not method or method not in ("clspooling", "lastpooling", "latepooling", "maxpooling", "meanpooling")) and isinstance(path, str):
method = PoolingFactory.method(path)
# Check for cls pooling
if method == "clspooling":
return ClsPooling(path, device, tokenizer, maxlength, loadprompts, modelargs)
- # Check for max pooling
- if method == "maxpooling":
- return MaxPooling(path, device, tokenizer, maxlength, loadprompts, modelargs)
-
# Check for last pooling
if method == "lastpooling":
return LastPooling(path, device, tokenizer, maxlength, loadprompts, modelargs)
@@ -64,6 +60,10 @@ class PoolingFactory:
if method == "latepooling":
return LatePooling(path, device, tokenizer, maxlength, loadprompts, modelargs)
+ # Check for max pooling
+ if method == "maxpooling":
+ return MaxPooling(path, device, tokenizer, maxlength, loadprompts, modelargs)
+
# Default to mean pooling
return MeanPooling(path, device, tokenizer, maxlength, loadprompts, modelargs)
@@ -89,14 +89,14 @@ class PoolingFactory:
if config and config.get("pooling_mode_cls_token") and not config.get("pooling_mode_mean_tokens"):
method = "clspooling"
- # Set to max pooling if it's enabled and mean pooling is disabled
- if config and config.get("pooling_mode_max_tokens") and not config["pooling_mode_mean_tokens"]:
- method = "maxpooling"
-
# Set to last token pooling if it's enabled and mean pooling is disabled
if config and config.get("pooling_mode_lasttoken") and not config.get("pooling_mode_mean_tokens"):
method = "lastpooling"
+ # Set to max pooling if it's enabled and mean pooling is disabled
+ if config and config.get("pooling_mode_max_tokens") and not config.get("pooling_mode_mean_tokens"):
+ method = "maxpooling"
+
# Check for late interaction pooling
if not config:
# Load 1_Dense/config.json
diff --git a/src/python/txtai/vectors/dense/huggingface.py b/src/python/txtai/vectors/dense/huggingface.py
index 05fa1066..8d9b4c37 100644
--- a/src/python/txtai/vectors/dense/huggingface.py
+++ b/src/python/txtai/vectors/dense/huggingface.py
@@ -24,7 +24,7 @@ class HFVectors(Vectors):
True if this is a local transformers-based model, False otherwise
"""
- return method in ("transformers", "pooling", "clspooling", "meanpooling", "maxpooling")
+ return method in ("transformers", "pooling", "clspooling", "maxpooling", "meanpooling")
def loadmodel(self, path):
# Build embeddings with transformers pooling
diff --git a/test/python/testmodels/testpooling.py b/test/python/testmodels/testpooling.py
index efd00ec9..3662f706 100644
--- a/test/python/testmodels/testpooling.py
+++ b/test/python/testmodels/testpooling.py
@@ -48,22 +48,6 @@ class TestPooling(unittest.TestCase):
# Test CLS pooling encoding
self.assertEqual(pooling.encode(["test"])[0].shape, (768,))
- def testMethodPartialConfig(self):
- """
- Test resolving a pooling method from a config that omits pooling_mode_mean_tokens
- """
-
- # Each flag on its own must resolve, rather than raising on the missing mean tokens key
- with patch.object(PoolingFactory, "load", return_value={"pooling_mode_cls_token": True}):
- self.assertEqual(PoolingFactory.method("any/model"), "clspooling")
-
- with patch.object(PoolingFactory, "load", return_value={"pooling_mode_lasttoken": True}):
- self.assertEqual(PoolingFactory.method("any/model"), "lastpooling")
-
- # Mean pooling still wins when it is explicitly enabled
- with patch.object(PoolingFactory, "load", return_value={"pooling_mode_cls_token": True, "pooling_mode_mean_tokens": True}):
- self.assertEqual(PoolingFactory.method("any/model"), "meanpooling")
-
def testLast(self):
"""
Test last pooling
@@ -436,6 +420,25 @@ class TestPooling(unittest.TestCase):
)
self.assertEqual(type(pooling), MeanPooling)
+ def testMethodPartialConfig(self):
+ """
+ Test pooling config files that omit pooling_mode_mean_tokens
+ """
+
+ # A missing mean flag reads as disabled, same as an explicit false, instead of raising KeyError
+ tests = [
+ ({"pooling_mode_cls_token": True}, "clspooling"),
+ ({"pooling_mode_cls_token": True, "pooling_mode_mean_tokens": False}, "clspooling"),
+ ({"pooling_mode_lasttoken": True}, "lastpooling"),
+ ({"pooling_mode_lasttoken": True, "pooling_mode_mean_tokens": False}, "lastpooling"),
+ ({"pooling_mode_cls_token": True, "pooling_mode_mean_tokens": True}, "meanpooling"),
+ ({}, "meanpooling"),
+ ]
+
+ for config, expected in tests:
+ with patch.object(PoolingFactory, "load", return_value=config):
+ self.assertEqual(PoolingFactory.method("sentence-transformers/nli-mpnet-base-v2"), expected)
+
def testMuvera(self):
"""
Test late pooling with MUVERA fixed dimensional encoding