Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Resolve pooling method from a config missing pooling_mode_mean_tokens

Kole Harvey8f3735101b6e
diff --git a/src/python/txtai/models/pooling/factory.py b/src/python/txtai/models/pooling/factory.py
index 3e4d800f..df127d50 100644
--- a/src/python/txtai/models/pooling/factory.py
+++ b/src/python/txtai/models/pooling/factory.py
@@ -86,7 +86,7 @@ class PoolingFactory:
config = PoolingFactory.load(path, "1_Pooling/config.json")
# Set to CLS pooling if it's enabled and mean pooling is disabled
- if config and config.get("pooling_mode_cls_token") and not config["pooling_mode_mean_tokens"]:
+ 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
@@ -94,7 +94,7 @@ class PoolingFactory:
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["pooling_mode_mean_tokens"]:
+ if config and config.get("pooling_mode_lasttoken") and not config.get("pooling_mode_mean_tokens"):
method = "lastpooling"
# Check for late interaction pooling
diff --git a/test/python/testmodels/testpooling.py b/test/python/testmodels/testpooling.py
index 992d6374..efd00ec9 100644
--- a/test/python/testmodels/testpooling.py
+++ b/test/python/testmodels/testpooling.py
@@ -48,6 +48,22 @@ 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