QA training labels spans without the answer as [SEP] instead of the CLS token
When a question-answering context is longer than maxlength, Questions splits it into overlapping spans using stride. Spans that don't contain the answer get a wrong training label instead of the CLS token.
Reproduction (google/bert_uncased_L-2_H-128_A-2, maxlength=32, stride=8, answer paris near the end of the context):
from transformers import AutoTokenizer
from txtai.data import Questions
tokenizer = AutoTokenizer.from_pretrained("google/bert_uncased_L-2_H-128_A-2")
process = Questions(tokenizer, None, 32, 8)
context = " ".join(f"filler{x}" for x in range(9)) + " paris filler10 filler11"
labels = process.process({"question": ["What is the capital?"], "context": [context], "answers": ["paris"]})
print(labels["start_positions"], labels["end_positions"])[31, 18] [31, 18]- Span 0 does not contain the answer. It is labeled
(31, 31), which is the trailing[SEP]token. Expected(0, 0), the CLS token. - Span 1 contains the answer. It is labeled
(18, 18), which isparis. This is correct.
So for every span of a long context that doesn't hold the answer, training teaches the model that the answer is the [SEP] token.
The cause is the out-of-span check that was removed in 2cd89891 ("Remove dead code"):
if not (offset[start][0] <= startchar and offset[end][1] >= endchar):
tokenized["start_positions"].append(clstoken)
tokenized["end_positions"].append(clstoken)I think it looked dead because it is only reachable when a context overflows into more than one span. Every context in testQA is a few words long, so the answer is always in the only span and coverage never enters that branch.
I have a fix ready that restores the check, plus a regression test that splits a context into multiple spans and checks the label for each one against whether the span actually contains the answer. I'll open a PR referencing this issue.
Source: neuml/txtai