Language Models · v1.0.8
2026-10-27 00:18:01
The motivating passage
The restaurant refused to serve me a ham sandwich because it only cooks vegetarian food. […] Their ambiance was just as good as the food and service.
Two requirements
A fully connected layer over the whole passage satisfies neither. An image of the wrong size can be resampled; a 40-word passage has no comparable operation.
Both are properties of the layer, not of the data.

A self-attention block maps \(N\) inputs of size \(D \times 1\) to \(N\) outputs of size \(D \times 1\).
\[\mathbf{v}_m = \boldsymbol\beta_v + \boldsymbol\Omega_v\mathbf{x}_m\]
\[\mathbf{sa}_n[\mathbf{x}_1, \ldots, \mathbf{x}_N] = \sum_{m=1}^{N} a[\mathbf{x}_m, \mathbf{x}_n]\,\mathbf{v}_m\]

\[\mathbf{q}_n = \boldsymbol\beta_q + \boldsymbol\Omega_q\mathbf{x}_n, \qquad \mathbf{k}_m = \boldsymbol\beta_k + \boldsymbol\Omega_k\mathbf{x}_m\]
\[a[\mathbf{x}_m, \mathbf{x}_n] = \frac{\exp\!\left[\mathbf{k}_m^T\mathbf{q}_n\right]}{\sum_{m'=1}^{N}\exp\!\left[\mathbf{k}_{m'}^T\mathbf{q}_n\right]}\]
Parameters
\[\boldsymbol\phi = \{\boldsymbol\beta_v, \boldsymbol\Omega_v, \boldsymbol\beta_q, \boldsymbol\Omega_q, \boldsymbol\beta_k, \boldsymbol\Omega_k\}\]
Scaling
Nonlinearity

With the \(N\) inputs as columns of \(\mathbf{X} \in \mathbb{R}^{D \times N}\):
\[\mathbf{V} = \boldsymbol\beta_v\mathbf{1}^T + \boldsymbol\Omega_v\mathbf{X}\] \[\mathbf{Q} = \boldsymbol\beta_q\mathbf{1}^T + \boldsymbol\Omega_q\mathbf{X}\] \[\mathbf{K} = \boldsymbol\beta_k\mathbf{1}^T + \boldsymbol\Omega_k\mathbf{X}\]
\[\mathbf{Sa}[\mathbf{X}] = \mathbf{V} \cdot \mathbf{Softmax}\!\left[\mathbf{K}^T\mathbf{Q}\right]\]

Self-attention is equivariant to permutation of its inputs:
\[\mathbf{Sa}[\mathbf{X}\mathbf{P}] = \mathbf{Sa}[\mathbf{X}]\mathbf{P}\]
for any permutation matrix \(\mathbf{P}\). Order is supplied from outside the mechanism.
\[\mathbf{Sa}[\mathbf{X}] = \mathbf{V} \cdot \mathbf{Softmax}\!\left[\frac{\mathbf{K}^T\mathbf{Q}}{\sqrt{D_q}}\right]\]
The scaling addresses a gradient problem, not an accuracy problem.

\(H\) sets of values, queries and keys are computed, and head \(h\) is
\[\mathbf{Sa}_h[\mathbf{X}] = \mathbf{V}_h \cdot \mathbf{Softmax}\!\left[\frac{\mathbf{K}_h^T\mathbf{Q}_h}{\sqrt{D_q}}\right]\]
The outputs are concatenated vertically and recombined:
\[\mathbf{MhSa}[\mathbf{X}] = \boldsymbol\Omega_c\!\left[\mathbf{Sa}_1[\mathbf{X}]^T, \ldots, \mathbf{Sa}_H[\mathbf{X}]^T\right]^T\]

\[ \begin{aligned} \mathbf{X} &\leftarrow \mathbf{X} + \mathbf{MhSa}[\mathbf{X}] \\ \mathbf{X} &\leftarrow \mathbf{LayerNorm}[\mathbf{X}] \\ \mathbf{x}_n &\leftarrow \mathbf{x}_n + \mathbf{mlp}[\mathbf{x}_n] \quad \forall\, n \\ \mathbf{X} &\leftarrow \mathbf{LayerNorm}[\mathbf{X}] \end{aligned} \]
Multi-head self-attention
Position-wise MLP


Byte pair encoding merges the most frequent adjacent token pair at each iteration, halting at a chosen vocabulary size.

\[\mathbf{X} = \boldsymbol\Omega_e\mathbf{T}\]
The embedding depends only on the token index. Position and context are supplied later, by the positional encoding and the attention layers.
| Model | Self-attention | Keys and values | Trained by |
|---|---|---|---|
| Encoder | Unmasked | Own embeddings | Predicting masked tokens |
| Decoder | Masked | Own embeddings | Predicting the next token |
| Encoder-decoder | Masked in the decoder | Encoder output, in cross-attention | Predicting the next output token |
Specification
| Vocabulary | 30,000 tokens |
| Embedding | 1024 |
| Layers | 24 |
| Heads | 16 |
| \(D_q\) per head | 64 |
| MLP hidden | 4096 |
| Parameters | ~340 million |
Two stages
\(\boldsymbol\Omega_{vh}, \boldsymbol\Omega_{qh}, \boldsymbol\Omega_{kh}\) are \(64 \times 1024\).

<cls> token are embedded and passed through the transformer layers; every token attends to every other token.A fraction of input tokens are replaced by <mask>; the corresponding outputs pass through softmax functions under a multiclass classification loss.
| Max input | 512 tokens |
| Batch size | 256 |
| Steps | 1 million |
| Corpus | 3.3 billion words |
<mask> pulled into the station
<cls> embedding, and named entity recognition from each word’s embedding.An extra layer converts the output vectors to the required format.
<cls> vector maps to one number through a logistic sigmoid, under binary cross-entropy\[Pr(t_1, t_2, \ldots, t_N) = Pr(t_1)\prod_{n=2}^{N} Pr(t_n \mid t_1, \ldots, t_{n-1})\]

Passing the whole sentence in one forward pass would give the term for \(\log Pr(\text{great} \mid \text{It takes})\) access to the answer and the right context.
Generation
A sequence, possibly just <start>, is fed in; a token is chosen and the extended sequence fed back. Prior embeddings do not depend on later ones, so computation is reused.
Scale
| Sequence length | 2048 |
| Batch | 3.2 million tokens |
| Layers | 96 |
| Embedding | 12288 |
| Heads | 96 |
| \(D_q\) per head | 128 |
| Training tokens | 300 billion |
| Parameters | 175 billion |
Given example pairs and a further question, the model often completes the sequence correctly without fine-tuning: few-shot learning.

Each output word is conditioned on the previous output words and on the source sentence.

\[\mathbf{Q} = \boldsymbol\beta_q\mathbf{1}^T + \boldsymbol\Omega_q\mathbf{X}_{dec}\] \[\mathbf{K} = \boldsymbol\beta_k\mathbf{1}^T + \boldsymbol\Omega_k\mathbf{X}_{enc}\] \[\mathbf{V} = \boldsymbol\beta_v\mathbf{1}^T + \boldsymbol\Omega_v\mathbf{X}_{enc}\]




Reduce the matrix
Sparsify the matrix
Modify the mechanism
The softmax terms \(\exp[\mathbf{k}^T\mathbf{q}]\) are treated as a kernel:
\[\exp[\mathbf{k}^T\mathbf{q}] \;\approx\; \mathbf{g}[\mathbf{k}]^T\mathbf{g}[\mathbf{q}]\]
The interaction is routed, not removed
Consequence
Why the application was not obvious
What the architectures supply
Transformers have nonetheless surpassed convolutional networks for image classification, through the scale of the models and of their pre-training data: what convolution supplies as an architectural bias, the transformer obtains from data.



<cls> token predicts the class probabilities.Without the supervised pre-training it does not match contemporary convolutional networks.

SWin
<cls> token; the final features are averagedDaViT