Zubnet AIAprenderWiki › Multi-Head Attention
Fundamentos

Multi-Head Attention

MHA
Executar múltiplas operações de atenção em paralelo, cada uma com sua própria projeção aprendida de queries, keys e values. Em vez de uma função de atenção olhando a dimensão inteira do modelo, multi-head attention divide a dimensão em múltiplas “cabeças” (ex. 32 cabeças de 128 dimensões cada para um modelo de 4096 dimensões). Cada cabeça pode focar em tipos diferentes de relações simultaneamente.

Por que importa

Multi-head attention é por que os Transformers são tão expressivos. Uma cabeça pode focar em relações sintáticas (sujeito-verbo), outra em padrões posicionais (palavras próximas), outra em similaridade semântica. Essa especialização paralela permite ao modelo capturar muitos tipos de dependências simultaneamente, o que uma única cabeça de atenção não consegue fazer tão efetivamente.

Deep Dive

The mechanism: for each head i, the model learns separate projection matrices W_Q^i, W_K^i, W_V^i that project the input into a lower-dimensional space (head_dim = model_dim / num_heads). Each head independently computes attention: softmax(Q_i · K_i^T / √d) · V_i. The outputs of all heads are concatenated and projected back to the full model dimension through a final linear layer W_O.

Head Specialization

Research shows that different heads learn different functions. Some heads attend to the previous token (positional). Some attend to syntactically related tokens (subject to its verb). Some implement "induction" (pattern completion). Some attend broadly (gathering global context). Not all heads are equally important — pruning 20–40% of heads often has minimal impact on performance, suggesting significant redundancy.

GQA and MQA

Multi-Query Attention (MQA) uses a single key-value head shared across all query heads, reducing KV cache size by the number of heads. Grouped-Query Attention (GQA) is a middle ground: groups of query heads share a key-value head (e.g., 32 query heads with 8 KV heads). GQA preserves most of MHA's quality while dramatically reducing memory for KV cache. Llama 2 70B, Mistral, and most modern LLMs use GQA.

Conceitos relacionados

← Todos os termos
← Multi-Agent Systems Multimodal →