#50Per-layer complexity — attention vs recurrenceMediumTransformersRNN
Per-layer complexity — attention vs recurrence
Background
The Vaswani et al. paper compares per-layer cost. For sequence length and representation dim :
So self-attention is cheaper when (typical for sentences); recurrence wins when (very long sequences) — the tension that motivates efficient-attention research.
Problem statement
Implement layer_complexity(n, d) returning (self_attention_ops, recurrent_ops, cheaper).
Input
n— sequence length.d— representation dimension.
Output
Returns (self_attention_ops, recurrent_ops, cheaper):
self_attention_ops = n * n * d,recurrent_ops = n * d * d,cheaper="self-attention"if it has strictly fewer ops, else"recurrent".
Examples
Example 1 — short sentence, big model
Input: n = 10, d = 512
Output: (51200, 2621440, "self-attention")
Example 2 — very long sequence
Input: n = 1000, d = 64
Output: (64000000, 4096000, "recurrent")
Constraints
self_attention_ops = n*n*d,recurrent_ops = n*d*d.cheaperis"self-attention"only when its op count is strictly less; ties go to"recurrent".- Return
(int, int, str).
Notes
- Self-attention is cheaper exactly when . As context length grows past the model width, the term dominates — why long-context efficiency is an active research area.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Short sentence example n=10 d=512
- •Long sequence sample n=1000 d=64
- •Tie reference n=50 d=50