Papers · Architectures and attention · 2022

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness

Tri Dao, Daniel Y. Fu, Stefano Ermon and others

The notation of the FlashAttention paper, explained: its 34 displayed equations, then every symbol it uses, grouped by category and shown the way the paper writes it.

Read the paper on arXiv

Read from arXiv's HTML version: 34 displayed equations and 246 pieces of inline math. 43 symbols and constructs from the catalog, read as attention and transformers.

The paper's displayed equations

Open any of them in the decoder for a symbol-by-symbol reading.

  1. S=QK⊤∈RN×N,P=softmax(S)∈RN×N,O=PV∈RN×d,\mathbf{S}=\mathbf{Q}\mathbf{K}^{\top}\in\mathbb{R}^{N\times N},\quad\mathbf{P}=\mathrm{softmax}(\mathbf{S})\in\mathbb{R}^{N\times N},\quad\mathbf{O}=\mathbf{P}\mathbf{V}\in\mathbb{R}^{N\times d},
    Decode →
  2. m(x):=max⁡i  xi,f(x):=[ex1−m(x)…exB−m(x)],ℓ(x):=∑if(x)i,softmax(x):=f(x)ℓ(x).m(x):=\max_{i}\ \ x_{i},\quad f(x):=\begin{bmatrix}e^{x_{1}-m(x)}&\ldots&e^{x_{B}-m(x)}\end{bmatrix},\quad\ell(x):=\sum_{i}f(x)_{i},\quad\mathrm{softmax}(x):=\frac{f(x)}{\ell(x)}.
    Decode →
  3. m(x)=m([x(1) x(2)])=max⁡(m(x(1)),m(x(2))),f(x)=[em(x(1))−m(x)f(x(1))em(x(2))−m(x)f(x(2))],ℓ(x)=ℓ([x(1) x(2)])=em(x(1))−m(x)ℓ(x(1))+em(x(2))−m(x)ℓ(x(2)),softmax(x)=f(x)ℓ(x).\begin{gathered}m(x)=m(\begin{bmatrix}x^{(1)}\ x^{(2)}\end{bmatrix})=\max(m(x^{(1)}),m(x^{(2)})),\quad f(x)=\begin{bmatrix}e^{m(x^{(1)})-m(x)}f(x^{(1)})&e^{m(x^{(2)})-m(x)}f(x^{(2)})\end{bmatrix}, \\ \ell(x)=\ell(\begin{bmatrix}x^{(1)}\ x^{(2)}\end{bmatrix})=e^{m(x^{(1)})-m(x)}\ell(x^{(1)})+e^{m(x^{(2)})-m(x)}\ell(x^{(2)}),\quad\mathrm{softmax}(x)=\frac{f(x)}{\ell(x)}.\end{gathered}
    Decode →
  4. S=QK⊤∈RN×N,P=softmax(S⊙1M~)∈RN×N,O=PV∈RN×d,\mathbf{S}=\mathbf{Q}\mathbf{K}^{\top}\in\mathbb{R}^{N\times N},\quad\mathbf{P}=\mathrm{softmax}(\mathbf{S}\odot\mathbb{1}_{\tilde{\mathbf{M}}})\in\mathbb{R}^{N\times N},\quad\mathbf{O}=\mathbf{P}\mathbf{V}\in\mathbb{R}^{N\times d},
    Decode →
  5. S=QK⊤∈RN×N,P=softmax(S)∈RN×N,O=PV∈RN×d.\mathbf{S}=\mathbf{Q}\mathbf{K}^{\top}\in\mathbb{R}^{N\times N},\quad\mathbf{P}=\mathrm{softmax}(\mathbf{S})\in\mathbb{R}^{N\times N},\quad\mathbf{O}=\mathbf{P}\mathbf{V}\in\mathbb{R}^{N\times d}.
    Decode →
  6. (1)
    Li=∑jeqiTkj.L_{i}=\sum_{j}e^{q_{i}^{T}k_{j}}.
    Decode →
  7. (2)
    oi=Pi:V=∑jPijvj=∑jeqiTkjLivj.o_{i}=P_{i:}\mathbf{V}=\sum_{j}P_{ij}v_{j}=\sum_{j}\frac{e^{q_{i}^{T}k_{j}}}{L_{i}}v_{j}.
    Decode →
  8. (3)
    dvj=∑iPijdoi=∑ieqiTkjLidoi.dv_{j}=\sum_{i}P_{ij}do_{i}=\sum_{i}\frac{e^{q_{i}^{T}k_{j}}}{L_{i}}do_{i}.
    Decode →
  9. dPij=doiTvj.dP_{ij}=do_{i}^{T}v_{j}.
    Decode →
  10. dSi:=(diag(Pi:)−Pi:Pi:T)dPi:=Pi:∘dPi:−(Pi:TdPi:)Pi:,dS_{i:}=(\mathrm{diag}(P_{i:})-P_{i:}P_{i:}^{T})dP_{i:}=P_{i:}\circ dP_{i:}-(P_{i:}^{T}dP_{i:})P_{i:},
    Decode →
  11. (4)
    Di=Pi:TdPi:=∑jeqiTkjLidoiTvj=doiT∑jeqi⊤kjLivj=doiToi,D_{i}=P_{i:}^{T}dP_{i:}=\sum_{j}\frac{e^{q_{i}^{T}k_{j}}}{L_{i}}do_{i}^{T}v_{j}=do_{i}^{T}\sum_{j}\frac{e^{q_{i}^{\top}k_{j}}}{L_{i}}v_{j}=do_{i}^{T}o_{i},
    Decode →
  12. dSi:=Pi:∘dPi:−DiPi:.dS_{i:}=P_{i:}\circ dP_{i:}-D_{i}P_{i:}.
    Decode →

Famous equations in this paper

Symbols, by category

Most used first. The examples are written as the paper writes them.

Notation conventions

  1. mm, xx, ff

    Italic letters (variables) 909 uses

    A plain italic letter is a variable; conventions say what kind (x input, y target, W weights…).

  2. xix_{i}, x1x_{1}, xBx_{B}

    Subscript (index or label) 666 uses

    A subscript picks an element (xᵢ), a time step (h_t), a named copy (W_q), or the parameters (p_θ).

  3. Smasked\mathbf{S}^{\mathrm{masked}}, Pdropped\mathbf{P}^{\mathrm{dropped}}, N2N^{2}

    Superscript (power or label) 100 uses

    Usually a power (x² = x·x), but a capital or a word up there is often just a label (W^Q).

  4. ℓ\ell

    Loss (ℒ) 76 uses

    The loss: the number training tries to make small. Also written L, J(θ) or ℓ.

  5. x(1)x^{(1)}, x(2)x^{(2)}, m(j)m^{(j)}

    Superscript in parentheses 61 uses

    An index in a superscript: x⁽ⁱ⁾ is the i-th training example, W⁽ˡ⁾ the weights of layer l. Not a power.

  6. M~\tilde{\mathbf{M}}, m~\tilde{m}, ℓ~\tilde{\ell}

    Tilde accent 58 uses

    A modified version of the variable: noisy, corrupted, proposed or intermediate.

  7. …\ldots, …\dots

    Dots (ellipsis) 31 uses

    “And so on”: continue the pattern.

Greek letters

  1. Θ\Theta

    Theta 36 uses

    The model’s parameters: all of its weights and biases, as one collection.

  2. τ\tau

    Tau 10 uses

    A temperature that sharpens or flattens a softmax; in RL, a trajectory.

  3. ϕ\phi

    Phi 5 uses

    The parameters of a second model (often the encoder q_φ), or a feature map φ(x).

  4. Ω\Omega

    Omega 4 uses

    A frequency ω (positional encodings, Fourier features) or a weight; capital Ω is a regularizer, a sample space or big-Omega.

Vectors and matrices

  1. S\mathbf{S}, Q\mathbf{Q}, K\mathbf{K}

    Bold uppercase (matrix) 422 uses

    A bold capital letter is a matrix (or a higher-order tensor).

  2. QQ, KK, VV

    Q, K, V (query, key, value) 288 uses

    In attention: queries Q, keys K and values V, each a matrix with one row per token.

  3. ×\times

    Times (cross) 159 uses

    Usually the “by” in a shape (n × d); also plain multiplication or a cross product.

  4. K⊤\mathbf{K}^{\top}, qiTq_{i}^{T}, oiTo_{i}^{T}

    Transpose 47 uses

    Swap rows and columns. xᵀy is a dot product; xyᵀ is an outer product.

  5. (ℓ(j+1))−1(\ell^{(j+1)})^{-1}, M−1M^{-1}, (ℓinew)−1(\ell_{i}^{\mathrm{new}})^{-1}

    Inverse 20 uses

    The matrix inverse, A⁻¹A = I; for a function, its inverse function; for a number, 1/x.

  6. diag\mathrm{diag}

    Diag 19 uses

    Build a diagonal matrix from a vector, or pull out the diagonal of a matrix.

  7. ⊙\odot

    Hadamard (elementwise) product 2 uses

    Multiply two arrays of the same shape entry by entry.

Calculus and gradients

  1. ∞\infty

    Infinity 5 uses

    Infinity; as a norm subscript, ‖x‖_∞ is the largest absolute entry.

  2. ∂ϕ∂O\frac{\partial\phi}{\partial\mathbf{O}}, ∂ϕ∂Q\frac{\partial\phi}{\partial\mathbf{Q}}, ∂ϕ∂K\frac{\partial\phi}{\partial\mathbf{K}}

    Partial derivative 4 uses

    A partial derivative: how one quantity changes when one input changes and the others are held fixed.

Probability and statistics

  1. qq, PP, pp

    Probability p(x) 74 uses

    A probability or probability density; p is usually the true or model distribution, q an approximation.

  2. ex1−m(x)e^{x_{1}-m(x)}, exB−m(x)e^{x_{B}-m(x)}, em(x(1))−m(x)e^{m(x^{(1)})-m(x)}

    Exponential 52 uses

    The exponential function eˣ, with e ≈ 2.718. Always positive.

  3. 1\mathbb{1}

    Indicator function 2 uses

    1 when the condition inside is true, 0 when it is false.

  4. log⁡\log

    Logarithm 2 uses

    A logarithm, almost always the natural log (base e) in ML papers.

Functions and operators

  1. ::

    Arrow (maps to) 77 uses

    Maps to (f: X → Y), tends to (x → 0), or implies (⇒).

  2. f(x)ℓ(x)\frac{f(x)}{\ell(x)}, eqiTkjLi\frac{e^{q_{i}^{T}k_{j}}}{L_{i}}, eqi⊤kjLi\frac{e^{q_{i}^{\top}k_{j}}}{L_{i}}

    Fraction 48 uses

    Division: the top divided by the bottom.

  3. OO

    Big-O notation 27 uses

    How a cost grows with size, ignoring constants: O(n²) means at most proportional to n² for large n.

  4. ⌈M4d⌉\left\lceil\frac{M}{4d}\right\rceil, ⌈NBr⌉\left\lceil\frac{N}{B_{r}}\right\rceil, ⌈NBc⌉\left\lceil\frac{N}{B_{c}}\right\rceil

    Floor and ceiling 20 uses

    Round down (floor ⌊x⌋) or round up (ceiling ⌈x⌉) to a whole number.

  5. softmax\mathrm{softmax}

    Softmax 18 uses

    Turns a vector of scores into probabilities: exponentiate each, then divide by the total.

  6. ∑i\sum_{i}, ∑j\sum_{j}, ∑l\sum_{l}

    Summation 15 uses

    Add up the expression for every value of the index, from the bottom limit to the top.

  7. ←\leftarrow

    Assignment arrow 14 uses

    Assignment in an algorithm: replace the variable on the left with the value on the right.

  8. max⁡i\max_{i}, max⁡\max, min⁡\min

    Max and min 13 uses

    The largest (or smallest) value, over the arguments or over the range in the subscript.

  9. ∘\circ

    Composition (circle) 7 uses

    Function composition: (f ∘ g)(x) = f(g(x)). Some papers use it for elementwise products.

  10. N\sqrt{N}, d\sqrt{d}

    Square root 2 uses

    The square root. In attention, dividing by √d_k keeps dot products at a sensible scale.

  11. ±\pm

    Plus or minus 2 uses

    Both signs at once: a ± b means a + b and a − b. In results tables, a mean and its spread.

Sets, shapes and definitions

  1. ∈\in

    Element of 110 uses

    “Is in” or “belongs to”: x ∈ S says x is a member of the set S.

  2. RN×N\mathbb{R}^{N\times N}, RN×d\mathbb{R}^{N\times d}, RN\mathbb{R}^{N}

    Tensor shapes 101 uses

    ℝ with a superscript of sizes joined by × declares an array’s shape: ℝ^{B×T×d} is a 3-axis tensor.

  3. ≫\gg, ≤\leq, ≪\ll

    Inequalities 29 uses

    Less than / greater than (or equal). ≪ means “much smaller than”.

  4. {0,1}\{0,1\}, {\{

    Set braces 5 uses

    A set: a collection of items. {x | condition} is every x that satisfies the condition.

  5. :=:=

    Defined as (:=) 4 uses

    “Is defined as”: introduces a new name for the expression on the other side.

  6. R\mathbb{R}

    Real numbers (ℝ) and shapes 4 uses

    The real numbers. ℝⁿˣᵈ is the set of n × d arrays of reals: it states a shape.

  7. ⇔\Leftrightarrow

    If and only if 2 uses

    “If and only if”: each side implies the other, so they are true together or false together.

  8. ≠\neq

    Not equal 1 use

    The two sides are different.

5 things the catalog has no entry for yet
  • rowsum\mathrm{rowsum} rowsum 7
  • \cal \cal 7
  • dropout\mathrm{dropout} dropout 5
  • rowmax\mathrm{rowmax} rowmax 5
  • }\} \} 1

Formulas from arXiv's HTML version of the paper, © their authors; the explanations are this site's. Title and authors as arXiv lists them.

More architectures and attention papers