Backpropagation Through Time › Truncated BPTT

Michael BrenndoerferMay 10, 20257 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 75Backpropagation Through Time › Truncated BPTT
Reading section 4 of 15 · Truncated BPTT · 7 minSection 4 of 15 · 7 min

Book contents

Section 4 of 15 · 7 min read

Backpropagation Through Time › Truncated BPTTOne section · Sections in this chapter4 / 15 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Full BPTT requires storing all hidden states and all activations for the entire sequence before running the backward pass. For a sequence of length TT, this means O(T)O(T) memory for activations and O(T)O(T) computation for the backward pass. When TT is thousands or tens of thousands of tokens, full BPTT becomes impractical.

Truncated BPTT addresses this by dividing the sequence into fixed-length chunks and running a separate forward/backward pass through each chunk. Within each chunk, gradients flow freely. Across chunks, the hidden state is passed forward but gradients are stopped at the chunk boundary.

How Truncated BPTT Works

The sequence of length TT is divided into non-overlapping chunks of length kk (the truncation length). For chunk cc spanning timesteps [ck+1,(c+1)k][ck+1, (c+1)k]:

  1. The forward pass starts with the final hidden state from the previous chunk, hck\mathbf{h}_{ck}, carried forward without gradient tracking
  2. The forward pass runs through all kk steps in the chunk, accumulating hidden states and losses
  3. The backward pass propagates gradients back through the kk steps in the chunk only
  4. The weight gradients are accumulated across all chunks

The stopping of gradients at chunk boundaries is implemented using PyTorch's detach() operation, which creates a new tensor with the same values but no gradient history.

The forward-pass hidden state still carries information from earlier in the sequence. When chunk cc begins, the initial hidden state hck\mathbf{h}_{ck} encodes everything the network saw in chunks 0 through c−1c-1. That information is used in the forward pass to make predictions and compute losses. But the backward pass cannot trace gradients through hck\mathbf{h}_{ck} into earlier chunks, because detach() has severed the computational graph. The result is an asymmetry: the model can use long-range information to make predictions, but cannot learn from gradient signal that would require looking beyond the current chunk boundary.

Truncated BPTT makes a practical trade-off: it gives up the ability to learn dependencies that span more than kk timesteps in exchange for bounded memory and predictable computation per update step. In practice, for most RNN training tasks, a truncation length between 20 and 200 captures the dependencies that matter. The exact value is a hyperparameter that depends on the sequence structure, and it interacts with the model's capacity to represent longer-range information in its hidden state.

Truncated BPTT

Truncated BPTT limits gradient propagation to the most recent kk timesteps. The hidden state carries information from earlier in the sequence via forward propagation, but the gradient signal only travels back kk steps. This means the model can use long-range information for predictions but cannot update weights based on gradients from beyond kk steps ago.

The following visualization shows how gradient coverage changes with the truncation window relative to the full sequence length. Each bar represents the fraction of the sequence covered by one backward pass:

Out[3]:
Visualization
Horizontal bar chart showing chunk coverage for truncated BPTT with chunk sizes 20, 35, 50, and 100 on a 200-step sequence.
Gradient coverage for truncated BPTT with different chunk sizes on a sequence of length 200. Each row shows one chunk size; alternating shading distinguishes individual backward pass windows. Larger chunks cover a greater fraction of the sequence per backward pass, capturing longer dependencies at the cost of more memory per step.

With k=20k = 20, the 200-step sequence requires 10 separate backward passes. With k=100k = 100, only 2 passes are needed. The trade-off is that larger chunks require more memory to store activations during the forward pass of each chunk.

Selecting the Truncation Length

The truncation length kk controls the trade-off between several competing factors:

  • Memory cost: full BPTT requires O(T)O(T) memory; truncated BPTT requires O(k)O(k) memory per chunk
  • Computational cost: each backward pass covers kk steps; you run T/kT/k backward passes per sequence
  • Gradient quality: longer chunks capture more long-range dependencies; shorter chunks lose information about distant correlations

A common choice is k=35k = 35 for language modeling, matching the LSTM training paper by Zaremba et al. (2014). For tasks with explicit long-range dependencies, larger kk values or gated architectures such as LSTMs help.

Choosing the truncation length is not purely a memory decision; it should reflect the structure of the data. If the sequence contains strong dependencies that span 100 tokens, a truncation window of 20 will prevent the model from ever learning them through gradient updates. In practice, you can estimate the relevant dependency range by examining what information would be needed to make accurate predictions. For next-word prediction in typical English text, dependencies rarely need to span more than a few dozen words, which explains why modest truncation windows work well for language modeling.

One underappreciated consideration is the interaction between the truncation length and the learning dynamics. Very short truncation windows produce gradients that are effectively local to a small context window. The model learns to make predictions using only nearby context, which is fast but limits what it can ultimately learn. Very long truncation windows give the model more gradient signal but also give it vanishing gradient problems of its own, since the gradient still has to propagate through kk matrix multiplications to reach the beginning of the chunk.

Memory Requirements

The memory comparison between full BPTT and truncated BPTT is significant. For a sequence of length TT with hidden size dd and batch size BB:

  • Full BPTT activations: O(T⋅d⋅B)O(T \cdot d \cdot B) bytes (all hidden states stored)
  • Truncated BPTT activations: O(k⋅d⋅B)O(k \cdot d \cdot B) bytes (only current chunk's states stored)

For long sequences (e.g., T=1000T = 1000, k=35k = 35), truncated BPTT reduces memory by roughly T/k=28.6×T / k = 28.6\times. The computational cost per step is similar, but the memory savings allow much larger batch sizes or hidden state sizes.

Beyond hidden states, the backward pass also requires storing intermediate activations, including the pre-activation values (the inputs to the tanh function) at each step, since the tanh derivative must be computed from these values. The total activation memory scales with the depth of the backward pass, making truncation doubly beneficial: it reduces both the hidden state storage and the intermediate activation storage.