How Lightweight Multimodal Models Manage the Image Token Budget
As large language models grow more capable, text-only processing can no longer cover many business scenarios such as reading screenshots, parsing scanned documents, or generating code from UI images. Lightweight multimodal models provide basic visual understanding with a smaller parameter count, making them suitable for GPU-memory-constrained or low-cost deployments. This article explains how these models work across four aspects—visual input organization, encoder architecture, image preprocessing, and token budgeting—and provides estimation methods you can verify yourself.
As large language models grow more capable, text-only processing can no longer cover many business scenarios, such as reading screenshots, parsing scanned documents, or generating code from UI images. Multimodal models have therefore become a foundational capability for many applications. Lightweight multimodal models deliver basic visual understanding with a relatively small parameter count, making them well suited for scenarios with limited GPU memory or tight deployment budgets. This article explains how these models work across four aspects—visual input organization, encoder architecture, image preprocessing, and token budgeting—and provides estimation methods you can verify on your own.
1. How Vision and Language Are Integrated
Early approaches often treated the vision module as an add-on: an image was first encoded into feature vectors, which were then concatenated into the language model’s input. Vision-native integration means the vision encoder and the language model are co-designed in both architecture and training, so visual tokens and text tokens sit in the same sequence and participate together in attention computation.
One point needs clarification: whether a model “looks at” an image is usually not decided by the model itself. In real systems, whether an image enters the context is typically triggered by user input, task routing rules, or application configuration. What the model can do is adjust its subsequent answers or code generation based on the visual information it has already received. Making the model autonomously decide when visual information is needed requires additional training and evaluation support—refer to the documentation of the specific model for details.
2. Architecture of the Vision Encoder
The vision encoder is typically a Vision Transformer (ViT). It splits an image into fixed-size patches, projects each patch linearly into a vector, and then runs the vectors through multiple Transformer blocks for global interaction.
Structural parameters should be read from the model’s configuration file rather than from articles or marketing materials. Common fields include:
- depth: the number of Transformer layers
- hidden_size: the hidden dimension
- num_heads: the number of attention heads; each head’s dimension equals hidden_size divided by num_heads
- patch_size: the side length (in pixels) of a base patch
- spatial_merge_size: the side length of spatial merging; for example, 2 means 2×2 merging
- out_hidden_size: the output dimension, which must match the hidden dimension of the language model
The proportion of total parameters taken up by the vision encoder should be computed according to the counting methodology stated in the model card. Methodologies vary across sources—for example, whether the projection layer is included or whether BF16 is assumed—so ratios from different documents should not be compared directly. If out_hidden_size does not match the language model’s hidden dimension, the projection layer handles the dimension alignment.
Inference precision (such as BF16 or FP8) affects both memory usage and numerical accuracy. Whether the vision module is quantized depends on the configuration of the inference framework you use, and accuracy should be compared on your target tasks.
3. Image Preprocessing and Size Alignment
Preprocessing first resizes the image to an appropriate size and then applies zero padding so that both height and width are multiples of 28. The number 28 comes from multiplying two values: the base patch side length of 14 pixels, times the side length 2 of the 2×2 merge. As a result, each 28×28 pixel region corresponds to exactly one visual token, with no partial patches left at the boundaries during splitting and merging.
The token count can be estimated with the following formula:
Visual tokens = (height ÷ 28) × (width ÷ 28)
For example, a 560×392 pixel image has a width and height equal to 20 and 14 multiples of 28 respectively, producing 20 × 14 = 280 visual tokens. Resizing and padding rules differ slightly across implementations, so the estimate may differ from the actual value by a small number of tokens—use the token count reported by the framework as the ground truth.
Image resolution and quantity are hard to predict in production. To prevent visual tokens from filling up the context window, inference frameworks usually provide parameters such as min_image_tokens and max_image_tokens to cap the number of tokens per image. Keep three things in mind when using them: the unit is token count, not pixels; the parameter names and default values depend on the specific model and framework version; and when the limit is exceeded, the common behavior is to downscale the image—check the actual implementation for exact behavior. Also, setting the minimum too high causes small images to be upscaled, wasting tokens unnecessarily.
4. Forward Pass Execution Steps
The code below is illustrative pseudocode that explains the data flow; it cannot be run directly.
def forward(self, pixel_patches, grid_thw, cu_seqlens):# pixel_patches: flattened patches from all images, shape [N, C*P*P]# grid_thw: patch grid dimensions (time, height, width) for each image# cu_seqlens: cumulative patch counts, used to separate images in the same batch# merge_size: side length of spatial merging, typically 2x = self.patch_embed(pixel_patches) # [N, hidden_size]pos = self.rotary_pos_emb(grid_thw) # compute rotary position embeddings from grid indicesfor blk in self.blocks: # multiple Transformer blocksx = blk(x, pos, cu_seqlens) # attention is computed only within each imagex = self.post_layernorm(x) # [N, hidden_size]x = self.reorder_2x2(x, grid_thw) # move patches of each 2x2 neighborhood to adjacent positionsx = x.view(-1, merge_size * merge_size * hidden_size) # merge every 4 patches into 1 tokenreturn self.merger(x) # [num_tokens, out_hidden_size]
The role of each step is as follows. The first step maps pixel patches to vectors, with position information determined by grid indices. Rotary position embeddings preserve the relative positions of the image in the horizontal and vertical directions. cu_seqlens allows images of different sizes in the same batch to be concatenated and computed in parallel, while keeping their attention computations isolated from one another.
The merging step is the most error-prone. The view operation groups four adjacent vectors together according to memory order, so a reordering step must first place the patches of each 2×2 neighborhood in adjacent positions. If this step is skipped, view will incorrectly merge non-adjacent patches into a single token. Finally, the projection layer maps the merged features to the hidden dimension of the language model.
5. Common Issues and Checklist
| Issue | Symptom | How to Handle |
|
