Speed

Activation Cells Also Grow

Compare fixed parameter storage against batch-scaled hidden activation tensors retained in memory during training.

The problem

Counting model parameters often creates a false sense of safety about memory. Suppose a compact four-layer network has width vector [8, 16, 16, 16, 2]: 88 input features, three hidden layers of width 1616, and 22 output logits. Its weights and biases fit in fewer than a thousand scalars, yet training on a large mini-batch can still exhaust device RAM on deeper versions of the same pattern.

Why? During training, backpropagation must retain every hidden layer's forward activations across all BB rows of the current mini-batch until the backward pass consumes them. How do fixed parameter cells compare against stored hidden activation cells as batch_size grows?

Work through the numbers

Count trainable parameters across the four dense transitions in [8, 16, 16, 16, 2]. Layer 00 (8→168 \to 16) holds (8+1)⋅16=144(8 + 1) \cdot 16 = 144 parameters. Layer 11 (16→1616 \to 16) holds (16+1)⋅16=272(16 + 1) \cdot 16 = 272 parameters. Layer 22 (16→1616 \to 16) holds another 272272 parameters, and Layer 33 (16→216 \to 2) holds (16+1)⋅2=34(16 + 1) \cdot 2 = 34 parameters.

Adding 144+272+272+34=722144 + 272 + 272 + 34 = 722 gives 722 parameter cells. Because weights are shared across every row in a batch, that 722-cell footprint stays constant whether batch_size is 11 or 6464.

Now count hidden activation cells across the interior widths [16, 16, 16]. One example produces 16+16+16=4816 + 16 + 16 = 48 hidden values. At the default batch_size =32= 32, storing those three hidden tensors requires 32⋅48=1,53632 \cdot 48 = 1{,}536 cells, giving [722, 1536]: hidden activations occupy more than twice the space of the weights themselves.

The live model

The blue bar shows fixed parameter cells (722) and the amber bar shows batch-scaled hidden activation cells across [8, 16, 16, 16, 2]. At batch_size =4= 4, activations take 192 cells, well below 722. At batch_size =12= 12, activations reach 576 cells; at batch_size =16= 16 they hit 768 cells and overtake the parameter budget. At the default 3232 they reach 1,536, and at 6464 they climb to 3,072 cells.

Comparison bars for [8, 16, 16, 16, 2] at batch_size 32 showing 722 parameter cells vs 1536 stored hidden activation cells.
let batch_size = scene.parameter("batch_size", 32.0, 4.0, 64.0, 4.0);
let n = input.size(0);
let d_in = input.slice(0, 0, n - 1);
let d_out = input.slice(0, 1, n);
let p_cells = ((d_in + 1.0) * d_out).sum();
let hidden = input.slice(0, 1, n - 1);
let a_cells = hidden.sum() * batch_size;
let pair = torch.list().add(p_cells.reshape(1)).add(a_cells.reshape(1));
let answer = torch.cat(pair, 0);
out.set("params_vs_activations", answer);
{
  "params_vs_activations": [
    722,
    1536
  ]
}
Watch the parameter change

Your turn

In the starter program, the parameter count (p_cells) is already computed for [8, 16, 16, 16, 2], while the hidden-activation slot is a zero placeholder ([722, 0]). Set answer to [parameter_cells, hidden_activation_cells] retained across the interior hidden layers during training; Check tests both the default [8, 16, 16, 16, 2] network at batch_size = 32 ([722, 1536]) and unseen depth, width, and mini-batch combinations.

Starter comparison bars showing 722 parameter cells and a zero placeholder for hidden activation cells.
let batch_size = scene.parameter("batch_size", 32.0, 4.0, 64.0, 4.0);
let n = input.size(0);
let d_in = input.slice(0, 0, n - 1);
let d_out = input.slice(0, 1, n);
let p_cells = ((d_in + 1.0) * d_out).sum();
let a_cells = torch.zeros(1).to(DType.F64);
let pair = torch.list().add(p_cells.reshape(1)).add(a_cells.reshape(1));
let answer = torch.cat(pair, 0);
out.set("params_vs_activations", answer);
{
  "params_vs_activations": [
    722,
    0
  ]
}

Return [parameter_cells, hidden_activation_cells] for the width vector input at the active batch_size.

Read the result carefully

Solve for the exact crossover batch size on [8, 16, 16, 16, 2]: dividing 722/48=15.0417722 / 48 = 15.0417, so B=16B = 16 is the first integer batch size where stored hidden activations exceed parameters (768 versus 722).

Notice the operational difference between inference and training. Inference can overwrite one hidden buffer as each layer finishes because no backward pass follows, whereas standard training must keep every intermediate activation tensor alive until gradients propagate back to the start.

Recap

  • Parameter storage sums (din+1)⋅dout(d_{\mathrm{in}} + 1) \cdot d_{\mathrm{out}} across layers and remains fixed regardless of mini-batch size BB.
  • Training retains hidden activation tensors for backpropagation, consuming BB times the sum of interior hidden widths.
  • Increasing batch_size linearly inflates activation memory, quickly overtaking parameter memory even on narrow networks.