Gradient LabStart the lesson

Lessons › Chapter 11: Transformers and LLMs

The transformer block

A transformer is built from identical blocks. Each block starts with attention (purple): every token gathers information from the others.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

Pip’s architecture

The team settles on a transformer-based language model for Pip.

Concept

Step 1: tokens share information

A transformer is built from identical blocks. Each block starts with attention (purple): every token gathers information from the others.

Build

Step 2: each token thinks alone

Next comes a small feed-forward network (teal) applied to each token separately, a chance to process what it just gathered. Much of a model’s stored knowledge is thought to live in these layers.

Build

Shortcuts and normalizing

Around each part runs a shortcut (yellow) that adds the input back to the output, as in ResNet, plus a normalization step. These keep signals steady through very deep stacks.

Key idea

Stack them, then predict

Stack dozens of these blocks. At the top, a softmax turns the final vector into a probability for every token in the vocabulary. One token is picked, usually one of the most likely, then added to the text, and the whole thing runs again.

Real life

In real life: the assistants you chat with

Chat assistants such as Claude are built from deep stacks of transformer blocks, often many dozens of layers. Every word of every reply comes from running your conversation up through that stack and picking the next token, over and over.

Quick check: What are the two main parts inside each transformer block?

Attention mixes information across tokens; the feed-forward layer processes each token.

Pseudocode

block(x):
    x = x + attention(normalize(x))       # tokens share information
    x = x + feed_forward(normalize(x))    # each token thinks on its own
    return x

model = a stack of dozens of blocks
output = softmax over the vocabulary → the next token

Key terms

transformer
A neural network built from stacked blocks of attention and feed-forward layers.

In the news

Recent models and papers that build on this idea: