Zucchetti LLM Course β Lecture 1 of 10
How Large Language Models work under the hood
Deep dive into the architecture
Kamyar Zeinalipour Β· 2 hours
Section 1 of 4
The architecture that changed everything
Think of it as the world's most sophisticated autocomplete
Your phone keyboard:
"See you" β tomorrow | later | soon
An LLM (same idea, but trained on the entire internet):
"Explain quantum computing" β generates a full, coherent answer
Each paper built on the previous one. Understanding this lineage helps you understand why current models work.
| Year | Paper | Key Innovation | Citations |
|---|---|---|---|
| 2013 | Word2Vec (Mikolov et al.) | Words as vectors β similar meaning = similar direction | 35,000+ |
| 2017 | Attention Is All You Need (Vaswani et al.) | Transformer architecture β parallel attention replaces RNNs | 250,000+ |
| 2018 | BERT (Devlin et al., Google) | Pre-train once β fine-tune for any task | 115,000+ |
| 2020 | GPT-3 (Brown et al., OpenAI) | In-context learning β no fine-tuning needed, just examples | 60,000+ |
| 2022 | InstructGPT (Ouyang et al.) | RLHF β train models to follow human instructions | 8,000+ |
| 2023 | LLaMA (Touvron et al., Meta) | Open-source foundation models β democratized LLMs | 12,000+ |
The original architecture from "Attention Is All You Need" (Vaswani et al., 2017)
Transformers process all words at once — they have no idea about word order.
Without position info, these two sentences look identical to the model:
“The dog bit the man” vs “The man bit the dog”
Same words, same embeddings — but completely different meaning!
Solution: add a unique position vector to each word’s embedding:
| Word | Embedding | + | Position | = | Final input x | |
| pos 1 | “The” | [0.10, 0.20] | + | [0.00, 1.00] | = | [0.10, 1.20] |
| pos 2 | “dog” | [0.80, 0.30] | + | [0.84, 0.54] | = | [1.64, 0.84] |
| pos 3 | “bit” | [0.50, 0.60] | + | [0.91, −0.42] | = | [1.41, 0.18] |
Now “dog” at position 2 has a different vector than “dog” at position 5 — the model can tell them apart!
Each position gets a unique vector computed using sine and cosine at different frequencies.
Every word asks: “Which other words are relevant to me?”
“The bank approved the loan for the new house”
← “bank” attends to “loan” (financial context, not river bank)
← “loan” attends to “bank” AND “house” (purpose of the loan)
Match query against all keys → read best-matching values
In a transformer, every word plays all 3 roles at the same time.
softmax turns scores into percentages
√d keeps the numbers from exploding
Q, K, V all come from the same input — next slide: the math with real numbers
Let’s trace what happens when “bank” computes its attention scores
“The bank approved the loan for the new house”
| 1. | “bank” Q × “loan” K | 4.2 |
| 2. | “bank” Q × “house” K | 2.1 |
| 3. | “bank” Q × “approved” K | 1.8 |
| 4. | Divide by √d = √3 ≈ 1.73 | 2.43, 1.21, 1.04 |
| 5. | Softmax → percentages | 0.40, 0.25, 0.22, ... |
| 6. | Weighted sum of V → new vector | |
The new “bank” vector is now:
The model now knows “bank” means a financial institution — because “loan” had the highest score.
But this was just ONE head. It found the meaning. What about grammar, position, purpose?
One head found “bank↔loan”. But language has many types of relationships. Multiple heads learn different patterns simultaneously.
π
Head 1: Syntax
bank ↔ approved
subject–verb agreement
π§
Head 2: Meaning
bank ↔ loan
financial context
π
Head 3: Position
The ↔ bank
nearby word context
π
Head 4: Purpose
loan ↔ house
loan purpose → house
Now you know what attention is. Let’s see where it fits inside the architecture.
All words look at each other (what we just learned) → + x adds the original back (residual)
Each word independently passes through a small neural network — the “memory” that stores learned facts
ReLU activation + two linear layers. LayerNorm stabilizes values. + x is the residual connection.
The same input passes through 3 different learned lenses
WQ → “What am I looking for?”
WK → “What do I contain?”
WV → “What info do I carry?”
These are learned during training — not hand-coded!
Input: [0.23, −0.11, 0.55]
A. LayerNorm: [0.21, −0.10, 0.52]
B. × WQ: Q = [1.2, 0.8]
“looking for financial context”
× WK: K = [0.9, 1.4]
“I am an institution”
× WV: V = [0.5, 0.3]
“banking services”
C. Attention: [0.31, −0.08, 0.60]
D. + Residual: [0.54, −0.19, 1.15]
After FFN: [0.48, −0.22, 0.95] ✓
Each row = a word's attention distribution. Notice bank ↔ loan = 0.40 in both directions (symmetric). “The” attends mostly to itself (0.50).
Three architecture families: different attention patterns, different capabilities, different use cases.
Input: "The capital of Italy is"
β probability distribution:
| Rome | 87.3% |
| Milan | 4.1% |
| Roma | 3.8% |
| the | 1.2% |
| ... | 3.6% |
This is called autoregressive generation β one token at a time, each conditioned on all previous tokens.
During training, the model optimizes a single objective: predict the next token.
Because LLMs predict plausible text β not truthful text β they can confidently generate false information.
The model doesn't know facts β it predicts likely-sounding text. If the training data is sparse, it fills gaps with plausible fiction.
Temperature divides the logits before softmax. It changes how “sharp” or “flat” the probability distribution is.
Prompt: “The capital of France is” — model outputs raw logits: Paris = 5.0, Lyon = 2.0, Berlin = 1.0
| Logit / T | “Paris” | “Lyon” | “Berlin” | Effect | |
| βοΈ T = 0.2 | logit / 0.2 | 99.4% | 0.5% | 0.1% | Very sharp — always Paris |
| π― T = 1.0 | logit / 1.0 | 84.4% | 10.6% | 5.0% | Balanced |
| π T = 2.0 | logit / 2.0 | 49.0% | 27.2% | 23.8% | Flat — anything could happen |
Low T: divides by a small number → logits become larger → softmax becomes sharper → top token wins
High T: divides by a big number → logits become smaller → softmax becomes flatter → any token can win
The model does NOT always pick the highest probability. It rolls a weighted dice.
Probabilities after softmax:
Paris 50% Lyon 30% Berlin 20%
↑ Spinning wheel — each word gets a slice proportional to its probability
The model spins the wheel. Most spins land on Paris (50%), but sometimes Lyon (30%) or Berlin (20%).
T = 0 is special:
No dice, no spinning. Just pick the highest logit, always. Same prompt = same answer, 100% reproducible.
You send “The capital of France is” 3 times at T = 0.7:
Run 1: dice lands on → “Paris”
Run 2: dice lands on → “Lyon”
Run 3: dice lands on → “Paris”
The probabilities never changed. The random spin gave different results.
3 tokens at 33.3% each? → perfectly fair dice. Pure randomness.
“Italy is known for its history. It is also known for its culture. It is also known for its food. It is also known for...”
↑ Greedy decoding — always top token → gets stuck in loops! Sampling adds variety = natural, human-like text.
Same model, same prompt — only temperature changes the output.
Prompt: “Write a one-sentence summary of machine learning.”
“Machine learning is a subset of artificial intelligence that enables computers to learn from data without being explicitly programmed.”
Always the same output. Best for: data extraction, classification, factual Q&A
“Machine learning allows computers to discover patterns in data and improve their performance over time, much like how humans learn from experience.”
Creative but coherent. Best for: writing, brainstorming, chat. Most common setting.
“Machine learning unfolds as a computational symphony where algorithms dance through probabilistic landscapes to sculpt emergent understanding.”
Surprising but can be incoherent. Rarely used in production.
βοΈ 0 ββββ 0.7 ββββ 1.0 ββββ 1.5+ π
precise, repeatable ββ creative, surprising
| Model | Parameters | Training Data |
|---|---|---|
| BERT (2018) | 340M | 16 GB |
| GPT-3 (2020) | 175B | 570 GB |
| LLaMA 3 (2024) | 405B | 15T tokens |
| GPT-4 (2023) | ~1.8T* | ~13T tokens |
*estimated
The standard metric: "on average, how many tokens is the model choosing between?"
| Model | Perplexity β |
|---|---|
| GPT-2 1.5B (2019) | ~35 |
| GPT-3 175B (2020) | ~20 |
| LLaMA 7B (2023) | ~7 |
| LLaMA 65B (2023) | ~4 |
Approximate, varies by benchmark dataset
π Touvron et al., "LLaMA: Open and Efficient Foundation Language Models", Meta 2023
The context window is the model's working memory β how much text it can "see" at once.
| Model | Context Window | β Pages of Text |
|---|---|---|
| GPT-3.5 | 4K tokens | ~3 pages |
| GPT-4 | 128K tokens | ~100 pages |
| Claude 3.5 | 200K tokens | ~150 pages |
| Gemini 1.5 | 1M+ tokens | ~750 pages |
Three levels of effort — from building a model to just talking to it:
Train from scratch on massive internet data. Only done by Google, Meta, OpenAI, etc.
Adapt a pre-trained model with your data → Lectures 8–9
Zero training — just write clever instructions → Lecture 2
Pre-training alone produces a text completer, not an assistant. RLHF bridges the gap.
Result: knows language, facts, patterns. But not helpful yet.
Result: can answer questions. But sometimes verbose or unhelpful.
Result: helpful, harmless, honest. This is ChatGPT.
Maximize reward while staying close to original model via KL divergence penalty
📄 Ouyang et al., "Training language models to follow instructions with human feedback", NeurIPS 2022
Lecture 2 will cover:
β
Thank you!
Kamyar Zeinalipour — Zucchetti LLM Course