Skip to content

Bridging frozen models: BLIP-2's Q-Former and Flamingo's gates

Reuse a pretrained image encoder and a pretrained language model, keep both frozen, and train only a small bridge: learned queries that compress the image into a few tokens, and gated cross-attention that starts switched off.

Advanced ~14 min8 steps

What runs in CodeSplash

You will be able to

  • Explain why bridging frozen models is cheaper than training a multimodal model end to end
  • Describe how a Q-Former turns any number of patches into a fixed number of tokens
  • Trace how a tanh gate lets cross-attention start with no effect
  • Distinguish in-context examples from fine-tuning

Read first

01

Don't retrain what already works

Read

A strong image encoder and a strong language model already exist, each trained at great cost. Training a multimodal model from scratch would throw that away. The cheaper route: keep both frozenFrozen encoderA pretrained model used as-is to turn each image, sound or text into a vector; it learns nothing from your data. and train only a small bridge that turns image features into something the language model can read.

The language model then keeps its knowledge and fluency, and the bridge holds the few million new weights. The catch is that the bridge must speak the language model's input "language" without changing the model.

02

BLIP-2: ask the image a few questions

Read
Reference · MM011 · Explained hereBLIP-2Bridges a frozen image encoder and frozen language model with a trainable querying transformer. The design reduces the number of trainable parameters while transferring visual information into a powerful…

BLIP-2's bridge is the Q-FormerQ-FormerA small set of learned queries that read a frozen image encoder by cross-attention and pass a fixed number of tokens on.: a small Transformer with a fixed set of learned query vectors (32 in the paper). The queries read the frozen image encoder's patch features by cross-attentionCross-attentionAttention whose queries come from one sequence and keys and values from another, such as text reading image features., and their outputs are projected into the language model's input as a short visual prompt.

In the program two queries attend over four patches and return a 2 × 2 block: one row per query, whatever the patch count. The first query mostly reads patches like itself, giving (0.575, 0.425).

03

Two training stages

Read

BLIP-2 trains the Q-Former in two stages. First, with only the image encoder attached, it learns to pull out text-relevant features using contrastive, matching and captioning losses. Then it is connected to the frozen language model and trained to produce prompts that make the model write the right caption.

The first stage matters: a bridge trained only through the language model's loss has to learn vision and the model's input space at once, which is slower and less stable.

04

Flamingo: switch vision on slowly

Read
Reference · MM012 · Explained hereFlamingoConnects pretrained vision and language models to process interleaved visual/text sequences and learn from demonstrations placed in context.

Flamingo inserts new gated cross-attentionGated cross-attentionNew cross-attention layers inside a frozen language model, multiplied by tanh of a gate that starts at zero. layers between the frozen language model's layers. Each adds tanh(g) times what it read from the image, with g starting at zero. So at the start the model is exactly the original language model; the gate opens as training finds the image useful.

In the program a two-number language vector receives a visual update: with the gate at 0 it stays (0.400, 0.600); at 1 it becomes (0.628, 0.448).

05

Before any training

Predict

Predict first

A Flamingo-style model has its gates at zero and hasn't been trained yet. You give it an image and "This picture shows". What does it produce?

Random textExactly what the frozen language model would sayAn empty answer
Reveal

Exactly what the frozen language model would say: the image is ignored because every gate multiplies its contribution by tanh(0) = 0. Training opens the gates gradually, so the language model is never disrupted by random visual input.

06

Examples in the prompt

Read

Flamingo trained on web pages where images and text interleave. As a result it can take a few image–answer examples in its prompt and continue the pattern for a new image, with no weight changes. That is in-context learning: the examples change the input, while fine-tuning changes the parameters. It is convenient, and sensitive to which examples and what order you show.

07

Check: bridging frozen models

Checkpoint

Check yourself

A Q-Former has 32 learned queries. The image encoder returns 256 patch features for one image and 1,024 for a larger one. How many visual tokens reach the language model in each case?

Show the answer

Answer: a

One output per query, whatever the patch count, so the language model's input stays short.

Check yourself

Which parts of BLIP-2 are trained?

Show the answer

Answer: b

Freezing both pretrained models is the point: few trainable weights, and the language model keeps its abilities.

Check yourself

Why does BLIP-2 train the Q-Former with the image encoder alone before attaching the language model?

Show the answer

Answer: a

The first stage gives the bridge good visual features; the second teaches it to present them to the language model.

Check yourself

A gated cross-attention layer adds tanh(g) × (visual update) to the language features. With g = 0, how large is the visual contribution?

Show the answer

Answer: 0

tanh(0) = 0, so nothing is added: the frozen language model runs unchanged until training moves g.

Check yourself

You show Flamingo three photo–answer examples and then a new photo. What changed in the model?

Show the answer

Answer: a

In-context examples change the prompt. Fine-tuning would change parameters and persist beyond this prompt.

08

Recap

Recap
  • Bridging keeps a pretrained image encoder and language model frozen and trains only a small connector.
  • BLIP-2's Q-Former: learned queries cross-attend to patches and always return the same number of tokens.
  • Flamingo's gated cross-attention starts at tanh(0) = 0, so the language model is unchanged until vision proves useful.
  • In-context examples change the input, not the weights.

Concepts in this lesson