Vision Encoders in Practice: How Images Become Tokens for Large Language Models
This article explains how vision encoders convert images into tokens for multimodal large language models. It covers size alignment, patch splitting, 2×2 spatial merging, projection into the language model's hidden dimension, token budget planning with a sample Python function, parameter estimation for a ViT backbone, and precision and deployment considerations.
1. Where the Vision Encoder Sits in a Multimodal System
Language models can only process sequences of discrete tokens or vectors. They cannot read pixels directly. Multimodal systems therefore add a vision encoder in front of the language model. The encoder converts images or video frames into representations that match the text vector dimension, and these representations are fed into the decoder together with the text. The design of the vision encoder determines three things: how much detail the model can see, how much context length an image occupies, and how high the inference latency is.
In recent years, the industry has increasingly moved toward integrating vision natively into models, rather than relying only on external image captioning. Native integration lets the model decide during inference when to look at an image and when to use visual feedback to guide later actions. This approach is especially valuable for programming assistance, interface understanding, and document parsing, because these tasks often require repeatedly checking screenshots against code or text.
2. The Overall Processing Pipeline
A typical visual input pipeline has four steps:
- Size normalization: Resize images of any resolution to the size range the model accepts.
- Patch splitting: Divide the image into fixed-size blocks, each corresponding to one base vector.
- Spatial merging: Combine adjacent blocks to reduce the length of the subsequent sequence.
- Projection alignment: Use a linear layer or a small MLP to map visual features into the language model’s hidden dimension.
The first three steps belong to preprocessing and the vision backbone. The fourth step ensures that visual vectors can be computed in the same space as text vectors. Each step affects the final token count, so they should be understood together.
3. Size Alignment and Zero Padding
Convolution or patch splitting requires the input size to be divisible by the stride. Otherwise, edge pixels are discarded or produce inconsistent shapes. A common approach is to scale the image first, then pad it with zero values up to the target multiple.
Assume the base patch edge length is 14 pixels and the spatial merge factor is 2×2. Then the region covered by one visual token has an edge length of 14 × 2 = 28 pixels. Once the image width and height are both aligned to multiples of 28, splitting and merging produce no incomplete blocks.
Alignment costs a small number of padding pixels. In return, it makes the implementation deterministic and lets the spatial position of each token be calculated precisely.
4. Patch Splitting and 2×2 Merging
The splitting stage divides the image into a grid of 14×14-pixel patches. Each patch goes through a linear projection to produce one base vector. Next, each adjacent 2×2 group of base vectors is concatenated into one merged vector. The merged vector has four times the dimension of a single base vector, and a projection layer maps it back to the unified hidden dimension.
This step performs spatial downsampling. It reduces the sequence length to one quarter of the original, which significantly lowers the attention computation and the input load on the language model. The cost is that local detail is compressed. Small text and dense charts may lose information after merging.
5. Visual Token Budget Control
Each 28×28-pixel region corresponds to one visual token, so the token count of an image can be calculated directly:
- Width token count = aligned width ÷ 28
- Height token count = aligned height ÷ 28
- Total token count = the product of the two
In actual deployments, systems usually set a minimum and a maximum token count. The minimum ensures that very small images still have enough representational capacity. The maximum prevents a single large image from taking up the entire context window. The deployer should set the specific upper limit based on context length, concurrency, and latency targets.
The following is an illustrative budget planning function. It shows only the calculation logic.
import mathPATCH = 14MERGE = 2UNIT = PATCH * MERGE # 28def align_size(length: int) -> int:"""Round up to the nearest multiple of UNIT"""return math.ceil(length / UNIT) * UNITdef count_tokens(w: int, h: int) -> int:return (align_size(w) // UNIT) * (align_size(h) // UNIT)def plan_image(width: int, height: int, max_tokens: int = 8000):"""Scale down proportionally when over budget, then align to multiples of 28"""w, h = width, heightwhile count_tokens(w, h) > max_tokens:w, h = int(w * 0.95), int(h * 0.95)return align_size(w), align_size(h), count_tokens(w, h)print(plan_image(1024, 768)) # (1036, 784, 1036)
When the token count exceeds the limit, shrink the image before alignment. Truncating the token sequence directly breaks the spatial structure, because the model cannot know where the removed regions were.
6. Estimating Parameters of the Vision Backbone
The vision backbone is usually a standard ViT structure made up of several Transformer blocks. Its main hyperparameters include the number of layers, the hidden dimension, the number of attention heads, and the output dimension.
The following is an illustrative configuration that does not correspond to any specific model:
| Parameter | Example value |
|---|---|
| Depth (layers) | 24 |
| Hidden dimension (hidden_size) | 1024 |
| Attention heads | 16 |
| Per-head dimension | 64 (1024 ÷ 16) |
| Patch size / merge factor | 14 / 2×2 |
| Projection output dimension | Aligned with the language model’s hidden dimension |
The per-head dimension equals the hidden dimension divided by the number of heads. In common multi-head attention implementations, the Q, K, and V projections together take about 4d² parameters, and a feed-forward network with a 4× expansion ratio takes about 8d² parameters. The parameter count of a single layer is therefore approximately 12d². With a hidden dimension of 1024, each layer has about 12.6 million parameters, and 24 layers total about 300 million parameters. A larger feed-forward expansion ratio or a gated structure increases the parameter count.
The vision backbone usually accounts for a very small share of the parameters in the whole multimodal model. This means its inference cost depends mainly on the number of input tokens, not on its own parameter size.
7. Precision and Deployment Considerations
The vision backbone is relatively small and fairly sensitive to numerical precision. Many engineering teams keep the vision part in BF16 or FP16 during inference instead of applying aggressive quantization to save memory. Quantization error propagates directly into visual features, which in turn affects text recognition and fine-grained judgment. The language model can use its own quantization scheme, chosen based on latency and memory budgets.
Video input requires an additional sampling strategy. A common approach is to extract frames at a fixed frame rate or a fixed frame count, run the pipeline above on each frame independently, and keep the total token budget under control so that long videos do not exhaust the context window.
8. Tuning Recommendations
- For text-dense images such as documents, screenshots, and tables, increase the resolution or raise the token limit. Otherwise, small text will be lost during the merging stage.
- For photographs, reducing the token count usually has limited impact on quality and can noticeably shorten the time to first token.
- When processing in batches, group requests by token count so that long and short requests do not slow each other down.
- Before going live, run regression tests on your own business images. Use recognition accuracy and latency as the main metrics, and do not rely only on general benchmarks.
9. Summary
The core of visual encoding is mapping a two-dimensional pixel grid into a finite-length sequence that a language model can consume. Size alignment keeps patch splitting deterministic. Patch splitting and 2×2 merging control the sequence length, and the token budget sets the boundary between accuracy and cost. Once developers understand this pipeline, they can set resolution and token limits based on their use case instead of relying on trial and error.
