Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Pack rows into chunks up to and including maxlength, closes #1269 (#1270)

Jin83c75ea28a44
diff --git a/src/python/txtai/data/texts.py b/src/python/txtai/data/texts.py
index 4817efae..2d40d853 100644
--- a/src/python/txtai/data/texts.py
+++ b/src/python/txtai/data/texts.py
@@ -104,7 +104,7 @@ class Texts(Data):
nextlength = len(rows[x + 1]) if x < len(rows) - 1 else 0
# New row
- if (length + nextlength) >= self.maxlength:
+ if (length + nextlength) > self.maxlength:
for column in columns:
results[column].append(list(chain(*inputs[column][index : x + 1])))
diff --git a/test/python/testpipeline/testtrain/testtexts.py b/test/python/testpipeline/testtrain/testtexts.py
new file mode 100644
index 00000000..b2cf29d2
--- /dev/null
+++ b/test/python/testpipeline/testtrain/testtexts.py
@@ -0,0 +1,39 @@
+"""
+Texts module tests
+"""
+
+import unittest
+
+from transformers import AutoTokenizer
+
+from txtai.data import Texts
+
+
+class TestTexts(unittest.TestCase):
+ """
+ Texts tests.
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ """
+ Create tokenizer.
+ """
+
+ cls.tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2")
+
+ def testPack(self):
+ """
+ Test packing rows into chunks up to maxlength
+ """
+
+ rows = ["a b c d", "e f g h", "i j k l"]
+ length = len(self.tokenizer(rows[0])["input_ids"])
+
+ # Two rows fill maxlength exactly and are packed into one chunk
+ packed = Texts(self.tokenizer, None, length * 2, "pack").process({"text": list(rows)})
+ self.assertEqual([len(chunk) for chunk in packed["input_ids"]], [length * 2, length])
+
+ # Rows are never split across chunks
+ packed = Texts(self.tokenizer, None, length * 2 - 1, "pack").process({"text": list(rows)})
+ self.assertEqual([len(chunk) for chunk in packed["input_ids"]], [length] * 3)