Explain sequence packing in SFT. What's the attention contamination problem and how is it solved?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
What it is: SFT datasets are mostly short - if your max sequence length is 4096 and the median example is 500 tokens, naive per-example batching means ~85%+ of each batch is padding: wasted memory and FLOPs. Packing concatenates multiple examples back-to-back into one full-length sequence, so every position is a real token. Throughput gains of several-fold are typical for short-example datasets; that's why every serious SFT framework (Axolotl, TRL, etc.) supports it.
Full worked answer with diagram
What it is: SFT datasets are mostly short - if your max sequence length is 4096 and the median example is 500 tokens, naive per-example batching means ~85%+ of each batch is padding: wasted memory and FLOPs. Packing concatenates multiple examples back-to-back into one full-length sequence, so every position is a real token. Throughput gains of several-fold are typical for short-example datasets; that's why every serious SFT framework (Axolotl, TRL, etc.) supports it.
The contamination problem: a naive concatenation lets causal attention flow across example boundaries - tokens of example 3 attend to examples 1 and 2. Consequences: responses conditioned on unrelated preceding conversations (the model can learn to expect random leading context), cross-example information leakage that corrupts the training signal, and position IDs continuing across boundaries so examples appear to start mid-context.
The correct solution is per-example isolation inside the packed sequence:
- Block-diagonal attention: mask attention so tokens attend only within their own example. Implemented efficiently with FlashAttention's variable-length kernels (
flash_attn_varlen_*taking cumulative sequence lengths) - no materialized 4096×4096 mask needed, and modern HF/TRL integrations pass exactly this through when packing correctly. - Reset position IDs at each example boundary so every example sees positions starting from 0 - important for RoPE-based models to see the position distribution they'll face at inference.
- Loss masking stays per-example: each example's prompt tokens masked, each response's EOS included.
Going further: follow-up territory
Nuances that signal depth: (1) some recipes skip the mask and accept contamination - for large diverse datasets the empirical damage is often small, but it's a measured risk, not a free pass, and it's most harmful with structured/similar examples where the model can genuinely exploit leakage; (2) packing changes the effective batch composition - a "batch" now weights long examples less per-sequence, subtly reweighting the loss toward short examples' tokens; (3) greedy bin-packing (first-fit decreasing) minimises leftover slack better than sequential filling.
Worth sketching. it makes "contamination" concrete instead of a phrase you assert.
flowchart LR
A["Example 1"] --> P["One packed<br/>4096-token sequence"]
B["Example 2"] --> P
C["Example 3"] --> P
P -->|"naive concat"| X["Example 3 attends back<br/>into examples 1 and 2"]
P -->|"varlen cu_seqlens"| Y["Block-diagonal attention,<br/>position ids reset to 0"]
Y --> Z["Loss mask stays per example,<br/>every EOS kept"]
What a good answer covers
- What it is: SFT datasets are mostly short - if your max sequence length is 4096 and the median example…
- Packing concatenates multiple examples back-to-back into one full-length sequence, so every position is…
- Throughput gains of several-fold are typical for short-example datasets; that's why every serious SFT…
- The contamination problem
- Consequences: responses conditioned on unrelated preceding conversations (the model can learn to expect…
Key terms: What it is · The contamination problem · The correct solution · Block-diagonal attention · Reset position IDs · Loss masking stays per-example
In the glossary: SFT · Contamination · Attention · FlashAttention
Interviewer follow-ups to expect
Why is contamination worse on templated datasets than diverse ones?
How do FlashAttention varlen kernels avoid materializing the block-diagonal mask?
What breaks if you pack but forget to reset position IDs?
- Block-diagonal attention: mask attention so tokens attend only within their own example. Implemented efficiently with FlashAttention's variable-length kernels (
flash_attn_varlen_*taking cumulative sequence lengths) - no materialized 4096×4096 mask needed, and modern HF/TRL integrations pass exactly this through when packing correctly. - Reset position IDs at each example boundary so every example sees positions starting from 0 - important for RoPE-based models to see the position distribution they'll face at inference.
- Loss masking stays per-example: each example's prompt tokens masked, each response's EOS included.
- Block-diagonal attention: mask attention so tokens attend only within their own example. Implemented efficiently with FlashAttention's variable-length kernels (
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep