Zubnet AIAprenderWiki › Self-Attention
Fundamentos

Self-Attention

Scaled Dot-Product Attention
Un mecanismo de atención donde una secuencia se atiende a sí misma — cada token calcula su relevancia con respecto a cada otro token en la misma secuencia. Las queries, keys y values vienen todas de la misma entrada. Esto permite que cada token recoja información de todos los otros tokens, ponderada por relevancia. El self-attention es la operación central en cada capa Transformer.

Por qué importa

El self-attention es lo que hace que los Transformers funcionen. Reemplazó el procesamiento secuencial de las RNNs con conexiones paralelas y directas entre todas las posiciones. La palabra «bank» en «river bank» se atiende a «river» para resolver su significado, sin importar cuán lejos estén. Esta capacidad de conectar directamente dos posiciones cualesquiera es por qué los Transformers manejan tan bien las dependencias de largo alcance.

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.

Conceptos relacionados

← Todos los términos
← Scaling Laws Self-Supervised Aprendering →