Home/AI/ML Notes/GenAI and LLMs Summary/Transformer Architecture
⌂ Main Page
GenAI and LLMs Summary

Transformer Architecture

The transformer stack, input representation, subword tokenization, positional embeddings (absolute, relative, RoPE), residual connections, and Transformer-XL.

On this page

TRANSFORMERS

Transformer contains a stack of Encoder layers (Encoders) and Decoder layers. The Encoder stack and the Decoder stack each have their corresponding Embedding layers. Output layer generates the final output.

Data inputs for both the Encoder and Decoder, which contains:

Each Encoder in the encoder stack contains:

Each Decoder in the decoder stack contains:

Output (top right) — generates the final output, and contains:

Input Representation in Transformers

Subword tokenization – splits rare words into smaller units to deal with complex words and misspellings, but keeping frequent words as unique entities (more efficient + managing the size).

  1. Byte Pair Encoding (BPE) - used by OpenAI (GPT-2/3) and Meta (RoBERTa). Starts with characters, iteratively merges the most frequent pairs of adjacent symbols until desired vocabulary size. E.g. "lowest" → "low" + "est".

  2. WordPiece - introduced by Google, used in BERT, DistilBERT. Starts with characters, iteratively merges based on maximizing likelihood (proba) of training corpus. Algo compares likelihood of training corpus tokenized with vs. without the merged token => the merge that gives the biggest likelihood increase wins. This merge typically has fewer tokens + more frequent subwords have higher probabilities. E.g., "playing" → "play" + "##ing". Disambiguation: SentencePiece = library that implements WordPiece, BPE, other tokenization algos.

Positional embeddings - vectors that encode the position of each token in seq because unlike RNNs / LSTMs transformers don’t understand the order of input tokens (crucial for NLU). Input embeddings represent tokens in a high-D space where similar tokens are closer together, but they don’t contain order info. Positional embeds + input embeds = transformer understands both meaning AND position of tokens.

Absolute Positional Embeddings - Encode exact position in seq (pos. 0, 1, 2, 3...) with a fixed representation regardless of context. Position 5 always gets the same encoding which doesn't depend on other pos. in seq. Limited generalization to longer unseen sequences.

1. Fixed (Sinusoidal) Positional Encodings

2. Learned Positional Embeddings

Relative Positional Embeddings - encode relative distance betw. positions: how far apart two tokens are (pairwise relations), e.g. encoding for pos. 5 attending to pos. 3 is based on the offset (-2). Better generalization to unseen sequence lengths. Used in Transformer-XL, T5 (relative bias), Llama, GPT-NeoX (RoPE).

Key Advantage of Relative (like RoPE) - translation invariance: relationship between token #5 & token #9 is same as between #105 & #108. More efficient for tasks where relative order matters more than absolute position.

  1. Rotary Positional Embeddings (RoPE) - power long-context in most modern LLMs. Attention cares about relative distance, not absolute position. RoPE makes long-context generalization far more stable than learned position embeddings because it encodes relative offsets directly inside attention which is in line with how attention works => extrapolates better to longer sequences.

How RoPE works:

Residual connections - output of each sub-layer (MHA + FFN) is added to its input, and the sum is normalized (layer normalization) before being fed to the next sub-layer. Allows model to preserve info from earlier layers and addresses vanishing gradients and the fact that with greater network depth (more layers in Dl model), accuracy gets saturated and degrades rapidly. H(x) = F (x) + x, but network still trains by SGD w/backpropagation.

A diagram of a graph AI-generated content may be incorrect.

Transformer-XL (w/extra-long context) overcomes issues w/long-range dependencies (translation and language modeling) by implementing a recursive mechanism that connects different segments => allows to store and access info from prev. segments.

Vision Transformers (ViT) - image = sequence of patches. Image classification, competes with CNNs, achieves SOTA results on ImageNet.