Visual Connector

A visual connector is the trainable module that turns the output of a vision encoder into vectors a language model can read as part of its input sequence. A language model consumes a sequence of embeddings of a fixed width, and a vision encoder produces patch features of a different width, in a different space, and in a count set by the image size. The connector solves both mismatches: it maps the features to the language model’s embedding width and decides how many vectors (visual tokens) the image contributes.

Role in the pipeline

The common arrangement keeps a pretrained image encoder and a pretrained language model and trains the connector (and, in a later stage, often the language model). The image goes through the encoder to give \(N\) features \(z_1,\dots,z_N\) of width \(D_v\). The connector produces \(M\) vectors of width \(D_\text{llm}\). These replace the embeddings of placeholder tokens in the prompt, and the language model continues the sequence as usual. Training uses the language-model loss on the response tokens, so the connector learns whatever helps predict the response. Because only the connector is new, a poor result can be localized: the encoder may not capture the detail, the connector may discard it, or the language model may ignore it.

Designs

Linear or MLP projection. Each patch feature is mapped independently, \(h=Wz\) (LLaVA), or with a small MLP, \(h=W_2\,\mathrm{GELU}(W_1 z)\) (LLaVA-1.5). Every patch becomes a token, so \(M=N\). The connector keeps the patch count; its projection can still lose feature information depending on its rank and training, and the language model’s attention decides what to use.

Learned-query resampler. A fixed set of \(M\) learned query vectors attends to the encoder features through cross-attention, so with queries \(Q\) and features \(K=V\) each output is \(\mathrm{softmax}(q K^\top/\sqrt{D})V\), a weighted average of the features chosen by that query. The output size is \(M\) whatever \(N\) is. Flamingo’s Perceiver Resampler produces a fixed number of visual tokens per image (about 64); BLIP-2’s Q-Former uses 32 learnable queries of width 768, and a linear layer then projects them to the language model’s width. The BLIP-2 authors describe this as an information bottleneck.

Pooling or merging. Neighboring patches are averaged or concatenated (for instance 2ร—2) before projection, which cuts the token count by a fixed factor with no learned selection.

Cross-attention layers inside the language model. Visual features enter through added attention layers per block (Flamingo) instead of the input sequence, so images add no tokens to the context but change the language model more invasively.

Token count trade-off

The number of visual tokens decides how much context an image uses and how much compute follows. Arithmetic for a ViT-style encoder with patch size 14 (all numbers computed):

image side (px)tokens, one per patchattention pairs (tokensยฒ)
22425665,536
336576331,776
4481,0241,048,576

Doubling the side of the image multiplies the token count by 4 and the pairwise attention cost by 16 in the language model’s layers. A 336 px image with a projection connector costs 576 positions of context; 2ร—2 pooling of the 24ร—24 patch grid gives 144 tokens, 4ร—4 gives 36, and a 32-query resampler gives 32. Splitting a large image into tiles grows tokens linearly with the number of tiles (four 576-token tiles plus a 576-token thumbnail is 2,880 tokens).

Connector size is a separate matter. With \(D_v=1024\) and \(D_\text{llm}=4096\), a linear projection has \(1024\cdot4096+4096=4{,}198{,}400\) parameters and a two-layer MLP \(20{,}979{,}712\). In both cases the number of positions the language model must process, not the connector’s parameter count, dominates inference cost.

What compression can lose

Compression discards evidence in a way that depends on the design. Fixed average pooling over a \(g\times g\) area scales a localized signal down by \(1/g^2\) before noise, so small objects, thin lines, and small text fade first. A learned resampler can keep small evidence only for the kinds of evidence its training rewarded; queries never useful for counting small objects will not learn to find them. Attention over many nearly uniform keys averages a single distinctive patch away unless a query is aligned with it. For a ViT-L/14 encoder at 224 px, the Q-Former input is 257ร—1024=263,168 values and its output is 32ร—768=24,576, about a 10.7-fold reduction.

Choosing

A projection connector keeps one token per patch and costs context and compute; preserving task-relevant detail still depends on the encoder and projection. A resampler gives a short, fixed-length visual prefix at the risk of losing small detail; pooling is a cheap version of the same trade. The decision should be made on an evaluation that includes detail-dependent tasks (reading small text, counting), since benchmarks of coarse scene description may not separate the designs.

References


Discover more from Insightful Data Lab

Subscribe to get the latest posts sent to your email.

Similar Posts

Questions, corrections, or additional insights?

This site uses Akismet to reduce spam. Learn how your comment data is processed.