All formulas

Machine learning building blocks

Scaled dot-product attention

ml.attentionAlso called attention, scaled dot product attention, sdpa, self attention

Attention⁡(Q,K,V)=softmax⁡(QK⊤dk)V\operatorname{Attention}(Q, K, V) = \operatorname{softmax}\left(\frac{QK^\top}{\sqrt{d_k}}\right)V

Each query row of Q takes a weighted average of the rows of V, weighted by the softmax over keys of its scaled dot products with the rows of K. The softmax runs along each row (over keys).

Notation

QQ

queries, one per row

Q ∈ ℝm×d

KK

keys, one per row

K ∈ ℝn×d

VV

values, one per row

V ∈ ℝn×e

mm

number of queries

Size, at least 1

nn

number of keys

Size, at least 1

dkd_k

query and key dimension

Size, at least 1

dvd_v

value dimension

Size, at least 1

Attention⁡(Q,K,V)\operatorname{Attention}(Q, K, V)

Result: one output row per query

matrix

Worked examples

Each result is solved exactly and confirmed by two independent computations.

  1. Given

    Q=[[1,0]],K=[[1,0],[0,1]],V=[[1,2],[3,4]]Q = \left[\left[1, 0\right]\right], K = \left[\left[1, 0\right], \left[0, 1\right]\right], V = \left[\left[1, 2\right], \left[3, 4\right]\right]

    Result

    Attention⁡(Q,K,V)=(e22+31+e224+2e221+e22)\displaystyle \operatorname{Attention}(Q, K, V) = \begin{pmatrix}\frac{e^{\frac{\sqrt{2}}{2}} + 3}{1 + e^{\frac{\sqrt{2}}{2}}} & \frac{4 + 2 e^{\frac{\sqrt{2}}{2}}}{1 + e^{\frac{\sqrt{2}}{2}}}\end{pmatrix}
  2. Given

    Q=[[0,0]],K=[[0,0],[0,0],[0,0]],V=[[1],[2],[6]]Q = \left[\left[0, 0\right]\right], K = \left[\left[0, 0\right], \left[0, 0\right], \left[0, 0\right]\right], V = \left[\left[1\right], \left[2\right], \left[6\right]\right]

    Result

    Attention⁡(Q,K,V)=(3)\displaystyle \operatorname{Attention}(Q, K, V) = \begin{pmatrix}3\end{pmatrix}
  3. Given

    Q=[[2]],K=[[1],[0]],V=[[1],[0]]Q = \left[\left[2\right]\right], K = \left[\left[1\right], \left[0\right]\right], V = \left[\left[1\right], \left[0\right]\right]

    Result

    Attention⁡(Q,K,V)=(e21+e2)\displaystyle \operatorname{Attention}(Q, K, V) = \begin{pmatrix}\frac{e^{2}}{1 + e^{2}}\end{pmatrix}
  4. Given

    Q=[[0],[1]],K=[[0],[1]],V=[[2],[4]]Q = \left[\left[0\right], \left[1\right]\right], K = \left[\left[0\right], \left[1\right]\right], V = \left[\left[2\right], \left[4\right]\right]

    Result

    Attention⁡(Q,K,V)=(32+4e1+e)\displaystyle \operatorname{Attention}(Q, K, V) = \begin{pmatrix}3 \\ \frac{2 + 4 e}{1 + e}\end{pmatrix}

Common mistakes

  • Omits the 1/√dk scale

    The dot products are not divided by √dk, which sharpens the softmax.

    Fix: Divide QKT by √dk before the softmax.

  • Divides by dk instead of √dk

    The dot products are divided by dk.

    Fix: Divide by the square root of dk.

  • Softmax over queries instead of keys

    The softmax normalizes each column of QKT (over queries) instead of each row (over keys).

    Fix: Apply the softmax along the last axis, dim=−1.

Source

  • Vaswani et al. 2017, eq. 1

    Vaswani, Ashish et al. Attention is All you Need. In Advances in Neural Information Processing Systems 30, pp. 5998-6008, 2017.

    arxiv.org