Inspect any ONNX model in your browser
← All answersVision transformers

How does self-attention differ from a traditional convolutional layer operation?

Short answer

A convolution combines a pixel with its immediate neighbors inside a small fixed window, using the same learned filter everywhere (local and static). Self-attention lets every token compare itself to every other token and weight them by relevance, with those weights computed on the fly from the data (global and dynamic). Convolutions bake in locality; attention learns which relationships matter.

The CLIP image-search tutorial shows where transformer attention becomes useful in practice: turning images and text into embeddings that can be compared semantically.

Convolution: local and fixed

  • Sees only a small neighborhood (e.g. 3x3) at a time.
  • Uses the same filter weights at every position (translation-equivariant, parameter-efficient).
  • Long-range relationships only emerge after stacking many layers.

Self-attention: global and dynamic

  • Every token can attend to every other token in a single layer.
  • Attention weights are computed from the input (query-key similarity), so they change per image.
  • Captures long-range dependencies immediately, at a compute cost that grows with the square of the number of tokens.

Neither is strictly better. Convolutions have a strong, useful bias (nearby pixels are related) that makes them data-efficient. Attention has almost no built-in bias, so it's more flexible but needs more data to discover structure. That trade-off is why hybrid designs, which combine both, are so popular.

Side-by-side comparison

Property3x3 convolutionGlobal self-attention
What it mixesLocal 3x3 neighborhood per layerEvery image token with every other token
WeightsLearned filter shared across locationsInput-dependent attention scores from queries and keys
Long-distance contextBuilds with depth or larger kernelsAvailable in one global attention layer
Scaling with image sizeApproximately linear in spatial positions for fixed kernel sizeQuadratic in token count for full attention
Useful biasTranslation equivariance and local structureContent-adaptive relationships across distant regions

A concrete image example

Consider a 224x224 image split into 16x16 patches: it produces 196 tokens. A global attention layer can compare each token with all 196 tokens, or 38,416 query-key pairs per attention head. By contrast, a single 3x3 convolution samples at most nine spatial locations per output position. This is a comparison of connectivity, not a fair speed benchmark: patch embedding, channels, heads, implementations and memory access all affect runtime.

Real vision architectures blur this distinction: windowed attention can avoid full quadratic scaling, and deep or dilated convolutions can cover long distances. The useful question is whether the task needs content-adaptive global interactions enough to justify their cost.

In practice

This trade-off is exactly why YOLO still dominates production detection: its convolutional backbone gets strong accuracy from far less data and runs fast on modest hardware. When I do want attention in a real system, I add it in small doses - an attention block, or a hybrid backbone - rather than going full transformer, which usually costs latency I can't spare on a live camera feed.