Complete Technical Paper Content β Attention Mechanisms Ai
1. Executive Abstract, Problem Formulation & Background
explore-attention Attention Mechanisms in AI β A Comprehensive Guide π§ Understanding Attention What is Attention? Attention is a mechanism that allows models to focus on different parts of the input sequence when making predictions. Think of it like how humans pay attention to different words when reading a sentence or different parts of an image when looking at it. Why Attention? Long-Range Dependencies : Traditional RNNs struggle with long sequences. Attention helps capture relationships between distant elements. Parallelization : Unlike RNNs, attention can be computed in parallel, making it more efficient. Interpretability : Attention weights provide insights into what the model is focusing on. The Math Behind Attention The core of attention is the attention score computation: Attention(Q, K, V) = softmax(QK^T/βd_k)V Where: Q: Query matrix K: Key matrix V: Value matrix d_k: Dimension of the key vectors Visual representation of the computation flow: Input: [x1, x2, x3, x4] β β β β βΌ βΌ βΌ βΌ Q = [q1, q2, q3, q4] K = [k1, k2, k3, k4] V = [v1, v2, v3, v4] β β β β β β β β β β β β ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ β β β βΌ βΌ βΌ QK^T/βd_k = [scores] β softmax β [weights] β V = [output] π― Types of Attention 1. Basic Attention Mechanisms Additive Attention (Bahdanau) Uses a feed-forward neural network to compute attention scores More computationally expensive but can be more powerful Formula: score(q, k) = v^T * tanh(W1*q + W2*k) Visual representation: Query (q) βββ ββ> W1*q + W2*k ββ> tanh ββ> v^T ββ> score Key (k) βββββ Example with actual values: Input: q = [0.1, 0.2, 0.3], k = [0.4, 0.5, 0.6] W1 = [[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]] W2 = [[0.2, 0.3], [0.4, 0.5], [0.6, 0.7]] v = [0.1, 0.2] Computation: 1. W1*q = [0.14, 0.32, 0.50] 2. W2*k = [0.23, 0.41, 0.59] 3. Sum = [0.37, 0.73, 1.09] 4. tanh = [0.35, 0.62, 0.80] 5. v^T * result = 0.255 Multiplicative Attention (Luong) Uses dot product between query and key vectors More computationally efficient Formula: score(q, k) = q^T * k Visual representation: Query (q) βββ ββ> dot product ββ> score Key (k) βββββ Example with actual values: Input: q = [0.1, 0.2, 0.3], k = [0.4, 0.5, 0.6] Computation: 0.1*0.4 + 0.2*0.5 + 0.3*0.6 = 0.32 2. Self-Attention Self-attention allows each position to attend to all positions in the sequence: SelfAttention(X) = softmax(XW_Q(XW_K)^T/βd_k)XW_V Key components: Query projection: XW_Q Key projection: XW_K Value projection: XW_V Scaled dot-product: QK^T/βd_k Softmax normalization Weighted sum with values Visual representation of self-attention flow: Input: [x1, x2, x3, x4] β β β β βΌ βΌ βΌ βΌ Q = [q1, q2, q3, q4] K = [k1, k2, k3, k4] V = [v1, v2, v3, v4] β β β β β β β β β β β β ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ β β β βΌ βΌ βΌ QK^T/βd_k = [scores] β softmax β [weights] β V = [output] Example with a simple sentence: Input: "The cat sat on the mat" Step 1: Token Embeddings [0.1, 0.2, 0.3] # "The" [0.4, 0.5, 0.6] # "cat" [0.7, 0.8, 0.9] # "sat" [0.2, 0.3, 0.4] # "on" [0.1, 0.2, 0.3] # "the" [0.5, 0.6, 0.7] # "mat" Step 2: Self-Attention Weights [0.8 0.1 0.0 0.0 0.0 0.1] # "The" [0.1 0.7 0.1 0.0 0.0 0.1] # "cat" [0.0 0.1 0.6 0.2 0.0 0.1] # "sat" [0.0 0.0 0.2 0.6 0.1 0.1] # "on" [0.0 0.0 0.0 0.1 0.7 0.2] # "the" [0.1 0.1 0.1 0.1 0.2 0.4] # "mat" 3. Multi-Head Attention Multi-head attention allows the model to jointly attend to information from different representation subspaces: MultiHead(Q, K, V) = Concat(head_1, ..., head_h)W^O where each head is computed as: head_i = Attention(QW^Q_i, KW^K_i, VW^V_i) Visual representation of multi-head attention: Input: [x1, x2, x3, x4] β β β β βΌ βΌ βΌ βΌ Head 1: [Q1, K1, V1] β Attention β [O1] Head 2: [Q2, K2, V2] β Attention β [O2] Head 3: [Q3, K3, V3] β Attention β [O3] Head 4: [Q4, K4, V4] β Attention β [O4] β β β β ββββββ΄βββββ΄βββββ β βΌ Concat + W^O β Output Example with different attention patterns: Head 1 (Local attention): [0.9 0.1 0.0 0.0 0.0 0.0] # Focuses on adjacent words [0.1 0.8 0.1 0.0 0.0 0.0] [0.0 0.1 0.8 0.1 0.0 0.0] [0.0 0.0 0.1 0.8 0.1 0.0] [0.0 0.0 0.0 0.1 0.8 0.1] [0.0 0.0 0.0 0.0 0.1 0.9] Head 2 (Global attention): [0.3 0.2 0.2 0.1 0.1 0.1] # Distributes attention globally [0.2 0.3 0.2 0.1 0.1 0.1] [0.2 0.2 0.3 0.1 0.1 0.1] [0.1 0.1 0.1 0.3 0.2 0.2] [0.1 0.1 0.1 0.2 0.3 0.2] [0.1 0.1 0.1 0.2 0.2 0.3] Head 3 (Subject-verb attention): [0.1 0.8 0.1 0.0 0.0 0.0] # Focuses on subject-verb relationships [0.8 0.1 0.1 0.0 0.0 0.0] [0.1 0.1 0.1 0.7 0.0 0.0] [0.0 0.0 0.7 0.1 0.1 0.1] [0.0 0.0 0.0 0.1 0.1 0.8] [0.0 0.0 0.0 0.1 0.8 0.1] 4. Cross-Attention Cross-attention allows one sequence to attend to another sequence: CrossAttention(Q, K, V) = softmax(QK^T/βd_k)V Visual representation: Source: [s1, s2, s3] Target: [t1, t2, t3, t4] β β β β β β β βΌ βΌ βΌ βΌ βΌ βΌ βΌ K,V = [k1, k2, k3] Q = [q1, q2, q3, q4] β β β β β β β βββββ΄ββββ βββββ΄ββββ΄ββββ β β βΌ βΌ Attention Weights β Weighted Sum β Output Example in machine translation: Source: "The cat sat" Target: "Le chat s'est assis" Attention Weights: [0.8 0.1 0.1] # "Le" β "The" [0.1 0.8 0.1] # "chat" β "cat" [0.1 0.1 0.8] # "s'est" β "sat" [0.1 0.1 0.8] # "assis" β "sat" π¨ Attention Visualizations 1. Basic Attention Pattern Input: "The cat sat on the mat" Attention Weights: [0.8 0.1 0.0 0.0 0.0 0.1] # "The" [0.1 0.7 0.1 0.0 0.0 0.1] # "cat" [0.0 0.1 0.6 0.2 0.0 0.1] # "sat" [0.0 0.0 0.2 0.6 0.1 0.1] # "on" [0.0 0.0 0.0 0.1 0.7 0.2] # "the" [0.1 0.1 0.1 0.1 0.2 0.4] # "mat" 2. Multi-Head Attention Patterns Different heads can learn different patterns: Head 1 (Local attention): [0.9 0.1 0.0 0.0 0.0 0.0] [0.1 0.8 0.1 0.0 0.0 0.0] [0.0 0.1 0.8 0.1 0.0 0.0] [0.0 0.0 0.1 0.8 0.1 0.0] [0.0 0.0 0.0 0.1 0.8 0.1] [0.0 0.0 0.0 0.0 0.1 0.9] Head 2 (Global attention): [0.3 0.2 0.2 0.1 0.1 0.1] [0.2 0.3 0.2 0.1 0.1 0.1] [0.2 0.2 0.3 0.1 0.1 0.1] [0.1 0.1 0.1 0.3 0.2 0.2] [0.1 0.1 0.1 0.2 0.3 0.2] [0.1 0.1 0.1 0.2 0.2 0.3] 3. Cross-Attention in Translation English: "The cat sat on the mat" French: "Le chat s'est assis sur le tapis" Attention Weights: [0.8 0.1 0.0 0.0 0.0 0.1] # "Le" β "The" [0.1 0.8 0.1 0.0 0.0 0.0] # "chat" β "cat" [0.0 0.1 0.7 0.1 0.0 0.1] # "s'est" β "sat" [0.0 0.0 0.1 0.7 0.1 0.1] # "assis" β "sat" [0.0 0.0 0.0 0.1 0.7 0.2] # "sur" β "on" [0.0 0.0 0.0 0.0 0.1 0.9] # "le" β "the" [0.1 0.1 0.1 0.1 0.1 0.5] # "tapis" β "mat" π Applications 1. Machine Translation Source sentence: "The cat sat on the mat" Target sentence: "Le chat s'est assis sur le tapis" Attention helps align words between languages Example attention pattern: English: The cat sat on the mat French: Le chat s'est assis sur le tapis β β β β β β β βΌ βΌ βΌ βΌ βΌ βΌ βΌ [0.8 0.1 0.0 0.0 0.0 0.0 0.1] # "Le" [0.1 0.8 0.1 0.0 0.0 0.0 0.0] # "chat" [0.0 0.1 0.7 0.1 0.0 0.0 0.1] # "s'est" [0.0 0.0 0.1 0.7 0.1 0.0 0.1] # "assis" [0.0 0.0 0.0 0.1 0.7 0.1 0.1] # "sur" [0.0 0.0 0.0 0.0 0.1 0.8 0.1] # "le" [0.1 0.1 0.1 0.1 0.1 0.1 0.4] # "tapis" 2. Image Recognition Vision Transformers (ViT) use attention to process images Patches attend to other patches to understand relationships Example: Object detection with attention weights Example patch attention: Image divided into 16x16 patches: [P1 P2 P3 P4] [P5 P6 P7 P8] [P9 P10 P11 P12] [P13 P14 P15 P16] Attention weights for P6 (cat's face): [0.1 0.8 0.1 0.0] # High attention to P2 (cat's ear) [0.7 0.1 0.1 0.1] # High attention to P5 (cat's body) [0.1 0.1 0.1 0.1] # Low attention to P9-P12 [0.1 0.1 0.1 0.1] # Low attention to P13-P16 3. Audio Processing Speech recognition using attention Audio classification with attention patterns Example: Speaker diarization Example audio attention: Time steps: [t1 t2 t3 t4 t5 t6] Speaker 1: [0.8 0.7 0.1 0.1 0.1 0.1] Speaker 2: [0.1 0.1 0.8 0.7 0.1 0.1] Speaker 3: [0.1 0.1 0.1 0.1 0.8 0.7] 4. Multimodal Systems Combining attention across different modalities Example: Image captioning with attention Example multimodal attention: Image regions: [R1 R2 R3 R4] Text tokens: [T1 T2 T3 T4] Attention weights: [0.8 0.1 0.1 0.0] # T1 β R1 (high attention) [0.1 0.7 0.1 0.1] # T2 β R2 (high attention) [0.1 0.1 0.6 0.2] # T3 β R3 (high attention) [0.0 0.1 0.2 0.7] # T4 β R4 (high attention) π Advanced Topics 1. Attention Variants Sparse Attention Linear Attention Local Attention Global Attention 2. Positional Information Sinusoidal Positional Encoding Learned Positional Encoding Relative Positional Encoding 3. Attention in Transformers Encoder-Decoder Architecture Layer Normalization Feed-Forward Networks Residual Connections π Further Reading "Attention Is All You Need" (Vaswani et al., 2017) "BERT: Pre-training of Deep Bidirectional Transformers" (Devlin et al., 2019) "An Image is Worth 16x16 Words: Transformers for Image Recognition" (Dosovitskiy et al., 2021) π€ Contributing Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change. π License This project is licensed under the MIT License - see the LICENSE file for details.
Attention is a mechanism that allows models to focus on different parts of the input sequence when making predictions. Think of it like how humans pay attention to different words when reading a sentence or different parts of an image when looking at it.
Input: [x1, x2, x3, x4] β β β β βΌ βΌ βΌ βΌ Q = [q1, q2, q3, q4] K = [k1, k2, k3, k4] V = [v1, v2, v3, v4] β β β β β β β β β β β β ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ β β β βΌ βΌ βΌ QK^T/βd_k = [scores] β softmax β [weights] β V = [output]
Input: q = [0.1, 0.2, 0.3], k = [0.4, 0.5, 0.6] W1 = [[0.1, 0.2], [0.3, 0.4], [0.5, 0.6]] W2 = [[0.2, 0.3], [0.4, 0.5], [0.6, 0.7]] v = [0.1, 0.2] Computation: 1. W1*q = [0.14, 0.32, 0.50] 2. W2*k = [0.23, 0.41, 0.59] 3. Sum = [0.37, 0.73, 1.09] 4. tanh = [0.35, 0.62, 0.80] 5. v^T * result = 0.255
Input: q = [0.1, 0.2, 0.3], k = [0.4, 0.5, 0.6] Computation: 0.1*0.4 + 0.2*0.5 + 0.3*0.6 = 0.32
Input: [x1, x2, x3, x4] β β β β βΌ βΌ βΌ βΌ Q = [q1, q2, q3, q4] K = [k1, k2, k3, k4] V = [v1, v2, v3, v4] β β β β β β β β β β β β ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ ββββββ΄βββββ΄βββββ β β β βΌ βΌ βΌ QK^T/βd_k = [scores] β softmax β [weights] β V = [output]
Input: "The cat sat on the mat" Step 1: Token Embeddings [0.1, 0.2, 0.3] # "The" [0.4, 0.5, 0.6] # "cat" [0.7, 0.8, 0.9] # "sat" [0.2, 0.3, 0.4] # "on" [0.1, 0.2, 0.3] # "the" [0.5, 0.6, 0.7] # "mat" Step 2: Self-Attention Weights [0.8 0.1 0.0 0.0 0.0 0.1] # "The" [0.1 0.7 0.1 0.0 0.0 0.1] # "cat" [0.0 0.1 0.6 0.2 0.0 0.1] # "sat" [0.0 0.0 0.2 0.6 0.1 0.1] # "on" [0.0 0.0 0.0 0.1 0.7 0.2] # "the" [0.1 0.1 0.1 0.1 0.2 0.4] # "mat"
Multi-head attention allows the model to jointly attend to information from different representation subspaces:
2. Related Work, Literature Survey & Algorithmic Foundations
Input: [x1, x2, x3, x4] β β β β βΌ βΌ βΌ βΌ Head 1: [Q1, K1, V1] β Attention β [O1] Head 2: [Q2, K2, V2] β Attention β [O2] Head 3: [Q3, K3, V3] β Attention β [O3] Head 4: [Q4, K4, V4] β Attention β [O4] β β β β ββββββ΄βββββ΄βββββ β βΌ Concat + W^O β Output
[0.9 0.1 0.0 0.0 0.0 0.0] # Focuses on adjacent words [0.1 0.8 0.1 0.0 0.0 0.0] [0.0 0.1 0.8 0.1 0.0 0.0] [0.0 0.0 0.1 0.8 0.1 0.0] [0.0 0.0 0.0 0.1 0.8 0.1] [0.0 0.0 0.0 0.0 0.1 0.9]
[0.3 0.2 0.2 0.1 0.1 0.1] # Distributes attention globally [0.2 0.3 0.2 0.1 0.1 0.1] [0.2 0.2 0.3 0.1 0.1 0.1] [0.1 0.1 0.1 0.3 0.2 0.2] [0.1 0.1 0.1 0.2 0.3 0.2] [0.1 0.1 0.1 0.2 0.2 0.3]
[0.1 0.8 0.1 0.0 0.0 0.0] # Focuses on subject-verb relationships [0.8 0.1 0.1 0.0 0.0 0.0] [0.1 0.1 0.1 0.7 0.0 0.0] [0.0 0.0 0.7 0.1 0.1 0.1] [0.0 0.0 0.0 0.1 0.1 0.8] [0.0 0.0 0.0 0.1 0.8 0.1]
Source: [s1, s2, s3] Target: [t1, t2, t3, t4] β β β β β β β βΌ βΌ βΌ βΌ βΌ βΌ βΌ K,V = [k1, k2, k3] Q = [q1, q2, q3, q4] β β β β β β β βββββ΄ββββ βββββ΄ββββ΄ββββ β β βΌ βΌ Attention Weights β Weighted Sum β Output
Source: "The cat sat" Target: "Le chat s'est assis" Attention Weights: [0.8 0.1 0.1] # "Le" β "The" [0.1 0.8 0.1] # "chat" β "cat" [0.1 0.1 0.8] # "s'est" β "sat" [0.1 0.1 0.8] # "assis" β "sat"
Input: "The cat sat on the mat" Attention Weights: [0.8 0.1 0.0 0.0 0.0 0.1] # "The" [0.1 0.7 0.1 0.0 0.0 0.1] # "cat" [0.0 0.1 0.6 0.2 0.0 0.1] # "sat" [0.0 0.0 0.2 0.6 0.1 0.1] # "on" [0.0 0.0 0.0 0.1 0.7 0.2] # "the" [0.1 0.1 0.1 0.1 0.2 0.4] # "mat"
[0.9 0.1 0.0 0.0 0.0 0.0] [0.1 0.8 0.1 0.0 0.0 0.0] [0.0 0.1 0.8 0.1 0.0 0.0] [0.0 0.0 0.1 0.8 0.1 0.0] [0.0 0.0 0.0 0.1 0.8 0.1] [0.0 0.0 0.0 0.0 0.1 0.9]
3. Dataset Ingestion, Preprocessing & Stochastic Augmentations
[0.3 0.2 0.2 0.1 0.1 0.1] [0.2 0.3 0.2 0.1 0.1 0.1] [0.2 0.2 0.3 0.1 0.1 0.1] [0.1 0.1 0.1 0.3 0.2 0.2] [0.1 0.1 0.1 0.2 0.3 0.2] [0.1 0.1 0.1 0.2 0.2 0.3]
English: "The cat sat on the mat" French: "Le chat s'est assis sur le tapis" Attention Weights: [0.8 0.1 0.0 0.0 0.0 0.1] # "Le" β "The" [0.1 0.8 0.1 0.0 0.0 0.0] # "chat" β "cat" [0.0 0.1 0.7 0.1 0.0 0.1] # "s'est" β "sat" [0.0 0.0 0.1 0.7 0.1 0.1] # "assis" β "sat" [0.0 0.0 0.0 0.1 0.7 0.2] # "sur" β "on" [0.0 0.0 0.0 0.0 0.1 0.9] # "le" β "the" [0.1 0.1 0.1 0.1 0.1 0.5] # "tapis" β "mat"
English: The cat sat on the mat French: Le chat s'est assis sur le tapis β β β β β β β βΌ βΌ βΌ βΌ βΌ βΌ βΌ [0.8 0.1 0.0 0.0 0.0 0.0 0.1] # "Le" [0.1 0.8 0.1 0.0 0.0 0.0 0.0] # "chat" [0.0 0.1 0.7 0.1 0.0 0.0 0.1] # "s'est" [0.0 0.0 0.1 0.7 0.1 0.0 0.1] # "assis" [0.0 0.0 0.0 0.1 0.7 0.1 0.1] # "sur" [0.0 0.0 0.0 0.0 0.1 0.8 0.1] # "le" [0.1 0.1 0.1 0.1 0.1 0.1 0.4] # "tapis"
Image divided into 16x16 patches: [P1 P2 P3 P4] [P5 P6 P7 P8] [P9 P10 P11 P12] [P13 P14 P15 P16] Attention weights for P6 (cat's face): [0.1 0.8 0.1 0.0] # High attention to P2 (cat's ear) [0.7 0.1 0.1 0.1] # High attention to P5 (cat's body) [0.1 0.1 0.1 0.1] # Low attention to P9-P12 [0.1 0.1 0.1 0.1] # Low attention to P13-P16
Time steps: [t1 t2 t3 t4 t5 t6] Speaker 1: [0.8 0.7 0.1 0.1 0.1 0.1] Speaker 2: [0.1 0.1 0.8 0.7 0.1 0.1] Speaker 3: [0.1 0.1 0.1 0.1 0.8 0.7]
Image regions: [R1 R2 R3 R4] Text tokens: [T1 T2 T3 T4] Attention weights: [0.8 0.1 0.1 0.0] # T1 β R1 (high attention) [0.1 0.7 0.1 0.1] # T2 β R2 (high attention) [0.1 0.1 0.6 0.2] # T3 β R3 (high attention) [0.0 0.1 0.2 0.7] # T4 β R4 (high attention)
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Attention Mechanisms in AI β with theory, code, visualizations, and real-world examples.
4. Neural Network Backbone, Layer Specifications & Loss Formulations
Anti-hype LLM reading list Goals: Add links that are reasonable and good explanations of how stuff works. No hype and no vendor content if possible. Practical first-hand accounts of models in prod eagerly sought. Foundational Concepts Pre-Transformer Models The Illustrated Word2vec - A Gentle Intro to Word Embeddings in Machine Learning (YouTube) Transformers as Support Vector Machines Survey of LLMS Deep Learning Systems Fundamental ML Reading List Building Blocks What are embeddings Concepts from Operating Systems that Found their way into LLMS Talking about Large Language Models Language Modeling is Compression Vector Search - Long-Term Memory in AI Eight things to know about large language models The Bitter Lesson The Hardware Lottery The Scaling Hypothesis Tokenization LLM Course Foundational Deep Learning Papers (in semi-chronological order) Seq2Seq Attention is all you Need BERT GPT-1 Scaling Laws for Neural Language Models T5 GPT-2: Language Models are Unsupervised Multi-Task Learners InstructGPT: Training Language Models to Follow Instructions GPT-3: Language Models are Few-Shot Learners The Transformer Architecture Transformers from Scratch Transformer Math Five Years of GPT Progress Lost in the Middle: How Language Models Use Long Contexts Attention Self-attention and transformer networks Attention Understanding and Coding the Attention Mechanism Attention Mechanisms Keys, Queries, and Values GPT What is ChatGPT doing and why does it work My own notes from a few months back. Karpathy's The State of GPT (YouTube) OpenAI Cookbook Significant OSS Models Llama2 Mistral7B Mixtral Phi2 Falcon7B LLMs in 2023 Catching up on the weird world of LLMS How open are open architectures? Building an LLM from Scratch Large Language Models in 2023 and Slides Timeline of Transformer Models Large Language Model Evolutionary Tree Training Data What's in my Big Data "The βitβ in AI models is the dataset." Extracting Training Data from ChatGPT Pre-Training Why host your own LLM? How to train your own LLMs Hugging Face Resources on Training Your Own Training Compute-Optimal Large Language Models Opt-175B Logbook RLHF and DPO RLHF Supervised Fine-tuning How Abilities in LLMs Are Affected by SFT Instruction-tuning for LLMs: Survey Direct Preference Optimization: Your Language Model is Secretly a Reward Model RLHF and DPO Compared Fine-Tuning and Compression The Complete Guide to LLM Fine-tuning LLaMAntino: LLaMA 2 Models for Effective Text Generation in Italian Language - Really great overview of SOTA fine-tuning techniques On the Structural Pruning of Large Language Models Quantiztion A Gentle Introduction to 8-bit matrix multiplication Which Quantization Method is Right for You? Survey of Quantization for Inference PEFT Fine-tuning with LoRA and QLoRA Adapters Motivation for Parameter-Efficient Fine-tuning Small and Local LLMs How is LlamaCPP Possible? How to beat GPT-4 with a 13-B Model Efficient LLM Inference on CPUs Tiny Language Models Come of Age Efficiency LLM Spectrum TinyML at MIT Deployment and Production Building LLM Applications for Production Challenges and Applications of Large Language Models All the Hard Stuff Nobody talks about when building products with LLMs Scaling Kubernetes to run ChatGPT Numbers every LLM Developer should know Against LLM Maximalism A Guide to Inference and Performance (InThe)WildChat: 570K ChatGPT Interaction Logs In The Wild The State of Production LLMs in 2023 Machine Learning Engineering for successful training of large language models and multi-modal models. Fine-tuning RedPajama on Slack Data LLM Inference and K-V Cache LLM Inference Performance Engineering: Best Practices How to Make LLMs go Fast Transformer Inference Arithmetic Which serving technology to use for LLMs? Speeding up the K-V cache Large Transformer Model Inference Optimization Prompt Engineering and RAG On Prompt Engineering Prompt Engineering Versus Blind Prompting Building RAG-Based Applications for Production Full Fine-Tuning, PEFT, or RAG? Prompt Engineering Guide GPUs The Best GPUS for Deep Learning 2023 Making Deep Learning Go Brr from First Principles Everything about Distributed Training and Efficient Finetuning Training LLMs at Scale with AMD MI250 GPUs GPU Programming Evaluation Evaluating ChatGPT ChatGPT: Jack of All Trades, Master of None What's Going on with the Open LLM Leaderboard Challenges in Evaluating AI Systems LLM Evaluation Papers Evaluating LLMs is a MineField Eval Frameworks HELM LM Eval Harness LmSys Chatbot Arena UX Generative Interfaces Beyond Chat (YouTube) Why Chatbots are not the Future The Future of Search is Boutique As a Large Language Model, I Natural Language is an Unnatural Interface What's Next? Thanks to everyone who added suggestions on Twitter , Mastodon , and Bluesky.
Goals: Add links that are reasonable and good explanations of how stuff works. No hype and no vendor content if possible. Practical first-hand accounts of models in prod eagerly sought.
Attention Mechanism Notebooks Introduction This repository contains a collection of Jupyter notebooks designed to explain and illustrate various attention mechanisms used in modern natural language processing and machine learning. The notebooks are intended for educational purposes, providing step-by-step implementations and visualizations of different attention techniques. These notebooks do not provide optimized implementations of any of these techniques. Start here Follow the notebooks in this order: Self-attention Causal masking Multi-head attention Efficient attention variants: Linformer , Longformer , Performer and sparse attention Visualisation notebook Scope: educational implementations from 2024. The repository does not include a locked execution environment or automated notebook checks. The notebooks illustrate mechanisms; they do not establish production performance or exact equivalence to the reference implementations. Contents Self Attention : Basics of the self-attention mechanism. Masked Self Attention : Implementation of masked self-attention used in decoder architectures. Multi-Headed Attention : Exploration of multi-headed attention as used in transformer models. Linformer : Implementation of the Linformer attention mechanism for efficient transformers. Longformer : Implementation of the Longformer efficient attention mechanism designed to handle very long sequences. Performer : Demonstration of the Performer attention mechanism using random feature approximation. Sparse Attention : Implementation of sparse attention patterns for efficient computation. Attention Visualization : Advanced visualization techniques for various attention mechanisms, including animated attention patterns and word-to-word attention visualization. Purpose The primary goal of these notebooks is to provide a clear, visual understanding of how different attention mechanisms work. Each notebook includes: Theoretical explanations of the attention mechanism Step-by-step implementation in PyTorch Visualizations of intermediate steps and outputs Comparisons with standard attention where applicable Important Note These implementations are for educational purposes only and are not optimized for production use. For real-world applications, it's recommended to use well-optimized implementations provided by libraries such as Hugging Face Transformers, PyTorch, or TensorFlow. Key Resources "Attention Is All You Need" by Vaswani et al. (2017) - The original transformer paper that introduced the self-attention mechanism. https://arxiv.org/abs/1706.03762 "The Illustrated Transformer" by Jay Alammar - An excellent visual guide to transformers and attention mechanisms. Many of the images used in the self-attention notebook are from this blog post. http://jalammar.github.io/illustrated-transformer/ "Linformer: Self-Attention with Linear Complexity" by Wang et al. (2020) - Introduces the Linformer mechanism for efficient attention computation. https://arxiv.org/abs/2006.04768 "Rethinking Attention with Performers" by Choromanski et al. (2020) - Presents the Performer attention mechanism using random feature approximation. https://arxiv.org/abs/2009.14794 "Generating Long Sequences with Sparse Transformers" by Child et al. (2019) - Introduces sparse attention patterns for efficient computation in transformers. https://arxiv.org/abs/1904.10509 Additional Reading "Efficient Transformers: A Survey" by Tay et al. (2020) - A comprehensive overview of various efficient transformer architectures. https://arxiv.org/abs/2009.06732 "A Survey of Long-Term Context in Transformers" by Peng et al. (2022) - Discusses various techniques for extending the context length in transformer models. https://arxiv.org/abs/2307.03170 Potential Additional Notebooks Big Bird : Exploration of the Big Bird attention pattern for efficient processing of long sequences. Reformer : Implementation of the Reformer architecture using locality-sensitive hashing for efficient attention. Sliding Window Attention : Demonstration of the sliding window attention pattern for local context modeling. Contributing Contributions to improve the notebooks or add new attention mechanisms are welcome. Please feel free to submit pull requests or open issues for discussion. Acknowledgments Jay Alammar for his excellent visualizations (which I use in self attention notebook ) and explanations in "The Illustrated Transformer"
This repository contains a collection of Jupyter notebooks designed to explain and illustrate various attention mechanisms used in modern natural language processing and machine learning. The notebooks are intended for educational purposes, providing step-by-step implementations and visualizations of different attention techniques. These notebooks do not provide optimized implementations of any of these techniques.
Scope: educational implementations from 2024. The repository does not include a locked execution environment or automated notebook checks. The notebooks illustrate mechanisms; they do not establish production performance or exact equivalence to the reference implementations.
The primary goal of these notebooks is to provide a clear, visual understanding of how different attention mechanisms work. Each notebook includes:
These implementations are for educational purposes only and are not optimized for production use. For real-world applications, it's recommended to use well-optimized implementations provided by libraries such as Hugging Face Transformers, PyTorch, or TensorFlow.
"Attention Is All You Need" by Vaswani et al. (2017) - The original transformer paper that introduced the self-attention mechanism. https://arxiv.org/abs/1706.03762
5. Training Protocol, Convergence Behavior & Experimental Evaluation
"The Illustrated Transformer" by Jay Alammar - An excellent visual guide to transformers and attention mechanisms. Many of the images used in the self-attention notebook are from this blog post. http://jalammar.github.io/illustrated-transformer/
"Linformer: Self-Attention with Linear Complexity" by Wang et al. (2020) - Introduces the Linformer mechanism for efficient attention computation. https://arxiv.org/abs/2006.04768
"Rethinking Attention with Performers" by Choromanski et al. (2020) - Presents the Performer attention mechanism using random feature approximation. https://arxiv.org/abs/2009.14794
"Generating Long Sequences with Sparse Transformers" by Child et al. (2019) - Introduces sparse attention patterns for efficient computation in transformers. https://arxiv.org/abs/1904.10509
"Efficient Transformers: A Survey" by Tay et al. (2020) - A comprehensive overview of various efficient transformer architectures. https://arxiv.org/abs/2009.06732
"A Survey of Long-Term Context in Transformers" by Peng et al. (2022) - Discusses various techniques for extending the context length in transformer models. https://arxiv.org/abs/2307.03170
Contributions to improve the notebooks or add new attention mechanisms are welcome. Please feel free to submit pull requests or open issues for discussion.
Educational PyTorch notebooks exploring self-attention, causal masking, multi-head attention and efficient attention variants.
Model Figures & Evaluation Curves (4 Images)
Deep Learning Execution Pipeline
(Preprocessing)
(Feature Extraction)
(Self-Attention)
(Loss Optimization)