COE 592Lecture 03Part 04
MACs, FLOPs and operations
Counting computation as multiply-accumulate operations for matrix products and each layer type, AlexNet's 724M MACs, and converting to FLOPs, FLOPS, OPs and OPS.
- Concepts
- 5
- Slides
- 23-29
- Reading
- 30 min
Why this part matters
Part 02 measured what a network stores and part 03 what it must hold in memory. This part measures what it must compute. That number is the numerator of computation time in the Latency model of slide 8, the figure every accelerator datasheet is quoted against, and the figure you will report for any model in the research project.
The counting unit is the multiply-accumulate, and everything else is bookkeeping on top of it: how many MACs a matrix product costs, how many each layer type costs, what AlexNet adds up to, and how the count becomes FLOPs, OPs and finally milliseconds on a specific chip. The surprise waiting at the centre of the part is that the layers holding almost all of AlexNet's weights do almost none of its arithmetic. Every exam question on efficiency metrics circles back to that inversion.
By the end you can
- Define a multiply-accumulate operation and count MACs for a matrix-vector product and a matrix-matrix product.
- Fill the MACs table for linear, convolution, grouped and depthwise layers, and explain why conv MACs equal parameters times h_o·w_o.
- Reproduce AlexNet's 724M MACs layer by layer and explain why convolutions dominate compute while linear layers dominate parameters.
- Convert MACs to FLOPs and OPs, and tell a count (FLOPs, OPs) apart from a rate (FLOPS, OPS).
- Estimate T_computation for a model on a device from the model's OPs and the device's OPS.
Start with one neuron that has four inputs. Its output before the activation function is w₁x₁ + w₂x₂ + w₃x₃ + w₄x₄. A processor evaluates that as four identical steps: multiply a weight by an input, add the product to a running total. Each step is one multiply-accumulate operation, and the whole of neural network inference is this step repeated a few hundred million times.
The definition is standard well beyond this course. The Wikipedia entry on the multiply-accumulate operation gives exactly a ← a + (b × c), and notes that IEEE 754-2008 defines a fused multiply-add that performs the whole step with a single rounding. Sze, Chen, Yang and Emer call the MAC "the fundamental component" of DNN processing in their survey of efficient DNN hardware. Counting MACs is therefore counting what the hardware executes, not an abstraction layered over it.
From one neuron to a matrix-vector product
A layer of m neurons that each read the same n inputs is a matrix-vector multiplication: an m × n weight matrix times an n-vector. Each of the m outputs needs n MACs, one per weight in its row, so the product costs m · n MACs. Notice that this is also the number of weights. In a matrix-vector product, every weight is touched exactly once.
Feed the layer a batch of inputs at once and the vector becomes a matrix. The slide renames the dimensions here: the input count becomes k and the letter n is reused for the batch, the number of columns of B. The result is a general matrix-matrix multiplication: A of size m × k times B of size k × n gives C of size m × n. Every one of the m · n cells of C is a dot product over the shared dimension k, so it costs k MACs, and the whole product costs m · n · k.
Dimensions used in the GEMM count
- m
- Rows of A and of C: the number of outputs (neurons)
- k
- Columns of A and rows of B: the shared dimension summed over, the number of inputs per neuron
- n
- Columns of B and of C: the number of input vectors, the batch
Why a whole network reduces to this
A Linear layer is literally a matrix-vector product at batch size one and a GEMM for a batch. Convolution looks different but is executed the same way: frameworks reorder the input patches into columns (the im2col trick described in the CS231n notes) so that a convolution becomes one large GEMM, and the MobileNets paper points out that this is why convolutions can ride on "highly optimized general matrix multiply (GEMM) functions". NVIDIA's performance guide states the cost of a GEMM as 2 · M · N · K floating point operations because "each FMA is 2 operations, a multiply and an add", which previews the FLOP conversion at the end of this part. Once you can count MACs for a matrix product, you can count them for every layer.
Recall
Write the MAC operation, and state how many MACs an m × n matrix times an n-vector costs.
Take the first layer of AlexNet. One filter spans all 3 input channels with an 11 × 11 window, so it holds 3 × 11 × 11 = 363 weights. Producing a single output pixel is one dot product of those 363 weights against a patch of the image: 363 MACs. But the filter does not produce one pixel. It slides to every one of the 55 × 55 = 3,025 output positions, and there are 96 filters. So the layer costs 363 × 3,025 × 96 = 105,415,200 MACs from only 34,848 weights.
That example contains the whole rule. In a convolution layer every weight is applied once per output position, so the MAC count is the parameter count from part 02 multiplied by the output height and width:
The other rows of the slide's table follow from the same idea. A Linear layer has h_o = w_o = 1, so its MACs equal its parameters, c_o · c_i. A Grouped convolution splits the channels into g groups, so each filter sees only c_i / g input channels and both parameters and MACs are divided by g. A Depthwise convolution is the extreme case g = c_i = c_o: each filter sees exactly one channel, so c_i disappears from the formula entirely and only c_o · k_h · k_w weights remain, each still applied h_o · w_o times.
| Layer | Parameters | MACs | MACs per parameter |
|---|---|---|---|
| Linear | c_o · c_i | c_o · c_i | 1 |
| Convolution | c_o · c_i · k_h · k_w | c_o · c_i · k_h · k_w · h_o · w_o | h_o · w_o |
| Grouped convolution | c_o · c_i · k_h · k_w / g | c_o · c_i · k_h · k_w · h_o · w_o / g | h_o · w_o |
| Depthwise convolution | c_o · k_h · k_w | c_o · k_h · k_w · h_o · w_o | h_o · w_o |
How a convolution slides, how padding and stride set the output size, and how groups partition channels were covered in lecture 02, part 02 and part 03. This part only counts, and it takes the kernel size and output shape of each layer as given.
Why this mirrors part 02
Goodfellow, Bengio and Courville explain parameter sharing in one sentence that is the whole story of this part: sharing "does not affect the runtime of forward propagation, it is still O(k × n), but it does further reduce the storage requirements of the model to k parameters". Convolution saves storage by reusing weights, and reusing weights is exactly what makes the arithmetic grow. That is why parts 02 and 04 are mirror images. The layers cheapest to store are the most expensive to run.
Worked example: the MobileNet block that makes depthwise worth it
The MobileNets paper replaces one standard 3 × 3 convolution with a depthwise 3 × 3 followed by a pointwise 1 × 1. Its equations 4 and 5 give the depthwise and separable costs, and dividing by the standard cost gives the ratio 1 / N + 1 / D_K², where N is the number of output channels and D_K the kernel size. The formulas in the table let you reproduce that with real numbers.
Worked example
Standard versus depthwise separable at 112 × 112, 32 to 64 channels
Standard 3 × 3 convolution
64 × 32 × 3 × 3 × 112 × 112 = 231,211,008 MACs.Depthwise 3 × 3
32 × 3 × 3 × 112 × 112 = 3,612,672 MACs. No c_i factor.Pointwise 1 × 1
64 × 32 × 1 × 1 × 112 × 112 = 25,690,112 MACs. A convolution with k = 1.Ratio
(3,612,672 + 25,690,112) / 231,211,008 = 0.1267, and 1 / 64 + 1 / 9 = 0.1267.Result
About 7.9× fewer MACs at 64 output channels, approaching the paper's "between 8 to 9 times less computation" as the channel count grows and the 1 / D_K² = 1 / 9 pointwise term is all that remains.
The calculator below lets you scrub every symbol in the table. Its FLOPs, OPs and hardware rows are explained in the last concept of this part; for now watch how parameters and MACs move apart as the output size grows.
illustrative mobile GPU-class figure
Drag h_o = w_o and watch MACs climb while parameters stay put: a convolution reuses every weight at every output position. Switch to linear and the two counts collapse into one. The hardware box only divides: the same layer takes 1000× longer at 1 GFLOPS than at 1 TFLOPS, and marketing peak TOPS are rarely sustained, so treat the time as a floor.
Quick check
A grouped convolution has c_i = 96, c_o = 256, a 5 × 5 kernel, a 27 × 27 output and g = 2. How many MACs does it perform?
Recall
A grouped convolution has 384 filters over 384 input channels, a 3 × 3 kernel, a 13 × 13 output and g = 2. How many MACs, how many parameters, and what is their ratio?
Slide 25 hands you the eight weighted layers of AlexNet with their output shapes and an empty MACs column. Slide 26 fills it in, except for one layer hidden behind a question mark. Work through the column yourself before you read the answers. Every entry is the formula from the previous concept with the layer's numbers substituted, and the pooling layers contribute nothing because they hold no weights.
Worked example
AlexNet, layer by layer (batch size 1, bias ignored)
conv1: 11 × 11, 3 to 96 channels, output 55 × 55
96 × 3 × 11 × 11 × 55 × 55 = 105,415,200conv2: 5 × 5, 96 to 256 channels, groups 2, output 27 × 27
256 × 96 × 5 × 5 × 27 × 27 / 2 = 223,948,800. Without the groups it would be 447,897,600; g = 2 halves it.conv3: 3 × 3, 256 to 384 channels, output 13 × 13
384 × 256 × 3 × 3 × 13 × 13 = 149,520,384conv4: 3 × 3, 384 to 384 channels, groups 2, output 13 × 13
384 × 384 × 3 × 3 × 13 × 13 / 2 = 112,140,288conv5: 3 × 3, 384 to 256 channels, groups 2, output 13 × 13
This is the entry the slide hides. Compute it before revealing.Recall
conv5 MACs?
256 × 384 × 3 × 3 × 13 × 13 / 2 = 74,760,192fc6: 256 × 6 × 6 = 9,216 inputs to 4,096 outputs
4,096 × 9,216 = 37,748,736. The flattened pooling output is the input vector.fc7: 4,096 to 4,096
4,096 × 4,096 = 16,777,216fc8: 4,096 to 1,000 classes
1,000 × 4,096 = 4,096,000Total
724,406,816 MACs, the slide's "724M in total". Sze et al. list the same figure, 724M MACs and 61M weights, in their survey table.
Put the parameters next to the MACs
The total is not the interesting number. The interesting number appears when you place the parameter counts from part 02 beside the MACs and add a column for their ratio, which by the previous concept is just h_o · w_o.
| Layer | Shape | Parameters | MACs | MACs per parameter |
|---|---|---|---|---|
| conv1 | 96 × 3 × 11 × 11, out 55 × 55 | 34,848 | 105,415,200 | 3,025 |
| conv2 | 256 × 96 × 5 × 5 / 2, out 27 × 27 | 307,200 | 223,948,800 | 729 |
| conv3 | 384 × 256 × 3 × 3, out 13 × 13 | 884,736 | 149,520,384 | 169 |
| conv4 | 384 × 384 × 3 × 3 / 2, out 13 × 13 | 663,552 | 112,140,288 | 169 |
| conv5 | 256 × 384 × 3 × 3 / 2, out 13 × 13 | 442,368 | 74,760,192 | 169 |
| fc6 | 4096 × (256 × 6 × 6) | 37,748,736 | 37,748,736 | 1 |
| fc7 | 4096 × 4096 | 16,777,216 | 16,777,216 | 1 |
| fc8 | 1000 × 4096 | 4,096,000 | 4,096,000 | 1 |
| conv1 to conv5 | five convolutions | 2,332,704 (3.8%) | 665,784,864 (91.9%) | |
| fc6 to fc8 | three linear layers | 58,621,952 (96.2%) | 58,621,952 (8.1%) | |
| Total | eight weighted layers | 60,954,656 | 724,406,816 |
The five convolutions hold 2,332,704 parameters, under 4% of the network, yet perform 665,784,864 MACs, 92% of the compute. The three linear layers hold 58,621,952 parameters, 96% of the network, yet perform only 58,621,952 MACs, 8% of the compute. The last column explains it: each conv1 weight is used 3,025 times, each conv3 to conv5 weight 169 times, each linear weight once. Large spatial maps early in the network make convolution compute heavy. Large c_i · c_o products late in the network make the linear layers parameter heavy.
Flip the toggle and watch which single layer is largest. Under parameters it is fc6, about 62% of the network on its own. Under MACs it is conv2, about 31%, while conv1, invisible on the parameter bar at 0.06%, grows to 15% because its 34,848 weights each fire 3,025 times. Each layer's exact count is listed below the bar.
- conv134,848 (0.1%)
- conv2307,200 (0.5%)
- conv3884,736 (1.5%)
- conv4663,552 (1.1%)
- conv5442,368 (0.7%)
- fc637,748,736 (61.9%)
- fc716,777,216 (27.5%)
- fc84,096,000 (6.7%)
What this means for making a model faster
The inversion sets the strategy for the rest of the course. Pruning or quantizing the linear layers shrinks Model size dramatically, because that is where the weights are, but it barely changes the MAC count and therefore barely changes computation time. To make inference faster you must attack the convolutions: fewer channels, grouped or depthwise filters as in MobileNet, or a lower input resolution so that h_o · w_o shrinks in every layer at once. The pruning lecture that follows will keep returning to this table, and when you profile a model for the research project, report both columns, not one.
Quick check
Pruning removes 90 percent of the weights in AlexNet's fc6 and fc7. Roughly how much does the network's total MAC count fall?
Recall
Which AlexNet layer family holds most of the parameters, which performs most of the MACs, and why?
A MAC hides two arithmetic operations: a multiply and an add. When each is counted as one floating point operation, one MAC is two FLOPs, and AlexNet's 724,406,816 MACs become 1,448,813,632 FLOPs. The slide rounds that to 1.4G; the exact figure is 1.449G, and showing the factor of two matters more than the rounding.
The count on its own says nothing about time. To get time you need to know how fast a processor can drain it, and that is a different quantity with an almost identical name. FLOPS, with a capital S, is floating point operations per second: a throughput that belongs to the hardware. FLOPs, with a lowercase s, is a plural count that belongs to the model. TOP500 ranks supercomputers by exactly this rate, reporting Linpack results as Rmax and Rpeak in Flop/s, and NVIDIA's GPU performance background guide writes the time a math-limited kernel takes as its number of operations divided by the processor's math bandwidth, which is FLOPs over FLOPS.
This is the computation time term of the latency model on slide 8, where the numerator is a network specification and the denominator a hardware specification. Run AlexNet's count through a few devices and the scale of the answer changes by orders of magnitude while the model does not change at all.
| Processor | Count ÷ rate | T_computation |
|---|---|---|
| 1 GFLOPS | 1.449 × 10^9 / 10^9 | 1,449 ms |
| 100 GFLOPS | 1.449 × 10^9 / 10^11 | 14.5 ms |
| 1 TFLOPS | 1.449 × 10^9 / 10^12 | 1.45 ms |
| 67 TOPS (Orin Nano, INT8 sparse peak) | 1.449 × 10^9 / 6.7 × 10^13 | 0.022 ms |
OPs: the same count for any data type
The F in FLOP assumes floating point arithmetic. A quantized network, the kind you deploy on a microcontroller or an INT8 accelerator, stores weights and activations at an Bit width of 8 bits and multiplies integers. Those are not floating point operations, but they are still operations, and there are still two per MAC. The lecture therefore generalizes: an operation (OP) is one multiply or add of any data type, AlexNet is 724M × 2 = 1.4G OPs, and OPS is operations per second. The layer compute calculator in the second concept of this part uses OPS in its hardware box for exactly this reason: the formula for time does not care whether the operands were floats.
| Term | Kind | Meaning | Belongs to |
|---|---|---|---|
| MAC | Count | One multiply accumulated into a sum | A model, per inference |
| FLOP | Count | One floating point multiply or add; a MAC is two | A model, per inference |
| OP | Count | One operation of any data type; a MAC is two | A model, per inference |
| FLOPS | Rate | Floating point operations per second | A processor |
| OPS | Rate | Operations per second, any data type | A processor |
Quick check
A model needs 3.6G FLOPs and a chip sustains 400 GFLOPS. What is T_computation?
Quick check
Why does the lecture generalize from FLOPs to OPs?
Quick check
A datasheet advertises a board at 67 TOPS. What kind of quantity is that?
Recall
State the rule that turns a MAC count into a FLOP count and a FLOP count into a time, then apply it: AlexNet on a 1 TFLOPS processor.
Recall
Why use OPs rather than FLOPs for an INT8 model, and how many OPs per MAC?
Slide 29 lists the ten works the lecture draws on. Six were used directly in this lecture's counting and examples; the other four appear here as context for the architectures and normalization layers of lecture 02 and the pruning lectures ahead.
Used in this lecture
- Dumoulin and Visin, Convolution arithmetic (GitHub repository and arXiv 1603.07285): the output-shape arithmetic behind every h_o × w_o in this part.
- Stanford CS231n, lecture 5, Image Classification with CNNs: parameter sharing, the output-size formula and im2col.
- Krizhevsky, Sutskever and Hinton, ImageNet Classification with Deep Convolutional Neural Networks, NeurIPS 2012: the AlexNet layers counted on slides 25 and 26.
- Sandler et al., MobileNetV2: Inverted Residuals and Linear Bottlenecks, CVPR 2018: the depthwise design that cuts both parameters and MACs, and the "MAdd" convention.
- Deng, Li, Han, Shi and Xie, Model Compression and Hardware Acceleration for Neural Networks: A Comprehensive Survey, Proceedings of the IEEE 108(4), 2020: the wider map of compression techniques that these metrics evaluate.
- Song Han, MIT 6.5940 TinyML and Efficient Deep Learning Computing, lecture 2: the direct source of slides 23 to 28.
Context for other lectures
- Ioffe and Szegedy, Batch Normalization, ICML 2015.
- Wu and He, Group Normalization, ECCV 2018.
- Simonyan and Zisserman, Very Deep Convolutional Networks for Large-Scale Image Recognition, ICLR 2015.
- He, Zhang, Ren and Sun, Deep Residual Learning for Image Recognition, CVPR 2016.
Recap
If you remember nothing else
- A MAC is a <- a + b·c. A matrix-vector product costs m·n MACs and a matrix-matrix product m·n·k MACs.
- Linear: c_o·c_i. Convolution: c_o·c_i·k_h·k_w·h_o·w_o. Grouped: divide by g. Depthwise: c_o·k_h·k_w·h_o·w_o, with no c_i.
- Convolution MACs equal parameters times h_o·w_o. Linear MACs equal parameters.
- AlexNet performs 724,406,816 MACs. Its convolutions hold 3.8 percent of the parameters but perform 92 percent of the MACs; the linear layers are the reverse.
- One MAC is two FLOPs, so AlexNet is about 1.45G FLOPs, which the slide rounds to 1.4G.
- FLOPs and OPs are counts that belong to a model. FLOPS and OPS are rates that belong to a processor. T_computation = OPs / OPS.
- OPs generalizes FLOPs to quantized integer networks; the count per MAC stays two.
- Conventions differ: many papers and tools report MACs under the label FLOPs. Check before comparing numbers.
Sources
- MIT 6.5940 Fall 2023, Lecture 2: Basics of Deep LearningVideoMIT HAN Lab, Song HanSlides 77 to 85 are the source of slides 23 to 28: MAC, MV, GEMM, per-layer MACs, AlexNet 724M, FLOP, FLOPS, OP, OPS.(opens in a new tab)
- ImageNet Classification with Deep Convolutional Neural NetworksPaperNeurIPS 2012, Krizhevsky, Sutskever and HintonAlexNet: five convolution and three linear layers, about 60M parameters.(opens in a new tab)
- Efficient Processing of Deep Neural Networks: A Tutorial and SurveyPaperProceedings of the IEEE, Sze, Chen, Yang and EmerMAC as the fundamental component; AlexNet at 61M weights and 724M MACs, with FC layers at 58.6M of each.(opens in a new tab)
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision ApplicationsPaperarXiv, Howard et al.Equations 4 and 5 give the depthwise and separable costs; the unnumbered ratio that follows is 1/N + 1/D_K^2, 8 to 9 times less computation; Mult-Adds; GEMM via im2col.(opens in a new tab)
- MobileNetV2: Inverted Residuals and Linear BottlenecksPaperCVPR 2018, Sandler et al.Operations reported as multiply-adds (MAdd).(opens in a new tab)
- Deep Learning, chapter 9: Convolutional NetworksBookMIT Press, Goodfellow, Bengio and CourvilleSection 9.2: parameter sharing keeps forward runtime O(k x n) while cutting storage to k parameters.(opens in a new tab)
- Matrix Multiplication Background User's GuideDocsNVIDIA Deep Learning Performance documentationGEMM costs 2 * M * N * K FLOPs (the page itself writes FLOPS) because each FMA is two operations, a multiply and an add.(opens in a new tab)
- GPU Performance Background User's GuideDocsNVIDIA Deep Learning Performance documentationMath time equals the number of operations divided by the processor's math bandwidth: T_computation = FLOPs / FLOPS when math limited.(opens in a new tab)
- torch.nn.Conv2dDocsPyTorch documentationOutput-size formula, groups semantics, groups = in_channels as depthwise convolution.(opens in a new tab)
- CS231n Convolutional Neural Networks for Visual Recognition: course notesDocsStanford UniversityParameter sharing, output-size arithmetic, AlexNet conv1 at 55 x 55 x 96, im2col.(opens in a new tab)
- TOP500 List, November 2024DocsTOP500Rmax and Rpeak columns reported in PFlop/s.(opens in a new tab)
- fvcore FlopCountAnalysisDocsDetectron2 documentation, MetaCounts one fused multiply-add as one flop: the opposite convention to this course.(opens in a new tab)
- Multiply-accumulate operationArticleWikipediaDefinition a <- a + (b x c) and the IEEE 754-2008 fused multiply-add. Used only for the definition.(opens in a new tab)
- Jetson modulesDocsNVIDIA DeveloperJetson Orin Nano up to 67 TOPS and AGX Orin up to 275 TOPS.(opens in a new tab)
- Jetson Orin specificationsDocsNVIDIAThe TOPS figures are sparse INT8; dense INT8 ratings are about half, for example 170 sparse against 85 dense.(opens in a new tab)
- Model Compression and Hardware Acceleration for Neural Networks: A Comprehensive SurveyPaperProceedings of the IEEE 108(4), Deng, Li, Han, Shi and XieReference 9 on slide 29.(opens in a new tab)
- A guide to convolution arithmetic for deep learningDocsDumoulin and Visin, GitHub and arXiv 1603.07285Reference 1 on slide 29: output-shape arithmetic.(opens in a new tab)