You pad a batch to equal length. How does the model know to ignore the filler tokens?
A pad token is filler added to short rows so the batch is rectangular; the attention mask carries zeros at pad positions so attention assigns them zero weight and the loss ignores them.
Imagine a classroom that has to seat everyone in a perfect rectangle of desks. Some rows have eight students, some have three. To make the rectangle work you put cardboard cutouts in the empty desks so the shape is right. The cutouts are pad tokens. Now you give every teacher a seating chart that marks which desks have real students and which have cutouts, and you tell them to only call on real students. That seating chart is the attention mask. If you forget to hand out the chart, the teacher starts asking the cutouts questions, and the lesson goes off the rails.
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.
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 looks like a trivial implementation detail until you trace what would happen without it. GPUs cannot process a batch of unequal length sequences directly; the matmul that the attention block runs has to be rectangular. Padding makes the batch rectangular, and the attention mask makes the padding invisible. Together they are the bridge between irregular real text and the regular tensors the hardware needs.
This explanation walks through what a pad token is, what the attention mask does inside the attention block, how the loss is masked during training, the convention of reusing EOS as PAD in causal LMs, and the production bugs that come up when one of these layers is missing or wrong.
Why padding exists at all
Real text comes in unequal lengths. A batch of three sentences might tokenize to lengths 8, 12, and 4. The GPU runs the attention block as one big matrix multiplication over the whole batch, and that matmul needs the input tensor to be rectangular. The shape has to be [batch_size, seq_len, hidden] with a single seq_len for the whole batch.
The fix is to choose a common length (usually the longest in the batch) and fill the shorter rows up to that length with a filler token id. That filler id is the pad token. After padding, all three rows are length 12 and the tensor is rectangular.
The pad id itself is arbitrary in the sense that the model gives it no special semantic meaning. What makes the id behave as filler is everything that happens around it: the attention mask, the loss mask, and the way the runtime feeds both into the model. Without those, the pad id is just another token the model treats as content.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Hugging Face `tokenizer(batch, padding=True, return_tensors='pt')` returns both `input_ids` and `attention_mask`; both must flow into the model call.
- Llama 3 and Mistral fine tuning recipes set `tokenizer.pad_token = tokenizer.eos_token` and use `padding_side='left'` for generation.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does a decoder only model require left padding at inference but tolerate right padding during training?
Inference generates from the last position in the sequence, so the last real token must be at the last index; right padding would put pad there. Training computes loss with a labels tensor that masks pad positions to -100, so they generate no gradient regardless of position, which makes right padding harmless.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Tokenizing with `padding=True` and then forgetting to pass `attention_mask` to the model. The model attends to the pad ids as if they were real text and quality drops.
60 second bullets to scan on the way to the call.
Define the pad token and explain why batching forces it into existence.
Describe how the attention mask makes pad positions inert inside the softmax.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.