AI / MLModel ArchitectureDiagramIntermediate
Self-Attention
KO: 셀프 어텐션 (Self-Attention)·EN: Self-Attention·JA: セルフアテンション(Self-Attention)
The core mechanism allowing every token in a sequence to dynamically attend to and aggregate information from all other tokens via Query, Key, and Value projections
Table of Contents
Structure Diagram
Self-Attention: Dynamic Context Weighting via Q·KᵀTap a node
ROOTQuery: "it"
1 linkQuick Answer
What is Self-Attention?
The core mechanism allowing every token in a sequence to dynamically attend to and aggregate information from all other tokens via Query, Key, and Value projections. It is a core concept in Model Architecture within AI / ML.
Core Principles
Self-Attention projects each token into Query (Q), Key (K), and Value (V) vectors and computes context-aware representations via Softmax(Q·Kᵀ / √d_k) · V.
Multi-Head Attention runs multiple attention subspaces in parallel so different heads specialize in syntax, coreference, code structure, or long-range semantics simultaneously.
Practical Takeaways
- Why divide by
√d_k: Without scaling, dot products grow large in high dimensions, pushing Softmax into near-zero gradient saturation. - Always enable FlashAttention: FlashAttention tiles attention inside fast GPU SRAM without materializing the full
N × Nmatrix in HBM, delivering exact attention withO(N)memory and 2–4× speedups. - Causal Masking for generation: Autoregressive LLMs mask future positions with
-∞before Softmax so tokentattends only to positions≤ t.
Notable Examples
- Coreference Resolution: Links pronouns like "it" back to the right antecedent across long sentences.
- Induction Heads: Specialized attention heads that power pattern copying and In-Context Learning.
- Grouped-Query Attention (GQA): Shares K/V heads across Query groups to shrink KV Cache VRAM dramatically.