02-parameter-counting counts how many numbers a model has, and 03-memory-footprint-estimation counts how many bytes storing them costs — neither says anything about how much compute a forward pass actually takes. Two models can have the same parameter count and wildly different runtime cost (a Linear layer applied once vs. a Conv2d layer's kernel sliding across every spatial position), so a separate estimate is needed: FLOPs, the number of individual floating-point multiply/add operations a forward pass performs.
Implement linear_flops(batch_size, in_features, out_features, bias=True) and conv2d_flops(batch_size, in_channels, out_channels, kernel_size, output_height, output_width, bias=True), each returning the total FLOP count for one forward pass of that layer type.
2 FLOPs — the standard convention used by real profiling tools.linear_flops: 2 * batch_size * in_features * out_features multiply-add FLOPs, plus batch_size * out_features more if bias=True (one addition per output element).conv2d_flops: assumes a square kernel, no groups, no dilation. Each of batch_size * out_channels * output_height * output_width output positions costs in_channels * kernel_size * kernel_size multiply-adds; add one more FLOP per output position if bias=True.bias defaults to True in both functions.Open one at a time. Each gives away a little more than the last.
Compute the total number of output elements first (batch_size * out_features for Linear, batch_size * out_channels * output_height * output_width for Conv2d) — everything else is "how much work does producing ONE output element cost," multiplied by that count.
One output element of a Conv2d is a dot product over the kernel's entire receptive field: in_channels * kernel_size * kernel_size multiply-adds, exactly analogous to Linear's in_features multiply-adds per output element.
No results yet
Run your code to check it against the examples, or submit to run every test.