Sub topic 13
Hallucination, Repetition and Other Failure Modes
Assume you know basic Python, neural networks, loss functions, Transformer self-attention, and the Hugging Face Transformers library. This chapter focuses on diagnosing failures in encoder-decoder models rather than treating every poor output as a decoding problem.
A useful failure model
An encoder-decoder model can fail at several stages:
A hallucination is generated content that is unsupported by the source. In summarisation, the model may invent a date, name, cause, or result. The output can sound fluent because fluency is not the same as factual accuracy.
A repetition failure occurs when the decoder loops over words, phrases, or sentences. It can repeat an important phrase several times or produce a longer cycle such as “the company said” followed by the same sentence.
A mistranslation changes the meaning of the source. Rare names, technical terms, negation, measurements, and idioms are especially risky. A translation can be grammatically correct while still being wrong.
Why these failures happen
The decoder predicts one token at a time from a probability distribution. It does not copy facts from a database. During generation, it conditions on the encoded source and on the tokens it has already produced. If several continuations are plausible, the selected path can drift from the source.
Training also uses teacher forcing: the decoder receives the correct previous target token during training. At inference time, it receives its own previous prediction. An early mistake can therefore affect every later prediction. This difference is called exposure bias.
Long inputs create another problem. If the source exceeds the model's context window, tokenisation may truncate the end or the application may split the text poorly. A summary can then omit the document's conclusion or combine unrelated chunks.
Decoding controls reduce some errors but do not prove correctness:
from transformers import pipeline
summarizer = pipeline(
"summarization",
model="facebook/bart-large-cnn",
)
article = (
"The audit found that the factory reduced water use by 18 percent in 2024. "
"It also reported that equipment failures delayed two shipments. "
"The report recommended maintenance changes, but it did not announce a new target."
)
result = summarizer(
article,
min_new_tokens=20,
max_new_tokens=60,
num_beams=4,
no_repeat_ngram_size=3,
repetition_penalty=1.1,
do_sample=False,
)
print(result[0]["summary_text"])num_beams=4 keeps several candidate sequences before selecting one. no_repeat_ngram_size=3 blocks repeated three-token sequences. repetition_penalty lowers the probability of tokens that have already appeared. Length limits prevent runaway output, but an overly small limit can cut off a required fact.
Detecting and reducing failures
Use a layered process:
- Check the source and output side by side, especially names, numbers, dates, negation, and causal claims.
- Preserve important entities with terminology lists or post-generation checks.
- For long documents, chunk by sections and keep headings or overlap between chunks.
- Use deterministic decoding for reproducible testing. Compare beam search with greedy decoding rather than assuming beams are always better.
- Ask for evidence spans when building a review workflow, then verify each span against the source.
- Test rare terms and negative statements separately. Average quality scores often hide these cases.
- Keep a human review step for legal, medical, financial, or safety-related text.
ROUGE, BLEU, and chrF compare generated text with references. They can identify wording differences, but they do not reliably detect an invented fact that happens to use common words. Evaluate factuality and terminology accuracy with targeted examples as well as automatic metrics.
Exercise
Exercise
A summary repeats a sentence three times but contains no invented facts. Which control would you try first, and what problem would it address?
Show answer
Try norepeatngramsize or a modest repetitionpenalty. These controls reduce repeated token patterns, but you should still inspect the result because they can remove legitimate repeated terms.