Vision transformers and multimodal interfaces
Trace patch shapes and distinguish contrastive dual encoders from cross-attention fusion.
On this page
The question for today
What you’ll understand
- Calculate patch token counts and projection dimensions.
- Compare independent modality encoders with token-level fusion.
- Explain how resolution and interface choices affect cost and capability.
A transformer does not inherently understand pixels, sound, or words. Each modality needs an interface that turns observations into vectors and preserves the structure the task requires. Vision transformers commonly use image patches as tokens. Multimodal systems then decide whether modalities should merely share a similarity space or exchange information inside a joint computation. These choices determine what can be precomputed, where information is lost, and how computational cost grows.
Patchification is an information interface
For an H by W image with C channels and nonoverlapping square patches of side P, assume both spatial dimensions are divisible by P. Each patch flattens to P²C numbers and is projected to model width d. The sequence contains HW/P² patch tokens, possibly plus a class token. Position representations distinguish patch locations because the flattened patch collection alone does not encode the original grid order. Padding or cropping policies must be explicit when images do not divide evenly.
The patch projection is a learned linear map, often implemented as a convolution with kernel and stride both equal to the patch size. This implementation equivalence does not make the full architecture a conventional convolutional network: later token mixing and parameter sharing differ. Large patches reduce sequence length but compress larger regions at the input interface. Fine spatial details may be harder to recover after that compression, particularly for localization tasks requiring more than a global class label.
Alignment and fusion solve different problems
A dual encoder maps an image and a text independently into a shared embedding space. A similarity score then supports retrieval or classification against candidate text descriptions. Image embeddings can be precomputed because they do not depend on the current text query. Contrastive training aligns matched pairs relative to negatives, but it does not by itself provide a token-level explanation of which word corresponds to which region or a mechanism for generating an answer.
A fusion model instead permits tokens from one modality to influence tokens from another, through joint self-attention or cross-attention. An image representation can then depend on the question, enabling more detailed interactions at additional computation cost. In cross-attention, query length and key-value length may differ. A system with an image encoder, a projector, and a language decoder must train or otherwise specify the interface between feature spaces; matching vector dimensions alone does not align their meaning.
Resolution affects the whole system
Doubling both image dimensions at fixed patch size quadruples the token count. A dense attention score matrix then has sixteen times as many entries per head, though matrix projections and other operations scale differently. Fused attention implementations can avoid storing the full score matrix while retaining dense arithmetic. Resolution policy therefore affects memory, latency, and accessible detail. Report actual resized dimensions, aspect-ratio handling, and any token selection instead of just naming the backbone.
Multimodal evaluations should test whether the model uses the intended modality. Compare against text-only and image-only baselines, perturb irrelevant content, and test counting or spatial relations when those capabilities matter. Training image-text pairs may contain noisy captions, duplicates, or sensitive correlations. A strong retrieval average does not prove compositional reasoning, and an answer that sounds fluent does not prove visual grounding. Evaluate the complete encoder-interface-decoder path on deployment-style inputs.
Work it through
Count tokens before choosing resolution
Use a 224×224 RGB image with 16×16 patches and projection width 768. Count patch tokens and projection weights, then double both image dimensions with the same patch size.
There are 224/16=14 patches per axis, giving 196 patch tokens. Each flattened patch has 16×16×3=768 values.
The projection weight matrix is 768×768, containing 589,824 weights before bias. This count does not grow when image resolution increases.
At 448×448 there are 28²=784 patch tokens. Excluding class tokens, one dense attention score matrix grows from 196²=38,416 entries to 784²=614,656, a factor of sixteen.
Shared projection parameters stay fixed while token-dependent activations and attention costs increase sharply with resolution.
The living notebook
Explore the model
Interactive lab · vectors
Separate direction from scale
The lab displays one two-dimensional vector, its angle and magnitude, and its coordinate projections. Relate its direction to a normalized embedding compared with a fixed x-axis reference. It does not encode images or text, compute cross-attention, or establish semantic alignment between modalities.
- x component
- 1.393
- y component
- 0.975
- Magnitude
- 1.7
Playback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forSharing a coordinate space enables a comparison only after training gives its directions a useful task-dependent meaning.
Pause & reason
Check your understanding
Your turn
Try it yourself
A cross-attention head uses 20 text queries, 256 image keys, key width 64, and value width 80. Give the score and output shapes, excluding batch size.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Multiply a 20×64 query matrix by the transpose of a 256×64 key matrix, then multiply probabilities by values.
Compare with the solution
Scores have shape 20×256. Softmax over image-key positions preserves that shape. Multiplying by a 256×80 value matrix gives output shape 20×80. The output remains indexed by text query positions; it does not automatically become an image segmentation map.
Keep the idea
Take it with you
- Patch size sets an early tradeoff between token count and spatial detail.
- Dual-encoder alignment and token-level fusion enable different interactions.
- Resolution, grounding, and modality ablations belong in system evaluation.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.