Image Token Budget and BF16 Inference in Lightweight Multimodal Models
This article explains how visual encoders in lightweight multimodal models determine image token usage and impact inference memory. It covers calculating image token counts, configuring token budgets to manage context limits, and the technical reasons for selecting BF16 precision during inference.
Conclusion First
The visual encoder dictates how much context an image consumes and affects peak VRAM usage during inference. This article covers three main aspects: how to calculate image token counts, how to set a token budget, and why BF16 is typically chosen for inference. The parameters and formulas mentioned are based on common implementations of such models; always verify specific values against your model’s configuration files and inference code.
1. Visual Encoder Architecture
The visual component of these models generally consists of three parts: the Patch embedding layer, the Transformer backbone, and the fusion projection layer (often called the Merger). Before deployment, you can verify key configurations against the table below.
| Item | Example Config | Verification Method |
|---|---|---|
| Patch Size | 14x14 pixels | patch_size field in vision config |
| Spatial Merge Size | 2x2 | spatial_merge_size field |
| Transformer Layers | 24 layers | depth or num_layers field |
| Hidden Dimension | 1024 | hidden_size field |
| Attention Heads | 16 heads, 64 dims each | num_heads field; verify if GQA is used |
| LM Hidden Dimension | 4096 | hidden_size field in text model |
| Vision Parameter Count | ~0.56B | Calculate with a parameter counting script, then divide by total parameters for ratio |
You must calculate the parameter ratio for the visual part manually. If the total parameter count is approximately 7B, 0.56B represents about 8%, which is significantly higher than 1%. This figure is a theoretical estimate; verify it using the actual total parameter count.
2. Preprocessing: From Size Alignment to Token Count
Preprocessing consists of three steps:
- Size alignment: Scale the image proportionally based on the processor configuration so that both width and height are multiples of 28. The number 28 comes from the patch size (14) multiplied by the merge factor (2). If width or height is not a multiple of 28, the 2x2 merge will result in incomplete blocks. Check the preprocessing code for rounding methods and padding behaviors, as padding introduces black borders that may affect OCR recognition of small text.
- Patch splitting: Divide the image into 14x14 pixel blocks.
- 2x2 merging: Merge four adjacent patches into a single token.
The token count for a single image can be estimated using the following formula:
Tokens ≈ (Width / 28) * (Height / 28)
Example: For a 1344x896 image, the width yields 1344/28 = 48 and the height yields 896/28 = 32, resulting in 48 32 = 1536 tokens. If the same image is scaled to 224x224, it yields 8 8 = 64 tokens.
3. Token Budget: Setting Upper and Lower Limits
Processor configurations usually contain fields that control the pixel range, such as min_pixels and max_pixels, though exact names vary by model. These are measured in pixels. Converting them to token counts makes it easier to estimate context overhead. Since each token corresponds to 28x28 = 784 pixels:
Tokens ≈ Total Pixels / 784
Conversely, the pixel limit corresponding to the token limit is:
max_pixels ≈ max_tokens * 784
For example, if max_tokens is 8000, max_pixels is roughly 8000 * 784 = 6,272,000, equivalent to a ~2504x2504 square. These are conversion examples and do not represent a specific model’s defaults.
If the inference code processes images using a “scale down if over limit, scale up if under limit” logic, the flow is typically as follows:
- Calculate the token count at the original resolution first.
- If it exceeds the upper limit, scale the image down proportionally until the token count is within the limit.
- If it falls below the lower limit, scale the image up proportionally until the token count meets the minimum.
Whether scaling occurs and to what extent should be verified in the code.
Estimating context overhead is straightforward. If the upper limit for a single image is 8000 tokens, four full-capacity images total 32,000 tokens. In a 32K context window (32,768 tokens), these four images consume about 97.7%, leaving only 768 tokens for text and the model’s response, nearly maxing out the context. In such cases, you must either increase the context window or lower the token limit.
Consider the following approaches for practical configuration:
- Web screenshots and general QA: A medium budget is sufficient; determine exact values through empirical testing.
- OCR and long documents: The budget must be high enough; otherwise, small text will be lost during downscaling.
- Batch processing with tight context: Lower the upper limit and log the token count for each image to ensure the distribution meets expectations.
4. Inference Precision: Why BF16 is Commonly Chosen
There are three main reasons for choosing BF16:
- Visual features are fed directly into the language model. Errors introduced by low precision propagate through every step of autoregressive generation.
- OCR and chart understanding are highly sensitive to detail, and low precision is more likely to cause recognition errors.
- BF16 shares the same 8-bit exponent as FP32, offering a similar dynamic range, typically without requiring additional scaling factors. While its 7-bit mantissa provides lower precision than FP16, it uses 16 bits per value, halving VRAM usage compared to FP32.
This conclusion has specific conditions of applicability. It relies on GPUs that support BF16 operations and depends on the specific task. When using other models or hardware, this needs to be re-evaluated.
It is recommended to compare BF16 against FP8 on your own evaluation sets and fill in the table below:
| Metric | BF16 | FP8 | Test Conditions |
|---|---|---|---|
| OCR, Chart, QA Accuracy | To test | To test | Same evaluation set |
| Single Request Latency | To test | To test | Same batch size and sequence length |
| Peak VRAM | To test | To test | Same tool and framework version |
During testing, simultaneously record the GPU model, batch size, sequence length, and token budget; otherwise, the results cannot be reproduced.
If VRAM is insufficient, prioritize reducing max_image_tokens or decreasing concurrent requests before considering alternative precision formats.
5. Merger Layer Activation Functions
The Merger’s architecture must be verified in the source code. In some common implementations, the MLP within the Transformer Block uses SwiGLU gating, while the Merger uses simpler structures, such as a Linear layer combined with GELU.
SwiGLU takes the form SwiGLU(x) = Swish(xW1) ⊙ (xW2), meaning one path passes through a Swish activation function and is then multiplied element-wise with the other path. Gating allows the network to learn which features can pass through, at the cost of increased parameters and computation. To determine which activation function a specific layer uses, you should directly check the source code.
