v9.1.1
Menu

Chapter 3: Language Model Backbone

Week 3-4

Understand the language model architectures that serve as the reasoning engine in VLMs. From decoder-only transformers to encoder-decoder models, tokenization strategies for multimodal inputs, attention mechanisms over mixed sequences, popular LLM backbones, and the scaling laws that guide model selection.

Topics
6
Videos
6
Min read
42

Chapter Overview

The language model is the reasoning engine of every Vision-Language Model. While the vision encoder perceives the world and the bridge module translates visual features, it is the LLM backbone that performs inference, generates text, follows instructions, and chains together multi-step reasoning over what the model "sees."

Modern VLMs inherit their language capabilities from pretrained LLMs---GPT-class decoder-only transformers, encoder-decoder models like Flan-T5, or mixture-of-experts architectures like Mixtral. The choice of backbone profoundly affects the VLM's ability to follow complex instructions, perform multi-turn dialogue, reason about spatial relationships, and generate accurate descriptions.

A critical design question is how to extend a text-only LLM to process visual tokens. The model must handle a mixed sequence of image tokens and text tokens under a unified attention mechanism. This requires careful tokenization, positional encoding, and attention masking strategies.

This chapter covers:

  • Decoder-Only Transformers: The dominant architecture (GPT, LLaMA, Mistral) and causal attention
  • Encoder-Decoder Models: The T5/Flan-T5 paradigm with cross-attention for VLMs
  • Tokenization for Multimodal Input: How image tokens interleave with text in the input sequence
  • Attention Mechanisms in VLMs: Causal self-attention over mixed image+text sequences
  • Popular LLM Backbones: Vicuna, LLaMA, Mistral, Qwen, Phi---why each is chosen
  • Scaling Laws & Model Selection: Parameter count vs. performance trade-offs and quantization

Chapter Roadmap

Click any topic to jump in

Explainer video
1
Decoder-Only Transformers

Autoregressive generation with causal masking — the dominant architecture behind GPT, LLaMA, and most modern VLMs.

Autoregressive GenerationCausal Attention MaskNext-Token Prediction Loss
2
Encoder-Decoder Models

Bidirectional encoding with cross-attention decoding — the T5/Flan architecture used in some VLMs like BLIP-2.

Bidirectional EncoderCross-Attention MechanismSequence-to-Sequence Formulation
How multimodal data enters the LM
3
Multimodal Tokenization

How vision features become tokens the language model can process — visual tokens, delimiters, and multi-image sequences.

Visual Token CreationSpecial Delimiter TokensMulti-Image Sequences
Mechanisms for cross-modal understanding
4
VLM Attention Mechanisms

Full causal self-attention vs gated cross-attention — how VLMs let language attend to visual information.

Full Causal Self-Attention (Decoder-Only)Gated Cross-Attention (Flamingo)Attention Sink and Visual Token Placement
Choosing and sizing your backbone

Which model and how large

5
Popular LLM Backbones

LLaMA, Mistral, Phi, Qwen — the specific models used as VLM language backbones and their tradeoffs.

LLaMA Family (Meta)Mistral / Mixtral (Mistral AI)Small Language Models (Phi, Gemma)Multilingual Backbones (Qwen, InternLM)
6
Scaling Laws & Selection

Chinchilla scaling, VLM-specific compute allocation, and quantization — choosing the right model size.

Chinchilla Scaling LawVLM-Specific ScalingQuantization for Deployment

Topics

6 topics in this chapter

A vision encoder hands you a few hundred patch vectors, but vectors do not answer questions. Something has to read them alongside the user's prompt and produce fluent text, one word at a time, without peeking at words it has not written yet. The previous chapter ended with those patch features; this chapter is about the language model that consumes them. Nearly every open VLM since 2023 (LLaVA, Qwen-VL, Idefics2, InternVL) uses a decoder-only transformer for that job: a single stack of blocks in which every position may attend only to itself and earlier positions. That one constraint, the causal mask, is what lets the same network train on all positions in parallel and then generate autoregressively at inference. In a VLM the prefix simply contains image tokens before the text, so each generated word can look back at every patch. The arc has three steps: the chain-rule factorization that defines generation, the triangular mask that enforces it inside attention, and the next-token loss, including why VLM training masks the loss on image and prompt positions so only answer tokens are graded.

Definition

A decoder-only transformer is a stack of identical self-attention and feed-forward blocks with a causal mask, so position ii attends only to positions j≤ij \le i. It models a sequence as ∏tP(yt∣y<t)\prod_t P(y_t \mid y_{<t}), is trained with next-token cross-entropy on every position in one parallel pass, and generates by sampling one token at a time and appending it to the prefix.

In this topic

  1. 1Autoregressive Generation
  2. 2Causal Attention Mask
  3. 3Next-Token Prediction Loss
1 of 3
Autoregressive Generation

P(y1,y2,…,yT)=∏t=1TP(yt∣y1,…,yt−1)P(y_1, y_2, \ldots, y_T) = \prod_{t=1}^{T} P(y_t \mid y_1, \ldots, y_{t-1})

Why factorize at all? A joint distribution over every possible caption is far too large to write down, but the chain rule splits it exactly into one next-token distribution per position, P(yt∣y<t)P(y_t \mid y_{<t}). Here yty_t is the token at step tt, y<ty_{<t} the prefix before it, and TT the sequence length. Training computes all TT conditionals in one parallel pass because the targets are already known. Inference cannot: token tt must be sampled before token t+1t+1 can be conditioned on it, so generation costs TT sequential forward passes, made cheaper by caching keys and values. The failure mode is exposure bias: an early wrong token is never revised, because every later step conditions on it.

Mathematical Intuition

The chain rule of probability gives the exact factorization P(y1:T)=∏tP(yt∣y<t)P(y_{1:T}) = \prod_t P(y_t \mid y_{<t}). Each conditional is a categorical distribution over a vocabulary of size VV, so a single forward pass produces T×VT \times V logits. The log-likelihood of the whole sequence decomposes into a sum of per-token log-probabilities, making gradient computation embarrassingly parallel across positions during training.

Example:

For the sequence "A dog sits on grass", write out the conditional factorization.

2 of 3
Causal Attention Mask

CausalAttn(Q,K,V)=softmax(QKTdk+M)V,Mij={0j≤i−∞j>i\text{CausalAttn}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}} + M\right)V, \quad M_{ij} = \begin{cases} 0 & j \leq i \\ -\infty & j > i \end{cases}

How does one parallel pass avoid cheating? Without a mask, position ii could read the very token it is supposed to predict. The causal mask MM adds 0 where j≤ij \le i and −∞-\infty where j>ij > i to the scaled scores QKT/dkQK^T / \sqrt{d_k}, where QQ, KK and VV are the query, key and value matrices and dkd_k is the key width. Since e−∞=0e^{-\infty} = 0, future positions get exactly zero weight and every row still sums to 1. The mask has no parameters and is shared by every layer and head. A classic bug is an off-by-one mask that also hides the diagonal: the first row becomes all −∞-\infty and its softmax returns NaN.

Mathematical Intuition

Adding −∞-\infty to the upper triangle before softmax exploits the limit lim⁡z→−∞ez=0\lim_{z \to -\infty} e^z = 0, giving exact zeros in the attention matrix without any approximation. The resulting lower-triangular weight matrix AA satisfies A1=1A \mathbf{1} = \mathbf{1} (rows sum to 1), forming a row-stochastic matrix that acts as a weighted average operator over the value vectors.

Example:

In a 4-token sequence, what does the causal mask matrix look like?

3 of 3
Next-Token Prediction Loss

L=−1T∑t=1Tlog⁡Pθ(yt∣y<t)\mathcal{L} = -\frac{1}{T} \sum_{t=1}^{T} \log P_{\theta}(y_t \mid y_{<t})

What exactly is graded? The loss averages −log⁡Pθ(yt∣y<t)-\log P_\theta(y_t \mid y_{<t}) over the TT target positions, where θ\theta denotes the model weights. With the causal mask every position trains in the same pass, a scheme called teacher forcing because the true prefix is fed in rather than the model's own samples. Perplexity is eLe^{\mathcal{L}} when the log is natural. In VLM fine-tuning the loss is masked: image positions hold continuous embeddings with no vocabulary entry to predict, and prompt tokens come from the user, so only answer tokens count. Forgetting the prompt mask wastes gradient on teaching the model to imitate the user's question instead of answering it.

Mathematical Intuition

Cross-entropy in nats is the expected code length the model would need to encode the data, so minimizing it minimizes compression cost; the gap above the true entropy is the KL divergence from the data distribution to the model. Perplexity is eLe^{\mathcal{L}} for natural logs (2L2^{\mathcal{L}} only when L\mathcal{L} is measured in bits) and reads as an effective branching factor: GPT-2 small at 4.87 nats per token on a plain sentence has perplexity 131. Masking image and prompt positions simply removes terms from the sum, so the average runs over answer tokens only.

Example:

Why is the loss typically masked to exclude image tokens during VLM training?

Theory Exercise

Problem:

Explain why decoder-only transformers are preferred over encoder-decoder models as VLM backbones in recent architectures like LLaVA and GPT-4V. Consider computational efficiency, pretraining ecosystem, and instruction-following capability.

Hints:
  • Think about the availability of strong pretrained decoder-only models
  • Consider the simplicity of the architecture for multimodal input
  • Think about what instruction tuning provides