Zubnet AIApprendreWiki › Self-Attention
Fondamentaux

Self-Attention

Scaled Dot-Product Attention
Un mécanisme d'attention où une séquence s'applique à elle-même — chaque token calcule sa pertinence par rapport à chaque autre token dans la même séquence. Les queries, keys et values viennent toutes de la même entrée. Ça permet à chaque token de récolter de l'info de tous les autres tokens, pondérée par pertinence. Le self-attention est l'opération centrale de chaque couche d'un Transformer.

Pourquoi c'est important

Le self-attention est ce qui fait que les Transformers marchent. Il a remplacé le traitement séquentiel des RNN par des connexions parallèles et directes entre toutes les positions. Le mot « bank » dans « river bank » s'attache à « river » pour résoudre son sens, peu importe la distance qui les sépare. Cette capacité de connecter directement n'importe quelles deux positions est pourquoi les Transformers gèrent si bien les dépendances à longue portée.

Deep Dive

The computation: for input X, compute Q = X·W_Q, K = X·W_K, V = X·W_V. Then: Attention(Q,K,V) = softmax(Q·K^T / √d_k) · V. The softmax(Q·K^T) produces an N×N attention matrix where entry (i,j) represents how much token i attends to token j. The √d_k scaling prevents dot products from growing too large in high dimensions, which would push softmax into saturated regions with near-zero gradients.

Causal vs. Bidirectional

In decoder-only LLMs (GPT, Claude, Llama), self-attention is causal: each token can only attend to previous tokens (including itself). This is enforced by a causal mask that sets future positions to −∞ before softmax. In encoder models (BERT), self-attention is bidirectional: every token attends to every other token. The causal constraint is what makes autoregressive generation possible — the model can't "peek" at future tokens.

The Quadratic Cost

Self-attention computes an N×N attention matrix, making it O(N²) in both time and memory. For a 128K token context, that's ~16 billion entries per layer per head. This quadratic scaling is the fundamental limitation that drives research into sparse attention, linear attention, Flash Attention (which reduces memory but not compute), and SSMs (which avoid the N×N matrix entirely). Every approach to long-context modeling is ultimately about managing this quadratic cost.

Concepts liés

← Tous les termes
← Scaling Laws Self-Supervised Apprendreing →