From Self-Attention to GQA
PremiumStarting from Self-Attention, unpack the design trade-offs of Multi-Head, Causal Masking, and GQA / MQA in turn
What Is the Attention Mechanism
Attention is one of the most important innovations in modern deep learning. It allows a model to dynamically focus on information at different positions when processing sequence data.
In the Transformer architecture, the core idea of attention is: let each position's token perform a weighted sum over all other tokens according to its relevance to them.
The Standard Self-Attention Computation
Given an input sequence, Self-Attention computes its output through the following steps:
-
Linear projection: project the input into Query (), Key (), and Value ()
-
Compute attention scores: compute similarity through the dot product of Query and Key
where is the head dimension, and dividing by is for numerical stability (scaled dot-product)
-
Apply Softmax: convert scores into a probability distribution
-
Weighted sum: weight the Value with the attention weights
Intuition:
- (Query): what information do I want?
- (Key): what information do I provide?
- (Value): what information do I actually contain?
- Attention score: measures how well "what I need" matches "what you provide"
PyTorch Reference Implementation
import torch
import torch.nn.functional as F
def self_attention(X, W_q, W_k, W_v):
"""
X: (batch, seq_len, d_model)
W_q, W_k, W_v: (d_model, d_head)
"""
Q = X @ W_q # (batch, seq_len, d_head)
K = X @ W_k
V = X @ W_v
# Compute attention scores
scores = Q @ K.transpose(-2, -1) # (batch, seq_len, seq_len)
scores = scores / (K.shape[-1] ** 0.5) # scale
# Softmax normalization
attn_weights = F.softmax(scores, dim=-1)
# Weighted sum
output = attn_weights @ V # (batch, seq_len, d_head)
return output, attn_weightsLog in to continue reading
This is premium content. Please log in to access the full article.
CookLLM Docs