Transformer
A transformer is a deep neural network built around self-attention, a content-based operation that lets each token compute weighted interactions with every other token. Unlike RNNs, transformers process sequences in parallel, and unlike CNNs, they use global rather than local receptive fields. This makes them highly effective for modeling long-range dependencies across text, images, audio, and multimodal data.
Given an input sequence of token embeddings a transformer layer first linearly projects each token to queries, keys, and values:
with parameter matrices and . Having both and allows for asymmetric relevance relationships, whereas contains the actual content to aggregate from each token.The scaled dot-product attention computes, for all positions at once,
where the matrix contains pairwise similarity scores between tokens, scaled by to improve training by ensuring that the inner product variance stays close to . The row-wise softmax creates a probability distribution over tokens, producing a weighted average of their value vectors.To capture multiple types of relationships in parallel, transformers apply multi-head attention, running several attention modules in parallel and concatenating their outputs:
with . Different heads specialize to different structures (syntax, coreference, long-range links) giving layers richer representational capacity. Large models often use grouped-query attention (GQA) to share keys and values across heads to reduce memory and compute.Each transformer layer then alternates multi-head attention with a position-wise feedforward network (FFN) and residual connections:
with normalization applied before and after each block. Since self-attention is permutation-invariant, positional encodings are added to preserve sequence order.Quadratic Complexity requires computation and memory in the sequence length . This becomes a bottleneck for long sequences—both slow and memory-intensive. Transformers also cannot generalize beyond a pre-defined maximum sequence length (determined by the positional encoding scheme), unlike RNNs which process arbitrary-length inputs sequentially.
The attention matrix
Intuition
Self-attention can be understood as a soft, differentiable lookup table: the query says “here is what I’m looking for,” each key says “here is what I have,” and the attention weights determine how much each value contributes to the output. Unlike a hard lookup (which returns a single match), soft attention returns a weighted average over all values. In the RNN seq2seq setting, queries come from the decoder hidden state and keys/values from the encoder; in a transformer’s self-attention, all three come from the same sequence, letting every token attend to every other token simultaneously.
The FFN is critical because self-attention on its own is a linear operation over value vectors: stacking more self-attention layers without nonlinearities would just re-average the same values. The FFN introduces element-wise nonlinearity, giving the network the capacity to transform token representations beyond what weighted averaging can achieve. In practice, the FFN accounts for the majority of a transformer’s parameters and FLOPs—at GPT-3 scale (~175B parameters), roughly 80% of floating-point operations occur in the FFN layers.
Residual connections ensure an uninterrupted gradient flow through the full depth of the network (analogous to the cell state highway in LSTMs), while layer normalization keeps activations on a stable scale. Together with multi-head attention, these components form a clean, modular architecture that is highly parallelizable on GPUs—the key advantage over recurrent architectures.
Properties
Input Embedding
Given a sequence of tokens from a vocabulary of size , the input layer produces -dimensional vectors:
where is a learned embedding lookup table and is the positional encoding for position .
Feedforward Network (FFN)
(FFN Formula) The position-wise feedforward network applies a two-layer MLP independently to each token:
where , , , . The inner dimension is typically (e.g., for in the original transformer). Modern architectures replace ReLU with SwiGLU or GELU-GLU variants.
(Why FFN Is Needed) Self-attention computes a weighted sum of value vectors: . This is a linear operation over the values. Without the FFN, stacking multiple self-attention layers would only produce different weighted averages of the same value space—no element-wise nonlinear transformation is ever applied. The FFN breaks this by applying a nonlinear transformation to each token independently, giving the network the capacity to learn complex per-token features.
(FFN Operates Per-Token) Unlike self-attention, which models dependencies across tokens, the FFN and layer normalization operate independently on each token. Self-attention is the only component in a transformer layer that mixes information between different positions.
Residual Connections and Layer Normalization
(Residual Connection) Each sub-layer (attention or FFN) is wrapped in a residual connection: . This prevents vanishing gradients by providing a direct additive path for gradient flow, and allows to learn the residual difference from the identity rather than the full mapping.
(Layer Normalization) After each residual addition, layer normalization is applied: , where are learned scale and shift parameters. This normalizes activations to a consistent range, stabilizing training.
Self-Attention Complexity
(Time and Space) For a sequence of length with model dimension , a single self-attention layer requires time and memory. The term dominates for long sequences. By contrast, a recurrent layer is in time but in memory—cheaper in memory for long sequences but inherently sequential.
(Parallelism) Unlike RNNs where the forward pass at position depends on position (yielding sequential operations), self-attention computes all positions simultaneously in sequential steps. This is the fundamental reason transformers train much faster on modern GPU hardware.
Examples
Transformer Layer Forward Pass
Given input and a single-head transformer layer with :
Step-by-step computation
- Self-attention: Compute , , , then .
- Residual + Norm: .
- FFN: .
- Residual + Norm: .
The output has the same shape as the input, so layers can be stacked: the -th layer takes the -th layer’s output as input.