Lightweight Multimodal Vision Encoders: Architecture, Preprocessing, and Forward Pass
This article examines a typical lightweight multimodal vision encoder, covering its parameter configuration, image preprocessing rules, token-budget control, and forward pass. It explains how to estimate visual token counts, adjust token budgets for different tasks, and assess how precision and parameter choices affect inference cost. The values cited come from public descriptions of a reference model and should be verified against the configuration files and official documentation for the target model version.
Whether lightweight multimodal models can process images and video under limited compute depends heavily on the design of the vision encoder. Using a typical lightweight multimodal model as a reference, this article explains the vision encoder’s parameter configuration, image preprocessing rules, token-budget control, and forward pass. After reading it, you should be able to calculate the number of tokens an image consumes and judge how parameter changes affect inference cost.
The specific values below come from public descriptions of a reference model. Different model versions may vary. Before deployment, rely on the configuration files and official documentation for the corresponding version.
1. Architecture configuration and parameter scope
The vision encoder converts images or video at arbitrary resolutions into token sequences that a language model can read. Lightweight designs typically use a standard Vision Transformer (ViT) structure without complex optimizations such as grouped-query attention. A simple structure is easier to deploy and debug.
The reference configuration is as follows:
- Network depth and dimension: 24 Transformer blocks with a hidden size of 1024.
- Attention mechanism: 16 attention heads, each with a dimension of 64 (1024 divided by 16). This configuration uses standard multi-head attention (MHA).
- Output dimension: The encoder output dimension is 4096. If the connected language model also has a hidden size of 4096, visual features can enter the corresponding feature space directly without an additional dimension transformation. This alignment holds only for this specific version; when switching to another language model, verify it again.
- Parameter scale: The visual backbone has about 0.56B parameters. Its share depends on the overall model size. For a 10B-parameter model, the vision part is about 5.6%; for a 100B-parameter model, it is about 0.56%. Therefore, when citing conclusions such as “less than 1%,” include the total parameter count.
Regarding precision, the reference material states that the vision module uses BF16 computation during inference and does not use FP8 quantization. This approach preserves numerical precision, but memory usage and compute time are usually higher than with lower-precision schemes. If you plan to quantize the vision module, first confirm that the hardware supports the target precision, then compare visual feature error and downstream task metrics on a validation set. Quantization may affect the stability of visual features and should not be assumed to be lossless.
2. Image preprocessing and token-budget control
Align dimensions to multiples of 28
The base patch size is 14×14 pixels. After feature extraction, the model performs a 2×2 merge, so the merged perceptual unit is 14×2=28 pixels. Therefore, both the width and height of an image must be zero-padded to multiples of 28. This allows every region to be fully split and merged without losing edge pixels.
Patch splitting and 2×2 merging
The image is first split into 14×14 base patches, and then spatially adjacent 2×2 patches are merged into one visual token. After merging, each 28×28 pixel region corresponds to one token. This operation reduces the number of tokens while letting each token cover a larger receptive field, which helps capture higher-level semantic information.
Estimating token count
The number of visual tokens can be estimated with the following formula:
Visual tokens = (aligned height ÷ 28) × (aligned width ÷ 28)
Examples:
- 448×448 pixels: 16×16 = 256 tokens.
- 896×896 pixels: 32×32 = 1024 tokens.
- 1344×1344 pixels: 48×48 = 2304 tokens.
Token-budget parameters
Developers can use min_image_tokens and max_image_tokens to control the number of tokens generated per image or video frame. The reference material gives a common default range of 16 to 8000; check the configuration of the model you use for the actual default values. When image resolution is very high, the part exceeding the upper limit is constrained, preventing visual tokens from filling the context window. A higher upper limit preserves more detail, but inference latency and memory usage also increase.
Common questions
How should I tune the token budget?
First determine how much detail the task requires. Tasks such as text recognition and table parsing need more tokens; image classification or coarse-grained description can use a lower budget. Start with a medium budget on a representative sample set, record accuracy and latency, and then adjust up or down gradually.
What should I do when high-resolution images cause token inflation?
Prefer resizing the image at the input stage instead of relying entirely on model truncation. For example, limit the long side to a reasonable range before passing the image to the encoder. For video, reduce the frame-sampling rate and the number of frames per segment.
What should I do when the context is insufficient for mixed multi-image input?
Set an independent budget limit for each image and count the total number of tokens. When the total approaches the context window, reduce the number of images or lower the per-image budget.
3. Forward pass
The following describes the main steps of the vision encoder in execution order. The specific implementation depends on the model version; the flow below is generic, and tensor shapes are illustrative only.
- Position and sequence calculation: Compute 3D position IDs from the input grid information and obtain the visual attention sequence length. An image can be treated as a single frame, while a video contains multiple frames, providing the basis for multi-frame input.
- Patch Embedding and positional encoding: Use a 3D convolution (Conv3d) for Patch Embedding so that the same structure can process both images and video. For images, the temporal dimension length is 1. Rotary position encoding (RoPE) is then applied to inject spatial and temporal position information into the tokens.
- Transformer Block loop: The data passes through 24 blocks in sequence. Each layer contains self-attention and feed-forward computation, and the attention computation uses the sequence length obtained in the first step.
- Normalization and spatial rearrangement: After the final layer output is normalized, the tensor is reshaped to (-1, 2, 2, C), where C is the number of channels. The 2×2 here corresponds to the merge operation described earlier, placing each group of four adjacent patch features in the same dimension.
- 2D convolution downsampling and SwiGLU projection: A Conv2d operation performs downsampling, followed by SwiGLU (Swish-Gated Linear Unit) feature projection. SwiGLU is a gated activation function that regulates information flow through a gating branch and enhances nonlinear expression. The final features are mapped to the language model’s hidden dimension.
Illustrative flow code:
# Input x: [N, C_in, T, H, W]; T=1 for imagestokens = conv3d_patch_embed(x) # 14x14 patch splittingtokens = add_rotary_pos(tokens, grid) # inject spatial and temporal positionsfor block in blocks: # 24 blockstokens = block(tokens, seqlen)tokens = norm(tokens)tokens = tokens.reshape(-1, 2, 2, C) # merge each 2x2 patch grouptokens = conv2d_downsample(tokens)tokens = swiglu_proj(tokens) # map to LLM hidden dimension
4. Precision and performance evaluation
The lightweight structure is suitable for both edge devices and cloud services, and the token budget lets the same model adjust load according to resource conditions. However, actual benefits need to be measured rather than inferred from summary claims alone.
- Latency: On the target hardware, target resolution, and target batch size, measure the proportion of total inference time taken by vision encoding, and record P50 and P95 latency.
- Accuracy: Compare task metrics under different token budgets, such as OCR accuracy or question-answering score, to find the most cost-effective budget.
- Quantization: BF16 preserves relatively high precision. Lower-precision schemes such as FP8 can reduce memory usage, but first confirm hardware support and run regression tests.
- Memory: When estimating memory, account for both the vision encoder weights and intermediate activations during the forward pass. With larger batches, the share of intermediate activations rises noticeably.
5. Tuning checklist
- Confirm the model version and check whether the parameter configuration matches this article.
- Check whether image dimensions are aligned to multiples of 28.
- Estimate the number of tokens per image using the formula.
- Confirm that
min_image_tokensandmax_image_tokensmeet task requirements. - In multi-image scenarios, check whether the total token count is below the context-window limit.
- Validate the impact of the precision used by the vision module (BF16 or a quantization scheme) on task metrics.
- Measure latency and memory usage on the target hardware.
