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.
★☆☆☆☆ 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. RWKV's memory is…
2. RWKV trains…
3. Different channels have…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
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. WKV output at step 2 (values 2, 4; keys 0; u = 0):
★★★☆☆ 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 versionxk = 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_prevk, 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. Numerical stability in WKV uses…
2. The receptance r acts as…
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. WKV is a form of…
2. One decay rate represents…
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
B. Peng et al. (2023). “RWKV: reinventing RNNs for the Transformer era.” Findings of EMNLP.
B. Peng et al. (2024). “Eagle and Finch: RWKV with matrix-valued states and dynamic recurrence.” arXiv:2404.05892.
S. Zhai et al. (2021). “An attention free Transformer.” arXiv:2105.14103.
Because the weight e^(−(t−1−i)w) factorises over steps, WKV’s numerator and denominator obey a_t = e^(−w)a_(t−1) + e^(k_t)v_t and b_t = e^(−w)b_(t−1) + e^(k_t).
Recall R² at delay 20: one decay 0.17, eight decays 0.81; recurrence = parallel form to 4 × 10⁻¹⁶.
Practice
Show:
Level 0
Problem 1Level 0
Year RWKV appeared (EMNLP Findings)?
2023.
Problem 2Level 0
WKV output at step 2 for values 2, 4 (keys 0, u = 0)?
3.
Problem 3Level 0
Largest RWKV-4 model size, billions of parameters?
14.
Level 1
Problem 4Level 1
Weight at lag 10 with w = 0.1 (4 decimals)?
e^(−1) = 0.3679.
Problem 5Level 1
Mean look-back 1/(e^0.1 − 1) (2 decimals)?
9.51.
Problem 6Level 1
Token shift with μ = 0.25, x_t = 4, x_(t−1) = 2?
2.5.
Problem 7Level 1
Per-token work ratio of attention to RWKV at position 8192?
8192.
Level 2
Problem 8Level 2
Eight-decay recall R² at delay 20 in exercise 2?
0.814.
Problem 9Level 2
One-decay recall R² at delay 20?
0.171.
Problem 10Level 2
Eight-decay recall R² at delay 50?
0.38.
Problem 11Level 2
Eight-decay recall R² at delay 5?
0.96.
Problem 12Level 2
Decay rates in the multi-timescale model?
8.
Problem 13Level 2
rwkv_state_size(32, 4096)?
655360.
Level 3
Problem 14Level 3
Prove the WKV recurrence for the numerator.
Let a_t = Σ_(i≤t) e^(−(t−i)w + k_i) v_i (weights as seen from step t + 1). Then a_t = Σ_(i≤t−1) e^(−w)·e^(−(t−1−i)w + k_i) v_i + e^(k_t)v_t = e^(−w)a_(t−1) + e^(k_t)v_t. The output at step t + 1 uses a_t together with the current token’s bonus term. The denominator is identical with v ≡ 1. ∎
Problem 15Level 3
Explain why one decay could not recall 20 steps back.
A single exponential average mixes all past inputs with fixed weights, dominated by the most recent ~10 tokens for w = 0.1. Inputs 20 steps back carry little weight and are inseparable from their neighbours, so no linear readout of one channel can isolate them. With several decays, differences between channels act like band-pass filters that isolate specific lags. ∎
Problem 16Level 3
Compare RWKV with Mamba.
Both are linear recurrences trainable in parallel with constant-memory inference. RWKV-4 uses fixed per-channel decays with key weighting (positional relevance); Mamba uses input-dependent steps (content-based selection), which helps tasks requiring selective memory. RWKV-6 moved toward data-dependent decay, narrowing the gap. ∎
Problem 17Level 3
Advise on an on-device assistant for maintenance technicians.
A small RWKV or Mamba model gives constant-memory generation suitable for tablets offline. Quantise to 4–8 bits, pair with retrieval from manuals for exact facts (since recurrent states compress context), and evaluate on real technician queries for accuracy and latency. ∎
Level 4
Problem 18Level 4
Eight-decay recall R² at delay 50 with seed 2?
0.493.
Problem 19Level 4
One-decay recall R² at delay 5?
0.82.
Problem 20Level 4
Decay w of the single-channel model?
0.1.
Coding practice
🐍 Code 1 (Level 0): decay weight
e^(−lag·w).
🐍 Code 2 (Level 0): effective context
1/(e^w − 1).
🐍 Code 3 (Level 0): state size
About 5·layers·d_model.
🐍 Code 4 (Level 0): token shift
μx_t + (1 − μ)x_(t−1).
🐍 Code 5 (Level 0): per-token cost
(T·d, d).
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
RWKV: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.