Designing Visual Encoders for Lightweight Multimodal Models: Key Principles and Practices

    A practical guide to integrating vision encoders with language models in multimodal systems. This article explains why visual input should be natively accessible during inference, how a ViT-based encoder with patch splitting and spatial merging works, and how to handle dimension alignment and image preprocessing with zero padding.

    Why Visual Encoders Need Deep Integration with Language Models

    A common approach in multimodal large models is to first convert an image into a sequence of vectors using a visual encoder, then feed those vectors into the language model as ordinary tokens. The interface between the encoder and the language model directly determines whether the model can observe the image during inference and decide its next action accordingly.

    In scenarios such as code understanding, UI interaction, and chart analysis, the model often needs to observe the image multiple times. For example, the model might look at a screenshot, perform one operation, and then re-examine the result. If visual capability is merely a module attached once at the input stage, the model cannot decide when to look back at the image as the task progresses. Therefore, visual input should be natively integrated as part of the model’s capabilities, allowing the model to determine on its own when it needs visual feedback.

    This design principle leads to two engineering requirements:

    1. The output dimension of the visual encoder must align with the hidden dimension of the language model; otherwise, the visual vectors cannot be directly concatenated into the text sequence.
    2. The number of visual tokens must be controllable. Without limits, high-resolution images or long videos can quickly exhaust the context window.

    The rest of this article covers how to implement both points.

    Basic Structure of the Visual Encoder

    The encoder discussed here follows the standard Vision Transformer (ViT) architecture. The image is first split into fixed-size patches, each patch is linearly projected into a vector, and the resulting sequence passes through several layers of self-attention and feed-forward networks. Compared with a pure text model, the vision branch requires two additional steps: image splitting and spatial merging.

    Patch Splitting

    Assume the patch size is 14 pixels. A 1120×784 image is split into 80×56 = 4480 patches.

    Spatial Merge

    Adjacent 2×2 patches are concatenated into a single vector, which is then mapped to the language model’s hidden dimension through a linear layer. After merging, the sequence length is reduced to one quarter of the original. In the example above, the image yields 1120 visual tokens.

    The benefit of this design is twofold: the encoder still performs attention over fine-grained patches internally, preserving local details, while the sequence passed to the language model remains short, reducing attention overhead on the decoding side.

    Image Preprocessing: Size Alignment and Zero Padding

    The preprocessing stage must first solve the size alignment problem. Spatial merging requires every 2×2 patches to form one group, so both the width and height of the image must be integer multiples of “patch size × merge factor.”