Session #9: Transformer Architecture (Continued)
Free University Berlin
2026-04-26
Game plan
This lecture provides an introduction to the transformer architecture with a focus on the decoder.
Important: - https://arxiv.org/pdf/2305.17026
Amatriain X et al. (2024) Transformer models: an introduction and catalog. arXiv 2302.07730
An embarassment of riches
A decoder-only transformer is an autoregressive neural network architecture that uses masked causal self-attention to predict the next token in a sequence.
image: Perez L et al. (2021)Automatic Code Generation using Pre-Trained Language Models. arXiv 2102.10535
| Tensor | Dimension | Example | Explanation |
|---|---|---|---|
|
Input sequence [BOS] The quick brown fox jumps over the lazy dog [EOS] |
Input tokens with additional beginning/end of sequence tokens | ||
|
Tokenized Input sequence [50256, 791, 4062, 14198, 39935, 35308, 927, 279, 16053, 5679, 50256] |
\(N\) | \(11\) | Number of tokens, including [BOS]/[EOS] |
| \(\mathbf{X}\): Input embeddings | \(N\times d_{hidden}\) | \(11\times 512\) | \(d_{hidden}\) is the embedding dimension |
|
\(\mathbf{W}^Q\): query weights \(\mathbf{W}^K\): key weights \(\mathbf{W}^V\): value weights |
\(d_{hidden}\times d_{hidden}\) | \(512\times 512\) | Learnable model weights (single-head attention, so \(d_k=d_{hidden}\)) |
|
\(\mathbf{Q}=\mathbf{XW}^Q\): query matrix \(\mathbf{K}=\mathbf{XW}^K\): key matrix \(\mathbf{V}=\mathbf{XW}^V\): value matrix |
\(N\times d_{hidden}\) | \(11\times 512\) |
\(\mathbf{Q}\): What each token needs to know from others \(\mathbf{K}\): What information each token provides \(\mathbf{V}\): Content each token shares when attended to |
| \(\mathbf{QK}^T\): attention scores | \(N\times N\) | \(11\times 11\) | How well each token’s query matches the keys of the other tokens |
| \(\mathrm{softmax}\left(\dfrac{\mathbf{QK}^T}{\sqrt{d_k}}\right)\mathbf{V}\) | \(N\times d_{hidden}\) | \(11\times 512\) | Attention weights applied to Values — context-aware token representations |
| \(\times\ \mathbf{W}^O\): output projection (optional here) | \(N\times d_{hidden}\) | \(11\times 512\) | Required to combine multiple heads back to \(d_{hidden}\); with a single head the shape already matches, so \(W^O\) is optional (often kept anyway for extra learnable capacity) |
| Add & Norm | \(N\times d_{hidden}\) | \(11\times 512\) | Residual connection + LayerNorm (see Part 1) |
| FFN: \(\text{GELU}(\mathbf{X}W_1+b_1)W_2+b_2\) | \(N\times d_{hidden}\) | \(11\times 512\) | Position-wise, applied independently per token; expands to \(d_{ff}\) (e.g. \(2048\)) and projects back (see Part 1) |
This recap shows single-head attention, so \(d_k=d_{hidden}\)
Adapted from: Simon J Decoder-only inference: A step-by-step dive (video)
\[ \mathrm{MultiHead}(Q,K,V) = \mathrm{Concat}(\mathrm{head}_1, \ldots,\mathrm{head}_h)\mathrm{W}^{O} \] where \[ \mathrm{head}_i = \mathrm{Attention}(QW_i^Q, KW_i^K, VW_i^V) \]
| Tensor | Dimension | Example | Explanation |
|---|---|---|---|
|
Input sequence [BOS] The quick brown fox jumps over the lazy dog [EOS] |
Input tokens with additional beginning/end of sequence tokens | ||
|
Tokenized Input sequence [50256, 791, 4062, 14198, 39935, 35308, 927, 279, 16053, 5679, 50256] |
\(N\) | \(11\) | Number of tokens, including [BOS]/[EOS] |
| \(\mathbf{X}\): Input embeddings | \(N\times d_{hidden}\) | \(11\times 512\) | \(d_{hidden}\) is the embedding dimension |
|
\(\mathbf{W}^{Q_i}\): query weights \(\mathbf{W}^{K_i}\): key weights \(\mathbf{W}^{V_i}\): value weights |
\(d_{hidden}\times d_{mha}\) | \(512\times 64\) |
\(d_{mha}=d_{hidden}/h\), where \(h\) is the number of heads each head has its own weight matrices |
|
\(\mathbf{Q}_i=\mathbf{XW}^{Q_i}\): query matrix \(\mathbf{K}_i=\mathbf{XW}^{K_i}\): key matrix \(\mathbf{V}_i=\mathbf{XW}^{V_i}\): value matrix |
\(N\times d_{mha}\) | \(11\times 64\) | all heads run in parallel |
| \(\mathbf{Q}_i\mathbf{K}_i^T\): attention scores | \(N\times N\) | \(11\times 11\) | How well each token’s query matches the keys of the other tokens |
| \(\mathrm{softmax}\left(\dfrac{\mathbf{Q}_i\mathbf{K}^T}{\sqrt{d_k}}\right)\mathbf{V}_i\) | \(N\times d_{mha}\) | \(11\times 64\) | Attention weights applied to Values — context-aware token representations |
| Concatenate head outputs | \(N\times d_{hidden}\) | \(11\times 512\) | Attention weights applied to Values — context-aware token representations |
| \(\times\ \mathbf{W}^O\): output projection (optional here) | \(N\times d_{hidden}\) | \(11\times 512\) | Required to combine multiple heads back to \(d_{hidden}\); with a single head the shape already matches, so \(W^O\) is optional (often kept anyway for extra learnable capacity) |
| Add & Norm | \(N\times d_{hidden}\) | \(11\times 512\) | Residual connection + LayerNorm (see Part 1) |
| FFN: \(\text{GELU}(\mathbf{X}W_1+b_1)W_2+b_2\) | \(N\times d_{hidden}\) | \(11\times 512\) | Position-wise, applied independently per token; expands to \(d_{ff}\) (e.g. \(2048\)) and projects back (see Part 1) |
Adapted from: Simon J Decoder-only inference: A step-by-step dive (video)
| Tensor | Dimension | Example | Explanation |
|---|---|---|---|
|
Attention output for the input sequence (aka prefill) |
\(N\times d_{hidden}\) | \(11\times 512\) | This matrix represets the input embeddings that have been updated to consider the context of other tokens |
| Attention output for the last token | \(1\times d_{hidden}\) | \(11\times 512\) | |
|
\(\mathbf{W}_{out}\): linear layer (aka project layer) |
\(V\times d_{hidden}\) | \(100,000\times 512\) | \(V\) is the vocabulary size, i.e., number of tokens |
| \(\text{Logits} = \text{attention output}\times \mathbf{W}_{out}^T\) | \(1\times V\) | \(1\times 100,000\) | Raw scores for all tokens |
| \(\text{softmax}(\text{Logits})\) | \(1\times V\) | \(1\times 100,000\) | token probabilities |
| Decode the token | 1 token | 1 | outside the model; see next page |
Deterministic methods:
\[ \arg \max_w P(w\mid w_1:w_{t-1}) \]
Stochastic methods:
Human-generated text exhibits greater variance in token probabilities, reflecting a diverse range of word choices, often unexpected.
In contrast, the output from deterministic methods shows minimal variance, resulting in more predictable and potentially repetitive text.
Fan A (2018) [Hierarchical Neural Story Generation. arXiv 1805.04833]https://arxiv.org/abs/1805.04833)
Holtzman A (2020) [The Curious Case of Neural Text Degeneration. arXiv 1904.09751]https://arxiv.org/abs/1904.09751)
\[ P(x_i) = \frac{\exp(z_i / t)}{\sum_j \exp(z_j / t)} \]
| token | logit \(z_i\) |
|---|---|
| mat | 2.0 |
| floor | 1.0 |
| bed | 0.1 |
| couch | -1.0 |
| Temperature | mat | floor | bed | couch | |
|---|---|---|---|---|---|
| 0 | t=0.5 | 0.862 | 0.117 | 0.019 | 0.002 |
| 1 | t=1.0 | 0.638 | 0.235 | 0.095 | 0.032 |
| 2 | t=2.0 | 0.451 | 0.274 | 0.174 | 0.101 |
temperature and top_p as independent parameters.The problem
\[ \text{MaskedAttention}(Q,K,V) = \text{softmax}\left(\frac{QK^\top}{\sqrt{d_k}} + M\right)V \]
\[ M_{i,j} = \begin{cases} 0 & j \le i \quad \text{(allowed: current or past token)} \\ -\infty & j > i \quad \text{(disallowed: future token)} \end{cases} \]
\[ M = \begin{bmatrix} 0 & -\infty & -\infty & -\infty \\ 0 & 0 & -\infty & -\infty \\ 0 & 0 & 0 & -\infty \\ 0 & 0 & 0 & 0 \end{bmatrix} \]
Suppose the raw attention scores \(S =\frac{QK^\top}{\sqrt{d_k}}\) for our sentence. - The masking procedure is as follows
\[ \mathbf{S} + \mathbf{M} = \begin{bmatrix} 0.9 & 0.2 & -0.1 & 0.4 \\ 0.3 & 1.1 & 0.5 & -0.2 \\ 0.1 & 0.4 & 1.3 & 0.6 \\ -0.2 & 0.3 & 0.7 & 1.5 \end{bmatrix} + \begin{bmatrix} 0 & -\infty & -\infty & -\infty \\ 0 & 0 & -\infty & -\infty \\ 0 & 0 & 0 & -\infty \\ 0 & 0 & 0 & 0 \end{bmatrix} = \begin{bmatrix} 0.9 & -\infty & -\infty & -\infty \\ 0.3 & 1.1 & -\infty & -\infty \\ 0.1 & 0.4 & 1.3 &-\infty \\ -0.2 & 0.3 & 0.7 & 1.5 \end{bmatrix} \]
\[ \mathrm{softmax}(\mathbf{S} + \mathbf{M}) = \begin{bmatrix} 1.0 & 0 & 0 & 0 \\ 0.31 & 0,690 & 0 & 0 \\ 0.176 & 0.238 & 0.586 &0 \\ 0.094 & 0.156 & 0.232 & 0.517 \end{bmatrix} \]
Traditional ML:
transfer learning approach reused a trained model for a new task by “tuning” the previously trained model| LLM | Param count | Size |
|---|---|---|
| GPT-3 (2020) | 175 billion | \(\sim\) 350 GB |
| LLaMA-2 (2023) | 7B, 13B, 70B | \(\sim\) 14 GB–140 GB |
| Llama 3 | 15 trillion tokens | \(\sim\) 60 TB |
| GPT-4 | (Estimated)~1.8 trillion | Proprietary |
Kaplan J et al. (2020) Scaling Laws for Neural Language Models. arXiv 2001.08361
Hoffmann J et al. (2022) Training Compute-Optimal Large Language Models. arXiv 2203.15556
Causal language modeling
\[ P(x_1,\ldots,x_n) = \prod_{t=1}^n P(x_t \mid x_1,\ldots,x_{t-1}) \]
\[ \mathcal{L}_t = -\log \hat{P}(x_t \mid x_1,\ldots,x_{t-1}) \]
\[ \mathcal{L} = -\sum_{t=1}^n \log \hat{P}(x_t \mid x_1,\ldots,x_{t-1}) \]
Image: https://www.deeplearning.ai/
Continuing “I am an automaton” — suppose after “I am an” the model’s predicted distribution over the next token is:
| token | P(token) |
|---|---|
| automaton | 0.55 |
| apple | 0.05 |
| idea | 0.15 |
| the | 0.25 |
Image credit: Generative LLM inference with Neuron (https://awsdocs-neuron.readthedocs-hosted.com/)
\[ 2\times 2\times\text{batch-size}\times\text{seq-length}\times\text{num-layers}\times\text{embedding dimension} \] - We need to multiply by 2 because we have \(K\) and \(V\) - We need to multiply by 2 again because we have 16 bits (FP16) - Usually gigabytes of memory required - Many different attempts to shrink cache size to allow the batch size to be increased
HBM:
| Tensor | Dimension | Example | Explanation |
|---|---|---|---|
| \(\mathbf{X}\): input embeddings | \(N\times d_{hidden}\) | \(11\times 512\) | Embedded input tokens |
|
\(\mathbf{W}^{Q_i}\): query weights \(\mathbf{W}^{K_i}\): key weights \(\mathbf{W}^{V_i}\): value weights |
\(d_{hidden}\times d_{mha}\) | \(512\times 64\) |
\(d_{mha}=d_{hidden}/h\), where \(h\) is the number of heads each head has its own weight matrices |
|
\(W_{down}\): down-projection matrix \(W_{up}\): up-projection matrix |
\(d_{mha}\times d_{mha-latent}\) \(d_{latent}\times d_{hidden}\) |
\(64\times 4\) \(32\times 512\) |
\(d_{latent}\) should be much smaller than \(d_{hidden}\) (here, \(d_{latent}=32\) and \(d_{hidden}=512\)) \(d_{mha-latent} = d_{latent}/h\) where \(h\) is the number of attention heads (e.g. \(8\)) |
|
\(\mathbf{Q}_i=\mathbf{XW}^{Q_i}\): query matrix \(\mathbf{K}_i=\mathbf{XW}^{K_i}\mathbf{W}_{down}\): key matrix \(\mathbf{V}_i=\mathbf{XW}^{V_i}\mathbf{W}_{down}\): value matrix |
\(N\times d_{mha}\) \(N\times d_{mha-latent}\) |
\(11\times 64\) \(11\times 4\) \(11\times 4\) |
all heads run in parallel |
| \(\mathbf{Q}_i\mathbf{W}_{down}\mathbf{K}_i^T\): attention scores | \(N\times N\) | \(11\times 11\) | All heads run in parallel. \(Q\) is down-projected for this calculation only and not cached. |
| \(\mathrm{softmax}\left(\dfrac{\mathbf{Q}_i\mathbf{W}_{down}\mathbf{K}_i^T}{\sqrt{d_k}}\right)\mathbf{V}_i\) | \(N\times d_{mha-latent}\) | \(11\times 4\) | Attention weights applied to Values — all heads run this in parallel |
| Concatenate head outputs | \(N\times d_{latent}\) | \(11\times 32\) | |
| \(\mathrm{Outputs}\times \mathbf{W}_{up}\) | \(N\times d_{hidden}\) | \(11\times 512\) | Project output back to original dimension |
| Remaining steps | \(\ldots\) | \(\ldots\) | as before |