Speed

Counting One Forward Layer

Count exact multiplications, inner dot-product additions, and bias additions in one batched dense forward pass.

The problem

Knowing that a dense layer stores (din+1)⋅dout(d_{\mathrm{in}} + 1) \cdot d_{\mathrm{out}} parameters tells us its memory footprint, not how much arithmetic a forward pass performs. Suppose a batch of BB rows enters a dense layer Y=XWT+bY = X W^T + b mapping dind_{\mathrm{in}} inputs to doutd_{\mathrm{out}} outputs, and we compare two specs [B,din,dout][B, d_{\mathrm{in}}, d_{\mathrm{out}}]: [4, 3, 5] and [8, 4, 6].

How many scalar multiplications, how many inner dot-product additions, and how many total floating-point operations (including bias additions) does each batched layer execute? Separating those three counts exposes why hardware benchmarks quote 2⋅B⋅din⋅dout2 \cdot B \cdot d_{\mathrm{in}} \cdot d_{\mathrm{out}} FLOPs for a dense layer with bias.

Work through the numbers

Start with [B,din,dout]=[4,3,5][B, d_{\mathrm{in}}, d_{\mathrm{out}}] = [4, 3, 5]. The output matrix YY has shape 4×54 \times 5, so there are 4⋅5=204 \cdot 5 = 20 output cells. Every output cell computes a length-33 dot product: 33 multiplications and 3−1=23 - 1 = 2 inner additions. Across all 2020 output cells, that gives 20⋅3=6020 \cdot 3 = 60 multiplications and 20⋅2=4020 \cdot 2 = 40 inner additions.

Next add the bias vector bb of length 55 to every one of the 44 batch rows, adding 4⋅5=204 \cdot 5 = 20 bias additions. Total scalar operations equal 60+40+20=12060 + 40 + 20 = 120, so row 0 of our result table is [60, 40, 120]. Notice that the 2020 bias additions cancel the −1-1 gap in the 4040 inner additions, making total additions 40+20=6040 + 20 = 60 and total operations 2⋅4⋅3⋅5=1202 \cdot 4 \cdot 3 \cdot 5 = 120.

Repeat for [8, 4, 6], which produces 8⋅6=488 \cdot 6 = 48 output cells. Multiplications equal 48⋅4=19248 \cdot 4 = 192, inner dot-product additions equal 48⋅(4−1)=14448 \cdot (4 - 1) = 144, and bias additions equal 4848. Summing 192+144+48=384192 + 144 + 48 = 384 total operations yields [192, 144, 384] and a combined total of 120+384=504120 + 384 = 504 FLOPs across both specs.

The live model

Each row plots three bars on a shared scale: multiplications in blue, inner dot-product additions in amber, and total operations (with bias additions included) in green. At batch_scale =1= 1, the two rows show [60, 40, 120] and [192, 144, 384]. Dragging batch_scale to 22 doubles batch sizes to 88 and 1616, doubling every bar to [120, 80, 240] and [384, 288, 768]; at 44 they reach [240, 160, 480] and [768, 576, 1536], and at 88 they hit [480, 320, 960] and [1536, 1152, 3072].

Grouped bars for [4, 3, 5] showing [60, 40, 120] and [8, 4, 6] showing [192, 144, 384] for mults, inner adds, and total forward ops.
let batch_scale = scene.parameter("batch_scale", 1.0, 1.0, 8.0, 1.0);
let b = input.select(1, 0) * batch_scale;
let d_in = input.select(1, 1);
let d_out = input.select(1, 2);
let mults = b * d_in * d_out;
let dot_adds = b * (d_in - 1.0) * d_out;
let bias_adds = b * d_out;
let total = mults + dot_adds + bias_adds;
let cols = torch.list().add(mults.unsqueeze(1)).add(dot_adds.unsqueeze(1));
let answer = torch.cat(cols.add(total.unsqueeze(1)), 1);
out.set("forward_ops", answer);
{
  "forward_ops": [
    [
      60,
      40,
      120
    ],
    [
      192,
      144,
      384
    ]
  ]
}
Watch the parameter change

Your turn

For each [B, d_in, d_out] row in input, answer must hold the three-column row [multiplications, dot_product_additions, total_operations] where total_operations includes multiplications, inner dot-product additions, and bias additions. The starter already fills the first two columns and leaves a zero placeholder for total; on the visible specs, the completed table is [[60, 40, 120], [192, 144, 384]]. Check tests unseen batch sizes and layer dimensions.

Starter bars showing multiplications and inner dot-product additions with zero placeholders [60, 40, 0] and [192, 144, 0] in the total-operations column.
let b = input.select(1, 0);
let d_in = input.select(1, 1);
let d_out = input.select(1, 2);
let mults = b * d_in * d_out;
let dot_adds = b * (d_in - 1.0) * d_out;
let total = torch.zeros(input.size(0)).to(DType.F64);
let cols = torch.list().add(mults.unsqueeze(1)).add(dot_adds.unsqueeze(1));
let answer = torch.cat(cols.add(total.unsqueeze(1)), 1);
out.set("forward_ops", answer);
{
  "forward_ops": [
    [
      60,
      40,
      0
    ],
    [
      192,
      144,
      0
    ]
  ]
}

Return [multiplications, dot_product_additions, total_operations] (including bias additions) for every [B, d_in, d_out] row in input.

Read the result carefully

Compare parameter storage against forward arithmetic on [8, 4, 6]. That layer stores only (4+1)⋅6=30(4 + 1) \cdot 6 = 30 parameters regardless of batch size, yet evaluating a batch of 88 executes 384384 scalar operations, and scaling the batch by 88 executes 3,0723{,}072 operations without adding a single weight.

This algebra also clarifies why systems papers write 2Bdindout2 B d_{\mathrm{in}} d_{\mathrm{out}} FLOPs for a biased linear layer. Each of the BdoutB d_{\mathrm{out}} output cells performs dind_{\mathrm{in}} multiplications, din−1d_{\mathrm{in}} - 1 dot-product additions, and 11 bias addition: dind_{\mathrm{in}} multiplications and dind_{\mathrm{in}} additions per cell.

Recap

  • A batched dense layer [B,din,dout][B, d_{\mathrm{in}}, d_{\mathrm{out}}] performs BdindoutB d_{\mathrm{in}} d_{\mathrm{out}} multiplications and B(din−1)doutB (d_{\mathrm{in}} - 1) d_{\mathrm{out}} inner dot-product additions.
  • Adding one bias scalar to each of the BdoutB d_{\mathrm{out}} output cells contributes BdoutB d_{\mathrm{out}} additions, bringing total additions to BdindoutB d_{\mathrm{in}} d_{\mathrm{out}} and total operations to 2Bdindout2 B d_{\mathrm{in}} d_{\mathrm{out}}.
  • Parameter storage is independent of batch size BB, whereas forward arithmetic scales linearly with BB.