The Packing Bug That Never Erred
Why Does This Exist?
When you fine-tune an LLM, you want to maximize GPU efficiency. If your context window is 8192 tokens, but your training documents are only 1000 tokens long, you waste 80% of your compute on padding tokens. To fix this, developers use "sequence packing" — concatenating multiple short documents into a single 8192-token row separated by an EOS (End of Sequence) token.
The problem was that the model's attention mechanism didn't know these were separate documents. Without a custom attention mask, a token in Document C could attend to context in Document A, simply because they shared a row.
Think of It Like This
Imagine gluing three different books together into one massive volume and giving it to a student. You want them to learn the concepts separately. But because the books are physically connected, the student starts mixing up the plot of Moby Dick with a calculus textbook.
How It Actually Works
For years, the Hugging Face SFTTrainer (via the TRL library) offered a simple packing=True flag. When you set this, it packed the sequences efficiently. But up until Transformers 4.44, the default attention mask passed to the underlying model was often a standard causal mask (where every token attends to all previous tokens in the row).
This meant that if Document 1 was a Python script and Document 2 was a French poem, the model was learning that Python code is a great precursor to French poetry. This cross-document contamination degrades the model's ability to learn strict contextual boundaries.
In recent updates, the ecosystem formally rolled out robust support for block-diagonal attention masks (sometimes called "document masking" or "flash attention with cuSeqlens"). This ensures that even within a packed row, attention strictly resets at the document boundaries.
Watch Out For
The bug was silent. The code didn't crash, the loss curve still went down, and the model still learned. It just learned a slightly blurrier, noisier distribution of the world. If you trained a custom model using simple packing prior to the widespread adoption of block-diagonal masking, your model might be suffering from minor cross-contamination hallucinations.
(Correct as of August 2026).
The Quick Version
Sequence packing without proper document boundaries causes models to bleed context across unrelated training examples. Recent framework updates fixed this silent bug by natively supporting block-diagonal attention masks, isolating documents within the same packed row.
What to Read Next
To see the math behind this, check the attention-mechanisms concept page, and review continuous-batching for how this impacts serving.