Stanford CS336 Language Modeling from Scratch
Lecture 17: Multimodal Models
From Vision-Language Encoders to Omni Architectures Autumn 2026
From Language Models to Omni Models
Ultimate goal: an omni model accepting any combination of modalities as input and producing any combination as output
Frontier models are expected to be natively multimodal rather than text-only
Transformers remain the core engine — which forces a common representation interface
Core constraint: Transformers operate on sequences of tokens, so every modality must be converted into tokens.
The Core Interface: Turning Signals into Tokens
Transformers speak tokens (discrete IDs or continuous vectors), each carrying a roughly semantic unit of information
For text, BPE tokenization already converts byte streams into compact semantic chunks
Non-text modalities are much harder to tokenize:
Raw pixels, video frames, and audio waveforms are continuous, high-dimensional, and heavily redundant
Lower information density than text: a single image or video clip expands into hundreds or thousands of tokens
Arbitrary resolutions, aspect ratios, and durations do not map cleanly to fixed 1D sequences
Two guiding questions for this lecture:
1. How do we encode non-text inputs into a Transformer (understanding)?
2. How do we output non-text modalities from a Transformer (generation)?
We start with vision encoders, then inject them into LLMs, and finally examine unified discrete tokenization.
Three Architectural Paradigms for Multimodality
Notice the central tension: continuous features excel at comprehension, while generation requires reconstructing fine-grained details.
Encoding Images: From Labels to Web Captions (CLIP)
Every row and column requires a softmax over the entire batch of $N$ examples — coupling loss quality directly to batch size.
CLIP: Why Contrastive Ranking Wins — and Its Limits
Headline zero-shot result: on ImageNet, zero-shot CLIP outperformed a supervised ResNet-50 trained on 1.2M labeled ImageNet images
Ablation (contrastive vs. generative):
Alternative: predict caption text directly from the image (autoregressive language modeling)
Far less compute-efficient than CLIP-style contrastive ranking — predicting exact surface wording wastes capacity compared to distinguishing semantic matches
Limitations when reused in modern VLMs:
Design choices targeted coarse image classification, losing fine-grained spatial detail and small text (OCR)
$336 \times 336$ center-cropping discards aspect ratio and border content
Full-batch softmax demands huge batch sizes and global synchronization across workers
CLIP gave multimodal models their semantic backbone, motivating fixes for both its loss (SigLIP) and resolution (AnyRes, dynamic ViT).
SigLIP: Pairwise Sigmoid Loss
CLIP: $N$-way multiclass softmax — compare (text, image) against all other images in the batch
SigLIP (Sigmoid Loss for Language-Image Pre-Training, 2023): independent binary classification on every pair (image, text) — aligned ($z_{ij}=+1$) or not ($z_{ij}=-1$)?
Vision encoder: OpenCLIP ViT ($14 \times 14$ patches)
Adapter: 1-layer cross-attention resampler with 2D positional encodings — compresses any image to a fixed length of 256 tokens
Special grounding tokens:<img>, <box>, <ref> enable bounding-box localization in text
Stage
Data & Resolution
Vision
Adapter
LM
Stage 1
Large-scale, lower-quality web pairs
Train
Train
Frozen
Stage 2
Higher-quality task-specific data + higher resolution
Train
Train
Train
Stage 3
Instruction tuning data
Frozen
Train
Train
Unfreezing the vision encoder in Stages 1–2 improves visual alignment, but a fixed 256-token bottleneck still limits fine-grained reading.
Qwen2-VL (2024): Native Dynamic Resolution
Native dynamic resolution: drops fixed-size tiling — encodes images at their natural aspect ratio and patch count
Example: a $224 \times 224$ patch crop encoded with ViT/14 and compressed $2 \times 2 \Rightarrow$ 66 tokens (64 merged + boundary tokens)
Video handling: sample 2 frames/sec, capped at 16,384 tokens per video
3-stage training: (1) visual encoder only, (2) all parameters, (3) LM on instruction-following datasets
Multimodal Rotary Position Embedding (MRoPE)
Standard 1D RoPE flattens 2D patch grids into a line, making vertical neighbors look arbitrarily far apart
MRoPE (Qwen2-VL) partitions rotary embedding channels across three axes: temporal $t$, height $h$, and width $w$
Text tokens set $t = h = w$, preserving exact backward compatibility with pretrained text-only RoPE
Next: how Qwen3-VL fixes a subtle frequency-band flaw in MRoPE and deepens visual-language fusion.
Qwen3-VL (Late 2025): Refining the Modular VLM
Backbone & scale:
Vision encoder:SigLIP-2 (same core architecture as SigLIP)
Language model: Qwen-3 dense and sparse MoE backbones up to 235B-A22B
Long context: native multimodal understanding up to 256K tokens
Four targeted architectural upgrades:
Interleaved MRoPE — fixes frequency imbalance across $(t, w, h)$ axes
Explicit video timestamps — separate timestamp tokens instead of relying only on positional IDs
DeepStack adapter — injects visual features across multiple LLM layers
Square-root-normalized per-token loss — prevents long video sequences from dominating gradients
Illustrates the mature open VLM recipe: surgical architectural fixes combined with massive data engineering and scale.
Interleaved MRoPE & Explicit Video Timestamps
Interleaved MRoPE: interleaving [t w h t w h ...] gives temporal, horizontal, and vertical axes balanced short-range and long-range resolution
Explicit video timestamps: insert timestamp markers as separate tokens ahead of video frames rather than relying solely on positional embeddings for elapsed time
DeepStack: Cross-Layer Visual Feature Injection
Standard VLMs inject visual tokens only at LLM Layer 0, forcing the final ViT layer to summarize everything
DeepStack fuses intermediate visual encoder representations into multiple LLM layers
Preserves both fine-grained low-level cues (edges, OCR characters) and high-level semantics without inflating context length
Loss Balancing & Staged Pretraining (Qwen3-VL)
The long-video gradient problem: video sequences have many tokens with lower information density than text — standard per-token loss lets long videos dominate updates
Fix:square-root-normalized per-token loss — downweights very long multimodal examples to keep text and visual/video training balanced
Progressive context extension (8K → 32K → 256K) mirrors text-LLM training while scaling cleanly to long videos and documents.
Evolution of the Modular VLM Template
Model
Vision Encoder
Resolution
Adapter
Pos. / Backbone
LLaVA (2023 baseline)
CLIP ViT-L/14
Fixed 336×336 crop
Linear $W$
1D RoPE / Vicuna (LLaMA)
Qwen-VL (2023 baseline)
OpenCLIP ViT/14
Fixed crop, higher in Stage 2
1-layer cross-attn → 256 tokens
2D pos in adapter; <box>, <ref>
LLaVA-OneVision (2024)
SigLIP (2-layer grid)
AnyRes tiles + bilinear pool
2-layer MLP
Qwen-2 72B (img / multi / vid)
Qwen2-VL (2024)
675M ViT (DFN init)
Native dynamic + 2×2 merge
Patch merger
MRoPE / Qwen2 (vid 2 fps, ≤16K)
Qwen3-VL (late 2025)
SigLIP-2
Native dynamic + timestamps
DeepStack (multi-layer)
Interleaved MRoPE / Qwen-3 MoE (235B-A22B, 256K)
Clear trend: fixed crops and fixed-length resamplers gave way to native dynamic resolution, 3D rotary positions, and MoE backbones.
Late-2026 Backbones Powering Multimodal Systems
Sparse MoE decouples total capacity from per-token compute as images & video expand context:
Qwen3.8-Flash-Next (Aug 2026): 125B total, 6B active (512 experts, 10 active + 1 shared)
Keeps output logits centered near zero because softmax is shift-invariant ($\text{softmax}(\ell) = \text{softmax}(\ell + c)$)
Together, QK-Norm and z-loss stabilized Chameleon across trillions of mixed-modal tokens.
Continuous Encoders vs. Discrete Visual Tokenizers
Property
Continuous Encoders (CLIP / SigLIP / SigLIP-2)
Discrete Tokenizers (VQ-VAE / Chameleon)
Token type
Continuous ViT patch vectors
Discrete codebook IDs (e.g., 8,192)
Fine-grained detail & OCR
High — preserves subtle visual detail
Degraded — discretization loses fine detail (e.g., small text / OCR)
Image generation
Not native (needs external decoder)
Native — single autoregressive LM head
Training stability
Mature staged alignment
Harder — entropy mismatch needs QK-Norm + z-loss
Architectural simplicity
Requires encoder + adapter + decoder
Elegant — pure next-token modeling
Chameleon proved unified discrete autoregression works, but discretization still lags continuous encoders on visual comprehension.
Comprehension vs. Generation: The Fundamental Tension
Comprehension and generation place opposing demands on visual representations
Contrastive encoders discard pixel-level details to isolate semantics; reconstruction tokenizers preserve pixels at the cost of weaker semantics and OCR
Square-root-normalized per-token loss balances low-density video against text; QK-Norm and z-loss prevent mixed-modal divergence
Spatial-temporal alignment across depth:
Interleaved MRoPE, explicit timestamp tokens, and DeepStack cross-layer fusion preserve 3D visual structure inside deep LMs
Recap: Multimodal Models
Omni goal: any combination of modalities in and out through a shared Transformer token interface
Contrastive encoders: CLIP proved web-scale caption alignment beats fixed labels; SigLIP / SigLIP-2 replaced softmax with pairwise sigmoid loss on OCR-rich WebLI data
Modular VLMs: progressed from LLaVA (fixed crop + linear $W$) and AnyRes to dynamic resolution, Interleaved MRoPE, DeepStack, and sparse MoE / long-context backbones
Early-fusion discrete models: Chameleon (VQ-VAE + BPE) unifies reading and generation with QK-Norm and z-loss, but discretization still loses fine detail (OCR)
Current synthesis: continuous encoders + long-context Transformer + diffusion decoders resolve the semantics-vs-detail tension
← → / space to move · A to reveal answers · F fullscreen