Large Language Models (LLMs) are essentially stacks of Transformer blocks.
Transformer = a specialized neural-network architecture that generalizes feed-forward nets by replacing recurrence with the “attention” operation.
Core intuition: every token can dynamically look at ("attend to") every other token in its context and decide—by learned similarity scores—how much information to borrow.
Timeline of Language-Modeling Milestones
1990 – Static Word Embeddings
Hand-crafted or shallow‐learned vectors (e.g., word2vec, GloVe).
2003 – Neural Language Model
Bengio et al. introduce end-to-end neural probability models for sequences.
2008 – Multi-Task Learning
Jointly training on heterogeneous objectives.
2015 – Attention
Bahdanau attention for sequence-to-sequence translation.
2017 – Transformer
Vaswani et al. remove recurrence, make attention the complete workhorse.
Softmax over allowed keys ⇒ attention weights: α<em>ij=softmax</em>j(score(i,j))
Output vector: a<em>i=∑</em>j≤iα<em>ijv</em>j
Step-by-step Example (computing a3)
Compute q<em>3,k</em>1:3,v1:3.
Dot-product q<em>3⋅k</em>j for $j=1..3$.
Scale by dk.
Apply softmax to obtain weights $\alpha_{3j}$.
Multiply each v<em>j by α</em>3j.
Sum to obtain a3.
Multi-Head Attention
Instead of one set $(WQ,WK,W_V)$, use $h$ independent sets ⇒ $h$ heads.
Each head learns to focus on different relations: syntax, coreference, negation, positional offsets, etc.
Pipeline per head $t$: head<em>t=Attention(XW</em>Q(t),XW<em>K(t),XW</em>V(t))
Concatenate heads and apply output projection W<em>O:
MHA(X)=[head</em>1;…;head<em>h]W</em>O
Transformer Block Architecture
One residual stream per token propagates up the stack.
Each block performs (with residual connections):
LayerNormt<em>1=LayerNorm(x</em>i)
Multi-Head Attentiont<em>2=MHA(t</em>1)
Residual add t<em>2=x</em>i+t2
LayerNormt<em>3=LayerNorm(t</em>2)
Feed-Forward Networkt<em>4=FFN(t</em>3)
Residual add x<em>inext=t</em>2+t4
FFN: two linear layers with non-linearity (ReLU or GELU) FFN(z)=max(0,zW<em>1+b</em>1)W<em>2+b</em>2
LayerNorm: z-score over features of one vector.
Stack $L$ blocks ⇒ same dimensionality d throughout.
Information-Flow Perspective
Every component except attention only touches its own residual stream.
Attention literally moves information between streams (Elhage et al., 2021).
Parallelizing Attention Computation
Pack tokens into matrix X∈RN×d (rows = tokens).
Compute all queries, keys, values in one matmul: Q=XW<em>Q,K=XW</em>K,V=XW<em>V (shapes N×d</em>k, etc.)
Score matrix via batched dot-product: S=QKT (shape N×N).
Apply scaling, causal mask (upper triangle set to −∞), softmax row-wise ⇒ A.
Output matrix: O=AV (shape N×dv).
Quadratic cost O(N2) in sequence length ⇒ motivation for efficient/long-range variants.
Token & Position Embeddings
Initial input matrix X is sum of:
Token embedding from matrix E∈R∣V∣×d.
Positional embedding Epos∈RN×d (learned absolute positions in this lecture).
Example workflow (BPE-tokenized string "Thanks for all the"):
Token indices [5,4000,10532,2224].
Lookup rows in E.
Add corresponding positional rows [0,1,2,3].
Language Modeling Head
After $L$ blocks, final hidden matrix H∈RN×d.
Unembedding layer: linear map back to vocabulary logits using tied weightsET: U=HET (shape N×∣V∣).
Softmax over each row produces predictive distribution P(w<em>t+1∣context):
P</em>ti=∑</em>jeUtjeU<em>ti.
“Weight tying” constrains parameters and empirically improves generalization.
The Final Transformer Language Model
Pipeline summary for one training step:
Tokenize input sequence (max length N).
Form X=E[tokens]+Epos[0:N].
Pass through stacked Transformer blocks (with masking).
Compute logits via unembedding; softmax gives probabilities.
Compute cross-entropy loss vs. true next-token labels; back-propagate to update all parameters W<em>Q(t),W</em>K(t),…,E,Epos.
Practical, Philosophical & Ethical Notes
Attention’s context integration yields interpretability hooks (e.g., visualize heads for syntax or coreference).
Quadratic complexity poses environmental cost ⇒ active research on linear/efficient attention.
Weight tying re-uses parameters, shrinking model size and aligning embedding & predictive spaces, often improving fairness by consistent treatment of rare words.
Contextual embeddings revolutionize downstream NLP by providing task-agnostic meaning representations fine-tuned with minimal labeled data.
Key Takeaways
Transformers convert symbolic sequences into rich context-dependent vectors through stacked blocks of LayerNorm → Multi-Head Attention → FFN with residuals.