You pad a batch to equal length. How does the model know to ignore the filler tokens?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
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.