Multimodal LLMs (Vision-Language Models)
How a language model learns to see: a vision encoder, a connector (linear/MLP projection, Q-Former, Perceiver Resampler + gated cross-attention) and an LLM, trained in stages. Flamingo, BLIP-2, LLaVA, GPT-4V-style systems, their knobs and failure modes.
A vision-language model (VLM) takes images (and often video frames) plus text and answers in text: describing photos, reading charts and receipts, answering questions about screenshots, or acting as the 'eyes' of an Agent (LLM agent). Training one from scratch would be enormously expensive, so almost every VLM reuses two pretrained parts, a vision encoder such as CLIP's ViT and an Large language model (LLM), and learns a small bridge between them. This lesson is about that bridge.
The common blueprint
- Vision encoder (usually a frozen ViT from CLIP/SigLIP/EVA-CLIP) turns the image into a grid of patch features, e.g. 24×24 = 576 vectors for a 336 px image with 14 px patches.
- Connector maps those features into the LLM's embedding space, either keeping one token per patch (projection) or compressing them into a fixed number of tokens (learned queries).
- LLM reads the visual tokens together with the text tokens and generates the answer with ordinary Next-token prediction.
- H_v
- visual tokens in the LLM's embedding space
- W
- the projection matrix (1024 × 4096 for a 7B LLM)
- Z_v
- patch features from the vision encoder (576 × 1024 for CLIP ViT-L/14 at 336 px)
- MLP(Z_v)
- LLaVA-1.5's 2-layer MLP connector instead of a single matrix
- ;
- concatenation along the sequence
- embed(text tokens)
- the LLM's ordinary word embeddings of the prompt
- Π
- product over all answer tokens
- yᵢ
- the i-th answer token
- y_<i
- all answer tokens before position i
▸Worked example
Three landmark designs
Flamingo (DeepMind, 2022): gated cross-attention
Flamingo[1] keeps a vision encoder and a 70B Chinchilla LM frozen. A Perceiver Resampler compresses any number of image or video-frame features into 64 visual tokens. New gated cross-attention layers are inserted between the frozen LM layers; each has a tanh gate initialized at 0, so at the start the model behaves exactly like the original LM and gradually learns to 'look'. Trained on web pages with interleaved images and text, Flamingo is a strong few-shot learner: show it a few (image, answer) examples in the prompt and it adapts, like In-context learning in text LLMs.
BLIP-2 (Salesforce, 2023): the Q-Former
BLIP-2[2] keeps both the image encoder and the LLM frozen and trains only a lightweight Q-Former (~188M parameters): a small transformer with 32 learned query vectors that cross-attend to the image features and extract the most text-relevant information. Stage 1 trains the Q-Former against the frozen image encoder with image-text contrastive, matching and generation losses; stage 2 connects its 32 outputs to the frozen LLM through a linear layer. With 54× fewer trainable parameters than Flamingo-80B, it outperformed it by 8.7% on zero-shot VQAv2.
LLaVA (2023): simple projection + visual instruction tuning
LLaVA[3] showed that a plain linear projection from CLIP ViT-L/14 features into a Vicuna LLM works surprisingly well, if the training data is right. Stage 1 trains only the projection on ~595K image-caption pairs (feature alignment). Stage 2 fine-tunes the projection and the LLM on 158K multimodal instruction-following examples that were generated by prompting text-only GPT-4 with captions and bounding boxes (conversations, detailed descriptions, complex reasoning), i.e. Instruction tuning for vision. LLaVA-1.5[4] swapped in a 2-layer MLP connector, 336 px images (576 visual tokens) and academic VQA data, reaching state-of-the-art results with a training run of about a day on a single 8-GPU node.
| Model | Connector | Visual tokens per image | What is trained | Signature strength |
|---|---|---|---|---|
| Flamingo | Perceiver Resampler + gated cross-attention in the LM | 64 | Resampler + new cross-attn layers (LM and vision frozen) | Few-shot, interleaved images/video |
| BLIP-2 | Q-Former (32 learned queries) | 32 | Q-Former + projection only | Very parameter-efficient |
| LLaVA / LLaVA-1.5 | Linear / 2-layer MLP projection | 256 / 576 | Projection, then projection + LLM | Simple, strong chat ability, easy to reproduce |
GPT-4V-style systems. Commercial models (GPT-4 with vision, Gemini, Claude) accept images natively, but their architectures and training data are not published, so treat claims about their internals with caution. Publicly known trends: much higher and dynamic image resolution (splitting large images into tiles, as in LLaVA-NeXT's 'AnyRes'), large-scale interleaved and OCR-heavy data, video as sequences of frames, and some 'natively multimodal' models trained on mixed modalities from the start rather than glued together afterwards.
- What it is
- How many visual tokens each image becomes (set by input resolution, patch size, tiling and any compression in the connector).
- ↑ If you increase it
- Better OCR, small objects, charts and documents; but more tokens fill the Context window, slow down prefill and raise cost (LLaVA-1.5: 576 tokens per image; tiled high-res modes can use thousands).
- ↓ If you decrease it
- Faster and cheaper, more images fit in the context; fine text becomes unreadable and the model may hallucinate details it cannot see.
- Typical values
- 224–336 px (256–576 tokens) for single-image VLMs; dynamic tiling up to ~1–4 MP for document-focused models.
- What it is
- In Q-Former / Perceiver-Resampler connectors: the fixed number of output tokens that summarize the image.
- ↑ If you increase it
- More visual detail reaches the LLM; higher cost per image. Gains flatten once the queries can represent what the task needs.
- ↓ If you decrease it
- A tighter bottleneck: cheap, good for many images or long videos, but fine-grained information (text, counts, positions) is lost.
- Typical values
- 32 (BLIP-2), 64 (Flamingo).
- What it is
- The usual LLM decoding settings, applied to the VLM's text output.
- ↑ If you increase it
- More varied, creative captions, but also more invented objects and details (visual hallucination).
- ↓ If you decrease it
- Greedy or low temperature gives more factual, repeatable descriptions; preferred for OCR, extraction and evaluation.
- Typical values
- do_sample=False (greedy) or temperature 0–0.2 for factual tasks; 0.7 for creative captions.
import torch
from PIL import Image
from transformers import AutoProcessor, LlavaForConditionalGeneration
model_id = "llava-hf/llava-1.5-7b-hf"
model = LlavaForConditionalGeneration.from_pretrained(
model_id, torch_dtype=torch.float16, device_map="auto"
)
processor = AutoProcessor.from_pretrained(model_id)
conversation = [
{"role": "user", "content": [
{"type": "image"},
{"type": "text", "text": "What is written on the sign, and what is unusual here?"},
]},
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
image = Image.open("street.jpg")
inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device, torch.float16)
out = model.generate(**inputs, max_new_tokens=120, do_sample=False) # greedy: factual
print(processor.decode(out[0], skip_special_tokens=True))▸Go deeper: why freeze, and what the stages buy you
- Freezing the LLM protects its language ability and knowledge (no catastrophic forgetting) and makes training cheap; unfreezing it in a later stage (LLaVA) improves instruction following at higher cost.
- Alignment stage: with only the connector trainable, caption data teaches it to map visual features to things the LLM can name.
- Instruction stage: diverse question–answer data teaches the model to follow requests about images instead of just captioning them.
- Alignment with preferences (RLHF/DPO on visual data) is increasingly used to reduce hallucination.
CLIP[5] is the reason this works at all: its image features are already aligned with language, so a small projection is enough to make them 'readable' to an LLM. The transformers LLaVA docs[6] cover prompt formats, batching and newer LLaVA variants.
Quiz
References
- [1]Flamingo: a Visual Language Model for Few-Shot LearningPaperAlayrac et al.2022arxiv.org
- [2]BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language ModelsPaperLi et al.2023arxiv.org
- [3]Visual Instruction Tuning (LLaVA)PaperLiu et al.2023arxiv.org
- [4]Improved Baselines with Visual Instruction Tuning (LLaVA-1.5)PaperLiu et al.2023arxiv.org
- [5]Learning Transferable Visual Models From Natural Language Supervision (CLIP)PaperRadford et al.2021arxiv.org
- [6]LLaVA — Transformers documentationDocsHugging Facehuggingface.co