DL0133 VLM Fusion: Cross-Attention, Q-Former, and MLP

Compare Cross-Attention fusion, Perceiver Resampler / Q-Former, and Linear/MLP Projection (e.g., LLaVA) for vision-language alignment.

Answer

All three designs answer one question: where does the frozen vision encoder’s patch grid meet the language model? Linear/MLP projection (LLaVA, LLaVA-1.5, Qwen2-VL, InternVL) maps every patch embedding into the LLM’s token space with a two-layer MLP and concatenates the result onto the text sequence, so vision and language share one self-attention stack. Perceiver Resampler / Q-Former (Flamingo, BLIP-2) inserts a small cross-attention module in which a fixed set of K learned queries (64 latents in Flamingo, 32 in BLIP-2) attends over the N patch tokens and emits exactly K vectors, decoupling the LLM’s sequence length from image resolution. Gated cross-attention fusion (Flamingo’s xattn-dense blocks, Llama 3’s vision adapter, NVLM-X) never puts image tokens in the LLM sequence at all: new cross-attention layers are interleaved into the language stack (every fourth layer in Llama 3) and read the patch tokens as an external key/value memory, with a tanh gate initialized to zero so the model starts out behaviorally identical to the text-only LLM. The trade-off is compute versus fidelity versus intrusiveness: MLP concatenation is the simplest and preserves the most visual detail but makes prefill grow as O((N+T)^2); resampling is cheapest and constant-cost but imposes a hard information bottleneck; cross-attention keeps text throughput almost untouched and protects a frozen LLM, at the price of new parameters and a more complex training recipe.

(1) Where The Visual Tokens Live: MLP projection puts them inside the LLM sequence, a resampler puts a compressed K of them inside, and cross-attention keeps them outside as an external memory.
(2) Prefill Cost Scaling: concatenation pays O((N+T)^2 d) per layer, a resampler pays O((K+T)^2 d) with K \ll N, and cross-attention pays O(T N d) only in the inserted layers.
(3) Added Parameters: LLaVA-1.5’s connector is roughly 20M parameters of MLP, BLIP-2’s Q-Former is about 188M with a BERT-base initialization, and Llama 3’s cross-attention adapter adds tens of billions at the 405B scale.
(4) Information Bottleneck: a fixed K caps how much of a high-resolution image can survive, which is why resampler-based models underperform on dense OCR, charts, and counting.
(5) Frozen Versus Tuned Backbones: the Idefics2 ablation found cross-attention wins when the LLM is frozen, while the fully autoregressive concatenation design wins once the LLM is unfrozen.
(6) Token Count Beats Connector Design: MM1’s ablations show the connector type matters far less than image resolution and visual token count, which is why the field converged on MLP plus cheap token compression.

Three-panel architecture diagram. Panel A: ViT encoder produces 576 patch tokens, a two-layer MLP maps them into the LLM token space, and they are concatenated with T text tokens so self-attention runs over N plus T positions. Panel B: 32 learned queries cross-attend over the 576 patch tokens to produce 32 resampled tokens, which are concatenated with the text so self-attention runs over K plus T positions. Panel C: patch tokens stay outside the LLM as an external key value memory read by gated cross-attention layers inserted every fourth self-attention block, so self-attention still runs over T text tokens only.

Figure 1: The three fusion families differ only in where the patch tokens are allowed to go. Concatenation admits all N of them into the shared sequence, a resampler admits a fixed K, and gated cross-attention admits none, reading them instead as an external memory through layers whose tanh gate starts at zero.

The training recipe follows from the architecture. An MLP connector is so small that a two-stage schedule with about 558K caption pairs for alignment and 665K instruction samples for tuning is enough to reach state-of-the-art benchmark scores, which is what made LLaVA-1.5 reproducible on a single node. A Q-Former is a real transformer that must learn what to query, so BLIP-2 needs a dedicated representation-learning stage (contrastive, matching, and captioning objectives) before the generative stage, and remains the least data-efficient of the three per unit of final accuracy. Cross-attention sits in between: the gate makes optimization stable and lets you keep the language model frozen, so text benchmarks cannot regress, but you are training new layers that must learn to be useful without ever seeing visual tokens in the residual stream. NVLM’s controlled comparison is the cleanest evidence on the trade-off, finding the decoder-only concatenation variant stronger on OCR and multimodal reasoning while the cross-attention variant gave better throughput on high-resolution inputs.

Mathematical Formulation:
Z = E_v(I) \in \mathbb{R}^{N \times d_v}
H_v = W_2\,\sigma(W_1 Z) \in \mathbb{R}^{N \times d}
L = N + T
R = \mathrm{Attn}(Q, Z, Z) \in \mathbb{R}^{K \times d}
H_t \leftarrow H_t + \tanh(\alpha)\,\mathrm{Attn}(H_t, Z, Z)
C_{\mathrm{self}} = O(L^2 d)
C_{\mathrm{cross}} = O(T N d)

Where:

  • I is the input image, E_v the vision encoder (typically a frozen CLIP or SigLIP ViT), and Z its patch token grid.
  • H_v is the projected visual sequence, with W_1, W_2 the connector weights and \sigma a GELU nonlinearity; a single W recovers the original linear projection of LLaVA-1.
  • N is the patch count (576 for ViT-L/14 at 336 px), T the text length, L the LLM sequence length, d the LLM width, and d_v the vision width.
  • Q \in \mathbb{R}^{K \times d} are the learned latent queries and R the resampled output; K is fixed at design time, so R has the same size for a thumbnail and for a 4K page.
  • H_t is the text hidden state inside the LLM and \alpha the scalar gate parameter, initialized so that \tanh(\alpha) = 0 and the pretrained language behavior is exactly preserved at step zero.
  • C_{\mathrm{self}} is the per-layer self-attention cost that concatenation inflates, and C_{\mathrm{cross}} the per-layer cost of a cross-attention block, which is linear in N rather than quadratic.
Log-scale line chart of relative prefill attention cost versus number of visual tokens from zero to 2880, for a 512-token text prompt and a 32-layer language model. The MLP concatenation curve rises quadratically to about forty-four times the text-only baseline, the gated cross-attention curve rises linearly to about two point four times, and the 64-latent resampler curve stays nearly flat at about one point four times.

Figure 2: Prefill attention cost relative to a text-only forward pass at T = 512. At 2880 visual tokens, concatenation costs about 44\times the text-only baseline because the whole stack pays O((N+T)^2), gated cross-attention costs about 2.4\times since only 8 of 32 layers see the image and they scale as O(TN), and a 64-latent resampler stays near 1.4\times because the LLM never sees more than K + T positions.

PropertyLinear / MLP projectionPerceiver Resampler / Q-FormerGated cross-attention
Tokens entering the LLM sequenceAll N patch tokens (576 at 336 px, thousands with tiling)Exactly K latents (32 in BLIP-2, 64 in Flamingo)None; patch tokens are external keys and values
Prefill scaling in image sizeQuadratic, O((N+T)^2 d) in every layerConstant for the LLM, linear O(KN d) in the resamplerLinear, O(TN d) in the inserted layers only
Added parametersSmallest; a 2-layer GELU MLP, roughly 20MMedium; about 188M for the BERT-base Q-FormerLargest; new attention plus FFN blocks scaled to the LLM width
Training complexityLowest; align then instruction-tune on about 1.2M samplesHighest; needs a separate representation-learning stage to convergeModerate; the zero-init tanh gate makes the warm start stable
Effect on a frozen LLMWeakest option when frozen; usually the LLM must be tunedWorks frozen, which is exactly why BLIP-2 used itBest; text-only behavior is provably unchanged at initialization
Main weaknessContext blowup with high resolution, video, or many imagesFixed-K information bottleneck hurts OCR, charts, and countingExtra parameters and weaker reported OCR and reasoning transfer
Representative modelsLLaVA, LLaVA-1.5, Qwen2-VL, InternVL, NVLM-DFlamingo resampler, BLIP-2, InstructBLIP, IdeficsFlamingo xattn-dense, Llama 3.2 Vision, NVLM-X

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *