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 word embeddings regression with batch transformation, closes #245

davidmezzettia86e161d9b56
diff --git a/src/python/txtai/vectors/words.py b/src/python/txtai/vectors/words.py
index f783960e..378366ad 100644
--- a/src/python/txtai/vectors/words.py
+++ b/src/python/txtai/vectors/words.py
@@ -116,7 +116,7 @@ class WordVectors(Vectors):
dimensions = embedding.shape[0]
ids.append(uid)
- pickle.dump(embedding, output, protocol=4)
+ pickle.dump(embedding.reshape(1, -1), output, protocol=4)
return (ids, dimensions, len(ids), stream)
diff --git a/test/python/testvectors/testexternal.py b/test/python/testvectors/testexternal.py
index 49ebc3b5..8131cfd6 100644
--- a/test/python/testvectors/testexternal.py
+++ b/test/python/testvectors/testexternal.py
@@ -3,6 +3,7 @@ External module tests
"""
import os
+import pickle
import unittest
import numpy as np
@@ -40,3 +41,7 @@ class TestExternalVectors(unittest.TestCase):
self.assertEqual(dimension, 768)
self.assertEqual(batches, 2)
self.assertIsNotNone(os.path.exists(stream))
+
+ # Test shape of serialized embeddings
+ with open(stream, "rb") as queue:
+ self.assertEqual(pickle.load(queue).shape, (500, 768))
diff --git a/test/python/testvectors/testtransformers.py b/test/python/testvectors/testtransformers.py
index 85c065dd..764faacc 100644
--- a/test/python/testvectors/testtransformers.py
+++ b/test/python/testvectors/testtransformers.py
@@ -3,6 +3,7 @@ Vectors module tests
"""
import os
+import pickle
import unittest
import numpy as np
@@ -38,6 +39,10 @@ class TestTransformersVectors(unittest.TestCase):
self.assertEqual(batches, 2)
self.assertIsNotNone(os.path.exists(stream))
+ # Test shape of serialized embeddings
+ with open(stream, "rb") as queue:
+ self.assertEqual(pickle.load(queue).shape, (500, 768))
+
def testSentenceTransformers(self):
"""
Test creating a model with sentence transformers
diff --git a/test/python/testvectors/testwordvectors.py b/test/python/testvectors/testwordvectors.py
index d809ed31..6e25dd11 100644
--- a/test/python/testvectors/testwordvectors.py
+++ b/test/python/testvectors/testwordvectors.py
@@ -3,6 +3,7 @@ Vectors module tests
"""
import os
+import pickle
import tempfile
import unittest
@@ -68,6 +69,10 @@ class TestWordVectors(unittest.TestCase):
self.assertEqual(batches, 1000)
self.assertIsNotNone(os.path.exists(stream))
+ # Test shape of serialized embeddings
+ with open(stream, "rb") as queue:
+ self.assertEqual(pickle.load(queue).shape, (1, 10))
+
def testIndexSerial(self):
"""
Test word vector indexing in single process mode
@@ -85,6 +90,10 @@ class TestWordVectors(unittest.TestCase):
self.assertEqual(batches, 1000)
self.assertIsNotNone(os.path.exists(stream))
+ # Test shape of serialized embeddings
+ with open(stream, "rb") as queue:
+ self.assertEqual(pickle.load(queue).shape, (1, 10))
+
def testLookup(self):
"""
Test word vector lookup