liveBook · Manning

Padding token

As explained in Build a Text-to-Image Generator (from Scratch)

A padding token is a special marker appended to shorter sequences so that sequences in a batch have equal length.

A padding token is a special marker appended to shorter sequences so every sequence in a batch has the same length. Unlike start, end, or unknown tokens, it represents no input content; it provides a fixed-size shape for batching. section 2.2.1 section 4.2.2

A typical padding workflow proceeds as follows:

  • Append padding markers to shorter token sequences until they match the batch’s maximum or configured length. section 8.2.2
  • Prevent computation from treating padding as content by supplying an attention mask with 0 at padded positions and 1 elsewhere. section 8.2.2
  • Preserve the tokenizer’s expected special-token IDs when using pretrained weights, because those IDs are consumed internally by the model. section 12.2.1

Padding two captions

Tokenizing “two dogs run” and “an eagle flies in the sky” produces sequences of lengths five and eight after CLS and SEP are added. The shorter sequence receives three PAD tokens: [CLS, two, dogs, run, SEP, PAD, PAD, PAD]. Its attention mask is [1, 1, 1, 1, 1, 0, 0, 0], while the longer sequence needs no padding. section 8.2.2

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains vocabulary IDs, batch padding, attention masks, and fixed-length tensors, but does not derive padding internals.
Examples
Demonstrates padded German-English sequences, caption batches, DistilBERT outputs, and a manually padded 64-token tensor.
What it assumes
Assumes tokenization and integer vocabulary mappings, while introducing padding alongside start, end, unknown, and attention-mask tokens.
What you can do after
Apply it via tokenizers or PyTorch tensors, and mask padded positions during transformer processing.
How it connects
Connects padding tokens to tokenization, vocabulary mappings, attention masks, special tokens, batching, and transformer inputs.

Also explained in

Compare how 4 books explain padding token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.