Zubnet AIसीखेंWiki › Self-Attention
मूल सिद्धांत

Self-Attention

Scaled Dot-Product Attention
एक attention mechanism जहाँ एक sequence खुद पर attend करती है — हर token उसी sequence के हर दूसरे token के साथ अपनी relevance compute करता है। Queries, keys, और values सब एक ही input से आते हैं। ये हर token को allow करता है कि वो सभी दूसरे tokens से information gather करे, relevance से weighted। Self-attention हर Transformer layer का core operation है।

यह क्यों matter करता है

Self-attention ही वो चीज़ है जो Transformers को काम करने देती है। इसने RNNs के sequential processing को replace कर दिया, सभी positions के बीच parallel, direct connections के साथ। “river bank” में “bank” word “river” पर attend करता है अपना meaning resolve करने के लिए, चाहे वो कितने भी दूर हों। किन्हीं भी दो positions को directly connect करने की ये ability ही वजह है कि Transformers long-range dependencies इतनी अच्छी तरह handle करते हैं।

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.

संबंधित अवधारणाएँ

← सभी Terms
← Scaling Laws Self-Supervised सीखेंing →