Training a Vision Encoder From Scratch: Technical Breakdown
This article explains how to train a vision encoder from scratch and jointly pretrain it with text for multimodal large language models. It covers the limitations of the conventional pretrain-then-connect pipeline, a 27-layer bias-free vision Transformer with about 401M parameters, 2x2 pixel shuffle downsampling, fully shared image and video parameters, quantization-aware training, and a fixed-ratio interleaved image-text training strategy on roughly 15 trillion tokens. Readers will also find practical recommendations for building reproducible training and inference pipelines.
1. Why Train a Vision Encoder From Scratch?
In multimodal large language models, the vision encoder converts images or video into token sequences that the language model can process. The conventional approach first learns pretrained vision weights through large-scale image-text contrastive learning, then connects them to a language model for multi-stage training. This route is mature, but it has three clear problems.
First, the optimization objectives are misaligned. Contrastive learning optimizes image-text similarity, while the language model optimizes next-token prediction. Because the objectives differ, the vision representations may not directly fit the needs of language modeling.
Second, multi-stage training introduces instability. Switching weights, replacing projection layers, and restarting learning rates can easily cause gradient spikes that hurt overall convergence.
Third, images and video often use different parameters or different preprocessing pipelines, which adds engineering complexity.
A design worth considering is therefore to train the vision encoder entirely from scratch and jointly pretrain it with text, so that the vision representations directly serve the language modeling objective.
2. Overall Architecture: A 27-Layer Bias-Free Vision Transformer
The vision encoder uses a 27-layer Transformer with roughly 401M parameters. One of its core features is that it is bias-free. In a standard Transformer, linear and normalization layers usually include bias terms. Removing them slightly reduces the parameter count and, more importantly, eliminates unnecessary degrees of freedom, making training smoother and improving numerical stability in large-scale joint training.
The overall data flow can be summarized as follows:
Image / video frames-> Split into patches-> Linear projection into vision tokens-> 2x2 pixel shuffle downsampling-> 27-layer bias-free Transformer encoding-> Output vision representations for the language model
It is worth emphasizing that this encoder does not rely on any external pretrained vision weights. All parameters are learned from random initialization during joint pretraining.
3. Key Design 1: 2x2 Pixel Shuffle Downsampling
High-resolution images produce a large number of vision tokens when split into patches in the conventional way, which significantly increases the context pressure on the language model. To address this, the design introduces a 2x2 pixel shuffle operation.
The core idea is to rearrange each spatially adjacent 2x2 region into the channel dimension before encoding, reducing the token count to one quarter while preserving information. Compared with simple pooling, pixel shuffle does not discard high-frequency details, making it friendlier to dense information such as charts, formulas, and small text.
Illustrative pseudocode:
def pixel_shuffle_2x2(x):# x: [B, C, H, W]B, C, H, W = x.shapex = x.view(B, C, H // 2, 2, W // 2, 2)x = x.permute(0, 1, 3, 5, 2, 4) # Move the 2x2 neighborhood to the channel dimensionx = x.reshape(B, C * 4, H // 2, W // 2)return x
With fewer tokens, the same context budget can accommodate higher-resolution inputs and leaves room for long contexts.
4. Key Design 2: Fully Shared Image and Video Parameters
Images and video share the same encoding parameters, avoiding the need to maintain separate weights for different modalities. Video is treated as a sequence of image frames, and inter-frame relationships are modeled by the language model over a long context. This unified parameter design brings three benefits: training data can be sampled from a mixed pool to improve sample efficiency; inference deployment only needs to maintain one set of weights; and extending to new modalities does not require redesigning the encoding branch.
5. Key Design 3: Quantization-Aware Training
To reduce deployment cost, the encoder incorporates quantization-aware training during the training stage, using low-bit weights and relatively higher-bit activations. The key is to bring quantization error into the training loop so that the model learns to preserve representation quality under limited numerical precision. Compared with post-training quantization, quantization-aware training usually controls accuracy loss better, which is especially suitable for scenarios with a large number of vision tokens and clearly fluctuating activation distributions.
6. Training Strategy: Fixed-Ratio Image-Text Mixing and Stability
Joint pretraining runs on about 15 trillion mixed image-text tokens, with text and vision data always mixed at a fixed ratio. The fixed ratio avoids loss oscillation caused by sudden changes in data proportions and thereby avoids the gradient spikes common in multi-stage training. In practice, training stability can be further ensured with the following measures:
- Gradient clipping and a reasonable warmup strategy;
- Monitoring and alerting on attention logits and normalization statistics;
- Using logging services and monitoring dashboards to track loss curves and anomalies;
- Storing checkpoints, data, and configurations together in object storage for reproducibility.
In the end, this from-scratch training scheme performs on par with the pretrained-initialization baseline in vision evaluations and performs better on chart understanding, mathematical visual tasks, and video tasks, showing that the benefit of objective alignment is real.
7. Engineering Practice Recommendations
To borrow this approach in your own system, consider the following checklist:
- Unify the encoding path for images and video first to reduce branch maintenance cost;
- Use pixel shuffle rather than aggressive pooling to control token count;
- Introduce quantization awareness early in training to avoid late-stage accuracy collapse;
- Keep the image-text data ratio fixed, and use monitoring alerts to locate unstable intervals;
- Build reproducible training and inference pipelines with container platforms and message queues.
8. Conclusion
A vision encoder trained from scratch with joint pretraining essentially aligns the optimization objective of vision representations with the language modeling objective. The bias-free structure, pixel shuffle, parameter sharing, and quantization-aware training together form a simple and effective engineering paradigm, providing a reusable practical reference for high-resolution, long-context multimodal applications.
