M4-043M4: Sequences & TransformersAttention Variants (MHA / MQA / GQA)Medium
⚡Flashcard Drill➔Mastery:
Cross-Attention vs Self-Attention: Structural Differences and Multimodal Roles
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Self-attention derives Query, Key, and Value from the same sequence; Cross-attention derives Query from the target sequence and Key/Value from an external conditioning source.
📌 Key Takeaways
- •Self-Attention: ; models intra-sequence dependencies; square or causal triangular matrix
- •Cross-Attention: ; rectangular attention matrix ; models conditional alignment
- •Applications: Encoder-Decoder translation (T5), Diffusion conditioning (Stable Diffusion text-to-image), Multimodal perception (Flamingo)
📐 Mathematical Derivations
Structural Comparison:
Let source sequence be and target sequence be .
① Self-Attention:
.
Attention matrix: .
Every token routes information to and from tokens within its own sequence. Computes contextual representations.
② Cross-Attention:
Target acts as Query; Source acts as Key and Value:
.
Attention matrix: .
Output: .
Each target token dynamically queries the entire source memory. No causal mask is applied along because the conditioning context is fully available.
Let source sequence be and target sequence be .
① Self-Attention:
.
Attention matrix: .
Every token routes information to and from tokens within its own sequence. Computes contextual representations.
② Cross-Attention:
Target acts as Query; Source acts as Key and Value:
.
Attention matrix: .
Output: .
Each target token dynamically queries the entire source memory. No causal mask is applied along because the conditioning context is fully available.
🏭 Production Trade-offs
Serving optimization in Cross-Attention: Source sequence is processed only once. Its Key and Value tensors () are precomputed and frozen, allowing the target decoder to autoregressively query static KV states with zero recomputation.
⚠️ Common Interview Pitfalls
- ✕Applying causal masking along the source dimension in cross-attention; conditioning context is fully visible
- ✕Recomputing cross-attention Key and Value tensors at every autoregressive decoding step instead of caching them
🎯 Interviewer Follow-up Inquiries
- ?How does Stable Diffusion use cross-attention to condition image latent denoising on text embeddings?
- ?Why is the KV-cache of cross-attention static throughout autoregressive decoding while self-attention KV-cache grows dynamically?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
解释 cross-attention 与 self-attention 的差异与用途
self-attention 的 Q/K/V 同源(序列内交互);cross-attention 的 Q 来自解码器、K/V 来自编码器(跨序列对齐)。
核心要点
- 数学机理: self-attention 的 Q、K、V 都来自 同一个序列 X(Q=XW^Q、K=XW^K、V=XW^V),实现 序列内 的位置间信息交换(token mixing);序列长度前后一致(输出长度=输入长度)。 cross-attention 的 Q 来自 目标序列 Y(如解码器的隐状态),K/V 来自 源序列 X(如编码器的输出):Attn=softmax((YW^Q)(XW^K)ᵀ/√d)(XW^V)。它实现 跨序列 的信息对齐——解码器每步'查询'源序列的相关部分,这是 Enc-Dec 架构(翻译/摘要/ASR)的 信息桥梁 。 关键差异 :(1) 长度不同 ——Q 长度=|Y|、K/V 长度=|X|,注意力矩阵为 |Y|×|X|(非方阵);(2) 方向性 ——cross-attention 无因果 mask(源序列已完整可见,无需屏蔽未来);(3) KV cache 复用 ——K/V 来自编码器,对 所有解码步相同 ,故可只计算一次并缓存(无需随解码增长);这是 Enc-Dec 推理的一个优势。 在 Dec-only 模型中的替代 ——Dec-only 无 cross-attention,源信息通过'拼接进上下文'(self-attention 内部交互)实现;代价是源序列也要被自回归地'读'一遍(更长的序列、更多的 KV cache),且源序列的注意力是 因果 的(不能双向理解源)。这就是 Enc-Dec 在'源需双向理解'任务上的结构优势所在。 多模态中的 cross-attention ——VLM 中常用'文本 query 查询图像特征'(如 Flamingo 的 gated cross-attention),把图像作为 K/V 注入 LLM。
- 深度剖析与工程权衡:① KV cache 的差异 ——self-attention 的 KV cache 随解码 增长 (每步新增一个 K/V);cross-attention 的 KV cache 固定 (源序列的 K/V 只算一次),故 Enc-Dec 在长源 + 短输出的任务(摘要、检索增强生成)上更省显存。② Enc-Dec 与 Dec-only 的计算对比 ——Dec-only 把源拼进上下文后,源部分也要参与 O(L²) 注意力且被因果读取;Enc-Dec 用编码器双向处理源(一次 O(L²))+ 解码器 cross-attention(O(|Y|·|X|)),在'长源'场景更高效。③ 交叉注意力的可解释性 ——cross-attention 权重可直接视为'输出对输入的对齐'(翻译中近似对角),比 self-attention 更易解读。④ 多模态融合的三种方式 ——(a) cross-attention 注入 (Flamingo、早期的 VLM);(b) 前缀拼接 (把图像特征作为前缀 token 输入 LLM,如 LLaVA);(c) 统一 tokenization (原生多模态,如 Chameleon、Qwen-VL 的部分设计)。三者在训练难度、参数量、灵活性上各有取舍。⑤ 与检索增强的关系 ——RAG 的'检索结果注入'也可用 cross-attention(RETRO 用 chunked cross-attention 检索邻居)或前缀拼接(更常见);前者可处理更长的检索内容。⑥ 面试要点 ——被问'cross vs self attention',应给出' Q/K/V 来源(同源 vs 跨序列)+ 长度(相同 vs 不同)+ mask(因果 vs 无)+ KV cache(增长 vs 固定) '四维对比,并说明'Dec-only 用前缀拼接替代 cross-attention 的代价';能提到 VLM 的三种融合方式是加分。
- self:序列内 token 互相交互,长度相同
- cross:解码器查询编码器,长度可不同
- cross 是 Enc-Dec 架构的信息桥梁;Dec-only 模型无 cross
常见陷阱
- 以为 cross-attention 需要因果 mask
- 忽略 cross-attention 的 KV cache 可固定复用