Hugging Face, End to EndLesson 13 of 75

Module 2 · Transformers, from characters to output

Padding on the wrong side ruins generation quietly

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 4 of 9 in this chapter

This chapter’s goalTrace a piece of text from characters through a tokenizer to logits and back to text with the transformers library, and say which of the Auto class, the tokenizer, the padding side, the chat template or the generation config is at fault when a model loads without error and produces nonsense.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableWatch4:51
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Why padding exists at all

A batch is a rectangle. Three sentences of 4, 11 and 6 tokens cannot be stacked into a tensor until they are the same length, so the short ones get filled with a pad token:

Python
enc = tok(["hi", "a much longer sentence here"], padding=True, return_tensors="pt")
print(enc["input_ids"])
print(enc["attention_mask"])

The mask is the important half. It is 1 for real tokens and 0 for padding, and the attention mechanism uses it to make padded positions contribute nothing. Pass input_ids without the mask and the model attends to the padding as if it were content, a mistake that is easy to make when people build tensors by hand and then wonder why batched results differ from one-at-a-time results.

Rule: whatever the tokenizer returns, pass all of it. model(**enc), never model(enc["input_ids"]).

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Padding on the wrong side ruins generation quietly’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/padding-and-attention-masks (Accessed: 29 November 2026).