Transformers

This section covers attention and the transformer, which replaced recurrent networks as the standard model for sequences. The path starts from the bottleneck of the RNN encoder-decoder. 7.1. Attention removes it with a lookup table that gradient descent can train. A transformer stacks blocks of multi-head attention and MLPs with residual connections, as in 7.2. Transformer Architecture. Text enters through tokens and embeddings, and 7.4. Positional Encoding supplies the order of the tokens. 7.5. Encoder and Decoder Models compares the three ways to use the stack, with BERT and GPT-3 as examples. Lecture 18 covers attention, and lecture 19 covers the architecture, the embedding layer and positional encoding.

Prerequisites

Contents

  • 7.1. Attention: the hash table made differentiable, queries, keys and values, scaled dot-product attention, self, causal and cross-attention, the causal mask, permutation equivariance, the scale, softmax against argmax, and attention maps.
    • 7.1.1. Multi-Head Attention: heads, concatenation, the parameter count, the KV cache, MQA and GQA. Not taught: uptraining to GQA.
    • 7.1.2. Efficient Attention: the quadratic cost, sparse attention and FlashAttention with the online softmax. Not taught: everything except the quadratic cost. Kernelized and ridge attention come from HW10.
  • 7.2. Transformer Architecture: the five steps from the RNN encoder-decoder, the block, why the MLP is needed, block wiring, pre-LN and post-LN, RMSNorm, parameter and compute counts, and the weak inductive bias. Not taught: why post-LN needs a learning-rate warm-up.
  • 7.3. Token Embeddings: the embedding layer as a lookup, the de-embedding layer, their RMS scales, and static against contextual embeddings. Not taught: weight tying.
    • 7.3.1. Tokenization: characters, words and subwords, byte pair encoding, unique parses and the size of the vocabulary.
    • 7.3.2. Word2vec: the skip-gram model, negative sampling, analogies and the limits of static vectors. Not taught: the sampling details of Mikolov et al.
  • 7.4. Positional Encoding: sinusoidal encodings, NoPE, the learned relative bias and RoPE. Not taught: the general form of RoPE, why NoPE works, and learned absolute encodings.
  • 7.5. Encoder and Decoder Models: encoder-only, decoder-only and encoder-decoder models, BERT’s pretraining and use downstream, GPT-3 and few-shot prompting, and cross-attention to the encoder.

The Fall 2022 and Spring 2023 notes in the sources come from earlier offerings of the course with Anant Sahai. Content from them is not marked as untaught.

Three Forms of Attention

FormQueries fromKeys and values fromMaskUsed in
Self-attentionthe sequencethe same sequencenoneencoder blocks
Causal self-attentionthe sequencethe same sequence, earlier positions onlylower triangulardecoder blocks
Cross-attentionthe decoderthe encoder’s outputnoneencoder-decoder models

Recommended Path

Read 5.3.3. Sequence-to-Sequence Models first, then 7.1. Attention and 7.1.1. Multi-Head Attention. Continue with 7.2. Transformer Architecture, then 7.3. Token Embeddings and 7.4. Positional Encoding, which describe the input layer. Finish with 7.5. Encoder and Decoder Models. 7.1.2. Efficient Attention, 7.3.1. Tokenization and 7.3.2. Word2vec can be read at any point after their parent notes. 9.1. LLMs continues with decoder-only models at scale.