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 parameters tells us its memory footprint, not how much arithmetic a forward pass performs. Suppose a batch of rows enters a dense layer mapping inputs to outputs, and we compare two specs : [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 FLOPs for a dense layer with bias.
Work through the numbers
Start with . The output matrix has shape , so there are output cells. Every output cell computes a length- dot product: multiplications and inner additions. Across all output cells, that gives multiplications and inner additions.
Next add the bias vector of length to every one of the batch rows, adding bias additions. Total scalar operations equal , so row 0 of our result table is [60, 40, 120]. Notice that the bias additions cancel the gap in the inner additions, making total additions and total operations .
Repeat for [8, 4, 6], which produces output cells. Multiplications equal , inner dot-product additions equal , and bias additions equal . Summing total operations yields [192, 144, 384] and a combined total of 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 , the two rows show [60, 40, 120] and [192, 144, 384]. Dragging batch_scale to doubles batch sizes to and , doubling every bar to [120, 80, 240] and [384, 288, 768]; at they reach [240, 160, 480] and [768, 576, 1536], and at 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.](../assets/ml/counting-one-forward-layer--demo.png)
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.](../assets/ml/counting-one-forward-layer--exercise.png)
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 parameters regardless of batch size, yet evaluating a batch of executes scalar operations, and scaling the batch by executes operations without adding a single weight.
This algebra also clarifies why systems papers write FLOPs for a biased linear layer. Each of the output cells performs multiplications, dot-product additions, and bias addition: multiplications and additions per cell.
Recap
- A batched dense layer performs multiplications and inner dot-product additions.
- Adding one bias scalar to each of the output cells contributes additions, bringing total additions to and total operations to .
- Parameter storage is independent of batch size , whereas forward arithmetic scales linearly with .