Stacked RNNs › Computational Cost Scaling

Michael BrenndoerferMay 13, 20254 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 82Stacked RNNs › Computational Cost Scaling
Reading section 11 of 16 · Computational Cost Scaling · 4 minSection 11 of 16 · 4 min

Book contents

Section 11 of 16 · 4 min read

Stacked RNNs › Computational Cost ScalingOne section · Sections in this chapter11 / 16 ↓
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

Stacking layers has a direct and significant impact on computational cost. Understanding these costs helps you make informed decisions about architecture design.

Time Complexity

For a single LSTM layer processing a sequence of length TT with hidden size dd, the computation at each timestep involves matrix multiplications of size O(d2)O(d^2) (the recurrent matrix WhW_h) and O(d⋅m)O(d \cdot m) (the input matrix WxW_x). The total time for one forward pass is:

Olayer=O ⁣(T⋅(d2+d⋅m))O_{\text{layer}} = O\!\left(T \cdot (d^2 + d \cdot m)\right)

For a stacked model with LL layers and the same hidden size dd (and noting that after the first layer the "input" is also size dd):

Ostacked=O ⁣(T⋅L⋅d2)O_{\text{stacked}} = O\!\left(T \cdot L \cdot d^2\right)

Time scales linearly with the number of layers. A 4-layer model takes approximately 4 times as long per batch as a 1-layer model with the same hidden size.

Memory Complexity

During training, you must store intermediate activations for backpropagation. For a single-layer LSTM:

  • Hidden states: O(T⋅d)O(T \cdot d) per sample
  • Cell states: O(T⋅d)O(T \cdot d) per sample
  • Gate activations: O(T⋅d)O(T \cdot d) per sample (4 gates)

Total memory per sample: O(T⋅d)O(T \cdot d)

For an LL-layer stack, memory scales as O(T⋅L⋅d)O(T \cdot L \cdot d): you must store the full state history for every layer to compute gradients.

For a 4-layer LSTM processing sequences of length 100 with hidden size 512:

  • Memory for activations: 100×4×512×4≈800100 \times 4 \times 512 \times 4 \approx 800K floats per sample
  • At batch size 32: roughly 100 MB just for LSTM activations (in float32)

This is why long sequences with deep stacks require careful batch size management.

Parameter Count

Parameter count for a stacked LSTM with LL layers, input size mm, and hidden size dd:

P=4 ⁣[(m+d)⋅d+d]+4(L−1) ⁣[(2d)⋅d+d]P = 4\!\left[(m + d) \cdot d + d\right] + 4(L-1)\!\left[(2d) \cdot d + d\right]

Simplified: the first layer has O(d⋅m+d2)O(d \cdot m + d^2) parameters and each subsequent layer has O(d2)O(d^2) parameters. Total parameters scale as O(L⋅d2)O(L \cdot d^2) for L≫1L \gg 1.

Out[13]:
Visualization
Bar chart showing linear increase in training time from 1 to 5 layers.
Training time per epoch scales linearly with the number of stacked LSTM layers. Each additional layer adds a fixed overhead independent of sequence length, confirming the O(L) time complexity.
Bar chart showing linear increase in activation memory from 1 to 5 layers.
Memory usage per training sample increases linearly with both the number of layers and sequence length. A 4-layer model requires 4x the activation memory of a single-layer model.

The linear scaling in both time and memory confirms that adding layers has predictable costs. For a given computational budget, this creates a hard cap on useful depth: a 5-layer model that takes 5x the training time and fills all available GPU memory might not be the right choice even if, in theory, it could learn better representations.