Spatio-Temporal Transformers
Video is space plus time, and attention can span both — at quadratic cost. Spatio-temporal transformers factor the attention so frames talk within and across time affordably.
Why Does This Exist?
3-D convolutions see locally in time; full space-time attention sees everything at impossible cost. A modest clip ( frames, patches each) holds tokens, and joint attention prices million comparisons per head per layer — before going long. Video needed attention's range without its bill.
Spatio-temporal transformers factor the operation: temporal attention per spatial position, then spatial attention per frame (TimeSformer's divided scheme), or tubelet embeddings plus factorized encoders (ViViT). Cross-frame reasoning survives at a fraction of the price. The pretrained users are video foundation models; the live users are streaming models.
The one idea you need first. Divided attention runs two cheaper passes instead of one giant one: each patch first attends across time at its own position ( per patch), then across space within its frame ( per frame). Information still flows from any patch to any other — via a temporal hop plus a spatial hop — while the comparison count drops roughly eightfold.
Think of It Like This
Breakout rooms, then plenary
A -person conference cannot hold one conversation: all-to-all talk is noise at quadratic cost. Organizers split time into breakout rooms (local groups converse cheaply), then plenary (each room reports, everyone syncs). Every idea still reaches every ear — via room plus stage instead of direct shout.
Divided attention is that schedule. Temporal attention is the breakout (each position's timeline converses); spatial attention is the plenary (each frame syncs). Joint attention insists on the single giant hall and pays the noise bill; factorization keeps the reach and drops the price.
How It Actually Works
Tokenize frames into patches (or spacetime tubelets spanning frames to halve counts). Alternate temporal and spatial blocks: temporal first lets motion features form before spatial mixing (the TimeSformer order that ablates best), with learned space-time positional encodings keeping coordinates straight. Classification pools a class token or averages; dense tasks keep full resolution. Long-video variants add windowed, axial, or memory-compressed attention on top of the divided base.
Complexity per layer drops from to — same reach, scheduled sensibly.
Worked example
frames, patches: tokens.
- Joint: M comparisons.
- Divided temporal: per patch ; over patches .
- Divided spatial: per frame ; over frames .
- Total: M. Ratio: .
Nearly eightfold cheaper per layer, every layer, compounding through depth — the margin that makes video transformers trainable.
Code
The point of this snippet is the bill: joint versus divided counts reproduce the margin.
def attention_bills(frames: int, patches: int) -> tuple: joint = (frames * patches) ** 2 divided = frames**2 * patches + patches**2 * frames return joint, divided
joint, divided = attention_bills(8, 196)print(joint, divided) # -> (2458624, 319872)print(round(joint / divided, 1)) # -> 7.7Watch Out For
Diagonal motion the factorization blurs
Divided passes mix time-then-space, so fine diagonal trajectories (motion plus position changing together) cross the factorization awkwardly — each pass sees half the pattern. Small fast objects suffer most, exactly where tracking needs them sharp.
Check motion-heavy slices when choosing factorization; tubelet embeddings (spanning time at tokenization) recover part of the diagonal. Joint attention on short clips remains the accuracy reference.
Positions that expire past the horizon
Learned space-time encodings memorize training lengths; feeding longer videos at inference scrambles coordinates the model never learned. Performance cliffs at the pretraining horizon, silently.
Use relative or extrapolatable encodings for long targets, and test past-horizon explicitly before claiming length generalization. The cliff sits exactly where demos stop measuring.
The Quick Version
- Spatio-temporal transformers factor attention into temporal-then-spatial passes at .
- The worked fixture prices divided at M versus joint M: cheaper per layer.
- Reach survives via two-hop paths (temporal plus spatial) instead of direct all-to-all.
- Diagonal fast motion crosses factorizations awkwardly; verify on motion-heavy slices.
- Learned positions expire past training horizons, so test long inputs explicitly.