Saral Shiksha Yojna
Courses/Computer Vision

Computer Vision

CSE471
Prof. Makarand Tapaswi + Prof. Charu SharmaSpring 2025-264 credits

VLM Architecture — Encoders, Connectors, Positional Encoding

NotesStory
Unit 11 — Multimodal LLMs (PaliGemma / Qwen2-VL / Gemma 4)

When Vision Meets Language

For most of computer vision history, *image* and *text* lived in separate universes. Vision researchers built models that ate pixels and spit out class labels. NLP researchers built models that ate tokens and spit out other tokens. The two never talked to each other.

Then a wave of papers — CLIP, BLIP, Flamingo, LLaVA — taught a single neural network to fuse the two. Show it a photo and ask it a question in English. Hand it an X-ray and ask *"what's the diagnosis"*. Tell it *"pick up the cup"* and have it move a robot arm. These are Multimodal Large Language Models — VLMs (vision-language) when they just describe, VLAs (vision-language-action) when they actually do things.

The lecture walks through three generations of architecture — PaliGemma → Qwen2-VL → Gemma 4 — each fixing the previous one's limits, and then extends to VLAs that close the loop with action.

Part I — Why this is hard: the Modality Gap

The fundamental problem is a mathematical mismatch between how text and images are represented:

Text is discrete. Tokens are integers in . To get an embedding, you literally look up a row in a learned table: . Vocabulary size ~32k for most LLMs.

Images are continuous. An image is with pixel values in . You cannot index a lookup table with a pixel value — there are infinitely many possible images. You must run a learned encoder.

So the goal of every VLM:

Both modalities mapped into the same space, with semantic correspondence preserved. That's the only thing you're doing. Everything else is engineering.

The Three-Pillar VLM Blueprint

Every VLM in this unit follows this skeleton:

*Pixels → patch* → *Reshape dims to* → *Sequence reasoning*
*feature vectors* → *match LLM* → *over all tokens*
e.g. SigLIP ViT → Linear / MLP → Gemma 2B

One equation to memorise verbatim:

Visual tokens and text tokens are concatenated into one sequence, then fed to a standard autoregressive LLM. There is no special "cross-attention" mechanism — vanilla self-attention across the joint sequence does all the cross-modal work. This is the key insight. If a question asks *"how does a VLM perform cross-modal reasoning,"* the answer is: *it doesn't need a separate mechanism — self-attention over the concatenated sequence handles it naturally.*

Part II — PaliGemma: the simplest VLM that works

PaliGemma (Beyer et al., 2024) is the canonical didactic VLM. Memorise its three components:

| Component | Choice | Parameters | | --- | --- | --- | | Vision Encoder | SigLIP-So400m | 400M, frozen in Stage 1 | | Connector | Single linear layer, randomly initialised | small | | LLM | Gemma-2B decoder, pretrained | 2B | | Total | | < 3B |

Image tokens produced: 256 / 1024 / 4096 for input resolutions 224 / 448 / 896 respectively.

SigLIP — the vision encoder

SigLIP is a contrastive image-text encoder, conceptually identical to CLIP, with one critical mathematical difference in the loss.

Setup: two encoders, one shared space. (vision encoder, ViT-So400m, 400M params). (text encoder, Transformer).

Difference from CLIP: CLIP uses softmax normalised over the full batch — every pair's logit is normalised against the entire row. SigLIP uses independent sigmoid binary cross-entropy for every pair.

Step 1 — pairwise logits:

where is a learnable temperature (log-parameterised for stability) and a learnable bias.

Step 2 — independent sigmoid BCE:

with iff .

Why this matters: softmax normalises across the batch — changing batch size changes the loss landscape. Sigmoid treats every pair as its own independent binary classification problem. Result: SigLIP scales to arbitrary batch size without instability. *CLIP softmax → SigLIP sigmoid* is the key transition.

Inside SigLIP — ViT patch tokenisation

Step 1 — patch the image. SigLIP uses : . For : .

Step 2 — embed each patch:

for SigLIP-So400m.

Step 3 — Transformer:

**Memorise: , .**

The Connector — a single linear layer

SigLIP outputs 1152-d vectors; Gemma-2B expects 2048-d. So:

**Critical exam point: is the ONLY randomly initialised component of PaliGemma.** Everything else starts from pretrained checkpoints. The connector is "just a dimension change" that has to learn how to align two pretrained spaces.

Prefix-LM masking

The input sequence is laid out:

Image tokens | Prefix (prompt) | Suffix (answer)
  • Image + prefix = context. Attend bidirectionally — every token sees every other.
  • Suffix = the answer to generate. Attend causally — each token only sees tokens to its left.

Loss is computed only on suffix tokens. Image and prompt are free context — inputs the model doesn't have to predict.

PaliGemma's three training stages

1. Stage 1 — Unimodal pretraining. SigLIP on image-text pairs (contrastive). Gemma on text. No joint training. 2. Stage 2 — Multimodal pretraining. Freeze SigLIP, train + LLM on a large image-text corpus. Images at → 256 tokens. 3. Stage 3 — Task transfer. Fine-tune on specific tasks. Unfreeze all components, higher resolution.

The "everything is text" trick

PaliGemma's cleverest design choice: every task output is text — even spatial outputs.

  • Image Captioning — Prompt: "caption en". Output: natural sentence.
  • VQA — Prompt: "How many people?". Output: "3".
  • Object Detection — Prompt: "detect person". Output: "<loc0210><loc0142><loc0867><loc0821> person". The four tokens encode .
  • Referring Segmentation — Prompt: "segment the hammock". Output: bbox tokens + codewords.

Vocabulary extended with 1024 location tokens and 128 segmentation codewords. Exam-gold: *how does PaliGemma do detection if it only outputs text?* → special location tokens in the extended vocabulary.

Part III — Qwen2-VL: dynamic resolution + spatial encoding

PaliGemma works, but two fixable problems:

Problem 1 — Resolution bottleneck. SigLIP processes at fixed size (224/448/896). A 3mm nodule on a chest X-ray becomes sub-pixel after resizing. Once downsampled, the detail is irrecoverable.

Problem 2 — Aspect ratio distortion. A 16:9 photo forced into a 1:1 grid distorts spatial relationships. Receipt items appear ~40% shorter.

Qwen2-VL fixes both with dynamic resolution + M-RoPE.

Naïve Dynamic Resolution — tile at native aspect ratio

Stop resizing. Tile the image with patches at its actual size:

Varies per image. at : . at : .

Practical implications: batch collation needs padding to longest length (or dynamic packing). Inference cost scales with — *user controls the speed-quality trade-off*. Token budget control: .

1D RoPE breakdown for images

With raster index , two spatially adjacent patches at and have differing by 1 — they *look adjacent* — while and differ by — they look *far apart*, even though physically adjacent. The 1D ordering is incompatible with 2D geometry.

2D RoPE — encoding spatial position

Split the head dimension into two halves; apply RoPE independently to each — one for rows, one for columns. Patch at grid position :

  • Apply to first half — encodes row position.
  • Apply to second half — encodes column position.

**Attention score now encodes 2D relative displacement .** Invariant to image size — generalises to any resolution and aspect ratio.

M-RoPE — Multimodal RoPE (time × height × width)

For video, extend to a third dimension. Split head dim into thirds:

Token at :
Apply to first third — temporal position.
Apply to second third — row position.
Apply to third third — col position.

Edge cases:

  • *Static images:* for all patches.
  • *Text tokens:* same for all three: .

Why this is the cleverest part of Qwen2-VL: attention between two video patches now depends on — spatio-temporal displacement. The model can reason *"how did the object at move from frame to ?"*

Qwen2-VL vs PaliGemma — the table

| | PaliGemma | Qwen2-VL | | --- | --- | --- | | Vision encoder | SigLIP-So400m (400M) | ViT 600M + 2D-RoPE | | Connector | Linear | 2-layer MLP | | Position encoding | 1D-RoPE | M-RoPE | | Token count | Fixed 256/1024/4096 | Dynamic | | LLM | Gemma 2B | Qwen2 7B / 72B |

Qwen2-VL-72B matched GPT-4o on most multimodal benchmarks (October 2024).

Part IV — Gemma 4: close the connector seam

Even Qwen2-VL has a residual problem: vision and language are still trained in separate spaces before being stitched together. The connector remains an information bottleneck between two latent geometries optimised for *different* objectives:

  • SigLIP was trained for image-text cosine similarity (contrastive).
  • Gemma was trained to predict the next text token (language modelling).

A linear connector can only rotate and scale. A 2-layer MLP is more expressive but still a "narrow bridge between two independent latent spaces." Non-linear semantic relationships may not survive the projection.

Gemma 4's answer: native multimodal training from scratch. Vision and language share Transformer blocks from early layers. Visual patches and text tokens are processed by the *same* weight matrices. No dedicated projection layer — modalities meet inside the Transformer.

The lecture calls this the shift from "stitched" → "woven".

The three-generation comparison table

| Dimension | PaliGemma | Qwen2-VL | Gemma 4 | | --- | --- | --- | --- | | Vision Encoder | SigLIP-So400m (400M) | ViT + 2D-RoPE (600M) | Native / integrated | | Resolution | Fixed 224/448/896 | Dynamic (native AR) | Dynamic, multi-scale | | Connector | Linear | 2-layer MLP | Deep fusion (none) | | Positional Encoding | 1D-RoPE (text only) | M-RoPE | Unified spatial RoPE | | Position math | | | | | Token Count | Fixed 256/1024/4096 | Dynamic | Dynamic, multi-res | | LLM Backbone | Gemma-2B | Qwen2 7B / 72B | Gemma 4B–27B | | Training Paradigm | Frozen enc + tuned proj | Joint tuning | Native joint training |

Part V — From VLMs to VLAs: when models must act

A VLM can describe a coffee cup in precise detail. It cannot pick it up. Closing this gap is the point of Vision-Language-Action (VLA) models — robots driven by LLMs.

The key insight: actions are just another type of token

VLM vocabulary:
VLA vocabulary:

Each continuous action dim is discretised into 256 bins. A 7-DOF arm × 256 bins = 1792 new tokens. OpenVLA reuses the 256 least-frequent text tokens for this to avoid expanding the embedding table.

Action tokenisation — the math

De-tokenise with midpoint:

The centres within the bin. Quantisation error bounded by .

OpenVLA architecture (Kim et al., 2024)

A specific exemplar, with one twist worth knowing:

  • Two vision encoders concatenated: DinoV2 (strong spatial features, good for manipulation) + SigLIP (strong semantic features, good for instruction following).
  • MLP projector combines them.
  • LLaMA-2 7B backbone.
  • Action de-tokeniser at the output: the last 7 logits are over the 256 action bins per dimension; argmax → bin → continuous action.

VLA training objective

Initialise from a pretrained VLM (inherits language + vision understanding), then fine-tune on robot trajectories with a joint loss:

OpenVLA was trained on 970k robot trajectories from Open-X-Embodiment, ~7B params, fine-tunable on 1–2 hours of single-task demonstrations.

The full arc — perceive → align → reason → generate → act

👁 Perceive — ViT patches image into feature vectors .
Align — Connector : , same space as text.
🧠 Reason — LLM attends over with M-RoPE geometry.
Generate — Autoregressive decoder outputs text OR action tokens.
🤖 Act — Action de-tokeniser maps bins to continuous .

PaliGemma / Qwen2-VL / Gemma 4 cover steps 1–4. OpenVLA / RT-2 extend to step 5.

What you carry into the exam

The Modality Gap (text discrete, images continuous). The Three-Pillar Blueprint and the single equation — no special cross-attention; vanilla self-attention over the concatenated sequence. PaliGemma's three components and < 3B parameter count. SigLIP vs CLIP — pairwise sigmoid vs softmax over batch. ViT patch math: , . The connector is the ONLY random-init piece. Prefix-LM mask: bidirectional on image+prompt, causal on answer, loss on answer only. PaliGemma's three stages (unimodal → multimodal → task transfer). "Everything is text" with tokens for detection and segmentation codewords. Qwen2-VL's dynamic resolution and M-RoPE. The breakdown of 1D RoPE for images. M-RoPE edge cases (static ; text ). The three-generation comparison table. Gemma 4 stitched → woven. VLAs: actions as tokens, 7-DOF × 256 bins, bin/de-bin math, joint loss. OpenVLA's DinoV2 + SigLIP + LLaMA-2 stack. The five-stage perceive-align-reason-generate-act arc.

That's the heaviest unit in the course. You now have every equation the exam can possibly ask you to write.