Vision Transformer › ViT Architecture
Language AI HandbookAll chapters Vision Transformer › ViT ArchitectureOne section · Sections in this chapter5 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
With patches converted to embeddings and positional information injected, the remainder of the architecture follows the standard transformer encoder we detailed in Encoder Architecture. The embeddings pass through identical transformer blocks, each consisting of several key components working in concert.
The four core components of each transformer block are:
-
Multi-Head Self-Attention (MSA): As covered in Multi-Head Attention, this allows each patch to attend to all other patches in the sequence, modeling global relationships from the very first layer. This is in stark contrast to CNNs, where receptive fields grow gradually with depth and only achieve global context in the final layers. In ViT, a patch in the upper-left corner of an image can directly attend to a patch in the lower-right corner in layer 1, allowing the model to capture long-range visual relationships that would require many convolutional layers to achieve.
-
Feed-Forward Network (MLP): A two-layer MLP with GELU activation and hidden dimension typically set to , following the transformer convention from Feed-Forward Networks. Each position in the sequence passes through this MLP independently. This provides non-linear transformation capacity beyond what the attention mechanism alone can achieve. The MLP can be understood as transforming each patch representation in place, while attention mixes information across patches.
-
Layer Normalization: Applied before each sublayer (Pre-Norm configuration), as we discussed in Pre-Norm vs Post-Norm. Pre-Norm stabilizes training by ensuring that the inputs to each sublayer have controlled scale, which is particularly important for deep networks where vanishing or exploding gradients can derail training.
-
Residual Connections: Applied around both the attention and MLP blocks, creating gradient highways essential for training deep networks, as explored in Residual Connections. These connections allow gradients to flow directly back through the network to early layers without passing through the non-linearities of intermediate layers.
The Layer Equations
The equations for layer process the input through self-attention and feed-forward sublayers with residual connections:
where:
- : input to layer , the output from layer (or for the first layer)
- : intermediate representation after the Multi-Head Self-Attention sublayer and its residual connection
- : output of layer after the MLP sublayer and its residual connection
- : layer index, ranging from to (total number of layers)
- : Multi-Head Self-Attention function applied to all tokens in parallel
- : Feed-Forward Network applied position-wise with GELU activation and expansion ratio 4
- : Layer Normalization applied before each sublayer (Pre-Norm configuration)
The symmetry between the NLP transformer and ViT is striking: after the patch embedding step, the equations are identical. The same PyTorch TransformerEncoderLayer module used for BERT or GPT can be used for ViT without modification. This architectural equivalence is what makes unified multimodal models possible.
From CLS Token to Classification Logits
After layers, the final hidden state of the CLS token is extracted and passed to a classification head. This head is typically a single linear layer during fine-tuning, and a two-layer MLP during pre-training:
where maps from the embedding dimension to class logits. The CLS token is used here because it has no direct input content (it begins as a learned null token), which means its final representation is entirely determined by what it has aggregated through attention over the other tokens. It acts as a summary of the entire image, specialized through training to carry information relevant to the classification objective.
The computational cost of self-attention is , where is the number of patches. For a image with patches, , which is manageable on modern hardware. However, if we increase image resolution to , the number of patches grows to , and the attention computation becomes roughly 27 times more expensive due to the quadratic scaling. This limits ViT's ability to handle high-resolution images directly, motivating sparse attention patterns and hierarchical architectures such as the Swin Transformer, which we explore in the efficient attention chapters referenced from Part XVII.