This lecture covers critical improvements to the original transformer architecture, classifies modern transformer models into three core families, and provides a comprehensive deep dive into BERT, its pre-training objectives, and optimized variants.
Queries: What the current token is looking for in the sequence
Keys: What each token in the sequence has to offer
Values: The actual semantic content of each token
Attention(Q, K, V) = softmax(QKᵀ/√dₖ)VLearned position embeddings: Each position in the sequence has its own trainable embedding, which is added element-wise to the token embedding. This method is simple but cannot generalize to sequence lengths longer than those seen during training and can learn spurious biases from the training data.
Sinusoidal position embeddings: Used in the original transformer paper, these embeddings are generated using fixed sine and cosine functions of different frequencies. The dot product between two sinusoidal embeddings depends only on their relative distance, aligning with the intuition that closer tokens should be more similar. This method generalizes to arbitrary sequence lengths but is less flexible than learned embeddings.
ALiBi (Attention with Linear Biases): Instead of adding position information to the input embeddings, ALiBi adds a linear bias term directly to the attention scores, penalizing attention between tokens that are further apart. This method is simple, deterministic, and generalizes exceptionally well to long sequences.
Rotary Position Embeddings (RoPE): The current industry standard, RoPE rotates the query and key vectors by an angle proportional to their position in the sequence. The dot product between rotated queries and keys depends only on their relative distance, naturally encoding positional information. RoPE combines the best properties of all previous methods: it generalizes to arbitrary sequence lengths, encodes relative position naturally, and delivers state-of-the-art performance.
Sparse sliding window attention: Instead of allowing each token to attend to all other tokens, each token only attends to a fixed-size window of neighboring tokens. This reduces complexity to O(n) and is used in models like Longformer and Mistral. Multiple layers of sliding window attention create a large effective receptive field, similar to convolutions in computer vision.
Shared attention heads: To reduce memory usage during inference, modern models share projection matrices for keys and values across multiple attention heads. There are three standard variants:
Multi-Head Attention (MHA): Each head has its own query, key, and value projections (original transformer)
Multi-Query Attention (MQA): All heads share a single key and value projection
Group Query Attention (GQA): Heads are divided into groups, and each group shares a single key and value projection. This is the most common variant in modern LLMs, balancing performance and memory efficiency.
Encoder-decoder models: Retain both the encoder and decoder components of the original transformer. They are primarily used for sequence-to-sequence tasks like machine translation and summarization. The most prominent example is the T5 (Text-to-Text Transfer Transformer) family, which frames all NLP tasks as text-to-text problems and uses a span corruption pre-training objective. Variants include mT5 (multilingual) and ByT5 (byte-level tokenization).
Encoder-only models: Remove the decoder component entirely. They use bidirectional self-attention, meaning every token attends to every other token in the sequence. This makes them ideal for classification and token-level tasks like sentiment analysis, named entity recognition, and question answering. The most famous example is BERT, covered in detail below.
Decoder-only models: Remove the encoder component entirely. They use causal (masked) self-attention, where each token only attends to itself and previous tokens. This makes them ideal for text generation and is the architecture used by all modern large language models (GPT, Llama, Mistral, etc.).
<CLS> token: A placeholder token added to the beginning of every sequence. The final embedding of the <CLS> token is used as the aggregate representation of the entire sequence for classification tasks.
<SEP> token: A separator token used to distinguish between two sentences in paired tasks like next sentence prediction.
Token embeddings: Learned embeddings for each token in the vocabulary (BERT uses the WordPiece tokenizer with a vocabulary size of approximately 30,000).
Position embeddings: Learned embeddings that encode the position of each token in the sequence.
Segment embeddings: Learned embeddings that indicate which sentence a token belongs to (either segment A or segment B), used for paired tasks.
Masked Language Model (MLM): 15% of tokens in the input are randomly selected and modified. 80% of the time they are replaced with a special <MASK> token, 10% of the time they are replaced with a random token, and 10% of the time they are left unchanged. The model is trained to predict the original token. This objective forces the model to learn deep bidirectional context.
Next Sentence Prediction (NSP): The model is given pairs of sentences and trained to predict whether the second sentence naturally follows the first. This objective was intended to teach the model sentence-level relationships, but later research showed it provides minimal practical benefit.
Sentiment analysis: Linear layer on top of the <CLS> token to predict sentiment polarity.
Named entity recognition: Linear layer on top of each token to predict entity types.
Question answering: Two linear layers on top of each token to predict the start and end positions of the answer span.
DistilBERT: Uses knowledge distillation to train a smaller, faster student model that mimics the output distribution of a larger teacher BERT model. DistilBERT has 50% fewer parameters and runs 60% faster while retaining 97% of the performance of the original BERT base model.
RoBERTa (Robustly Optimized BERT Pretraining Approach): Improves BERT by removing the NSP objective entirely, using dynamic masking (changing the masked tokens every epoch), training on 10x more data, and using longer sequence lengths. These changes deliver significant performance improvements on all downstream NLP benchmarks.

