ABAB

Matrix multiplication

Two matrices written side by side, like AB, are multiplied. Each entry of the result is a row of A dotted with a column of B. An (m×n) times an (n×p) gives an (m×p).

Entry (i,j)(i,j) of ABAB is the dot product of row ii of AA with column jj of BB. For that to work, a row of AA must be as long as a column of BB, so the inner sizes must match. Two things to remember:

  • Shapes tell you what happens. xWQ\mathbf{x}W^Q, with x\mathbf{x} of width dmodeld_{\text{model}} and WQW^Q of shape dmodel×dkd_{\text{model}} \times d_k, produces a query of width dkd_k. The shared middle size disappears.
  • Order matters. ABAB and BABA are usually different, and one of them may not even be defined.

Every "projection" in the course is one matrix multiplication. In PyTorch it is A @ B.

Example

[12][3041]=[1⋅3+2⋅41⋅0+2⋅1]=[112]\begin{bmatrix} 1 & 2 \end{bmatrix} \begin{bmatrix} 3 & 0 \\ 4 & 1 \end{bmatrix} = \begin{bmatrix} 1{\cdot}3 + 2{\cdot}4 & 1{\cdot}0 + 2{\cdot}1 \end{bmatrix} = \begin{bmatrix} 11 & 2 \end{bmatrix}

A 1×2 times a 2×2 gives a 1×2.

Where the course uses it

Related terms