Hybrids Stage Convs Before Attention
Hybrids run convolutions early for local bias and attention late for global range, beating either alone on data efficiency and compute cost.
Why Does This Exist?
Pure CNNs bake in locality and translation equivariance but struggle to relate distant regions without deep stacks, while pure transformers model every pair of patches and therefore need large datasets to learn what convolutions get for free. Hybrids stage the two in order: convolutional layers shrink the maps where attention would be ruinous, then attention runs where the maps are small and global reasoning pays off. Families such as CoAtNet, CvT, and MaxViT differ in the block recipe, not the staging logic, and distilled transformers like DeiT attack the same data hunger from the training side.
This page covers the staging rule and its cost math. Convolution mechanics live in convolutional layers, classification heads in image classification, and pure transformer training in DeiT.
Think of It Like This
Local guides plus satellite view
A survey team walks the streets house by house for detail, then climbs to a hilltop to map how districts connect. Nobody starts from orbit when the street names are unknown, and nobody walks every road when one overlook shows the whole layout. One trip uses both in order.
Convolutions are the street walkers, attention is the hilltop view. Where it stops: swapping the order wastes money, just as running global attention on full-resolution maps burns compute before any detail is organized.
How It Actually Works
Early stages use MBConv or convolutional patch embeddings on large maps such as and , where cost grows linearly in tokens and the inductive bias matters most. Late stages switch to attention on small maps such as and , adding relative position bias or window partitions so the global mixing stays cheap. A pyramid or fused backbone then feeds the detection or classification head. The rule is order, not brand: convs first to compress, attention after to connect.
The savings come from token counts. Attention pairs grow quadratically, so shrinking the map edge by cuts tokens by and pairs by . That is why every hybrid family downsamples with convolutions before the first transformer block.
Worked example
Take a map with channels and a late map. Early tokens number ; a convolution costs about , roughly M multiplies, linear in tokens. Late tokens number ; full attention over them costs pairs, about K. Attention over the early map would cost pairs, about M. Token ratio , pair ratio : staging cuts attention pairs times while keeping early local bias and late global range.
Code
The fixture arithmetic with only the standard library:
early = 56 * 56 # 3136 tokenslate = 14 * 14 # 196 tokensconv_mults = early * 3 * 3 * 64early_pairs = early ** 2late_pairs = late ** 2print(f"tokens {early} vs {late}, ratio {early // late}")# -> tokens 3136 vs 196, ratio 16print(f"conv {conv_mults}, pairs {early_pairs} vs {late_pairs}, ratio {early_pairs // late_pairs}")# -> conv 1806336, pairs 9834496 vs 38416, ratio 256Early convolutions stay linear at about M multiplies while late attention costs K pairs instead of M, a -times saving.
Watch Out For
Running attention before downsampling
Symptom: training crawls and memory explodes while accuracy barely moves, because full attention on maps pays M pairs per layer for relationships convolutions would find cheaply. Keep convolutions first and verify the first attention block sees maps or smaller before scaling up.
Treating every hybrid paper as a new principle
Symptom: the codebase collects one-off CoAtNet, CvT, and MaxViT branches that drift apart, when all three implement convs early and attention late with different blocks. Fix: keep one staging utility and link the block details to their component pages instead of copying each paper here.
The Quick Version
- Hybrids run convolutions on large maps for local bias and attention on small maps for global range.
- Early tokens cost about M linear convolution multiplies at .
- Late tokens cost K attention pairs instead of M, a -times saving.
- Token ratio is and pair ratio is because attention is quadratic in tokens.
- Keep the order convs first and attention after shrinking, or the savings vanish.