Papers · Architectures and attention · 2021

RoFormer: Enhanced Transformer with Rotary Position Embedding

Jianlin Su, Yu Lu, Shengfeng Pan and others

The notation of the RoPE paper, explained: its 38 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: 38 displayed equations and 108 pieces of inline math. 39 symbols and constructs from the catalog.

The paper's displayed equations

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

  1. (1)
    qm=fq(xm,m)kn=fk(xn,n)vn=fv(xn,n),\begin{gathered}{\boldsymbol{q}}_{m} =f_{q}({\boldsymbol{x}}_{m},m) \\ {\boldsymbol{k}}_{n} =f_{k}({\boldsymbol{x}}_{n},n) \\ {\boldsymbol{v}}_{n} =f_{v}({\boldsymbol{x}}_{n},n),\end{gathered}
    Decode →
  2. (2)
    am,n=exp⁡(qm⊺knd)∑j=1Nexp⁡(qm⊺kjd)om=∑n=1Nam,nvn\begin{gathered}a_{m,n} =\frac{\exp(\frac{{\boldsymbol{q}}_{m}^{\intercal}{\boldsymbol{k}}_{n}}{\sqrt{d}})}{\sum_{j=1}^{N}\exp(\frac{{\boldsymbol{q}}_{m}^{\intercal}{\boldsymbol{k}}_{j}}{\sqrt{d}})} \\ \mathbf{o}_{m} =\sum_{n=1}^{N}a_{m,n}{\boldsymbol{v}}_{n}\end{gathered}
    Decode →
  3. (3)
    ft:t∈{q,k,v}(xi,i):=Wt:t∈{q,k,v}(xi+pi),f_{t:t\in\{q,k,v\}}({\boldsymbol{x}}_{i},i):={\boldsymbol{W}}_{t:t\in\{q,k,v\}}({\boldsymbol{x}}_{i}+{\boldsymbol{p}}_{i}),
    Decode →
  4. (4)
    {pi,2t=sin⁡(k/100002t/d)pi,2t+1=cos⁡(k/100002t/d)\begin{cases}{\boldsymbol{p}}_{i,2t}&=\sin(k/10000^{2t/d})\\ {\boldsymbol{p}}_{i,2t+1}&=\cos(k/10000^{2t/d})\end{cases}
    Decode →
  5. (5)
    fq(xm):=Wqxmfk(xn,n):=Wk(xn+p~rk)fv(xn,n):=Wv(xn+p~rv)\begin{gathered}f_{q}({\boldsymbol{x}}_{m}):={\boldsymbol{W}}_{q}{\boldsymbol{x}}_{m} \\ f_{k}({\boldsymbol{x}}_{n},n):={\boldsymbol{W}}_{k}({\boldsymbol{x}}_{n}+\tilde{{\boldsymbol{p}}}^{k}_{r}) \\ f_{v}({\boldsymbol{x}}_{n},n):={\boldsymbol{W}}_{v}({\boldsymbol{x}}_{n}+\tilde{{\boldsymbol{p}}}^{v}_{r})\end{gathered}
    Decode →
  6. (6)
    qm⊺kn=xm⊺Wq⊺Wkxn+xm⊺Wq⊺Wkpn+pm⊺Wq⊺Wkxn+pm⊺Wq⊺Wkpn,{\boldsymbol{q}}_{m}^{\intercal}{\boldsymbol{k}}_{n}={\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}+{\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{p}}_{n}+{\boldsymbol{p}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}+{\boldsymbol{p}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{p}}_{n},
    Decode →
  7. (7)
    qm⊺kn=xm⊺Wq⊺Wkxn+xm⊺Wq⊺W~kp~m−n+u⊺Wq⊺Wkxn+v⊺Wq⊺W~kp~m−n{\boldsymbol{q}}_{m}^{\intercal}{\boldsymbol{k}}_{n}={\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}+{\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}\widetilde{{\boldsymbol{W}}}_{k}\tilde{{\boldsymbol{p}}}_{m-n}+\mathbf{u}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}+\mathbf{v}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}\widetilde{{\boldsymbol{W}}}_{k}\tilde{{\boldsymbol{p}}}_{m-n}
    Decode →
  8. (8)
    qm⊺kn=xm⊺Wq⊺Wkxn+bi,j{\boldsymbol{q}}_{m}^{\intercal}{\boldsymbol{k}}_{n}={\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}+b_{i,j}
    Decode →
  9. (9)
    qm⊺kn=xm⊺Wq⊺Wkxn+pm⊺Uq⊺Ukpn+bi,j{\boldsymbol{q}}_{m}^{\intercal}{\boldsymbol{k}}_{n}={\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}+{\boldsymbol{p}}_{m}^{\intercal}\mathbf{U}_{q}^{\intercal}\mathbf{U}_{k}{\boldsymbol{p}}_{n}+b_{i,j}
    Decode →
  10. (10)
    qm⊺kn=xm⊺Wq⊺Wkxn+xm⊺Wq⊺Wkp~m−n+p~m−n⊺Wq⊺Wkxn{\boldsymbol{q}}_{m}^{\intercal}{\boldsymbol{k}}_{n}={\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}+{\boldsymbol{x}}_{m}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}\tilde{{\boldsymbol{p}}}_{m-n}+\tilde{{\boldsymbol{p}}}_{m-n}^{\intercal}{\boldsymbol{W}}_{q}^{\intercal}{\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n}
    Decode →
  11. (11)
    ⟨fq(xm,m),fk(xn,n)⟩=g(xm,xn,m−n).\langle f_{q}({\boldsymbol{x}}_{m},m),f_{k}({\boldsymbol{x}}_{n},n)\rangle=g({\boldsymbol{x}}_{m},{\boldsymbol{x}}_{n},m-n).
    Decode →
  12. (12)
    fq(xm,m)=(Wqxm)eimθfk(xn,n)=(Wkxn)einθg(xm,xn,m−n)=Re⁡[(Wqxm)(Wkxn)∗ei(m−n)θ]\begin{gathered}f_{q}({\boldsymbol{x}}_{m},m) =({\boldsymbol{W}}_{q}{\boldsymbol{x}}_{m})e^{im\theta} \\ f_{k}({\boldsymbol{x}}_{n},n) =({\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n})e^{in\theta} \\ g({\boldsymbol{x}}_{m},{\boldsymbol{x}}_{n},m-n) =\operatorname{Re}[({\boldsymbol{W}}_{q}{\boldsymbol{x}}_{m})({\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n})^{*}e^{i(m-n)\theta}]\end{gathered}
    Decode →

Famous equations in this paper

Symbols, by category

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

Notation conventions

  1. qm{\boldsymbol{q}}_{m}, fqf_{q}, xm{\boldsymbol{x}}_{m}

    Subscript (index or label) 503 uses

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

  2. ff, mm, nn

    Italic letters (variables) 351 uses

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

  3. 02t/d0^{2t/d}, p~rk\tilde{{\boldsymbol{p}}}^{k}_{r}, p~rv\tilde{{\boldsymbol{p}}}^{v}_{r}

    Superscript (power or label) 36 uses

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

  4. p~\tilde{{\boldsymbol{p}}}, W~\widetilde{{\boldsymbol{W}}}

    Tilde accent 13 uses

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

  5. W{q,k}(11)W^{(11)}_{\{q,k\}}, W{q,k}(12)W^{(12)}_{\{q,k\}}, W{q,k}(21)W^{(21)}_{\{q,k\}}

    Superscript in parentheses 8 uses

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

  6. ⋯\cdots, ⋱\ddots

    Dots (ellipsis) 7 uses

    “And so on”: continue the pattern.

  7. (Wkxn)∗({\boldsymbol{W}}_{k}{\boldsymbol{x}}_{n})^{*}, k[2i:2i+1]∗{\boldsymbol{k}}_{[2i:2i+1]}^{*}

    Star (optimal) 6 uses

    As a superscript, the optimal value: θ* is the best θ. Between two terms, convolution.

Greek letters

  1. θ\theta, Θ\Theta

    Theta 103 uses

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

  2. ϕ\phi, φ\varphi

    Phi 20 uses

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

  3. γ\gamma

    Gamma 7 uses

    The discount factor in reinforcement learning, or the learned scale in normalization layers.

  4. β\beta

    Beta 2 uses

    Usually a decay rate (Adam’s β₁, β₂), a shift parameter in normalization, or a weight on a loss term.

Vectors and matrices

  1. q\boldsymbol{q}, x\boldsymbol{x}, k\boldsymbol{k}

    Bold lowercase (vector) 267 uses

    A bold lowercase letter is a vector: a list of numbers.

  2. W\boldsymbol{W}, U\mathbf{U}

    Bold uppercase (matrix) 86 uses

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

  3. qm⊺{\boldsymbol{q}}_{m}^{\intercal}, xm⊺{\boldsymbol{x}}_{m}^{\intercal}, Wq⊺{\boldsymbol{W}}_{q}^{\intercal}

    Transpose 48 uses

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

  4. ∥q∥\|{\boldsymbol{q}}\|, ∥k∥\|{\boldsymbol{k}}\|

    Norm 12 uses

    The length (size) of a vector. With subscript 2 or none, the Euclidean length √(Σxᵢ²).

  5. QQ, KK, VV

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

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

  6. ∣∑i=0d/2−1Si+1(hi+1−hi)∣|\sum_{i=0}^{d/2-1}S_{i+1}(h_{i+1}-h_{i})\bigg|, ∣Si+1∣|S_{i+1}|, ∣(hi+1−hi)∣|(h_{i+1}-{h_{i}})|

    Absolute value (bars) 7 uses

    Absolute value of a number; for a set, its size; for a matrix, sometimes its determinant.

  7. ⋅\cdot, [⋅][\cdot], (⋅)(\cdot)

    Centre dot 6 uses

    A dot product between vectors, plain multiplication between numbers, or an empty slot f(·).

  8. Inner product (angle brackets) 2 uses

    The inner (dot) product of two vectors: multiply matching entries and add.

  9. ⊗\otimes

    Tensor / Kronecker product 2 uses

    An outer, Kronecker or tensor product: every entry of one times every entry of the other.

Probability and statistics

  1. qq

    Probability p(x) 106 uses

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

  2. exp⁡\exp, eimθe^{im\theta}, einθe^{in\theta}

    Exponential 24 uses

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

  3. EN\mathbb{E}_{N}

    Expectation 1 use

    The expected value: the average of what is inside, weighted by the probabilities in the subscript.

Functions and operators

  1. ∑j=1N\sum_{j=1}^{N}, ∑n=1N\sum_{n=1}^{N}, ∑i=0d/2−1\sum_{i=0}^{d/2-1}

    Summation 18 uses

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

  2. sin⁡\sin

    Sine 15 uses

    The sine function; in ML papers mostly inside positional encodings and rotary embeddings.

  3. cos⁡\cos

    Cosine 15 uses

    The cosine function; in ML papers usually cosine similarity, a cosine schedule, or positional encodings.

  4. ::

    Arrow (maps to) 11 uses

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

  5. 1d/2\frac{1}{d/2}

    Fraction 7 uses

    Division: the top divided by the bottom.

  6. d\sqrt{d}

    Square root 3 uses

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

  7. max⁡i\max_{i}, min\text{min}, max\text{max}

    Max and min 3 uses

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

  8. Cases (piecewise) 1 use

    A piecewise definition: the value on each line applies when that line’s condition holds.

  9. clip⁡\operatorname{clip}

    Clip 1 use

    Limit a value to a range: clip(x, a, b) is a if x < a, b if x > b, else x.

  10. softmax⁡\operatorname{softmax}

    Softmax 1 use

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

Sets, shapes and definitions

  1. {q,k,v}\{q,k,v\}, {q,k}\{q,k\}, {wi}\{w_{i}\}

    Set braces 22 uses

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

  2. ∈\in

    Element of 11 uses

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

  3. :=:=

    Defined as (:=) 6 uses

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

  4. Rd\mathbb{R}^{d}

    Tensor shapes 5 uses

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

  5. ≤\leq

    Inequalities 2 uses

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

  6. R\mathbb{R}

    Real numbers (ℝ) and shapes 2 uses

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

5 things the catalog has no entry for yet
  • Re⁡\operatorname{Re} Re 3
  • sim⁡\operatorname{sim} sim 3
  • S\mathbb{S} S 2
  • O\mathbb{O} O 1
  • elu⁡\operatorname{elu} elu 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