Skip to main content
Cloud & AI Hub
Browse
Glossary AI Directory Playgrounds Models Prompts Explainers Strategy Matrix Benchmark Decoder
intermediate 12 mins Read

How Transformers Work

An intuitive, visual and mathematical breakdown of the self-attention mechanism, feed-forward layers, and multi-head attention.

The Core Transformer Architecture

The Transformer architecture, introduced by Vaswani et al. in 2017, replaced recurrent neural networks (RNNs) by processing entire sequences of tokens in parallel. This shift eliminated the sequence-length bottleneck and enabled training at unprecedented scale.

The model relies on stackable layers composed of two primary sub-layers: Multi-Head Self-Attention and Position-Wise Feed-Forward Networks (FFN).

graph TD
    Input["Token Embeddings + Positional Encoding"] --> MultiHead["Multi-Head Self-Attention"]
    MultiHead --> AddNorm1["Add & LayerNorm"]
    AddNorm1 --> FFN["Feed-Forward Network"]
    FFN --> AddNorm2["Add & LayerNorm"]
    AddNorm2 --> Output["Output Logits"]
    
    %% Residual Connections
    Input -.-> AddNorm1
    AddNorm1 -.-> AddNorm2

The Multi-Head Self-Attention Pipeline

Self-attention allows the model to score the relevance of other tokens in a sequence relative to a target token. The mathematical operation relies on projecting input vectors into Query (QQ), Key (KK), and Value (VV) matrices:

  1. Linear Projections: Input embeddings XX are multiplied by weight matrices WQW^Q, WKW^K, and WVW^V: Q=XWQ,K=XWK,V=XWVQ = XW^Q, \quad K = XW^K, \quad V = XW^V

  2. Scaled Dot-Product Calculation: We compute the dot product similarity between queries and keys, scale by the dimension size dk\sqrt{d_k} to prevent gradient saturation, and apply a softmax function: Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

  3. Multi-Head Aggregation: Rather than performing attention once, multi-head attention projects the inputs into hh different subspaces, calculates attention in parallel, and concatenates the outputs: MultiHead(Q,K,V)=Concat(head1,…,headh)WO\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \dots, \text{head}_h)W^O where each head is calculated as: headi=Attention(QWiQ,KWiK,VWiV)\text{head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)


Layer Normalization and Residual Connections

To ensure stable gradient flow during deep network training, Transformers integrate residual connections (skip connections) around both major sub-layers. The output of each sub-layer is defined as:

SubLayerOutput=LayerNorm(x+SubLayer(x))\text{SubLayerOutput} = \text{LayerNorm}(x + \text{SubLayer}(x))

The Layer Normalization step normalizes values across the hidden dimension of each individual token, stabilizing activations independently of the batch size.


Position-Wise Feed-Forward Networks

Following attention normalization, each token’s representation passes through a Position-Wise Feed-Forward Network. This consists of two linear transformations with a non-linear activation (typically GeLU or SwiGLU) in between:

FFN(x)=max(0,xW1+b1)W2+b2\text{FFN}(x) = \text{max}(0, xW_1 + b_1)W_2 + b_2

This FFN processes each token coordinate independently and identically, introducing non-linear capacity to model complex feature interactions.

Historical figures, architectures, and capabilities are for informational purposes only. Not technical, professional, legal, or financial advice. Sources: Research papers, developer documentation.