💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Label QA spans without the answer with the CLS token, closes #1263 (#1264)
Jin95d4c1c88198
diff --git a/src/python/txtai/data/questions.py b/src/python/txtai/data/questions.pyindex f47c7deb..92ad9d11 100644--- a/src/python/txtai/data/questions.py+++ b/src/python/txtai/data/questions.py@@ -72,14 +72,20 @@ class Questions(Data):while sequences[end] != (1 if self.rpad else 0):end -= 1- # Map start character and end character to matching token index- while start < len(offset) and offset[start][0] <= startchar:- start += 1- tokenized["start_positions"].append(start - 1)-- while offset[end][1] >= endchar:- end -= 1- tokenized["end_positions"].append(end + 1)+ # Label with CLS token if the answer isn't fully in this span. Long contexts are split into+ # overlapping spans with stride and most spans don't contain the answer.+ if not (offset[start][0] <= startchar and offset[end][1] >= endchar):+ tokenized["start_positions"].append(clstoken)+ tokenized["end_positions"].append(clstoken)+ else:+ # Map start character and end character to matching token index+ while start < len(offset) and offset[start][0] <= startchar:+ start += 1+ tokenized["start_positions"].append(start - 1)++ while offset[end][1] >= endchar:+ end -= 1+ tokenized["end_positions"].append(end + 1)return tokenizeddiff --git a/test/python/testpipeline/testtrain/testtrainer.py b/test/python/testpipeline/testtrain/testtrainer.pyindex 70f16ae9..279512e9 100644--- a/test/python/testpipeline/testtrain/testtrainer.py+++ b/test/python/testpipeline/testtrain/testtrainer.py@@ -15,7 +15,7 @@ import torchfrom transformers import AutoTokenizer, AutoModelForSequenceClassification-from txtai.data import Data+from txtai.data import Data, Questions as QuestionsDatafrom txtai.models import Lemur, Models, PoolingFactoryfrom txtai.pipeline import HFTrainer, Labels, LemurTrainer, Questions, Sequences@@ -738,6 +738,39 @@ class TestTrainer(unittest.TestCase):questions = Questions((model, tokenizer), gpu=True)self.assertTrue("onion" in questions(["What ingredient?"], ["Peel 1 onion"])[0])+ def testQASpans(self):+ """+ Test QA training labels when a long context is split into multiple spans+ """++ tokenizer = AutoTokenizer.from_pretrained("google/bert_uncased_L-2_H-128_A-2")+ process = QuestionsData(tokenizer, None, 32, 8)++ # Context long enough to be split into overlapping spans, with the answer near the end+ context = " ".join(f"filler{x}" for x in range(9)) + " paris filler10 filler11"+ data = {"question": ["What is the capital?"], "context": [context], "answers": ["paris"]}++ spans = process.tokenize(dict(data))+ labels = process.process(dict(data))++ start, end = context.index("paris"), context.index("paris") + len("paris")+ inside = []+ for x, offsets in enumerate(spans["offset_mapping"]):+ # Character range of the context covered by this span+ covered = [offset for offset, sequence in zip(offsets, spans.sequence_ids(x)) if sequence == 1]+ inside.append(covered[0][0] <= start and covered[-1][1] >= end)++ tokens = labels["input_ids"][x][labels["start_positions"][x] : labels["end_positions"][x] + 1]+ if inside[-1]:+ self.assertEqual(tokenizer.decode(tokens), "paris")+ else:+ # Spans without the answer are labeled with the CLS token+ self.assertEqual((labels["start_positions"][x], labels["end_positions"][x]), (0, 0))++ # Both kinds of span must be present for this test to be meaningful+ self.assertIn(True, inside)+ self.assertIn(False, inside)+def testRegression(self):"""Test training a model with a regression (continuous) output