AI State of the Art
SOTA/Post-Transformer Architectures/24 min
Section 68

RWKV: Linear RNN at Transformer Scale

Listen as a podcast β†’

The dream of reconciling two opposing worlds

RNNs (recurrent neural networks, including the gated architectures LSTM, Long Short-Term Memory, and GRU, Gated Recurrent Unit) dominated natural language processing for nearly a decade. Their strength: efficient inference with a fixed-size state, like a constant working memory that updates at each word. Their fatal flaw: they can't be trained in parallel. Each token waits for the previous one to be processed. On modern GPUs, designed for massive parallelism, this is catastrophic.

Transformers solved the parallelization problem with attention: all the tokens of a sequence are processed at once, speeding up training by orders of magnitude. But at inference, the price is steep: a KV cache that grows with each generated token, memory that explodes with context length, and a per-token cost that increases instead of staying constant.

RWKV (pronounced "RWaKuV") is the most accomplished attempt to reconcile these two worlds: train in parallel like a Transformer, infer efficiently like an RNN. All backed by a remarkably active open-source community.

RWKV: Reinventing RNNs for the Transformer Era

The four letters of RWKV

The name isn't a mysterious acronym, it describes the four key components of the mechanism, each playing a role analogous to an element of classic attention:

  • R (Receptance): analogous to the Query. A sigmoid gate that controls how much accumulated information to accept into the current output. It's the output filter, it decides what deserves to be communicated.
  • W (Weight): the temporal decay factor. A vector of learned parameters controlling how quickly the model "forgets" past tokens. It's the model's internal clock.
  • K (Key): analogous to attention keys. Represents the "content" of the current token, what it contributes to the collective memory.
  • V (Value): analogous to attention values. Contains the concrete information to propagate to future positions.
Two-part schematic. On the left, the inside of an RWKV block: a LayerNorm feeds a Time Mixing box in which an interpolation labelled mu produces three branches R, K and V; K and V enter a WKV box, R goes through a sigmoid, and the two results are multiplied to give the Out output. Above it, a Channel Mixing box repeats the same pattern with an interpolation mu prime, branches R prime, K prime and V prime, a sigmoid and a multiplication. On the right, the complete residual block stacks LayerNorm, Time Mixing, LayerNorm and Channel Mixing with residual connections, topped by the RWKV-LM Head made of a LayerNorm, an Out layer and a Softmax.
Where the four letters actually live. Inside the Time Mixing box at the bottom left, R, K and V are all three produced from the same input vector: K and V feed the WKV module, while R goes through a sigmoid and acts as a gate on the output. The decay factor W has no box of its own because it is not a projection of the token but a learned parameter hidden inside WKV.Source : RWKV paper by Peng et al. (figure 2), via the RWKV Wiki

The WKV mechanism: RWKV's core

RWKV's Time Mixing replaces attention with the WKV mechanism:

wkvt=βˆ‘iexp⁑(βˆ’(tβˆ’1βˆ’i)β‹…w+ki)β‹…viβ€…β€Š+β€…β€Šexp⁑(u+kt)β‹…vtβˆ‘iexp⁑(βˆ’(tβˆ’1βˆ’i)β‹…w+ki)β€…β€Š+β€…β€Šexp⁑(u+kt)\text{wkv}_t = \frac{\sum_i \exp(-(t-1-i) \cdot w + k_i) \cdot v_i \;+\; \exp(u + k_t) \cdot v_t}{\sum_i \exp(-(t-1-i) \cdot w + k_i) \;+\; \exp(u + k_t)}

The formula looks intimidating, but the intuition is clear:

  • The sum βˆ‘i\sum_i runs over all the tokens already read (positions ii before the current position tt); viv_i is the value carried by token ii, the thing being propagated.
  • exp(-(t-1-i)*w): exponential decay with the distance tβˆ’1βˆ’it-1-i. Recent tokens weigh much more than older tokens, it's automatic and systematic. The pace of that decay is set by w, the learned parameter described above (the W in the name).
  • k_i: each token's "key" modulates its importance. A token with a high key remains influential even from afar.
  • u + k_t: a special bonus for the current token. The learned parameter u is a "freshness premium", the token just read has a systematic advantage.
  • The division normalizes the weights so they sum to 1, like softmax in standard attention.

In one sentence: the output is an average of all past tokens' values, weighted by their key and degraded by their age, with preferential treatment for the current token.

Exponential decay: a deliberate choice

Unlike standard attention where any token can have an arbitrary weight regardless of distance, RWKV imposes a rigid structure: recent tokens count more, exponentially so. It's a strong inductive bias. It helps in the majority of cases (most linguistic dependencies are local), but it can limit performance on tasks that require recalling a precise detail mentioned very far back in the text.

Channel Mixing: replacing the feed-forward

In addition to Time Mixing, each RWKV block contains a Channel Mixing, the equivalent of the Transformer's feed-forward layer:

rt=σ ⁣(Wrβ‹…(ΞΌrβ‹…xt+(1βˆ’ΞΌr)β‹…xtβˆ’1))kt=Wkβ‹…(ΞΌkβ‹…xt+(1βˆ’ΞΌk)β‹…xtβˆ’1)ot=rtβ‹…(Wvβ‹…max⁑(kt,0)2)\begin{aligned} r_t &= \sigma\!\bigl(W_r \cdot (\mu_r \cdot x_t + (1 - \mu_r) \cdot x_{t-1})\bigr) \\ k_t &= W_k \cdot (\mu_k \cdot x_t + (1 - \mu_k) \cdot x_{t-1}) \\ o_t &= r_t \cdot \bigl(W_v \cdot \max(k_t, 0)^2\bigr) \end{aligned}

The symbols: xtx_t and xtβˆ’1x_{t-1} are the representations of the current and previous tokens; WrW_r, WkW_k and WvW_v are learned weight matrices, frozen after training; ΞΌr\mu_r and ΞΌk\mu_k ("mu") are learned mixing coefficients between 0 and 1; Οƒ\sigma is the sigmoid, a function that squashes any number between 0 and 1, perfect for building a "gate"; and max⁑(kt,0)2\max(k_t, 0)^2 zeroes out negative values then squares the rest (a variant of the ReLU activation). The output oto_t is thus a transformation of the token, filtered through the gate rtr_t.

RWKV's distinctive feature is visible here: the token shift. Each layer mixes the current token with the previous token according to learned coefficients (mu_r, mu_k). This mixing gives each layer a minimal local context window of 2 tokens. Stacked across 24 or 32 layers, these micro-windows compose to cover increasingly large spans, somewhat like a convolutional network whose receptive field grows with depth.

Schematic with three columns matching the input tokens My, name and is, which produce the outputs name, is and Bob. Each column stacks, from bottom to top, two LayerNorms, a diamond-shaped Time Mix block, a LayerNorm, a Channel Mix block, then an LM Head, with residual connections drawn as dashed lines. Horizontal arrows labelled States connect the Time Mix block of one column to the Time Mix block of the next. Diagonal arrows labelled Token shift connect the LayerNorm of each column to the block of the next column, both at the Time Mix and the Channel Mix level.
The full architecture unrolled over three tokens. The horizontal arrows labelled States are the recurrent memory travelling from one token to the next through Time Mix. The diagonal arrows labelled Token shift are the mixing with the previous token described just above: every layer already sees a two-token window, and stacking layers widens that reach step by step.Source : RWKV paper by Peng et al. (figure 3), via the RWKV Wiki

The training/inference duality: why it works

Training mode: fully parallel

During training, all tokens in the sequence are known in advance. The WKV formulation can be reformulated to be computed in parallel via tensorized operations, exactly like a matrix multiplication. Each position can be processed simultaneously. No sequential recurrence, the GPU runs at full speed.

Inference mode: fixed state, constant cost

At inference, RWKV transforms into a pure RNN. The recurrent state reduces to only two vectors per layer:

at=exp⁑(βˆ’w)β‹…atβˆ’1+exp⁑(kt)β‹…vtbt=exp⁑(βˆ’w)β‹…btβˆ’1+exp⁑(kt)wkvt=atbt\begin{aligned} a_t &= \exp(-w) \cdot a_{t-1} + \exp(k_t) \cdot v_t \\ b_t &= \exp(-w) \cdot b_{t-1} + \exp(k_t) \\ \text{wkv}_t &= \frac{a_t}{b_t} \end{aligned}

These two vectors read simply: ata_t accumulates the weighted values (the numerator of the big formula), btb_t accumulates the weights alone (the denominator). At each token, both are multiplied by the forgetting factor exp⁑(βˆ’w)\exp(-w), the new token's contribution is added, and the output is their quotient. (The current-token bonus uu is omitted here for readability.)

Each new token is processed in O(1), constant cost: one multiplication, one addition, one division. No KV cache. The state remains fixed-size regardless of context length, 100 tokens or 100,000 tokens, same cost per token.

RWKV's practical advantage

For a chatbot deployed on mobile or on a memory-constrained server, RWKV is ideal. Generation throughput is constant, the 50,000th token is generated as fast as the first. No progressive slowdown, no growing KV cache, no memory threshold forcing context truncation. It's the inference cost predictability that Transformers can't offer.

RWKV-5 (Eagle) and RWKV-6 (Finch): the evolutions

RWKV-5 Eagle: toward multi-head

RWKV-5 introduces several improvements inspired by recent advances in SSMs and attention:

  • Multi-head WKV: like Multi-Head Attention, RWKV-5 organizes the WKV mechanism into multiple heads with distinct decay parameters w. Each head can specialize on a different time scale, one head attentive to adjacent words, another to recent sentences, a third to previous paragraphs.
  • Matrix states: the hidden state goes from a vector to a matrix, significantly increasing representation capacity at the cost of slightly higher memory.
  • Better initialization: inspired by HiPPO work on SSMs, for better long-range dependency capture from the start of training.

RWKV-6 Finch: selectivity arrives

RWKV-6 makes a crucial evolution by drawing direct inspiration from Mamba: making parameters input-dependent.

  • Data-dependent decay: the decay factor w is no longer fixed, it's computed from the current token. An important keyword can be assigned a slow decay rate (it will be remembered for a long time), while a mundane function word will be forgotten quickly.
  • Data-dependent token shift: the mixing coefficients mu become adaptive, adjusting the mix between current and previous token based on context.
Three-part schematic. On the left, a next-generation RWKV block: a Time Mixing containing an interpolation mu, branches G, R, w, K and V, a WKV box annotated with dimension D by D and a LayerNorm, topped by a Channel Mixing with a G prime branch and an MLP. Top right, the same Time Mixing redrawn as a recurrent cell: a hidden state h subscript t minus 1, annotated with dimension D by D, enters from the left, is multiplied by w, added to the KV product which is also D by D, and leaves on the right as h subscript t. Bottom right, two variants of the Token Shift: on the left a simple version where a coefficient mu and its complement one minus mu are shared parameters, on the right a more elaborate version where mu is corrected by two small matrices L1 and L2 separated by a hyperbolic tangent.
Both innovations of Eagle and Finch on a single figure. Top right, the hidden state h is no longer a vector but a D by D matrix: that is the move to matrix-valued states in RWKV-5. Bottom right, the two Token Shift versions sit side by side: the left one uses a fixed mixing coefficient, the right one recomputes it for every token through two small matrices, which is exactly the selectivity introduced by RWKV-6.Source : Eagle and Finch paper by Peng et al. (figure 1), via Semantic Scholar
Convergence with Mamba

RWKV-6 and Mamba independently arrive at the same fundamental conclusion: state transition parameters must be adaptive to achieve optimal performance. RWKV comes from the RNN world and adds selectivity. Mamba comes from the SSM world and adds the same selectivity. This independent convergence is a strong signal that selectivity isn't an implementation detail, it's a fundamental principle.

Available models and performance

RWKV stands out for a remarkably active open-source community that has trained models at impressive scales for a community project:

  • RWKV-4: from 169M to 14B parameters
  • RWKV-5 Eagle: from 400M to 7B parameters
  • RWKV-6 Finch: from 1.6B to 14B parameters

On standard language benchmarks, RWKV positions itself competitively against same-size Transformers. The slight disadvantage appears, unsurprisingly, on very long-range recall tasks, where compressing history into a fixed state penalizes against the exact KV cache of Transformers.

Advantages and limitations in plain terms

What RWKV does well:

  • O(1) inference per token, independent of context length
  • No KV cache: fixed and predictable inference memory
  • Parallel training as fast as a Transformer
  • Very active open-source community with freely available and regularly updated models
What RWKV does less well

Like all recurrent models, RWKV compresses history into a fixed-size state. Precisely retrieving a proper noun or exact number mentioned 5,000 tokens earlier is difficult, the information is there in compressed form, but the fine details are lost. Question-answering tasks on very long documents or few-shot learning with complex examples can suffer compared to a same-size Transformer.

On top of that, two ecosystem drawbacks:

  • Industrial adoption still limited compared to the Transformer ecosystem
  • Fewer optimized inference tools and deployment tooling available

Conclusion

RWKV proves you can build competitive language models at billions of parameters without a quadratic attention mechanism. Its pragmatic approach, taking the best of RNNs (constant inference) and Transformers (parallel training) makes it a serious alternative for deployments where inference efficiency and cost predictability are priorities. The evolution toward selectivity in RWKV-6 confirms the underlying trend: all families of sequential architectures converge toward the same principles, adaptive parameters, multi-head, and contextual gating mechanisms.

Check your understanding0 / 4

1. RWKV promises the best of both worlds: train like a Transformer, infer like an RNN. How does it actually deliver on that?

2. During generation, what memory advantage does RWKV have over a Transformer?

3. Summarizing the whole history into a fixed state has a downside. Which one?

4. What crucial evolution does RWKV-6 (Finch) introduce, converging with Mamba?