Spot the bug: packed-sequence SFT where the model learns to copy across unrelated examples
Click any words you think contain an error. Click again to unmark.
The plain lower-triangular mask lets attention cross example boundaries inside the pack, so the model learns to copy from unrelated earlier examples; the fix is a block-diagonal segment-causal mask.
Think of stuffing several short letters into one long envelope to save postage. That part is fine. The problem is the rule you gave the reader: read every previous word in the envelope before answering this question. The reader dutifully scans words from the unrelated letter above, sometimes finds something that sort of fits, and pastes it into the reply. Letters bleed into one another. The fix is not to stop sharing envelopes; it is to change the reading rule. Tell the reader: when you reach a sealed page divider, treat everything above as invisible. Each letter then stays a self-contained unit even though they all share the same envelope. The dividers are real, and the rule needs to respect them.
Detailed answer & concept explanation~7 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
6 min: identify the plain causal mask as the bug, explain how cross-segment attention leaks during training, prescribe block-diagonal mask plus position-id reset plus varlen kernel use, and verify with a two-example unit test.
Real products, models, and research that use this idea.
- Hugging Face TRL exposes SFTTrainer with packing=True, which builds a block-diagonal mask and resets position ids automatically through FlashAttention 2's varlen path.
- Axolotl's sample_packing=true configuration and Unsloth's packed dataloaders both implement segment-causal masking and document the cross-example leak this question asks about.
- FlashAttention 2's varlen_func interface takes a cu_seqlens tensor and is the production-grade way to pack without materialising large masks; it powers most modern open-weight LLM training stacks.
- Llama 4 and Qwen 3.5 published training recipes both rely on packing with block-diagonal masks at pretraining and fine-tuning scale.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you verify the mask is correct before kicking off a multi-day training run?
QWhat goes wrong if you fix the mask but forget to reset position ids?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Trusting the separator token to fix the leak. The token marks the boundary in the input but does nothing to block attention across it.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.