Backpropagation Through Time › Memory Tradeoffs: Full vs. Truncated BPTT
Language AI HandbookAll chapters Backpropagation Through Time › Memory Tradeoffs: Full vs. Truncated BPTTOne section · Sections in this chapter8 / 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
Let's visualize the memory tradeoff concretely:
def compute_memory_requirements(
seq_lengths, chunk_sizes, hidden_dim, batch_size, bytes_per_float=4
):
"""
Estimate memory requirements for full and truncated BPTT.
Memory proportional to number of stored activations.
"""
results = {}
for seq_len in seq_lengths:
# Full BPTT: store all hidden states for entire sequence
full_mem_mb = (seq_len * hidden_dim * batch_size * bytes_per_float) / (
1024**2
)
results[seq_len] = {"full": full_mem_mb}
for chunk_size in chunk_sizes:
# Truncated BPTT: store only current chunk's hidden states
trunc_mem_mb = (
chunk_size * hidden_dim * batch_size * bytes_per_float
) / (1024**2)
results[seq_len][f"trunc_{chunk_size}"] = trunc_mem_mb
return results
seq_lengths_mem = [50, 100, 250, 500, 1000]
chunk_sizes_mem = [25, 50, 100]
hidden_dim_large = 512
batch_size_large = 32
mem_data = compute_memory_requirements(
seq_lengths_mem, chunk_sizes_mem, hidden_dim_large, batch_size_large
)
The chart makes the advantage of truncated BPTT clear. For a sequence of length 1000 with hidden dimension 512 and batch size 32, full BPTT requires roughly 64 MB of activations just for the hidden states. Truncated BPTT with chunk size 50 reduces this to about 3.2 MB per chunk, a reduction. This is before accounting for the even larger memory needed to store the pre-activation values (inputs to the tanh function), which are also needed for computing the tanh derivatives during the backward pass.