Training Repeats the Forward Count
Multiply one layer's forward work by the 3x forward-plus-backward gradient factor, batches per epoch, and training epochs.
The problem
Evaluating a trained layer on one mini-batch is a one-shot calculation, whereas training repeats that layer inside an outer loop over batches and epochs. Worse, each optimizer step does more than run the layer forward: backpropagation must also compute gradients with respect to the layer's weights and with respect to its incoming activations.
Given two training specs equal to [8, 16, 16, 10] and [16, 32, 8, 20] over epochs passes, how many multiply-accumulates (MACs) occur in one forward batch, in one full forward-plus-backward step, and across the entire training run?
Work through the numbers
Count multiply-accumulates for [8, 16, 16, 10] first. In the forward pass , multiplying an input batch by a transposed weight matrix performs MACs.
Now trace backpropagation through that same dense layer. Computing the weight gradient multiplies a matrix by an matrix: another MACs. Propagating error to the previous layer via multiplies an matrix by a matrix: yet another MACs.
One training step therefore costs MACs, or the forward pass, counting only the three matrix products (bias gradients, activation derivatives, and the optimizer update are smaller terms). Across batches per epoch and epochs ( steps), row 0 executes MACs, giving [2048, 6144, 307200].
Repeat for [16, 32, 8, 20]. One forward batch performs MACs, one forward-plus-backward step performs MACs, and batches across epochs ( steps) execute MACs, giving [4096, 12288, 1228800] and a combined budget of MACs.
The live model
The left panel compares single-batch forward MACs (blue) against single-step forward-plus-backward MACs (amber), showing the ratio on every spec. The right panel plots full-run training MACs (green) as you drag epochs. At epochs , run totals stand at 307,200 and 1,228,800. Lowering epochs to drops them to 61,440 and 245,760; raising epochs to reaches 614,400 and 2,457,600, and at epochs they hit 1,228,800 and 4,915,200 (combined 6,144,000 MACs).
![Two-panel MAC comparison showing forward vs 3x step MACs [2048, 6144] and [4096, 12288] on the left and 5-epoch totals [307200, 1228800] on the right.](../assets/ml/training-repeats-the-forward-count--demo.png)
let epochs = scene.parameter("epochs", 5.0, 1.0, 20.0, 1.0);
let b = input.select(1, 0);
let d_in = input.select(1, 1);
let d_out = input.select(1, 2);
let batches = input.select(1, 3);
let fwd = b * d_in * d_out;
let step = 3.0 * fwd;
let run_macs = step * batches * epochs;
let cols = torch.list().add(fwd.unsqueeze(1)).add(step.unsqueeze(1));
let answer = torch.cat(cols.add(run_macs.unsqueeze(1)), 1);
out.set("training_macs", answer);
{
"training_macs": [
[
2048,
6144,
307200
],
[
4096,
12288,
1228800
]
]
}Watch the parameter change
Your turn
For each [B, d_in, d_out, batches] row in input and the active epochs setting, answer must return [forward_macs, step_macs, run_macs] counting the three matrix products per training step across all batches and epochs. The starter fills fwd and leaves zero placeholders for step and run_macs; at epochs , the completed output is [[2048, 6144, 307200], [4096, 12288, 1228800]]. Check varies both the layer specs and epochs across unseen probes.
![Starter MAC comparison computing forward MACs [2048, 0, 0] and [4096, 0, 0] with zero placeholders for single-step and full-run MACs.](../assets/ml/training-repeats-the-forward-count--exercise.png)
let epochs = scene.parameter("epochs", 5.0, 1.0, 20.0, 1.0);
let b = input.select(1, 0);
let d_in = input.select(1, 1);
let d_out = input.select(1, 2);
let batches = input.select(1, 3);
let fwd = b * d_in * d_out;
let step = torch.zeros(input.size(0)).to(DType.F64);
let run_macs = torch.zeros(input.size(0)).to(DType.F64);
let cols = torch.list().add(fwd.unsqueeze(1)).add(step.unsqueeze(1));
let answer = torch.cat(cols.add(run_macs.unsqueeze(1)), 1);
out.set("training_macs", answer);
{
"training_macs": [
[
2048,
0,
0
],
[
4096,
0,
0
]
]
}Return [forward_macs, step_macs, run_macs] for each [B, d_in, d_out, batches] row across epochs, counting the three matrix products per step.
Read the result carefully
Look at how fast repetition amplifies a tiny layer. A weight matrix holds weights, small enough to inspect in a spreadsheet, yet training steps already execute 307,200 multiply-accumulates (614,400 floating-point operations). Nobody traces half a million products by hand, even on an 80-row toy dataset.
Also note when the rule trims to : the very first layer touching raw input features does not need unless inputs themselves are being optimized, saving one matrix product on layer 0 while every deeper hidden layer pays the full matrix-product cost.
Recap
- Backpropagation through a hidden dense layer computes two matrix products ( and ), each matching the forward MAC count .
- Counting only the three matrix products, one forward-plus-backward step on a hidden dense layer executes multiply-accumulates.
- Full training multiplies single-step work by , turning thousands of per-batch operations into millions.