Lightweight Multimodal Vision Encoders: How to Calculate Image Tokens
As multimodal large language models evolve towards lightweight architectures, vision encoders play a crucial role in converting pixels into tokens readable by language models. This article explores the underlying architecture, image preprocessing pipeline, and token budget mechanism of a lightweight multimodal vision encoder. It also covers practical deployment considerations, including inference precision and performance optimization, to help developers efficiently integrate vision capabilities into their applications.
1. The Meaning of Native Vision Integration
Traditional multimodal architectures often treat the vision module as an independent preprocessor. An image is first converted into feature vectors and then concatenated into the input of the language model. Native vision integration, however, requires the vision encoder and the language model to be deeply aligned in both feature dimensions and semantic space, enabling their representations to work together directly.
Concepts such as “when to observe the image” or “how to adjust actions based on visual feedback” belong to the upper-layer multimodal strategy or AI Agent logic. These determine whether to invoke visual inputs and how to use the results. The vision encoder itself is solely responsible for converting the image into a token sequence. In tasks like UI automation, complex document parsing, and long-context scenarios with interleaved text and images, clarifying this division of labor helps troubleshoot issues effectively.
2. Underlying Architecture of the Vision Encoder
The vision backbone of this model is a Vision Transformer (ViT). The main parameters are as follows:
- Network layers: 24
- Hidden dimension: 1024
- Attention configuration: 16 heads, 64 dimensions per head (1024 ÷ 16)
- Patch size: 14×14
- Merge method: 2×2 Merge
- Backbone parameters: approximately 0.56B
- Output dimension: 4096, consistent with the hidden dimension of the language model
The output dimension requires special explanation. The internal hidden dimension of the ViT is 1024. To achieve a 4096-dimensional output, it must pass through a linear projection layer (often called a projector or merger) to complete the dimension mapping. This projection aligns the visual tokens with the embedding space of the language model, making it an indispensable step. The 0.56B figure mentioned refers to the backbone parameters; the parameters of the projection layer depend on the specific model implementation.
The proportion of the 0.56B backbone within the entire multimodal model depends on the total parameter scale. If the total model is in the 3B range, this accounts for about 18%; if the total model scale is larger, the proportion decreases accordingly. Since the source material does not specify the total model version, one cannot simply assert that the proportion is less than 1%. Please refer to the parameter table of your specific version for evaluation.
A smaller backbone helps reduce the computational overhead of vision encoding, which in turn affects the Time To First Token (TTFT). However, TTFT is also influenced by the number of image tokens, the size of the language model, and the inference framework, requiring empirical testing to confirm.
3. Image Preprocessing and Token Generation
1. Resize and Zero Padding
The image is first scaled proportionally and then zero-padded to a multiple of 28. The number 28 is determined by two factors: the 14×14 patch size and the 2×2 merge, multiplied together (14×2=28). Once padded, the image can be evenly divided by 28 in both dimensions, preventing edge pixel loss or duplication during the merge process.
2. Patch Splitting and Merging
The image is first split into 14×14 pixel patches. Subsequently, a 2×2 Merge module combines four adjacent patches into a single visual token, with each token corresponding to a 28×28 pixel area in the original image. This reduces the token count to one-quarter of the patch count, thereby alleviating the context window pressure on the language model. This is referred to as a merge module; its specific implementation should be based on the source code and should not be directly understood as standard pooling.
Below is a schematic code snippet illustrating the processing sequence:
class ImageProcessor:def preprocess(self, image):# 1. Scale and zero-pad to a multiple of 28target_size = self.calculate_target_size(image.size, base=28)image = self.resize_and_zero_pad(image, target_size)# 2. Split into 14x14 patchespatches = self.patchify(image, patch_size=14)# 3. 2x2 Merge, combining into visual tokensmerged_tokens = self.merge_patches(patches, merge_factor=2)return merged_tokens
3. How to Calculate Token Count
The token count can be estimated using the following formula:
Token Count ≈ (H ÷ 28) × (W ÷ 28)
Where H and W are the height and width after scaling and padding. For example, a 1120×784 pixel image remains the same size after padding, yielding a token count of 40 × 28 = 1120. For an 896×672 pixel image, the token count is 32 × 24 = 768. The actual count may increase slightly due to special tokens like start/end markers; always refer to the inference framework’s output for accuracy.
4. Dynamic Token Budget Control
The system controls the token count per image using two parameters: min_image_tokens and max_image_tokens. Their functions are as follows:
- Increase
max_image_tokens: Retains more texture and detail, suitable for scenarios like OCR, small text recognition, and table parsing. The trade-off is increased context consumption and inference time. - Decrease
max_image_tokens: Compresses the overhead per image, suitable for scenarios requiring only overall semantics, such as image classification or rough descriptions. This allows for more interleaved text and image history within the same context length. - Increase
min_image_tokens: Ensures that small images also receive sufficient tokens, preventing detail loss during the preprocessing stage.
The default values for these parameters depend on the model version and the inference framework configuration. As the source material lacks verifiable default values, specific numbers are not provided here. Before use, please consult the official documentation or configuration files of your specific version to confirm the actual defaults.
When tuning parameters, it is recommended to first test three metrics using representative samples: task accuracy, token count per image, and TTFT. Once a baseline is established, gradually adjust the upper limit. Setting the limit too high will drive up the memory peak, while setting it too low may lose crucial details. Both require a trade-off based on business needs.
5. Inference Precision and Deployment Considerations
This deployment configuration strictly uses BF16 (Bfloat16) precision without FP8 quantization. This choice is made for two main reasons. First, the ViT backbone has only about 0.56B parameters; the memory savings from quantization are limited, but it can introduce precision loss, which is particularly detrimental for vision tasks sensitive to colors and edges. Second, sticking to BF16 reduces adaptation work across different hardware and frameworks, simplifying the deployment process.
Whether this applies to other hardware depends on the actual accelerator cards, drivers, and framework versions in use.
During deployment, container platforms can handle auto-scaling, object storage can uniformly manage test images and videos, and monitoring systems can track GPU utilization and token generation speed. Before going live, it is recommended to test the following three aspects:
- Cold start time: Model loading and initial inference can be time-consuming, affecting the response speed of elastic scaling.
- Memory peak: High-resolution images or multi-image inputs cause significant fluctuations in memory usage.
- Throughput: Processing capacity per second varies noticeably under different token budgets and should be measured according to business load.
6. Conclusion
This lightweight vision encoder utilizes a 24-layer ViT backbone with a hidden dimension of 1024. Through 14×14 patch splitting and 2×2 merging, it outputs 4096-dimensional visual tokens aligned with the language model. The token count for an image is determined by its scaled dimensions and can be dynamically controlled using the min_image_tokens and max_image_tokens parameters. Understanding this pipeline and the underlying budget control is essential for optimizing multimodal model deployment.
