← Back to LLMs Mind Map
⚡ LLMsID: attention-scaled-dot

Scaled Dot-Product Attention

缩放点积注意力
🎯Core Definition
Scaled Dot-Product Attention is the core attention operator of the Transformer: Attention(Q,K,V)=softmax(QKTdk)VAttention(Q,K,V) = softmax(\frac{Q K^T}{\sqrt{d_k}}) V. Dividing by dk\sqrt{d_k} normalizes the dot-product variance: if Q and K have per-dimension variance 1, the variance of a dkd_k-dim dot product grows linearly with dkd_k; without scaling, softmax inputs saturate as dimensionality grows (e.g. dk=128d_k = 128, 128≈11.3\sqrt{128} \approx 11.3) and gradients vanish.
💡Use Cases
every head softly matches all tokens in the sequence — the foundation for MHA/GQA/MLA, FlashAttention and RoPE; interviews typically start with "why divide by dk\sqrt{d_k}" and then probe complexity, masks and head splitting.
⚡Key Problems Solved
unlike unscaled dot products, scaling keeps softmax entropy and gradients stable at any dimensionality so deep networks train reliably; but time and memory stay O(n2)O(n^2), which becomes the main bottleneck for long sequences and motivates IO-aware implementations like FlashAttention.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Scaled Dot-Product Attention.
Use Cases
In production engineering, what are the primary use cases for Scaled Dot-Product Attention and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Why divide by dk\sqrt{d_k}? What breaks without it (variance analysis, softmax saturation)?
2
Time and space complexity of scaled dot-product attention? Why is O(n2)O(n^2) the long-context bottleneck?
3
Shapes of the Q/K/V projections? How are heads split and concatenated?
4
Difference and implementation of padding mask vs causal mask?
5
Single vs multi-head: why h heads rather than one wide dd-dim head?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Scaled Dot-Product Attention"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardKV Cache ManagementNext Card →Attention Variants MHA/MQA/GQA

🔗 More LLMs Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/LLM/card/attention-scaled-dot
中文速览与面试要点

缩放点积注意力 (Scaled Dot-Product Attention)

定义: Scaled Dot-Product Attention (缩放点积注意力) 是 Transformer 的注意力核心算子,计算 Attention(Q,K,V) = softmax( Q K^T d_k) V。除以 d_k 用于归一化点积方差:当 Q、K 各分量方差为 1 时,d_k 维点积的方差与 d_k 成正比,若不缩放,softmax 输入随维度膨胀进入饱和区(如 d_k = 128 时 128 11.3),梯度趋近 0。 使用场景: 每个注意力头对序列内所有 token 做软匹配,是 MHA/GQA/MLA、FlashAttention、RoPE 等一切注意力话题的基础;面试常从"为什么除以 d_k"切入,再追问复杂度、mask 与多头拆分。 解决的问题: 相比不缩放的点积,缩放保证任意维度下 softmax 的熵与梯度稳定,深层网络可训练;但时间与显存仍是 O(n^2),长序列下成为主要瓶颈,需要 FlashAttention 等 IO 优化来缓解。

核心要点

  • 为什么注意力要除以 d_k?不除以会怎样(方差分析与 softmax 饱和)?
  • 缩放点积注意力的时间与空间复杂度是多少?为什么 O(n^2) 是长上下文瓶颈?
  • Q、K、V 的投影矩阵形状是什么?多头如何拆分与拼接?
  • padding mask 与 causal mask 的区别与实现方式?
  • 单头 vs 多头:为什么拆成 h 个头而不是一个 d 维大头?