Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Understanding images and sound

Vision encoders: how can image patches join the text?

Text has token embeddings. Could a Vision Transformer (ViT) give images something similar, without fixing their shape or discarding too much detail?

We want to ask a language model about a photograph, but its familiar input is a sequence of token vectors. Could each pixel become a position, or could one vector hold the whole image?

Follow the data through the mechanismWe want to ask a question about this picture896 × 896 pixelsOne position per pixelEarlier context is dimmed; outlined columns are current.Current token computation.3802,816 positionsEvery colour · a very long sequenceOne position for the whole imageEarlier context is dimmed; outlined columns are current.Current token computation.1,152One bottleneck for every detail
One position per pixel makes a huge sequence; a single vector for the image creates one bottleneck for all its detail.

Keep a small piece at each position

Both extremes leave a difficulty: too many positions, or too much information squeezed into one. Try a middle ground: split the picture into patches, then give each patch a learned vector.

Follow the data through the mechanismOne image crop896 pixels89614 × 14 RGB patch14 pixelsEarlier context is dimmed; outlined columns are current.Current token computation.588Flatten pixelsPatch projection + bias5881,152Earlier context is dimmed; outlined columns are current.Current token computation.1,152Patch vector
Cutting the image into patches gives many small inputs. A learned projection turns each patch’s 588 colour values into one 1,152-coordinate vector.

The sequence is shorter, but each vector was calculated from its own patch alone. A brown patch could belong to a roof, a branch or an animal. What could tell us which?

Keep track of where a patch came from

Attention could request the missing context, but the same patch projection is used everywhere in the image. How would it distinguish the top-left corner from the bottom-right?

Follow the data through the mechanismWhere the patch came from64 × 64 spatial positionsLearned position table4,0961,152Patch vectorEarlier context is dimmed; outlined columns are current.Current token computation.1,152Position vectorEarlier context is dimmed; outlined columns are current.Current token computation.1,152+Earlier context is dimmed; outlined columns are current.Current token computation.1,152Add the matching position vector at all 4,096 positions
A learned vector for each location is added to that patch’s content vector before any vision attention block.

One answer is a learned position vector (coming soon) for each place in the patch grid, added to its content vector. The table learns information about location; it is separate from the weights that recognise patch content.

Reuse the transformer block

Now the patches have content and location. Could the familiar attention and feed-forward layers let them exchange information?

Follow the data through the mechanismPatch vectors + learned positionsEarlier context is dimmed; outlined columns are current.Current token computation.1,1524,096 patch positions27 encoder blocksLayerNorm16 attention headsWq + bias1,15272QEarlier context is dimmed; outlined columns are current.Current token computation.724,096 positionsWk + bias1,15272KEarlier context is dimmed; outlined columns are current.Current token computation.724,096 positionsWv + bias1,15272VEarlier context is dimmed; outlined columns are current.Current token computation.724,096 positionsAttention map4,0964,096Bidirectionalsoftmax(QKᵀ / √72)×Combine valuesEarlier context is dimmed; outlined columns are current.Current token computation.724,096 head resultsJoin 16 × 72 coordinatesEarlier context is dimmed; outlined columns are current.Current token computation.1,1524,096 positionsOutput projection + bias1,1521,152+LayerNormFeed-forward W₁ + bias1,1524,304GELUFeed-forward W₂ + bias4,3041,152+Earlier context is dimmed; outlined columns are current.Current token computation.1,1524,096 positionsFinal LayerNormEarlier context is dimmed; outlined columns are current.Current token computation.1,1524,096 contextual positions
27 SigLIP blocks: each uses 16-head bidirectional attention, two residual additions and a 1,152 → 4,304 → 1,152 GELU feed-forward network, followed by a final LayerNorm.

This is ordinary multi-head attention, with one important change from causal text attention: a patch can attend to any patch in the image. The map is full rather than triangular; the complete image is already available.

Let the patch count change

We have drawn an 896 × 896 crop, but the attention weights act on vector width, not the number of patches. Could we keep a larger image instead of resizing it to that shape?

Follow the data through the mechanismReference cropEarlier context is dimmed; outlined columns are current.Current token computation.1,1524,096 patchesSame per-head Q/K/V widths1,15272Attention map4,096 × 4,09616,777,216 pairs / headDouble the height and widthEarlier context is dimmed; outlined columns are current.Current token computation.1,15216,384 patchesSame per-head Q/K/V widths1,15272Attention map16,384 × 16,384268,435,456 pairs / head4 × as many patches → 16 × as many attention pairs
The projection weights keep the same shape as patch count grows; full attention’s pair count grows with the square of that count.

Yes, provided the position scheme and surrounding model support the new grid: doubling both image dimensions gives four times as many patches and 16 times as many attention pairs. Fixed-size crops make training batches, compute and the downstream token budget per crop predictable; they are not a requirement of attention.

Our worked encoder uses a learned 64 × 64 position grid and a standard fixed-crop path. A different grid needs positions too, but those can be interpolated or represented another way; the optional comparison below explores that choice.

Combine patches after they have context

Could we average neighbouring vectors now that they have exchanged information?

Follow the data through the mechanismKeep spatial arrangement64 × 64 patch positionsOne group: 4 × 4 positionsAverage16 × 16 pooled positionsOne pooled position16 contextual vectorsEarlier context is dimmed; outlined columns are current.Current token computation.1,15216 positionsAverage each coordinateEarlier context is dimmed; outlined columns are current.Current token computation.1,1521 vector · same width4,096 positions → 256 positions · width stays 1,152
Pooling combines 16 neighbouring contextual vectors into one. The image uses 16 times fewer sequence positions; each vector still has 1,152 coordinates.

Averaging each 4 × 4 group leaves 256 positions, at the cost of some fine detail. Each vector still has 1,152 coordinates; averaging is one compression choice, not a rule for vision encoders.

Make image and text vectors fit together

The text vectors have a different width. Could another learned projection bridge that gap?

Follow the data through the mechanismImage vectorsText embeddingsEarlier context is dimmed; outlined columns are current.Current token computation.1,152256 positionsEarlier context is dimmed; outlined columns are current.Current token computation.2,560Text positionsRMSNormImage-to-text projection1,1522,560Earlier context is dimmed; outlined columns are current.Current token computation.2,560256 positionsSame sequenceEarlier context is dimmed; outlined columns are current.Current token computation.2,560Image + text positions
Gemma 3 4B: a learned matrix changes image-vector width from 1,152 to 2,560. It keeps all 256 image positions, now at the same width as the text.

These are continuous image representations, often called soft tokens. They can sit among text embeddings; we do not need to turn the image into words first.

Follow the resulting image path

Each step now has a reason: patches keep local detail, attention adds context, pooling shortens the sequence, and projection matches its width to the text.

Follow the data through the mechanismImage crop896 × 896 × RGBPatch projection + bias5881,152Earlier context is dimmed; outlined columns are current.Current token computation.1,1524,096 patch positionsLearned position table4,0961,152+SigLIP vision encoder27 layers1,152Final LayerNormEarlier context is dimmed; outlined columns are current.Current token computation.1,1524,096 contextual positionsAverage each 4 × 4 groupEarlier context is dimmed; outlined columns are current.Current token computation.1,152256 image positionsRMSNormImage-to-text projection1,1522,560Earlier context is dimmed; outlined columns are current.Current token computation.2,560256 image positionsText embeddingsEarlier context is dimmed; outlined columns are current.Current token computation.2,560Text positionsPlace vectors in the sequenceEarlier context is dimmed; outlined columns are current.Current token computation.2,560Text + 256 image positionsLanguage model34 layers2,560Predict the next text token
Gemma 3 4B: 4,096 patch vectors become 256 image positions after spatial pooling. A learned matrix changes their width to match the text vectors; the language model receives both in one sequence.

This worked path belongs to Gemma 3, using a 27-layer SigLIP Vision Transformer (ViT). Extra crops preserve more high-resolution detail, but add positions again. Could we make a similarly useful sequence from sound?

Keep a different image shape or resolution

Resizing every image to a fixed square can discard detail or distort its proportions. Could we keep its aspect ratio and choose a patch grid within a compute budget instead?

Follow the data through the mechanismResample a learned position grid64 × 64 locationsInterpolate in 2D128 × 128 locationsUse row and column coordinates(row, column)2D rotary positionsRotate QRotate KEarlier context is dimmed; outlined columns are current.Current token computation.72Earlier context is dimmed; outlined columns are current.Current token computation.72Positions enter the attention scoresA model can use both a learned grid and rotary positions
Learned grids can be interpolated for a different patch layout; 2D RoPE instead rotates queries and keys using their spatial coordinates, and some encoders combine both.

A learned position grid can be resampled for the new layout: the NaFlex path of SigLIP2 does this. Alternatively, Pixtral-based encoders use 2D RoPE, rotating queries and keys according to row and column.

These choices can coexist: Qwen3.5 and Kimi’s MoonViT combine learned spatial positions with rotary positions. Variable grids still have a patch budget, and a checkpoint needs training and preprocessing that support the layouts we give it.

Keep the coordinates before learning how to combine them

Averaging merges neighbouring coordinates immediately. Could we retain them separately until learned weights decide how to mix them?

Follow the data through the mechanismCould we keep the coordinates before learning the mixture?Earlier context is dimmed; outlined columns are current.Current token computation.1,1524 neighbouring positionsConcatenate 2 × 2 neighboursEarlier context is dimmed; outlined columns are current.Current token computation.4,6081 wider vector · 4,608 coordinatesLearned mixing4,6081,152Earlier context is dimmed; outlined columns are current.Current token computation.1,152Fewer positions · the mixture is learned
One alternative to averaging: concatenate neighbouring vectors, then let learned weights mix their coordinates into a single output.

Concatenating a 2 × 2 group turns four vectors into one vector four times as wide, before learned mixing reduces that width. This rearrangement is often called spatial patch merging or pixel unshuffle; unlike averaging, the rearrangement itself discards no coordinates.

Qwen3.5, Llama 4 and C-RADIO in Nemotron use 2 × 2 merging. DeepSeek-V4.1 uses 3 × 3 unshuffle before a two-layer projector; the learned width reduction can still lose information.

Change how the image features are built

The goal was useful image vectors, rather than a particular transformer. Other encoders change how those vectors gain context:

ChoiceWhat changesExamples in our model catalogue
Transformer blocksAttention exchanges information between image patches; normalisation, feed-forward layers and positional rules vary.SigLIP, MoonViT, Pixtral, DeepSeek-ViT, Qwen vision
Convolutional encoderConvolutions build a feature hierarchy instead of a stack of self-attention blocks.MobileNet-V5 in Gemma 3n
Convolution and attentionLocal pattern processing and attention appear within the same encoder.LFM3
Restricted and full attentionSome layers connect a smaller neighbourhood; others exchange information more widely.Perception Encoder in Muse Spark
More than the last feature layerIntermediate image features also feed later processing.Qwen3-VL-derived WeVisOne

These encoders can still produce a shorter sequence at the language model’s input width. The diagram above expands the SigLIP example; it does not imply that every vision encoder uses its LayerNorm, GELU, biases or 16-head layout.

Sources and worked example

The worked profiles are Gemma 3 4B, 12B and 27B; the text-only 1B is excluded. All three use the illustrated image path. The picture and learned matrix cells illustrate roles and shapes. One 896-pixel crop produces 4,096 patch positions and 256 pooled positions; image markers and extra crops add sequence positions beyond that count. The resolution comparison is a hypothetical larger crop using the same projection shapes, not a supported Gemma 3 input mode. Pair counts are per head and exclude implementation overhead. The optional position-choice and merging comparisons illustrate proposed alternatives at the selected reference widths; they do not change Gemma 3’s actual learned-grid and averaging path. The position-grid cells are schematic; interpolation does not guarantee accuracy on unseen resolutions. The main diagram follows one image, so it shows bidirectional attention without a decoding KV cache. This article explains input understanding, rather than image generation.

← Browse all subjectsBack to the beginning ↑