Majid Al-RaimiReference sheet

COE 592Lecture 03Reference

Reference sheet

Neural network efficiency metrics compressed onto one page: the definitions, formulas and numbers to have in your head before a quiz or exam.

Efficiency triangle

Three goals, three costs, two resources. Memory feeds all three costs; computation feeds latency and energy. Part 01: Latency, throughput and energy

GoalCostResourceMetrics that predict it
SmallerStorageMemory#parameters, model size
FasterLatencyComputation and memoryMACs, OPs, model size, peak #activations
GreenerEnergyComputation and memoryMACs, OPs, and above all the bytes moved
Goal, cost, resource and the metrics that predict it
GroupMembersHow obtainedPredicts
Memory-related#parameters, model size, total and peak #activationsCounted on paper from layer shapesStorage, and the memory share of latency and energy
Computation-relatedMACs, FLOPs and FLOPS, OPs and OPSCounts on paper, rates from the datasheetThe computation share of latency and energy
The two metric groups

Latency and throughput

PropertyLatencyThroughput
DefinitionDelay to complete one specific taskWork completed per unit time
Also calledResponse time, execution timeBandwidth
Unitmsimage/s
Measured atBatch size 1Whatever batch fills the processor
Who caresA brake camera, an on-device detectorA data center indexing video overnight
BatchingRaises it: each input waits for the whole batchRaises it: more inputs finish per second
Definitions side by side
throughput=bTbatch,latency=Tbatch\text{throughput} = \frac{b}{T_{\text{batch}}}, \qquad \text{latency} = T_{\text{batch}}
b inputs processed together in one batch of duration T_batch
DesignHow inputs are handledLatencyInputs per batchThroughput
Design 1One image at a time50 ms11 / 0.050 = 20 image/s
Design 2Four images together100 ms44 / 0.100 = 40 image/s
Server, batch 1One request2 ms1500 image/s
Server, batch 32Thirty-two requests8 ms324000 image/s
Batching raises both latency and throughput

The four conditions every reported latency must state (slide 5 footnote)

Hardware
NVIDIA Jetson AGX Orin: 43 dense FP16 TFLOPS on Tensor Cores, 204.8 GB/s LPDDR5
Runtime
TensorRT
Precision
fp16
Batch size
1
ModelmIoUParametersMACsOrin latencyA100 throughput
SegFormer-B582.485M1460G638 ms12 image/s
EfficientViT-L182.740M282G45.9 ms122 image/s
Ratio+0.32.1x fewer5.2x fewer13.9x faster10x more
SegFormer-B5 versus EfficientViT-L1 on Cityscapes at 1024 x 2048

The latency model

Data movement and computation overlap on real hardware, so the layer finishes when the slower stream finishes. Numerators are NN specification, denominators are hardware specification. Part 01: Latency, throughput and energy

Latencymax(Tcomputation,  Tmemory)\text{Latency} \approx \max\left(T_{\text{computation}},\; T_{\text{memory}}\right)
Max, not sum: the two streams overlap
Tcomputationoperations in the modeloperations per second,TmemoryTmove(activations)+Tmove(weights)T_{\text{computation}} \approx \frac{\text{operations in the model}}{\text{operations per second}}, \qquad T_{\text{memory}} \approx T_{\text{move}}(\text{activations}) + T_{\text{move}}(\text{weights})
The memory terms add because both cross the same bus
Tmove(weights)model sizebandwidth,Tmove(activations)input size+output sizebandwidthT_{\text{move}}(\text{weights}) \approx \frac{\text{model size}}{\text{bandwidth}}, \qquad T_{\text{move}}(\text{activations}) \approx \frac{\text{input size} + \text{output size}}{\text{bandwidth}}
Bytes over bytes per second
TermNumerator: NN specificationDenominator: hardware specification
T_computationNumber of operations (MACs, OPs)Operations per second (OPS)
T_move(weights)Model sizeMemory bandwidth
T_move(activations)Input activation size + output activation sizeMemory bandwidth
Each term, and which part of the lecture counts its numerator
ConfigurationT_computationT_memoryLatencyRegime
100 GOPS, 32-bit weights10 ms5 + 2 = 7 ms10 msCompute-bound
200 GOPS, 32-bit weights5 ms5 + 2 = 7 ms7 msMemory-bound
200 GOPS, 8-bit weights5 ms1.25 + 2 = 3.25 ms5 msCompute-bound
Toy model: 1 GOP of work, 50 MB weights, 20 MB activations, 10 GB/s
ModelT_computationT_memoryFloorMeasuredMiss, utilisation
EfficientViT-L1564 / 43 000 = 13.1 ms0.39 + 0.45 = 0.84 ms13.1 ms45.9 ms3.5x, about 29%
SegFormer-B52920 / 43 000 = 68 ms0.83 + 0.45 = 1.3 ms68 ms638 ms9.4x, about 11%
Slide 5 predicted from Orin datasheet numbers, fp16, 43 TFLOPS, 204.8 GB/s

Energy of data movement

45 nm, 0.9 V, 32-bit operations, Horowitz ISSCC 2014 as tabulated by Han et al. 2015. Part 01: Latency, throughput and energy

Rough energy per operation (slide 9)

int ADD
0.1 pJ
float ADD
0.9 pJ
Register file access
1 pJ
int MULT
3.1 pJ
float MULT
3.7 pJ
SRAM cache access
5 pJ
DRAM memory access
640 pJ
RatioArithmeticMeaning
DRAM over int MULT640 / 3.1 ≈ 206The slide's 200x
DRAM over one int MAC640 / 3.2 = 200Exact reading of the slide icon
DRAM over SRAM640 / 5 = 128Why on-chip storage is worth two decades
float ADD over int ADD0.9 / 0.1 = 9The seed for quantization
DRAM over int ADD640 / 0.1 = 6400Three orders of magnitude (Han et al.)
Ratios worth memorizing
ItemCountEnergy eachTotal
Integer MACs724M3.1 + 0.1 = 3.2 pJ2.32 mJ
Weights read once from DRAM61M640 pJ39.0 mJ
Same weights read from SRAM61M5 pJ0.31 mJ
Float MACs instead of integer724M3.7 + 0.9 = 4.6 pJ3.33 mJ
One AlexNet inference: 724M MACs, 61M weights

Counting formulas for parameters and MACs

Bias is ignored throughout. Parameters never depend on n, h or w; MACs multiply by h_o · w_o because every filter is reapplied at every output position. Part 02: Parameters and model size, Part 04: MACs, FLOPs and operations

Notation

n
Batch size
c_i, c_o
Input and output channels (features for a linear layer)
h_i, w_i, h_o, w_o
Input and output height and width
k_h, k_w
Kernel height and width
g
Number of groups in a grouped convolution
Layer#ParametersMACsMACs per parameter
Linearc_o · c_ic_o · c_i1
Convolutionc_o · c_i · k_h · k_wc_o · c_i · k_h · k_w · h_o · w_oh_o · w_o
Grouped convolutionc_o · c_i · k_h · k_w / gc_o · c_i · k_h · k_w · h_o · w_o / gh_o · w_o
Depthwise convolutionc_o · k_h · k_wc_o · k_h · k_w · h_o · w_oh_o · w_o
Pooling00
Per layer type, batch size 1
#Paramsgrouped=cogcigkhkwg=cocikhkwg\#\text{Params}_{\text{grouped}} = \frac{c_o}{g}\cdot\frac{c_i}{g}\cdot k_h k_w \cdot g = \frac{c_o \cdot c_i \cdot k_h \cdot k_w}{g}
Each group loses g squared, but there are g groups: one division by g
MACsMV=mnMACsGEMM=mnkaa+bc\text{MACs}_{MV} = m \cdot n \qquad \text{MACs}_{GEMM} = m \cdot n \cdot k \qquad a \leftarrow a + b \cdot c
Matrix-vector, matrix-matrix, and the MAC itself
LayerCount#ParametersRatio
Standard, g = 1256 · 256 · 9589,8241
Grouped, g = 2256 · 256 · 9 / 2294,9121/2
Grouped, g = 8256 · 256 · 9 / 873,7281/8
Depthwise, g = 256256 · 92,3041/256
One 3 x 3 layer, 256 in and 256 out, under each grouping
LayerCountMACs
Standard 3 × 364 · 32 · 9 · 112 · 112231,211,008
Depthwise 3 × 332 · 9 · 112 · 1123,612,672
Pointwise 1 × 164 · 32 · 112 · 11225,690,112
Separable over standard1 / 64 + 1 / 90.1267, about 7.9x fewer
MobileNet block at 112 x 112, 32 to 64 channels: MACs

AlexNet, every column

Input 3 × 224 × 224. The linear layers hold 96% of the weights and do 8% of the MACs; the convolutions are the reverse. Part 02, Part 03, Part 04

LayerOutput C × H × WActivations#ParametersMACsMACs per parameter
Image3 × 224 × 224150,52800
conv1, 11 × 11, stride 4, pad 296 × 55 × 55290,40034,848105,415,2003,025
maxpool96 × 27 × 2769,98400
conv2, 5 × 5, pad 2, g = 2256 × 27 × 27186,624307,200223,948,800729
maxpool256 × 13 × 1343,26400
conv3, 3 × 3, pad 1384 × 13 × 1364,896884,736149,520,384169
conv4, 3 × 3, pad 1, g = 2384 × 13 × 1364,896663,552112,140,288169
conv5, 3 × 3, pad 1, g = 2256 × 13 × 1343,264442,36874,760,192169
maxpool256 × 6 × 69,21600
fc6, 9,216 → 409640964,09637,748,73637,748,7361
fc7, 4096 → 409640964,09616,777,21616,777,2161
fc8, 4096 → 100010001,0004,096,0004,096,0001
Five convolutions2,332,704 (3.8%)665,784,864 (91.9%)
Three linear layers58,621,952 (96.2%)58,621,952 (8.1%)
Total932,26460,954,656724,406,816
Output shape, output activations, parameters, MACs and uses per weight, per layer

Model size from bit width

Model Size=#ParametersBit Width,SizeMB=#Parametersbits8106\text{Model Size} = \#\text{Parameters} \cdot \text{Bit Width}, \qquad \text{Size}_{\text{MB}} = \frac{\#\text{Parameters} \cdot \text{bits}}{8 \cdot 10^{6}}
The product is in bits; divide by 8 for bytes and by 10^6 for decimal MB
Model Size=l#Paramslbitsl\text{Model Size} = \sum_{l} \#\text{Params}_l \cdot \text{bits}_l
Mixed precision: sum per layer
Data typeSize in bitsSize (decimal MB)Slide rounding
fp321,950.5 Mbit243.8 MB244 MB
fp16 or bf16975.3 Mbit121.9 MB122 MB
int8487.6 Mbit61.0 MB61 MB
int4243.8 Mbit30.5 MB30.5 MB
1-bit61.0 Mbit7.6 MB7.6 MB
AlexNet at each bit width, exact count 60,954,656 and the slide's rounded 61M
  • 1 MB = 10^6 B and 1 MiB = 2^20 B (NIST). The slide's 244 MB is decimal, about 232.5 MiB. Say which you use.
  • The 8-bit size is simply N bytes. Forgetting the divide by 8 overstates the size eightfold and is the most common error on this question.
  • Halving the bit width halves the size, not the accuracy; those are separate questions.

Activations decide fit

SRAM constrains activations (read and write); flash constrains model size (read only). SRAM is the scarcer budget, so peak activation, not parameter count, decides whether a CNN runs on a microcontroller. Part 03: Activations and peak memory

TensorLives inAccess patternMetricBudget
WeightsFlashRead only, written once at flashingModel sizeLarger, about 1 MB to 2 MB
ActivationsSRAMWritten and read on every inferencePeak #activationsSmaller, 256 kB to 512 kB
Two budgets on a microcontroller
ModelParam memory (flash)Peak activation (SRAM)Fits F746?Fits H743?
ResNet-1811.2 MB0.9 MBNoNo
MobileNetV2-0.752.5 MB1.7 MBNoNo
MCUNet1.9 MB0.49 MBNoYes
Ratio ResNet-18 to MobileNetV24.6x smaller1.8x larger
Inference, int8, about 70 percent ImageNet top-1, against STM32F746 (1 MB flash, 320 kB SRAM) and STM32H743 (2 MB flash, 512 kB SRAM)
ModelWeightsActivationsActivations over weights
ResNet-50102 MB707 MBAbout 7x
MobileNetV2-1.4About 24 MBAbout 640 MBAbout 27x
Ratio4.3x smaller1.1x smaller
Training, fp32, batch size 16 (TinyTL)
LWi=aiTLai+1,Lbi=Lai+1\frac{\partial L}{\partial W_i} = a_i^{\mathsf{T}}\,\frac{\partial L}{\partial a_{i+1}}, \qquad \frac{\partial L}{\partial b_i} = \frac{\partial L}{\partial a_{i+1}}
Backprop needs the stored input a_i for the weight gradient, but not for the bias gradient
ModeWhich activations are heldMemory rule
InferenceInput and output of the layer running nowLargest input + output pair
TrainingEvery layer input a_i until dL/dW_i is computedSum of all activations × batch size, plus weights, gradients and optimizer state
What must be alive at once
StageShapeDominant tensorWhat it decides
Early layersLarge H × W, few channelsActivationsSRAM peak; patch-based inference fixes it
Middle layersBoth smallNeitherCheapest place for on-device training
Late layersLarge C_o · C_i, tiny H × WWeightsFlash budget; pruning and quantization pay here
Where memory sits across depth: resolution shrinks faster than channels grow

Total and peak activations

#acttotal=x0+i=1Lai,#actpeakmaxi(ai1+ai)\#\text{act}_{\text{total}} = |x_0| + \sum_{i=1}^{L} |a_i|, \qquad \#\text{act}_{\text{peak}} \approx \max_{i}\big(|a_{i-1}| + |a_i|\big)
Sum every tensor for the total; scan adjacent input + output pairs for the peak

AlexNet adjacent pair sums; the first pair wins

conv1 (image + conv1 out)
150,528 + 290,400 = 440,928
pool1 (conv1 out + pool1 out)
290,400 + 69,984 = 360,384
conv2 (pool1 out + conv2 out)
69,984 + 186,624 = 256,608
pool2 (conv2 out + pool2 out)
186,624 + 43,264 = 229,888
conv3 to fc8
All under 130,000; fc8 is 4,096 + 1,000 = 5,096
QuantityValues8-bit32-bit
Peak440,928441 kB1.76 MB
Total932,264932 kB3.73 MB
MobileNetV2 first conv150,528 + 401,408 = 551,936552 kB (539 KiB)2.21 MB
Values to bytes (decimal kB and MB)
  • Assumptions behind the approximation: layer-by-layer execution, weights streamed from flash and not counted, no residual branches (else add all branches alive at once, sharing the input), no in-place tricks (a depthwise layer can run in N + 1 instead of 2N), plus real runtimes add scratch buffers.
  • The peak is a pair, never the largest single tensor. Forgetting the input undercounts AlexNet by a third.
  • The total (932,264) is what training holds; inference holds only the peak pair (440,928, about 47% of the total).
  • Per-layer input + output is also the activation traffic in T_move(activations) of the latency model.

FLOPs, OPs and computation time

Counts belong to models, rates belong to chips. Divide a count by a rate and you have a time. Part 04: MACs, FLOPs and operations

FLOPs=2MACs,FLOPS=FLOPssecond,Tcomputation=FLOPs of the modelFLOPS of the processor\text{FLOPs} = 2 \cdot \text{MACs}, \qquad \text{FLOPS} = \frac{\text{FLOPs}}{\text{second}}, \qquad T_{computation} = \frac{\text{FLOPs of the model}}{\text{FLOPS of the processor}}
One multiply plus one add per MAC; OPs and OPS are the same with any data type
TermKindMeaningBelongs to
MACCountOne multiply accumulated into a sumA model, per inference
FLOPCountOne floating point multiply or add; a MAC is twoA model, per inference
OPCountOne operation of any data type; a MAC is twoA model, per inference
FLOPSRateFloating point operations per secondA processor
OPSRateOperations per second, any data typeA processor
Five terms, two kinds of quantity
ProcessorCount ÷ rateT_computation
1 GFLOPS1.449 × 10^9 / 10^91,449 ms
100 GFLOPS1.449 × 10^9 / 10^1114.5 ms
1 TFLOPS1.449 × 10^9 / 10^121.45 ms
67 TOPS (Orin Nano, INT8 sparse peak)1.449 × 10^9 / 6.7 × 10^130.022 ms
AlexNet's 1.449G FLOPs on different processors

Procedures

Six steps that cover every numeric question in this lecture

1. Parameters
One formula instance per weighted layer, pooling is 0, write /g explicitly, sum. Add a share column if asked which layer dominates.
2. Model size
Bits first (N × bits), divide by 8 for bytes, then by 10^6, and say "decimal MB".
3. Activations
Write the C × H × W column, sum for the total, scan adjacent input + output pairs for the peak, multiply by bytes per value last. State the layer-by-layer assumption.
4. MACs
Parameter formula with h_o · w_o appended; h_o · w_o = 1 for a linear layer; c_i drops out of depthwise.
5. Time
FLOPs = 2 × MACs, then T_computation = FLOPs / FLOPS in milliseconds. Note that a peak rate gives a floor, not a prediction.
6. Regime
Compare T_computation with T_memory; the larger one names the bound and the lever that helps.

Slide errata

Answer with the corrected fact and name the slide if a question depends on it. Part 02, Part 03, Part 04

What the slides get wrong or leave out

Slide 5
46 ms is the rounded 45.9 ms; the 82.4 mIoU is single-scale, the paper's 84.0 is multi-scale.
Slide 8
The MIT original has a Load Input, Load Weight, Compute, Store Output pipeline above the formula. That overlap is the whole reason for max rather than plus.
Slide 9
The 200x arrow is 640 / 3.1 ≈ 206 (int MULT) or 640 / 3.2 = 200 (int MAC). Memory rows are halved from Horowitz's 64-bit figures, via Han et al. 2015.
Slides 15, 16
conv1 is printed as 24,848; the product 96 · 3 · 11 · 11 is 34,848. Exact total 60,954,656; the rounded 61M stands.
Slide 18
Right chart x-axis reads 14, 16, 16, 17, should be 14, 15, 16, 17. Peak activation grew 1.8x, it did not merely "not improve". The right chart is MCUNetV2 (2021) Figure 1, not MCUNet (2020).
Slide 20
The figure is Figure 11(a) of On-Device Training Under 256KB Memory (NeurIPS 2022), a per-layer training update cost, not an inference profile; the x-axis (layers 0 to 42) was cropped.
Slide 21
The peak formula is stated without its assumptions: layer-by-layer execution, no residual branches, no in-place operations, no scratch buffers.
Slide 23
The GEMM figure is loosely labelled. Read m · n · k as rows of C times columns of C times the shared dimension.
Slides 24, 25
The grouped MAC formula appears in two factor orders; both equal c_o · c_i · k_h · k_w · h_o · w_o / g.
Slide 27
"1.4G FLOPs" is 2 × 724,406,816 = 1,448,813,632, so 1.45G exactly. Show the factor of two.