VLM notes 1: from pixels to tokens
How a vision-language model turns an image into a sequence of tokens that a language model can read.
A vision-language model (VLM) is best understood as a large language model that receives a few extra "words" in its input, words that happen to have been computed from an image. This chapter describes how those words are made.
In the LLaVA family of models, which we use as the running example, the input side consists of three stages. A vision encoder, usually a Vision Transformer (ViT), converts the image into a set of patch features. A small projector maps these features into the embedding space of the language model. Finally, the resulting visual tokens are concatenated with the text tokens, and the combined sequence is passed to the language model.
1. Patchify
Let the image be and let be the patch size. The image is divided into a grid of non-overlapping patches, giving
where each is one patch flattened into a vector. Each of these vectors is then mapped to the model width by a shared linear projection , and a learned position embedding is added:
The position embedding is essential. Attention by itself is invariant to the order of its inputs, so without the model would have no way of knowing where in the image a patch came from. The classification token is optional, and many VLMs discard it and use all patch tokens instead.
The number of tokens depends strongly on the resolution, as the following table shows.
| Resolution | Grid | ||
|---|---|---|---|
| 224 × 224 | 16 | 14 × 14 | 196 |
| 224 × 224 | 14 | 16 × 16 | 256 |
| 336 × 336 | 14 | 24 × 24 | 576 |
Because grows with the square of the side length, doubling the resolution produces four times as many visual tokens. Since the cost of attention is , the computational cost grows faster still. This is the reason high-resolution VLMs divide the image into tiles or merge neighboring tokens.
Flattening a patch and multiplying it by is equivalent to a convolution whose kernel size and stride are both . Implementations usually take advantage of this:
2. The ViT block
The sequence is processed by identical pre-norm transformer encoder blocks:
Inside the multi-head self-attention (MSA), the queries, keys, and values are computed as , , and , and combined as
The matrix has shape , so every patch attends to every other patch. Unlike the language model, the encoder uses no causal mask. The factor prevents the dot products from growing with the dimension, which would otherwise saturate the softmax. The residual connections mean that each layer adds to the representation rather than replacing it.
One practical detail deserves mention. VLMs often take their features from the second-to-last layer of the encoder rather than the last. The final layer is specialized for the encoder's own pretraining objective, such as CLIP's image–text matching, whereas the preceding layer retains more spatial detail.
3. The projector
The encoder produces , but the language model expects vectors of width , for example 1024 versus 4096. The projector bridges this gap. In the original LLaVA it is a single linear layer,
and LLaVA-1.5 replaces it with a two-layer MLP:
Note that this projector does not change the number of tokens . Other designs use the projector to compress the sequence. Pooling or pixel shuffle merges each neighborhood into a single token, reducing to while the channel dimension grows fourfold. A Q-Former or Perceiver resampler instead uses a fixed set of learned queries that cross-attend to the patch features, so the output always has tokens regardless of resolution. The trade-off is that fewer tokens make the model cheaper and leave more context for text, at the cost of fine detail such as small text in images or the ability to count objects.
4. Joining with text
The text tokens pass through the language model's ordinary embedding table, , and the two sequences are concatenated:
In practice the prompt contains a placeholder such as <image>, and that single position is replaced by the visual embeddings:
From this point on, the language model runs exactly as usual, with a causal mask. It has no special mode for vision; the visual tokens are simply earlier positions in the sequence that it can attend to.
Summary
An image passes through four steps: it is cut into patches, encoded by a ViT, mapped by a projector, and inserted into the language model as soft tokens in its embedding space. The number of tokens is the main factor that determines cost, since it grows quadratically with resolution and attention grows quadratically with . The projector is small compared with the encoder and the language model, but it is the only component that must be trained from scratch. How that training is done is the subject of the next chapter.