# Transformers and Modern Attention Architectures: A Technical Survey ## 1. The Original Transformer — "Attention Is All You Need" (Vaswani et al., 2017) The Transformer (Vaswani et al., 2017, *Attention Is All You Need*, NeurIPS) dispensed entirely with recurrence and convolutions, relying solely on attention mechanisms to model dependencies between sequence positions. This enabled full parallelization over sequence length during training and became the foundation of virtually all modern large-scale models. ### 1.1 Scaled Dot-Product Attention Given queries $Q \in \mathbb{R}^{n \times d_k}$, keys $K \in \mathbb{R}^{m \times d_k}$, and values $V \in \mathbb{R}^{m \times d_v}$: $$\mathrm{Attention}(Q, K, V) = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right)V$$ Each query is compared against all keys via dot products; the softmax converts these similarity scores into a probability distribution over positions, which is then used to compute a weighted average of the values. The scaling factor $1/\sqrt{d_k}$ is essential: for large $d_k$, the dot products $q \cdot k = \sum_{i=1}^{d_k} q_i k_i$ grow in magnitude with variance proportional to $d_k$ (assuming unit-variance components), pushing the softmax into regions of extremely small gradients. Dividing by $\sqrt{d_k}$ keeps the logits at unit variance and stabilizes training. ### 1.2 Multi-Head Attention Rather than a single attention function over $d_{\text{model}}$-dimensional vectors, the Transformer projects $Q$, $K$, $V$ into $h$ lower-dimensional subspaces and applies attention in parallel: $$\mathrm{MultiHead}(Q, K, V) = \mathrm{Concat}(\mathrm{head}_1, \dots, \mathrm{head}_h)\,W^O$$ $$\mathrm{head}_i = \mathrm{Attention}(QW_i^Q,\; KW_i^K,\; VW_i^V)$$ with learned projections $W_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k}$, $W_i^K \in \mathbb{R}^{d_{\text{model}} \times d_k}$, $W_i^V \in \mathbb{R}^{d_{\text{model}} \times d_v}$, and $W^O \in \mathbb{R}^{hd_v \times d_{\text{model}}}$. In the base model, $h = 8$ and $d_k = d_v = d_{\text{model}}/h = 64$. Multiple heads allow the model to jointly attend to information from different representation subspaces at different positions — e.g., one head tracking syntactic dependencies, another tracking coreference. ### 1.3 Sinusoidal Positional Encoding Since attention is permutation-invariant, position information must be injected. The original paper adds fixed sinusoidal encodings to the input embeddings: $$PE_{(pos, 2i)} = \sin\!\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right), \qquad PE_{(pos, 2i+1)} = \cos\!\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)$$ where $pos$ is the position and $i$ indexes the dimension pair. Wavelengths form a geometric progression from $2\pi$ to $10000 \cdot 2\pi$. The key property: for any fixed offset $k$, $PE_{pos+k}$ is a linear function of $PE_{pos}$ (a rotation), allowing the model to learn relative positioning easily. ### 1.4 Encoder–Decoder Architecture, Feed-Forward, Residuals, LayerNorm - **Encoder**: a stack of $N = 6$ identical layers, each containing (i) multi-head self-attention and (ii) a position-wise feed-forward network, each wrapped in a residual connection followed by layer normalization: $\mathrm{LayerNorm}(x + \mathrm{Sublayer}(x))$ ("post-LN"; modern models typically use pre-LN for stability). - **Decoder**: also $N = 6$ layers, with three sub-layers: *masked* self-attention (a causal mask sets $-\infty$ on positions $j > i$ before the softmax, preserving the autoregressive property), *cross-attention* where queries come from the decoder and keys/values from the encoder output, and the feed-forward network. - **Position-wise feed-forward network**, applied identically at each position: $$\mathrm{FFN}(x) = \max(0,\; xW_1 + b_1)\,W_2 + b_2$$ with inner dimension $d_{ff} = 2048$ (a $4\times$ expansion over $d_{\text{model}} = 512$). - **Layer normalization** (Ba et al., 2016): $\mathrm{LN}(x) = \gamma \odot \frac{x - \mu}{\sigma} + \beta$, where $\mu, \sigma$ are the mean and standard deviation over the feature dimension. Self-attention costs $O(n^2 \cdot d)$ per layer in time and $O(n^2)$ in memory — the quadratic bottleneck motivating Section 4. ## 2. Positional Encoding Variants **Learned absolute embeddings.** A trainable matrix $E_{pos} \in \mathbb{R}^{L_{\max} \times d}$ is added to token embeddings (GPT-2, BERT, ViT). Simple but does not extrapolate beyond $L_{\max}$. **RoPE — Rotary Position Embedding** (Su et al., 2021, *RoFormer: Enhanced Transformer with Rotary Position Embedding*, arXiv:2104.09864). Instead of adding position vectors, RoPE *rotates* each 2D pair of query/key components by an angle proportional to the position $m$. For dimension pair $i$ with frequency $\theta_i = 10000^{-2i/d}$: $$f(x, m) = R_{\Theta, m}\, x, \qquad R_{\Theta,m} = \bigoplus_{i=1}^{d/2} \begin{pmatrix} \cos m\theta_i & -\sin m\theta_i \\ \sin m\theta_i & \cos m\theta_i \end{pmatrix}$$ The crucial property is that the attention score depends only on relative position: $$\langle f(q, m),\, f(k, n) \rangle = \langle R_{\Theta, m} q,\; R_{\Theta, n} k \rangle = q^\top R_{\Theta, n-m}\, k$$ RoPE unifies absolute encoding (applied per position) with relative behavior (in the inner product), and is used in GPT-NeoX, LLaMA, Mistral, Qwen, and most modern LLMs. Long-context extensions (position interpolation, NTK-aware scaling, YaRN) rescale its frequencies. **ALiBi — Attention with Linear Biases** (Press et al., 2021/2022, *Train Short, Test Long*, ICLR). No embeddings at all; instead a static distance-proportional penalty is added to attention logits: $$\mathrm{softmax}\!\left(q_i K^\top / \sqrt{d_k} \;+\; m \cdot [-(i-1), \dots, -1, 0]\right)$$ where $m$ is a fixed, head-specific slope (a geometric sequence such as $2^{-8/h}, 2^{-16/h}, \dots$). ALiBi gives strong length extrapolation: models trained on short sequences degrade gracefully at much longer inference lengths (used in BLOOM and MPT). ## 3. The Major Model Families **BERT** (Devlin et al., 2018, *BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding*). Encoder-only, bidirectional attention. Pre-trained with **Masked Language Modeling**: 15% of tokens are selected; of these, 80% replaced by `[MASK]`, 10% by a random token, 10% left unchanged, and the model predicts the originals by minimizing cross-entropy over masked positions: $$\mathcal{L}_{\text{MLM}} = -\mathbb{E}\left[\sum_{i \in \mathcal{M}} \log p_\theta(x_i \mid x_{\setminus \mathcal{M}})\right]$$ plus Next Sentence Prediction (later dropped in RoBERTa, Liu et al., 2019). Ideal for understanding/classification tasks, not generation. **GPT** (Radford et al., 2018; GPT-2, 2019; GPT-3, Brown et al., 2020, *Language Models are Few-Shot Learners*; GPT-4, OpenAI, 2023). Decoder-only, causal attention. Trained by maximizing the autoregressive log-likelihood: $$\mathcal{L}(\theta) = \sum_{t=1}^{T} \log p_\theta(x_t \mid x_1, \dots, x_{t-1}), \qquad p_\theta(x) = \prod_{t=1}^{T} p_\theta(x_t \mid x_{