Inferensys

Glossary

Patch Embedding

The process of dividing an image into fixed-size patches and linearly projecting each into a vector to serve as input tokens for a Vision Transformer.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
VISION TRANSFORMER INPUT TOKENIZATION

What is Patch Embedding?

Patch embedding is the foundational tokenization process for Vision Transformers (ViTs) that converts a 2D image into a sequence of 1D vector tokens suitable for a standard Transformer encoder.

Patch Embedding is the process of dividing an input image into a grid of fixed-size, non-overlapping patches and linearly projecting each patch into a flat vector. This operation transforms the raw image from a 3D tensor (H, W, C) into a sequence of 2D tokens (N, D), where N is the number of patches and D is the embedding dimension, effectively converting a spatial computer vision problem into a sequence-to-sequence problem.

A learnable [CLS] token is prepended to the sequence of patch embeddings, and 1D learnable positional encodings are added to retain spatial information lost during flattening. This sequence is then fed directly into a standard Transformer encoder, allowing the model to apply global self-attention across all image regions from the very first layer, unlike CNNs which build receptive fields hierarchically.

Vision Transformer Tokenization

Key Characteristics of Patch Embedding

Patch embedding is the foundational tokenization step for Vision Transformers (ViTs), converting raw 2D images into a sequence of 1D vector tokens that a transformer encoder can process.

01

Linear Projection of Spatial Pixels

An input image x ∈ R^(H×W×C) is reshaped into a sequence of flattened 2D patches x_p ∈ R^(N×(P²·C)), where (P, P) is the patch resolution and N = HW/P² is the resulting number of tokens. Each patch is then mapped to a latent D-dimensional embedding via a trainable linear projection E ∈ R^((P²·C)×D). This is mathematically equivalent to a 2D convolution with a stride and kernel size equal to the patch size, making it a fully learned, non-overlapping feature extractor.

02

Positional Encoding Injection

Unlike CNNs which inherently capture spatial structure via sliding filters, the subsequent transformer self-attention is permutation-invariant. To retain spatial inductive bias, learnable 1D position embeddings are added to the patch embeddings. This allows the model to distinguish patch_2,3 (row 2, column 3) from patch_3,2. Standard ViTs use absolute position embeddings, while later variants explore relative positional biases or 2D-aware encodings to improve resolution generalization.

03

Class Token Prepending

Following BERT's design, a learnable [class] token embedding is prepended to the sequence of patch embeddings before entering the transformer encoder. The final hidden state corresponding to this token serves as the aggregate image representation y for classification heads. This avoids imposing a specific pooling structure on the patch-level outputs and allows the transformer to dynamically weigh information from all patches through self-attention.

04

Resolution-Agnostic Sequence Length

The number of patches N is a quadratic function of input resolution: N = (H/P) × (W/P). This creates a direct compute-performance trade-off. A 224×224 image with P=16 yields 196 tokens, while a 384×384 image yields 576 tokens. Since self-attention complexity is O(N²), higher resolutions dramatically increase memory usage. Techniques like 2D interpolation of pre-trained position embeddings are required to fine-tune models on resolutions unseen during pre-training.

05

Hybrid Stem Alternatives

Instead of raw pixel patches, a standard ResNet feature map can serve as the input sequence. In this hybrid architecture, the patches are formed from a lower-resolution convolutional feature map (e.g., stride-32 output), where each spatial position is treated as a 'patch' embedding. This injects stronger local inductive biases like translation equivariance directly into the token stream, often improving sample efficiency and accuracy on smaller datasets like ImageNet-1k.

06

Information Compression Bottleneck

The patch size P defines the granularity of the initial visual vocabulary. A small patch size (e.g., P=8) preserves fine-grained textures and small objects but generates a long sequence (e.g., 784 tokens), increasing compute. A large patch size (e.g., P=32) creates a coarse, highly compressed token sequence (e.g., 49 tokens), which is computationally cheap but risks losing small-scale structural details crucial for dense prediction tasks like semantic segmentation.

PATCH EMBEDDING

Frequently Asked Questions

Clear, technical answers to the most common questions about how Vision Transformers convert raw images into token sequences using patch embedding.

Patch embedding is the process of dividing an input image into a grid of fixed-size, non-overlapping patches and linearly projecting each patch into a flat vector, creating a sequence of tokens that a Vision Transformer (ViT) can process. The mechanism operates by reshaping an image of dimensions H x W x C into a sequence of N flattened 2D patches, where N = HW / P² and P is the patch size. Each patch is then multiplied by a learnable weight matrix E to project it into a D-dimensional embedding space. A learnable [class] token is prepended to this sequence, and 1D positional embeddings are added to retain spatial information. This transforms the raw image into a format identical to a sentence of word embeddings, allowing the standard Transformer encoder to apply multi-head self-attention across all patches.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.