Files
myWiki/concepts/multi-head-attention.md

40 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Multi-Head Attention (MHA)
created: 2025-04-15
updated: 2026-05-01
type: concept
tags: []
sources: []
---
# Multi-Head Attention (MHA)
**标准多头注意力**Transformer 架构的核心注意力变体。
## 定义
MHA 将输入经过 h 个并行的注意力头处理,每个头学习不同的表示子空间,最后拼接所有头的输出并通过线性变换融合。核心操作:
$$\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, ..., \text{head}_h) W^O$$
$$\text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)$$
## 关键特性
- **并行子空间**: h 个注意力头各自学习不同的注意模式(语法、语义、位置等)
- **参数量**: 每个头维度 d_k = d_v = d/h总参数量与单头一致
- **计算复杂度**: O(n² · d)n 为序列长度d 为模型维度
## 优势与局限
**优势**: 表达能力最强,每个头独立学习不同的 Token 关系模式。
**局限**: KV 缓存为 n × d长序列推理时内存开销极大每个 Token 需要计算与所有 Token 的注意力。
## 相关概念
- [[multi-query-attention]] — 共享 KV 头以减少缓存
- [[grouped-query-attention]] — MHA 与 MQA 之间的折中
- [[multi-head-latent-attention]] — 低秩压缩的极致优化
- [[attention-entropy-collapse]] — MHA 中深层退化问题
- [[llm-attention-survey-2026]] — 综述参考