Zenaique

You are fine-tuning Llama 3 8B on variable length instruction data. Which padding strategy and side do you pick?

MCQ·Medium·4.0 · 0·~1 min·Asked atMidjourneySambanovaTwo Sigma·Relevant atMeta
Attempt it
TL;DR

Left-pad at inference (decoder only generates from the last position), either side at training with the attention mask. Use `padding='longest'` plus bucketing or packing, not fixed `max_length`.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine a row of bookshelves where every shelf has to be the same width. You can either jam every shelf to the warehouse's maximum width (mostly air), or you can size each shelf to the widest book in that group (much less air). The second way wastes far less wood. That is `padding='longest'` versus `padding='max_length'`. Then there is the question of which side of the shelf to put the air pocket on. For a model that reads a story and continues writing it from the last word, the last book on the shelf has to be a real one, not air. So air goes on the left side. That is left padding for decoder only inference.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Padding decisions look like implementation detail until they double your training cost or quietly degrade your inference quality. For fine tuning a decoder only Llama 3 8B on variable length instruction data, the right combination is padding='longest' plus bucketing or packing for compute efficiency, left padding at inference for correctness, either side at training as long as the attention mask is set and pad labels are masked to -100.

This explanation works through each decision separately, explains why the other options in the question fail, and closes on the two production bugs that come up most often when these decisions are made carelessly.

Strategy: longest, bucket, or pack, never max_length to ceiling

The strategy controls how much pad gets added to each batch. padding='longest' pads to the longest sequence in the current batch, which is the right default for most training. padding='max_length' pads to a fixed value (often the model's model_max_length), which is the wrong default for almost any real dataset.

The reason is the length distribution. Instruction tuning data is heavy tailed: most examples are a few hundred tokens, a small fraction are a few thousand. If you pad every batch to 8k (Llama 3's base context), 80 to 90 percent of the per step FLOPs go to pad positions that contribute no loss and produce no gradient. The model trains exactly as well on the unpadded portion but the per step cost is 5 to 10x what it needs to be.

Sequence bucketing reduces this further by grouping examples of similar length into the same batch. Within a bucket the pad waste is small. The downside is a small loss of randomization, which most teams accept.

Sequence packing is the most efficient option. Multiple short examples are concatenated into one row up to a target length (often the model's full context window), with a block diagonal attention mask so each example only attends within itself. Cross example attention is masked out. Flash Attention 2 and 3 expose varlen kernels that take per sequence offsets and run packed batches at near peak throughput. Axolotl, TRL, and Unsloth all default to packing for Llama 3 fine tuning in 2026.

Side: left at inference, either at training
The attention mask plays two roles
Why the distractors fail and the bug patterns to watch for
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • Axolotl, TRL, and Unsloth all default to sequence packing for Llama 3 instruction fine tuning in 2026, with `sample_packing=true` in the config.
  • Hugging Face `Trainer` with `DataCollatorForLanguageModeling` defaults to right padding for training and handles the loss mask via `labels = -100` automatically.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy is sequence packing more efficient than longest padding plus bucketing?
A

Even bucketed batches have within bucket length variance, so there is still some pad waste. Packing concatenates short examples to fill each row up to a fixed length, dropping waste to near zero. The cost is a block diagonal attention mask that Flash Attention 2 and 3 handle efficiently. Loss is still masked at the seams so examples do not contaminate each other.

1 more follow-up an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Right padding a Llama 3 model at inference and watching the model generate from a pad position because the last index of the sequence is pad, not the user's last real token.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • State why a decoder only model requires left padding at inference

  • Contrast padding='longest' against padding='max_length' in compute terms

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Why does BPE tokenization use subwords instead of words or characters?
Flashcard·Easy