GAUNTLET / AI · Transformers from First Principles
01/22
Lecture 01 · Foundations

TRANS
FORMERS

From First Principles

Brief

A 45-minute walkthrough of the architecture that powers GPT, Claude, Gemini, LLaMA, Mistral — every modern LLM.

Format

45 min lecture · 15 min Q&A · Working takeaway repo to keep.

Cohort

Challengers ramping into AI-first engineering. Interrupt anytime.

01 · The Why

Why bother
understanding transformers?

One architecture. Almost every modern AI system.

Language

GPT-4, Claude, Gemini, LLaMA, Mistral. All transformers.

Vision

Vision Transformers (ViT) match or beat CNNs on most benchmarks.

Multimodal

Image, audio, video — same architecture, different tokenizers.

Code & Agents

Cursor, Claude Code, agentic systems — all transformer-based.

Learn this one architecture and you can read 90% of modern AI papers and ship with 90% of modern AI models.
02 · The Problem

Sequence modeling.

How do we get a neural network to understand order-dependent data?

Given a sequence of tokens, predict the next one.
The
cat
sat
on
the
?
01

Variable length. Sentences, paragraphs, books — input size isn't fixed.

02

Long-range dependencies. "The girl who lived in the house with the red door is sad" — sad refers to girl, 11 tokens back.

03

Order matters. "Dog bites man" ≠ "Man bites dog".

04

Ambiguity. "Bank" means river-side or financial — needs context.

03 · 2014 – 2017

Before transformers: RNNs & LSTMs.

Process tokens one at a time, maintaining a hidden state.

RNN
The
RNN
cat
RNN
sat
RNN
on
RNN
the
— hidden state passes forward —

The core idea

At each step, read one token + the previous hidden state, produce a new hidden state. The hidden state is supposed to be a running summary of everything seen so far.

LSTMs and GRUs improved on this with gating mechanisms, but the fundamental shape stayed the same: sequential, state-passing.

04 · The Bottlenecks

Two fatal flaws of recurrent models.

These motivated the entire transformer architecture.

Bottleneck 01

Sequential
= Slow.

You can't compute step t+1 until step t is done. Modern GPUs love parallelism — RNNs refuse to give it to them.

Real-world impact
Training time scales linearly with sequence length. Can't easily scale to long contexts.
Bottleneck 02

Long-range
= Forgotten.

By the time you've processed 1,000 tokens, the hidden state has been overwritten 1,000 times. Information from early tokens fades.

Real-world impact
Gradients vanish through time. LSTMs help, but don't fully solve it.
2017 · Vaswani et al. · NeurIPS

Attention
Is All
You Need.

Drop the recurrence. Entirely.

Let every token look at every other token, directly, in one step.

No hidden state · No sequential bottleneck · No forgetting · Computed in parallel.
05 · Intuition

Attention, intuitively.

Each token asks: "which other tokens are most relevant to me?"

STRONG weak QUERY: "it"
The
animal
didn't
cross
the
street
because
it
was
tired
Q

Query

what I'm looking for

K

Key

what I offer / what describes me

V

Value

what I contribute if you pick me

06 · The Math

Self-attention, formally.

Three linear projections, one matrix multiplication, one softmax. That's it.

Attention(Q, K, V) = softmax(Q·KT / √d) · V
01

Project each token to Q, K, V

Three learned linear layers. X is (n × d), Q/K/V are (n × d).

Q = X·Wq · K = X·Wk · V = X·Wv
02

Score: query matches key

Result is (n × n): one score for every (token, token) pair.

scores = Q · Kᵀ
03

Scale and softmax

Dividing by √d keeps gradients stable. Softmax turns scores into a probability distribution.

weights = softmax(scores / √d)
04

Weighted sum of values

Each token's new representation is a blend of all values, weighted by relevance.

output = weights · V
07 · Visualizing It

What the attention matrix looks like.

An n × n grid where cell (i, j) is how much token i attends to token j.

Key · what each token offers →
Query · what each looks for →

Three things to notice

  • Each row sums to 1.

    Softmax over keys. Every query distributes its attention across all available keys.

  • Upper triangle is empty.

    Causal mask — during training a token can't attend to future tokens (would be cheating).

  • Darker = stronger attention.

    These weights are learned. The model decides what to attend to.

08 · Going Wider

Multi-head attention.

Run attention many times in parallel, each on a different slice of the embedding.

Input X (n × d)
H1n × d/h
H2n × d/h
H3n × d/h
H4n × d/h
Concat + Linear
Output (n × d)
One head can only learn one relationship pattern.

With h heads, the model simultaneously tracks:

  • Syntactic dependencies (subject ↔ verb)
  • Coreference (pronouns ↔ nouns)
  • Positional patterns (next-word, prev-word)
  • Semantic similarity
All in one layer. All in parallel.
09 · The Missing Piece

Position.

Attention is permutation-invariant. "Dog bites man" and "Man bites dog" look identical to it.

The Bug

Attention treats the input as a set, not a sequence.

Shuffle the input tokens — same attention weights, same output. That's not what we want for language.

The Fix

Add a position vector to each token embedding.

Now "cat" at position 3 is a different vector than "cat" at position 7.

— Three common flavors —

Sinusoidal

Original 2017 paper

Fixed cos/sin functions of position. No parameters. Extends to longer sequences than seen in training.

PE(pos, 2i) = sin(pos / 10000^(2i/d))

Learned

GPT-2, BERT

Just an embedding table indexed by position. Simple, but capped at max training length.

PE = nn.Embedding(max_len, d)

Rotary (RoPE)

LLaMA, Mistral, modern

Rotate Q and K vectors by position-dependent angles. Encodes relative position naturally.

applied in attention, not at embedding
10 · Stack It

The transformer block.

Attention is the core. Stack it with a feed-forward network, residuals, and layer-norm — that's a block.

Input
LayerNorm
Multi-Head Self-Attention
+ Residual
LayerNorm
Feed-Forward (MLP)
+ Residual
Output
Repeat N times

Self-Attention

Lets each token gather information from every other token. This is where context comes in.

Feed-Forward Network

A two-layer MLP applied per-position. Where most parameters live; does the heavy lifting on each token's representation.

Residual Connections

Add the input back to the output of each sublayer. Lets gradients flow through deep stacks without vanishing.

Layer Normalization

Normalize activations across the embedding dimension. Stabilizes training, lets us go deep.

11 · Why It Trains

Why residuals & LayerNorm matter.

These two tricks are why we can stack 100+ transformer blocks and still train them.

Residual connection

output = x + sublayer(x)
skip path
x
Sublayer
+
Why it matters
Gradients flow back through the skip path unchanged. Even with 100 stacked blocks, the gradient at layer 1 is well-behaved. Without residuals, deep transformers don't train.

Layer Normalization

ŷ = (x μ) / σ · γ + β

For each token vector, subtract its mean, divide by its standard deviation. Then a learned scale (γ) and shift (β) per dimension.

Why it matters
Activations and gradients stay on a similar scale layer-to-layer. Without it, things explode or vanish as you go deep. Unlike BatchNorm, LayerNorm doesn't depend on other examples in the batch — critical for variable-length sequences.
12 · The Three Flavors

Same block. Different connectivity.

Very different capabilities.

Encoder
only

BERT · RoBERTa · ViT
All-to-all · no mask

Every token attends to every other token. Great for understanding tasks — classification, NER, embedding generation.

Good for "what is this text about?"

Decoder
only

GPT · Claude · LLaMA
Causal mask · triangle

Each token only attends to previous tokens. Trained on next-token prediction. The architecture behind every modern chatbot.

Good for "continue this text."

Encoder
Decoder

T5 · BART · 2017 paper
Encoder + cross-attn

Encoder reads input bidirectionally; decoder generates output causally, attending to the encoded input via cross-attention. Built for seq-to-seq.

Good for translate, summarize.
13 · Training

How they learn: next-token prediction.

One simple objective, applied to the internet. That's it.

The
cat
sat
on
the
predict
mat
Input · context
Target
Loss = cross-entropy(model's probability distribution, actual next token)

Why it scales so well

Training data is free. Every sentence on the internet is a training example. No human labels needed — the next word IS the label.

What the model actually learns

To predict the next word well, you need to model grammar, facts, reasoning, style, code, math, dialogue. Compression of human knowledge.

14 · Tokenization

What is a "token", anyway?

Models don't see characters or words. They see tokens — chunks chosen by a learned algorithm.

Character-level

T | h | e | _ | c | a | t
Vocab size
~ 100 tokens
  • Trivial. No OOV.
  • Long sequences, low semantic content per token.
Used byToy models · the demo repo

Word-level

The | cat | sat
Vocab size
~ 50K – 1M tokens
  • Each token is semantically meaningful.
  • Out-of-vocab words break it. Massive vocabulary.
Used byOlder NLP · mostly retired

Byte-Pair Encoding (BPE)

◆ Winner
The | _cat | _sat
Vocab size
~ 32K – 100K tokens
  • Best of both. Frequent words are single tokens; rare words split into subwords.
  • More complex to implement.
Used byGPT-4 · Claude · LLaMA · every modern LLM
15 · The Win

Why transformers won.

It's not just that they're more accurate. They train an order of magnitude faster.

RNN · Sequential

Step by step
t1
t2
t3
t4
t5
t6

Each step waits for the previous one.

Latency: O(n) where n = sequence length.

Transformer · Parallel

All at once
t1   t2   t3   t4   t5   t6

Computed in one matrix multiplication.

Latency: O(1) wall-clock — assuming enough GPU.

The consequence

Once you can parallelize across the sequence dim, you can throw orders-of-magnitude more compute at the problem. That's what unlocked the entire scaling-laws era.

Caveat: attention is O(n²) in memory, which is its own bottleneck and the main focus of long-context research today.

16 · Scaling

Bigger really is better.

Test loss falls predictably as a power law in parameters, data, and compute.

4.0 3.0 2.0 1.0 0 10⁶ 10⁷ 10⁸ 10⁹ 10¹⁰ 10¹¹ 10¹² Loss vs. parameters (illustrative) PARAMETERS (LOG SCALE) → TEST LOSS

Three levers you can pull

N

Parameters

Bigger model — more layers, wider FFN, more heads.

D

Data

More tokens to train on.

C

Compute

More FLOPs (= N × D × constant).

Chinchilla (2022): for a given compute budget, scale parameters and data together. Most LLMs before 2022 were under-trained.
17 · State of the Art

What's in production today.

vs. the 2017 paper.

Flash Attention

2022
What
Fused attention kernel that never materializes the full n×n matrix.
Why
3–10× speedup, dramatic memory savings, enables longer context.

Rotary Position Embeddings (RoPE)

2021
What
Encode position by rotating Q/K vectors instead of adding embeddings.
Why
Better extrapolation to lengths not seen in training.

Grouped-Query Attention (GQA)

2023
What
Multiple query heads share a single key/value head.
Why
Cuts memory bandwidth bottleneck during inference.

Mixture of Experts (MoE)

2017+
What
Replace FFN with N parallel FFNs; a router picks 2 of them per token.
Why
Scale total params without scaling per-token compute. (Mixtral, GPT-4.)

Long context (1M+ tokens)

ongoing
What
Sparse attention, ring attention, position interpolation, retrieval-augmentation.
Why
Open research area. Quadratic attention is the wall.

Speculative decoding

2023
What
Tiny model drafts tokens, big model verifies in parallel.
Why
2–3× inference speedup with no quality loss.
18 · The Mental Model

Putting it together.

One mental model to carry away.

01

Tokens become vectors.

Tokenizer turns text into integers; embedding table turns integers into vectors. Position info is added.

02

Vectors talk to each other via attention.

Q, K, V projection → softmax(QKᵀ/√d) · V. Every token gets to look at every other token.

03

FFN refines each token individually.

Two-layer MLP applied per-position. This is where most of the model's capacity lives.

04

Stack this block N times.

Residual connections + layer norm let you go deep. 12 blocks (BERT-base) to 100+ (GPT-4 class).

05

Predict the next token. Repeat.

Final linear projects back to vocab. Cross-entropy loss. Trained on trillions of tokens. That's a modern LLM.

19 · Your Takeaway

A working mini-GPT. Yours.

Everything we just covered, in ~250 lines of PyTorch. Trains on a laptop.

transformer-from-scratch ~
model.py the architecture
train.py training loop
generate.py sample from trained model
download_data.py fetch full corpus
data/sample.txt bundled Shakespeare
exercises.md things to try
README.md walkthrough
requirements.txt just torch

Get started

$pip install -r requirements.txt
$python download_data.py# optional — better results
$python train.py
$python generate.py --prompt "ROMEO:"
What you'll see

Loss drops from ~4.2 to ~1.5 in a few minutes on CPU. Then the model produces text like:

ROMEO:
Thou shalt not fear, the morning of my love
Hath made the heavens to weep, and the world
To mourn the loss of thy companion...
20 · The End
Questions?
— To dig deeper —

Attention Is All You Need

Vaswani et al., 2017

The original paper. Surprisingly readable. Start here.

The Illustrated Transformer

Jay Alammar

Best visual explanation on the internet. Free.

Let's build GPT, from scratch

Andrej Karpathy · YouTube

2-hour video. Builds essentially this repo, live-coded. Excellent.

The Annotated Transformer

Harvard NLP

The original paper, line-by-line, with PyTorch.

nanoGPT

Karpathy · GitHub

Production-quality cousin of the takeaway repo. Reproduces GPT-2.

Anthropic interpretability

transformer-circuits.pub

If you want to understand what's happening inside the weights.

Keyboard

SPACEN
Next slide
P
Previous slide
HOME
First slide
END
Last slide
F
Toggle fullscreen
19
Jump to slide N
?ESC
Close this help