9.6 RNN language model with stateful TBPTT
The n-gram model and Bengio’s neural probabilistic language model predict the next token from a fixed window: whatever happened 20 characters ago is invisible to them. The recurrent layers of this chapter, the vanilla RNN, the LSTM, and the GRU, have a state that can, in principle, carry information across any distance. In this section you turn them into a language model and train it the way recurrent LMs were trained in practice: on one long stream, with a state that persists across training windows.
Concepts from other sections (14)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Bits per byte (bpb): , total information divided by the UTF-8 length; comparable across tokenizers. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- Gated recurrent unit (GRU): a recurrent cell with one state and two gates, . Read more: 9.3 GRU (torch gate order)
- biLM: a forward and a backward language model over the same sentence, sharing the embedding and the softmax, trained separately in each direction. Read more: 9.5 ELMo: biLM, ScalarMix, linear probes
- safetensors: a checkpoint format of an 8-byte header length, a JSON header, and raw little-endian row-major tensor bytes. Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- Backpropagation through time (BPTT): reverse-mode differentiation of the recurrence unrolled over time, with weights shared across steps. Read more: 9.1 Vanilla RNN with manual BPTT and truncation
- embedding table: the matrix whose row is the learned vector for token , shared across every window. Read more: 8.2 Bengio’s neural probabilistic language model
- Negative log-likelihood (NLL): , the surprise of the true token under the model, in nats. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- global gradient norm: the square root of the sum of squares of every gradient entry across all parameters. Read more: 6.6 Training loop and the autograd bigram
- Gradient norm: a measure of update magnitude before optimizer-specific scaling. Read more: 17.5 Training failures, profiling, and recovery
- Gradcheck: a comparison of a hand-written gradient against central differences of . Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- Entropy: , the average surprise of a distribution. Read more: 17.1 Entropy, cross-entropy, KL, JS, and the k3 estimator in Mathematics
- Dropout: randomly zeroing activations during training to approximate an ensemble of subnetworks. Read more: Chapter 2: Deep Learning
- Regularization: penalizing model complexity to trade bias for lower variance. Read more: Chapter 1: Statistical Learning
The result is the first model in the model zoo whose context is not bounded by its architecture, and its bits per byte can be compared directly with the NPLM’s on the same text; the zoo trains rnnlm checkpoints and reports them next to the n-gram and the transformer. At the end of this chapter, {tinyllm} train rnnlm --cell {rnn,lstm,gru} trains all three cells, with the LSTM and GRU reaching the calibrated bar and the plain RNN worse than the LSTM. ELMo’s biLM, in the optional previous section, is two of these language models, one per direction.
This section pulls together most of what came before. The Elman layer reuses rnn_forward and rnn_backward from the vanilla RNN section, and the other cells are the LSTM and GRU modules. From Part 0 it uses train_step from Training loop and the autograd bigram, cross_entropy from Losses with a fused backward, Embedding and Linear from the module system, the op library, Tensor, from_op, and no_grad from Tensor, broadcasting backward, and no_grad, and the safetensors I/O of Safetensors for every dtype, atomic checkpoints, and the token stream. The learning test encodes its corpus with the CharTokenizer of The tokenizer protocol and the char tokenizer. From Mathematics it uses xavier_uniform (Variance propagation and initialization), orthogonal_init (Orthogonality, Householder QR, and orthogonal initialization), PCG32 (Modular arithmetic, hashing, and PCG32), and AdamW (Adam and AdamW), with gradient clipping and bits per byte as background reading.
The solution is rnnlm.py: ElmanRNN (the vanilla RNN’s forward and backward as one autograd op), RNNLM (forward, init_state, detach_state, nll, generate), tbptt_batches, train_tbptt, save_rnnlm, and load_rnnlm. Its contract is rnnlm.pyi and its property tests are test_rnnlm.py.
Learning objectives
- Build an RNN language model with a pluggable cell, and wrap the hand-written BPTT as one fused autograd op.
- Cut a stream into contiguous lanes and windows so the carried state always belongs to the same text.
- Implement stateful truncated BPTT: carry the state’s value across windows, detach its graph, reset at each epoch, clip every step.
- Evaluate a long held-out stream in chunks with a result independent of the chunk size, and sample from the model with a seeded generator.
Three cells, one interface
Section titled “Three cells, one interface”The notation:
| Symbol | Meaning | Type / shape |
|---|---|---|
| , , | vocabulary size, embedding width, hidden width | int |
| token id at position | int | |
| embedding table | float32[V, d_e] | |
| recurrent state after reading ( and for an LSTM) | float32[n_layers, B, d_h] | |
| , | read-out | float32[V, d_h], [V] |
| stream length | int | |
number of lanes (batch) | int | |
| lane length | int | |
| window length (truncation) | int | |
| bits per character: mean NLL in nats divided by | float |
An RNN language model is an embedding, a recurrent layer, and a linear read-out:
cell is the LSTM, the GRU, or ElmanRNN, all with the same forward(x, state) -> (out, state). ElmanRNN is the vanilla in torch’s parameter names. Its forward over a whole sequence is one autograd node built with from_op: forward calls rnn_forward, backward calls rnn_backward on the saved cache. That is how cuDNN runs recurrent layers (one fused kernel per layer, not one graph node per time step), and it makes the hand-written BPTT of the vanilla RNN section the engine of a real model. Two details matter: the vanilla RNN uses , so the weight gradients are transposed back; and the two biases are summed in the forward, so each receives the full bias gradient.
Stateful training
Section titled “Stateful training”With the model fixed, the question is how to train it on a long text. Backpropagating through it in one piece would need memory proportional to its length. Truncated BPTT runs windows of tokens, and the key choice is what state starts each window. A stateless trainer starts from zeros every time. That is simple, but the model never sees context from before the window, so it cannot learn dependencies longer than . A stateful trainer starts from the state the previous window ended with. The forward pass sees unbounded context; only the gradient is truncated at the window start.
Stateful training needs the state to be a value at the boundary: detach_state keeps the arrays and drops the graph, so backward on window stops there instead of running on into window ‘s graph, whose gradients were already applied. The carried state is detached, not reset.
Lanes and windows
Section titled “Lanes and windows”For the carried state to make sense, row of window must continue row of window . So the stream is cut into contiguous lanes, lane = stream[b L : (b + 1) L] (the tail is dropped), and window is columns of every lane as inputs, shifted by one as targets. A window needs tokens, so the windows start at while . After the last window the lanes start over and the state is reset to zeros: carrying the end of a lane into its own beginning would condition text on what never precedes it.
Clipping, evaluation, and sampling
Section titled “Clipping, evaluation, and sampling”The exploding gradients diagnosed in the vanilla RNN section are real in training: one bad window can produce a gradient that throws the weights far away. train_tbptt passes clip to train_step, which rescales the global gradient norm to at most clip before every step.
nll(ids) scores a held-out stream in one lane, chunk by chunk under no_grad, carrying the state, so its result does not depend on the chunk size. The mean divided by is bits per character, and bits per byte with the NLL accumulator. generate feeds each sampled token back in, drawing from PCG32(seed) exactly as the autograd bigram’s sampler does.
Working it by hand
Section titled “Working it by hand”Take the stream , lanes, and . Then : lane 0 is and lane 1 is . A window needs tokens, so it starts at ( would need token 12 of a 10-token lane). The three windows are:
| window | inputs | targets |
|---|---|---|
| 0 | [[0 1 2] [10 11 12]] | [[1 2 3] [11 12 13]] |
| 1 | [[3 4 5] [13 14 15]] | [[4 5 6] [14 15 16]] |
| 2 | [[6 7 8] [16 17 18]] | [[7 8 9] [17 18 19]] |
The state after reading 0 1 2 starts the read of 3 4 5: lane 0 continues itself. Tokens 9 and 19 are only targets. With interleaved lanes (reshape(L, B).T), lane 0 would be and the state would carry across text that is not contiguous. A correct tbptt_batches returns exactly these three windows.
Designing the interface
Section titled “Designing the interface”The module’s public surface:
class ElmanRNN(Module): def __init__(self, d_in, d_h, num_layers=1, rng=None): ... def forward(self, x, state=None, lengths=None) -> tuple[Tensor, Tensor]: ...class RNNLM(Module): def __init__(self, vocab, d_emb, d_h, cell, n_layers=1, rng=None): ... def forward(self, ids, state=None) -> tuple[Tensor, Any]: ... # ids [B, T] -> logits [B, T, V] def init_state(self, batch): ...; def detach_state(self, state): ... def nll(self, ids, chunk=256) -> NDArray: ...; def generate(self, prefix, n, temperature, seed) -> list[int]: ...def tbptt_batches(stream, k, batch) -> list[tuple[NDArray, NDArray]]: ...def train_tbptt(model, stream, k, batch, opt, clip, steps) -> list[float]: ...def save_rnnlm(model, dir, tokenizer="bytes") -> None: ...; def load_rnnlm(dir) -> RNNLM: ...The solution rests on properties that hold for any seed: two calls with a carried state equal one call; nll does not depend on the chunk; and, with an optimizer that only records gradients (a small class with zero_grad and step), step 2’s gradients equal those of window 2 from a constant state, the step len(windows) loss equals step 0’s, and every recorded gradient norm is at most clip. The model suite test_rnnlm.py writes these as tests against the contract only, together with the hand-worked windows, the Elman layer against rnn_forward, greedy generation, and a save and load.
Building it step by step
Section titled “Building it step by step”Six steps of reasoning fix the design.
- The state carried between windows must belong to the same text. So row of every window must be the continuation of row of the previous one. That forces contiguous lanes,
stream[:BL].reshape(B, L), and windows as column slices of that matrix. - A window of inputs needs tokens. Targets are inputs shifted by one. Starts are with , which is
range(0, L - k, k). In the worked example , gives starts . - Carry the value, cut the graph. The next window’s forward must start from the previous window’s final state (stateful), but its backward must stop there, because the previous window’s gradients have already been applied.
Tensor.detach()gives exactly that: same array, no parents. - Reset at the epoch boundary. After the last window the lanes restart from their beginnings, so the carried state (from the lane’s end) does not precede that text. Starting from zeros is the honest choice.
- The Elman layer can reuse the vanilla RNN wholesale.
rnn_forward/rnn_backwardalready compute a full sequence and its BPTT. Wrapping them as onefrom_opnode needs only two conversions: torch stores , as the transposes of the vanilla RNN’s , , and torch’s two biases enter as their sum, so each gets the full bias gradient. - Evaluation must be chunk-invariant. Scoring a long held-out stream in chunks is fine as long as the state flows from chunk to chunk; then the result equals one pass, whatever the chunk size.
Lanes and windows
Section titled “Lanes and windows”def tbptt_batches(stream, k, batch): s = np.asarray(stream) # ... (1-D integer stream, k >= 1, batch >= 1) L = len(s) // batch if L < k + 1: raise ValueError(f"each of {batch} lanes has {L} tokens; a window needs k + 1 = {k + 1}") # Lane b is one contiguous stretch of the stream, so the state that lane b # carries from window j into window j + 1 belongs to the same text. lanes = s[: L * batch].astype(np.int64).reshape(batch, L) # tail N - BL dropped return [(lanes[:, p : p + k], lanes[:, p + 1 : p + k + 1]) for p in range(0, L - k, k)]reshape(batch, L) is the whole point: with reshape(L, batch).T lane 0 would be (see the interleaved-lanes warning below).
The training loop
Section titled “The training loop”The loop keeps the carried state in a small dict that loss_fn closes over, because train_step calls loss_fn(model, batch) and owns zero_grad, backward, clipping and the optimizer step:
def train_tbptt(model, stream, k, batch, opt, clip, steps): # ... (steps >= 0) windows = tbptt_batches(stream, k, batch) carry = {"state": None}
def loss_fn(m, b): logits, state = m(b["x"], carry["state"]) # stateful: start from the last window's end # Keep the values, cut the graph: the next window starts here. carry["state"] = m.detach_state(state) V = logits.shape[-1] return cross_entropy(F.reshape(logits, (-1, V)), b["y"].reshape(-1))
losses = [] model.train() j = 0 while len(losses) < steps: if j == 0: carry["state"] = None # a new epoch starts from zeros x, y = windows[j] losses.append(train_step(model, {"x": x, "y": y}, loss_fn, opt, clip=clip)["loss"]) j = (j + 1) % len(windows) # wrap: the next epoch return lossesDetaching happens inside the forward of window , before window ‘s backward. That is safe because the detached copy is only used by window ; window ‘s own graph still runs through the undetached state. detach_state recurses into the LSTM’s (h, c) tuple:
def detach_state(self, state): if isinstance(state, tuple): return tuple(self.detach_state(s) for s in state) # LSTM: (h, c) return state.detach() if isinstance(state, Tensor) else stateThe Elman layer as one autograd node
Section titled “The Elman layer as one autograd node”def _rnn_layer(x, h0, w_ih, w_hh, b_ih, b_hh): """One Elman layer over the whole sequence as a single autograd op.""" # L3.1 takes Wxh = W_ih^T, Whh = W_hh^T and one bias. h, cache = rnn_forward(x.data, h0.data, w_ih.data.T, w_hh.data.T, b_ih.data + b_hh.data)
def vjp(g): d = rnn_backward(g, cache) # L3.1's BPTT over all T steps db = d["bh"].astype(b_ih.dtype) # torch's weights are the transposes of L3.1's Wxh and Whh; the two # biases are summed in the forward, so each gets the same gradient. return (d["x"].astype(x.dtype), d["h0"].astype(h0.dtype), d["Wxh"].T.astype(w_ih.dtype), d["Whh"].T.astype(w_hh.dtype), db, db.copy())
return from_op(h.astype(x.dtype), (x, h0, w_ih, w_hh, b_ih, b_hh), vjp, "elman_rnn")ElmanRNN.forward stacks layers by feeding each layer’s output sequence to the next, and returns the last time step of each layer as h_n, shape [n_layers, B, d_h], the same convention as torch, the LSTM, and the GRU. It rejects lengths, because a language-model stream has no padding.
The model
Section titled “The model”The model itself is the three equations of the language model, with a transpose on each side because the recurrent layers are time first:
def forward(self, ids, state=None): a = self._ids(ids) B, T = a.shape if state is None: state = self.init_state(B) # zeros; (h, c) for an LSTM x = self.emb(a.T) # time first for the recurrent layer: [T, B, d_emb] out, new_state = self.rnn(x, state) logits = self.out(F.transpose(out, 0, 1)) # back to batch first: [B, T, V] return logits, new_stateEvaluation, sampling, and checkpoints
Section titled “Evaluation, sampling, and checkpoints”Chunked evaluation carries one state across all chunks and computes a stable log-softmax in float64:
def nll(self, ids, chunk=256): a = np.asarray(ids) # ... (1-D, at least 2 ids, chunk >= 1) out = np.empty(len(a) - 1, dtype=np.float64) state = None with no_grad(): for s in range(0, len(a) - 1, chunk): inp = a[s : min(s + chunk, len(a) - 1)] # the last id is only a target logits, state = self.forward(inp[None, :], state) z = logits.data[0].astype(np.float64) m = z.max(axis=-1, keepdims=True) ls = z - m - np.log(np.exp(z - m).sum(axis=-1, keepdims=True)) tgt = a[s + 1 : s + 1 + len(inp)] out[s : s + len(inp)] = -ls[np.arange(len(inp)), tgt] return outgenerate runs the prefix once, then feeds back each chosen token with the carried state: greedy (argmax) at temperature 0, otherwise the autograd bigram’s inverse-CDF draw on one rng.uniform() per token from PCG32(seed). save_rnnlm writes config.json (architecture, cell, sizes and the tokenizer name) and model.safetensors with torch’s key names from state_dict(); load_rnnlm rebuilds the model from the config and loads strictly. The full solution is rnnlm.py.
Why this design
Section titled “Why this design”Each step costs time and activation memory, independent of the stream length . Full BPTT would need memory per lane.
Stateless windows would be simpler (no carry, no detach) but cap the usable context at . Stateful training gives unbounded forward context for the same memory; only the gradient horizon is . Overlapping (sliding) windows would score every token with long context but cost times more compute per epoch; non-overlapping windows plus a carried state get most of the context at no extra cost. And building the Elman layer from per-step Tensor ops would also work, but creates graph nodes per layer; the fused op mirrors cuDNN and reuses already-verified BPTT.
The tradeoff accepted is truncation itself: the gradient never connects a token to context more than steps back, so long dependencies can be used (via the carried state) but are learned only indirectly.
What can go wrong
Section titled “What can go wrong”The checks a correct solution passes:
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| hand example: windows | unit | the three hand-worked windows | the lanes the state flows along |
| windows drop the tail and validate | boundary | the tail is dropped; too-short lanes raise | no empty epochs |
| Elman layer is one fused op over the vanilla RNN | differential | output and every gradient equal rnn_forward and rnn_backward | the hand-written BPTT drives a real model |
| gradcheck of the whole model | gradcheck | every parameter of a 2-layer model, Elman and LSTM cells, float64 | the model trains through all its layers |
| state carries across calls | property | two calls with the carried state equal one call | the definition of stateful |
| gradient is cut at the window boundary | differential | step 2’s gradients equal window 2 alone from a constant state | truncation is where it is meant to be |
| state flows into the next window | differential | step 2’s loss equals the run from window 1’s state, not from zeros | context crosses windows |
| epoch wraps and resets the state | unit | step len(windows) repeats step 0 exactly | each epoch starts clean |
| clip is applied | unit | the optimizer sees a gradient norm of at most clip | the guard against explosions |
| NLL does not depend on the chunk | property | chunk 1, 7, 256 agree and equal one pass | evaluation of long held-out text |
| generate is seeded, greedy is argmax | unit | same seed, same ids; greedy feeds back the argmax | {tinyllm} generate |
| save and load roundtrip | unit | config.json, torch key names, identical logits | the zoo’s checkpoint contract |
| validation | boundary | unknown cell, bad ids, lengths for the Elman layer, negative steps | caller bugs fail loudly |
| LSTM learns characters | learning | 150 steps: held-out bpc at the reference bar and 0.5 below the unigram entropy | the model learns |
The four mistakes that break stateful training:
Warning: If the carried state is not detached, backward runs into the previous window’s graph again and gradients double-count.
loss_fnstoresm.detach_state(state)incarry.
Warning: “Detaching” by resetting to zeros every window gives stateless training: no context beyond . The detached state keeps its values; only the graph is dropped.
Warning: Interleaved lanes make the state carry across non-contiguous text.
reshape(batch, L)makes row one contiguous stretch.
Warning: Without clipping, one exploding window ruins the run. The solution calls
train_step(..., clip=clip)on every window.
Several smaller bugs are caught by the tests above. Sending the bias gradient to bias_ih only means bias_hh never trains; the vjp returns db and db.copy(). Not transposing the weight gradients back gives wrong updates, or a crash when ; the vjp returns d["Wxh"].T and d["Whh"].T. A forward that ignores its state has no statefulness at all; forward passes state into self.rnn and returns the new state. Without a reset at the epoch start, the first window is conditioned on the lane’s end; the loop sets carry["state"] = None when j == 0. An nll that restarts the state per chunk gives a score that depends on the chunk size; one state variable is carried across all chunks. A generate that feeds back the wrong token produces greedy text that differs from the argmax loop; the next forward is fed [[nxt]], the token just chosen. And a save_rnnlm that ignores tokenizer makes the zoo load the wrong tokenizer; the config records "tl_tokenizer": tokenizer.
Where this leads
Section titled “Where this leads”This section is the end of the recurrent chapter, and it leaned on nearly everything before it: the vanilla RNN’s rnn_forward and rnn_backward as the Elman layer’s forward and backward; the LSTM and GRU as cell = "lstm" and cell = "gru"; train_step for each window (zero_grad, backward, clip, step); cross_entropy over the window; Embedding and Linear; F.transpose, F.stack, and F.reshape; Tensor, from_op, and no_grad; save_safetensors and load_safetensors for the model directory; xavier_uniform and orthogonal_init for the Elman input and recurrent weights; PCG32 for initialization and sampling; CharTokenizer and AdamW in the learning test.
Ahead, the model zoo trains rnnlm checkpoints and reports their bits per byte next to the n-gram and the transformer, and ELMo’s biLM is two of these language models, one per direction. Part 4 takes the recurrent cells into sequence-to-sequence translation, starting with Encoder-decoder with teacher forcing.
Production equivalents:
| This piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
train_tbptt | PyTorch’s word language model example | the same batchify lanes and repackage_hidden detach | pytorch/examples, word_language_model/main.py |
ElmanRNN fused op | cuDNN RNN kernels | one kernel per layer, weights packed for both directions | torch/nn/modules/rnn.py, _VF.rnn_tanh |
| LSTM LM | AWD-LSTM | weight-dropped hidden matrices, variational dropout, averaged SGD | Merity, Keskar, and Socher (2017), salesforce/awd-lstm-lm |
| truncation | RWKV, Mamba | recurrent models trained in parallel over the whole sequence, run as RNNs at inference | Peng et al. 2023; Gu and Dao 2023 |
Further reading
Section titled “Further reading”- Mikolov et al., “Recurrent neural network based language model” (Interspeech 2010).
- Zaremba, Sutskever, and Vinyals, “Recurrent Neural Network Regularization” (2014), section 4.
- Williams and Peng, “An efficient gradient-based algorithm for on-line training of recurrent network trajectories” (1990).
Key terms
Section titled “Key terms”- RNN language model: an embedding, a recurrent layer, and a linear read-out trained to predict the next token.
- Fused op: a whole layer over a whole sequence as one autograd node, with a hand-written backward.
- Stateful TBPTT: truncated BPTT in which each window starts from the previous window’s final state.
- Detach: keep a tensor’s values but drop its graph, so backward stops there.
- Lane: one contiguous stretch of the stream; row of every window comes from lane .
- Bits per character (bpc): mean negative log-likelihood in nats divided by .
Summary
Section titled “Summary”- An RNN language model is an embedding, a recurrent layer, and a linear read-out; the three cells share one interface, and the Elman cell is the vanilla RNN’s hand-written BPTT wrapped as a single autograd op.
- Stateful training cuts the stream into contiguous lanes and walks them window by window: the hidden state flows from one window to the next.
- The gradient stops at the window boundary: the carried state is detached, not reset.
- Each new epoch starts from zeros, and clipping guards every step.
- A character LSTM trained this way for 150 steps beats the unigram entropy of the text by more than half a bit per character.
Review questions
Section titled “Review questions”- Why does
train_tbpttdetach the state instead of resetting it to zeros?Answer
Resetting would make training stateless: each window would see no context from before it. Detaching keeps the values, so the forward pass still sees all earlier text in the lane, and only removes the graph so backward stops at the window boundary. - What goes wrong if the carried state is not detached at all?
Answer
Window $j + 1$'s backward would run on into window $j$'s graph, whose gradients were already applied. The earlier windows' gradients get counted again, and memory grows with every step. - Why does
tbptt_batchesusereshape(batch, L)and notreshape(L, batch).T?Answer
The carried state of row $b$ must describe the text right before row $b$'s next window. `reshape(batch, L)` makes each row one contiguous stretch; the transposed form interleaves tokens, so row 0 would be $0, 2, 4, \dots$ and the state would carry across text that never appears together. - Why does the Elman vjp return the same bias gradient twice?
Answer
The forward uses $b_{ih} + b_{hh}$. The derivative of a sum with respect to each term is 1, so both biases receive the full gradient `d["bh"]`. - Why is
nllindependent ofchunk?Answer
The recurrence is a function of the whole prefix through the state alone. Carrying `state` from chunk to chunk makes the chunked computation identical to one pass; only resetting it per chunk would make the score depend on the chunk size.