Speed

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 [B,din,dout,batches][B, d_{\mathrm{in}}, d_{\mathrm{out}}, \mathrm{batches}] equal to [8, 16, 16, 10] and [16, 32, 8, 20] over epochs =5= 5 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 Y=XWT+bY = X W^T + b, multiplying an 8×168 \times 16 input batch by a 16×1616 \times 16 transposed weight matrix performs 8⋅16⋅16=2,0488 \cdot 16 \cdot 16 = 2{,}048 MACs.

Now trace backpropagation through that same dense layer. Computing the weight gradient dW=dYTXdW = dY^T X multiplies a 16×816 \times 8 matrix by an 8×168 \times 16 matrix: another 16⋅8⋅16=2,04816 \cdot 8 \cdot 16 = 2{,}048 MACs. Propagating error to the previous layer via dX=dYWdX = dY W multiplies an 8×168 \times 16 matrix by a 16×1616 \times 16 matrix: yet another 8⋅16⋅16=2,0488 \cdot 16 \cdot 16 = 2{,}048 MACs.

One training step therefore costs 2,048+2,048+2,048=6,1442{,}048 + 2{,}048 + 2{,}048 = 6{,}144 MACs, or 3×3\times the forward pass, counting only the three matrix products (bias gradients, activation derivatives, and the optimizer update are smaller terms). Across 1010 batches per epoch and 55 epochs (10⋅5=5010 \cdot 5 = 50 steps), row 0 executes 6,144⋅50=307,2006{,}144 \cdot 50 = 307{,}200 MACs, giving [2048, 6144, 307200].

Repeat for [16, 32, 8, 20]. One forward batch performs 16⋅32⋅8=4,09616 \cdot 32 \cdot 8 = 4{,}096 MACs, one forward-plus-backward step performs 3⋅4,096=12,2883 \cdot 4{,}096 = 12{,}288 MACs, and 2020 batches across 55 epochs (100100 steps) execute 12,288⋅100=1,228,80012{,}288 \cdot 100 = 1{,}228{,}800 MACs, giving [4096, 12288, 1228800] and a combined budget of 1,536,0001{,}536{,}000 MACs.

The live model

The left panel compares single-batch forward MACs (blue) against single-step forward-plus-backward MACs (amber), showing the 3:13 : 1 ratio on every spec. The right panel plots full-run training MACs (green) as you drag epochs. At epochs =5= 5, run totals stand at 307,200 and 1,228,800. Lowering epochs to 11 drops them to 61,440 and 245,760; raising epochs to 1010 reaches 614,400 and 2,457,600, and at 2020 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.
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 =5= 5, 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.
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 16×1616 \times 16 weight matrix holds 16⋅16=25616 \cdot 16 = 256 weights, small enough to inspect in a spreadsheet, yet 5050 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 3×3\times rule trims to 2×2\times: the very first layer touching raw input features XX does not need dXdX unless inputs themselves are being optimized, saving one matrix product on layer 0 while every deeper hidden layer pays the full 3×3\times matrix-product cost.

Recap

  • Backpropagation through a hidden dense layer computes two matrix products (dWdW and dXdX), each matching the forward MAC count BdindoutB d_{\mathrm{in}} d_{\mathrm{out}}.
  • Counting only the three matrix products, one forward-plus-backward step on a hidden dense layer executes 3Bdindout3 B d_{\mathrm{in}} d_{\mathrm{out}} multiply-accumulates.
  • Full training multiplies single-step work by batches⋅epochs\mathrm{batches} \cdot \mathrm{epochs}, turning thousands of per-batch operations into millions.