Skip to main content
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
Dot ProdWeight 0.7Weight 0.1Query: "i…Softmax(Q…"dog" (0.…"tired" (…"street" …
ROOTQuery: "it"
1 link
Quick 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 × N matrix in HBM, delivering exact attention with O(N) memory and 2–4× speedups.
  • Causal Masking for generation: Autoregressive LLMs mask future positions with -∞ before Softmax so token t attends 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.