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.
Detailed answer & concept explanation~5 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.
3 min: define pad as a batching artifact, explain how the attention mask zeros pad weights inside the softmax, explain loss masking with `-100`, note the EOS reuse convention for causal LMs, and close on the missing attention mask bug.
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.
- vLLM and SGLang use sequence packing in 2026 production serving so the GPU never spends FLOPs on pad positions across a continuous batch.
- Axolotl and Hugging Face TRL training configs default to right padding with the loss masked via `labels` set to `-100` at pad positions.
What an interviewer would ask next. Try answering before peeking at the approach.
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.
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.
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.