Training a Vision Encoder from Scratch: Key Practices for Joint Multimodal Pretraining

    This article explores the practice of training vision encoders from scratch through joint multimodal pretraining. It compares this approach with pretrained initialization, discusses optimization alignment and gradient stability, details the core architecture including a bias-free Vision Transformer (ViT) and pixel shuffle downsampling, and outlines engineering essentials such as low-bit quantization and fixed-ratio data mixtures.

    I. Two Main Training Routes

    When building multimodal large models, the training method of the vision encoder limits the capability ceiling of the entire system. Currently, there are two main routes.

    The pretrained initialization route first uses large-scale image-text pairs for contrastive learning to obtain a vision encoder, which is then frozen or fine-tuned before being integrated into the language model. It has a high starting point and fast convergence, but the training is split into multiple stages, and the optimization objectives of each stage are not entirely consistent.

    The joint training from scratch route uses random initialization for the vision encoder, which is co-optimized with the language model in the same training loop. The only supervision signal comes from next-token prediction.

    This article uses a scheme described in a public material as an example: a 27-layer, approximately 401M parameter bias-free Vision Transformer, trained from scratch without loading any pretrained visual weights. The material reports that it performs comparably to the pretrained initialization baseline in general vision evaluations, with improvements in charts, mathematical vision, and video tasks. These figures are from the original material and have not been independently reproduced.

    II. When to Choose Joint Training from Scratch

    2.1 Optimization Objective Misalignment

    Pretrained vision encoders commonly use image-text contrastive loss, which brings matching images and texts closer together in the embedding space. The language model, however, requires visual information that supports subsequent token prediction. A common explanation is that contrastive learning leans more towards global semantics and easily discards local details such as text, table numbers, and coordinates. During joint training, the gradients of the vision encoder come directly from the language modeling loss, so the representations adjust in a direction useful for prediction.

    2.2 Gradient Spikes During Stage Switching

    In multi-stage schemes, when switching stages, the output distribution of newly integrated modules may differ significantly from the internal activations of the language model, and instantaneous large gradients may destroy existing weights. Joint training uses the same set of data mixture and optimizer states from start to finish, allowing the activation distribution to change gradually during training. This reduces the spikes caused by stage switching but does not completely eliminate instability; continuous monitoring is still required.

    III. Core Architecture

    3.1 Bias-Free ViT and Pixel Shuffle Downsampling

    This encoder uses a 27-layer bias-free Transformer. After removing the bias terms, the parameter distribution is more regular, making it more friendly to low-precision quantization. In modern architectures with normalization, the role of bias terms can typically be assumed by other parameters.

    The number of visual tokens directly determines the cost of multimodal inference. Taking a 16×16 patch as an example:

    • A 1024×1024 input yields about 4096 tokens;
    • A 3584×3584 input yields about 50176 tokens;
    • After 2×2 pixel shuffle, these are reduced to about 1024 and 12544 tokens, respectively.

    Pixel shuffle rearranges the features of spatially adjacent 2×2 patches into the channel dimension, and then uses a linear layer to project them to the target dimension. The number of tokens becomes one-fourth of the original, while information is retained in the channel dimension. Below is a schematic implementation. The dimensions of the linear layer are assumed values and should be adjusted according to the actual model configuration:

    1. import torch
    2. import torch.nn as nn
    3. class PixelShuffleProjector(nn.Module):
    4. def __init__(self, in_dim, out_dim, r=2):
    5. super().__init__()
    6. self.r = r
    7. self.proj = nn.Linear(in_dim * r * r, out_dim)
    8. def forward(self, x):
    9. # x: (B, H, W, C), NHWC layout
    10. B, H, W, C = x.shape
    11. r = self.r
    12. x = x.reshape(B, H // r, r, W // r, r, C)
    13. x = x.permute(0, 1, 3, 2, 4, 5).reshape(B, H // r, W // r, C * r * r)
    14. return self.proj(x) # (B, H//r, W//r, out_dim)

    When only reshape and permute are performed, the number of channels becomes 4 times the original without dimensionality reduction. Only after adding the linear projection can a unified channel dimension be obtained for use by subsequent layers.

    3.2 Shared Parameters for Image and Video

    After sampling video by frames, it is treated as an image sequence and fed into the same encoder. This eliminates the need to maintain two separate vision models, allowing representations learned from image data to be directly applied to video.

    Sharing parameters itself does not inherently bring temporal understanding. The material does not specify how temporal information is injected. In practice, there are two common approaches: adding time position embeddings to each frame, or inserting text-form timestamps between frames so that the language side can read the temporal relationships. Which method to adopt needs to be confirmed through video evaluation.

    The input resolution can reach up to 3584×3584. High-definition images can occupy a relatively long context window after encoding. The longer the context, the higher the memory and latency during training and inference. This cost must be factored in during planning.

    3.3 Low-Bit Quantization and QAT

    Deployment costs often prompt teams to consider quantization. The material mentions that weights and activations use fine-grained 4-bit and 8-bit floating-point formats, respectively, along with Quantization-Aware Training (QAT). QAT simulates quantization errors during forward computation, allowing parameters to adapt to low-precision representations in advance.

    QAT typically loses less precision than Post-Training Quantization (PTQ), but the magnitude of the loss depends on the task, bit-width, and quantization group granularity. The bias-free structure makes quantization calibration less susceptible to bias distribution interference; this is a reasonable inference but still requires empirical testing. It is recommended to compare at least three sets of results: a high-precision baseline, PTQ, and QAT, and record the precision changes on chart, document, and video tasks, respectively.

    IV. Engineering Essentials of Joint Pretraining

    4.1 Fixed-Ratio Data Mixture

    The joint pretraining scale in the material is about 15 trillion mixed image-text tokens, with text and vision data mixed at a fixed ratio throughout the process. This early fusion approach allows the model to optimize on a multimodal distribution from the very first step.