Foundations to Frontier
Tests
E. Deep Learning › E17 State-Space and Recurrent-Revival Models

RWKV

RWKV (Peng and colleagues, EMNLP Findings 2023) reinvents the RNN for the Transformer era. Its time-mixing block computes WKV, an attention-like average of past values weighted by exponentially decaying factors and learned keys, with a bonus for the current token. That average can be written in parallel, like attention, for training, or as a recurrence with constant memory for inference. RWKV was scaled to 14 billion parameters with quality close to Transformers of similar size, and later versions (Eagle, Finch) added matrix-valued states and data-dependent decay. This page covers WKV, token shift, channel mixing and the decay–memory trade-off. In the exercises you implement token shift and the WKV step, check that the recurrence equals the parallel formula, and measure recall. The two forms agreed to 4 × 10⁻¹⁶. One decay rate recalled inputs from 20 steps back with R² 0.17, while eight different decay rates reached 0.81.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

An RNN that trains like a Transformer

RWKV keeps a running, slowly fading average of past information, weighted by how relevant each token looks (its key). Because the fading is a fixed exponential, the same quantity can be computed all at once during training (fast, like attention) or step by step during generation (cheap, like an RNN). Different channels fade at different speeds, so some remember only the last few tokens and others look far back. The demo shows how a decay rate sets the effective memory length.

State-space lab
Past tokens weighted by γ^k; effective memory about 1/(1 − γ) = 10 tokens. RetNet uses different γ per head to mix short and long memories.
Why engineers care

RWKV models generate text and process streams with constant memory and simple kernels, which makes them attractive for edge devices and long-running services.

📝 Quick check (Level 0)
0/3 answered
  1. 1. RWKV's memory is…
  2. 2. RWKV trains…
  3. 3. Different channels have…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • WKV for one channel: wkv_t = [Σ_(i < t) e^(−(t−1−i)w + k_i) v_i + e^(u + k_t) v_t] / [Σ_(i < t) e^(−(t−1−i)w + k_i) + e^(u + k_t)].
    • w > 0 is the decay, u the bonus for the current token, and k the keys.
  • Recurrence: keep a numerator a and a denominator b.
    • out = (a + e^(u+k_t) v_t)/(b + e^(u+k_t));
    • then a ← e^(−w)a + e^(k_t)v_t and b ← e^(−w)b + e^(k_t).
    • With keys 0, values 2, 4, 6, w = 0.5 and u = 0, the outputs are 2, 3, then (2e^(−0.5) + 4 + 6)/(e^(−0.5) + 2).
  • Weights on the past: a token “lag” steps before the previous one gets e^(−lag·w); e^(−1) for w = 0.1 at lag 10. The mean look-back is 1/(e^w − 1) ≈ 9.5 tokens.
  • Token shift: x’t = μx_t + (1 − μ)x(t−1), a cheap 2-token mix before projections. With μ = 0.25, inputs 4 and 2 give 2.5.
  • Cost: per generated token, attention at position T does about T·d work, while RWKV does about d.
📝 Quick check (Level 1)
0/3 answered
  1. 1. WKV output at step 2 (values 2, 4; keys 0; u = 0):
  2. 2. Weight at lag 10 with w = 0.1:
  3. 3. Token shift with μ = 0.25, x_t = 4, x_(t−1) = 2:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

RWKV in code

Exercise 1.

  • Token shift mixed neighbouring tokens.
  • The WKV step produced the expected outputs and states over three tokens.
  • Past weights decayed exponentially.

Practice.

  • The official RWKV-LM repository and the rwkv pip package provide models and CUDA kernels.
  • Implementations keep a running maximum exponent for numerical stability.
  • Channels get decays spread across scales at initialisation. Use the parallel (chunked) form for training and the recurrent form for inference.
# recurrent RWKV-4 time mixing for one token (vectors over channels), numerically naive version
xk = mu_k * x + (1 - mu_k) * x_prev; xv = mu_v * x + (1 - mu_v) * x_prev; xr = mu_r * x + (1 - mu_r) * x_prev
k, v, r = Wk @ xk, Wv @ xv, torch.sigmoid(Wr @ xr)
wkv = (a + torch.exp(u + k) * v) / (b + torch.exp(u + k))
a = torch.exp(-w) * a + torch.exp(k) * v; b = torch.exp(-w) * b + torch.exp(k)
out = Wo @ (r * wkv)
🐍 Exercise 1: token shift and WKV
Implement RWKV's token shift, one WKV recurrence step and the weight on past tokens.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Numerical stability in WKV uses…
  2. 2. The receptance r acts as…
  3. 3. Inference uses…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Linear attention with decay.

  • WKV is softmax-free attention: past values weighted by e^(k_i) times a fixed positional decay e^(−(t−1−i)w), normalised by the total weight.
  • Because the decay factorises across time steps, the sums obey a two-number recurrence. This is the same reason linear attention and SSMs can be run as RNNs.

Timescales. Each channel’s memory is set by its decay w: small w averages over many tokens, large w focuses on the last few. A single decay can only represent one timescale, so information from far back is blurred together with everything in between. Many channels with spread-out decays form a multi-scale basis, like an SSM with many modes.

Limits and successors.

  • Fixed decay means relevance is mostly positional plus key weight, which is weaker than content-based softmax attention for precise recall.
  • RWKV-5 (Eagle) uses matrix-valued states (outer products of keys and values); RWKV-6 (Finch) makes the decay data-dependent, moving toward selective models like Mamba.
📝 Quick check (Level 3)
0/3 answered
  1. 1. WKV is a form of…
  2. 2. One decay rate represents…
  3. 3. RWKV-6 adds…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Is RWKV’s recurrence exact, and how many timescales does it need?

History.

  • B. Peng et al., “RWKV: reinventing RNNs for the Transformer era” (Findings of EMNLP 2023).
  • B. Peng et al., “Eagle and Finch: RWKV with matrix-valued states and dynamic recurrence” (2024).
  • Related: the Attention Free Transformer (Zhai et al., 2021).

Exercise 2.

  • Equivalence: WKV is computed with the explicit attention-like formula (O(T²)) and with the recurrence (O(1) state), on random keys and values (w = 0.3, u = 0.5).
  • Recall: a smooth input is passed through WKV channels with one decay (w = 0.1) or eight decays (0.005 to 2). A linear readout of the channel outputs is fitted to recall the input from 5, 20 or 50 steps ago.

Recurrence versus parallel formula: maximum difference 4 × 10⁻¹⁶.

WKV channels recall R², delay 5 delay 20 delay 50
one decay (w = 0.1) 0.820 0.171 −0.074
eight decays (0.005 to 2) 0.960 0.814 0.380
  • The recurrence is exact: constant-memory inference computes precisely the parallel training formula.
  • One timescale forgets selectively: a single decaying average blends everything, so inputs 20 or 50 steps back cannot be separated (R² 0.17 and below 0).
  • Several timescales recall much further: differences between channels with different decays isolate older inputs, giving 0.81 at 20 steps and 0.38 at 50. This is why RWKV spreads decays across channels. Seeds 1 and 2 gave 0.80/0.43 and 0.84/0.49 for eight decays.
🐍 Exercise 2 (research): is RWKV's recurrence exact, and how many timescales does it need?
Check WKV's recurrent and parallel forms agree, and compare recall with one versus eight decay rates.

RWKV in AI, robotics and aerospace

  • Edge language models: constant-memory generation on embedded hardware.
  • Streaming analytics: running averages with learned keys over continuous telemetry.
  • Design lesson: a recurrence with fixed decays covers only the timescales its channels span. Spread them on purpose.

References

  1. B. Peng et al. (2023). “RWKV: reinventing RNNs for the Transformer era.” Findings of EMNLP.
  2. B. Peng et al. (2024). “Eagle and Finch: RWKV with matrix-valued states and dynamic recurrence.” arXiv:2404.05892.
  3. S. Zhai et al. (2021). “An attention free Transformer.” arXiv:2105.14103.