Branch Log · Open in interactive viewer →

Lecture 02 - Basics of Neural Networks

Lecture 02 - Basics of Neural Networks | MIT 6.S965


2.5 Efficiency Metrics

보통 network를 설계할 때, 크게 세 가지 요소를 고려한다.

그렇다면 다른 network 사이에서 efficiency(효율성)을 비교할 때 어떤 지표를 사용하여 비교해야 할까?

metrics

storage가 weight만 의미한다면, memory는 추가로 activation까지 고려한다.


2.5.1 Latency

Neural Network 추론에 있어서, latency(지연시간)는 NN 자체의 특성과 hardware 특성에 모두 영향을 받는다. 예를 들어 pipelining을 이용하면 computation과 data movement는 동시에 이루어질 수 있다.

latency

병렬화가 가능한 자원만 충분하다면 latency는 다음 수식으로 계산할 수 있다.

$$ Latency \approx \max(T_{computation}, T_{memory}) $$

분모는 hardware 특성, 분자는 NN 특성이다.

$$ \approx { {Number \ of \ Operations \ of \ NN} \over {Number \ of \ Opeartions \ that \ Processor \ can \ Process \ Per \ Second} } $$

$$ \approx T_{data \ movement \ of \ activations} + T_{data \ movement \ of \ weights} $$

분모는 hardware 특성, 분자는 NN 특성이다.

weight를 SRAM에 모두 저장하면, main memory에 접근하지 않을 수 있다.

$$ \approx { {Model \ size} \over {Memory \ Bandwidth \ of \ Processor} } $$

$$ \approx { {Input \ activation \ size + Output \ activation \ size} \over {Memory \ Bandwidth \ of \ Processor} } $$


2.5.2 Energy Consumption

Energy Consumption

(생략)


2.5.3 Number of Parameters (#Parameters)

#Parameters는 기본적으로 총 weight 개수를 의미한다. 여러 레이어 종류에 따른 #Parameters를 구해보자.(bias는 무시)

$W^{T}$ : 입력 채널 수( $c_i$ )와 출력 채널 수( $c_o$ )를 곱하면 된다.

linear layer

$$ c_o \cdot c_i $$

filters

$$ c_o \cdot c_i \cdot k_h \cdot k_w $$

grouped convolution

$$ c_o/g \cdot c_i/g \cdot k_h \cdot k_w \cdot g $$

$$ = c_o \cdot c_i \cdot k_h \cdot k_w / g $$

depthwise convolution

$$ = c_o \cdot k_h \cdot k_w $$

   📝 예제 1: AlexNet #Parameters    

AlexNet의 #Parameters를 구하라. 단, bias는 무시한다.

AlexNet example

   🔍 풀이   

레이어별 #parameters를 구해보자.

$$ 96 \times 3 \times 11 \times 11 = 24,848 $$

$$ { {256 \times 96 \times 5 \times 5} \over {2} } = 307,200 $$

$$ 384 \times 256 \times 3 \times 3 = 884,736 $$

$$ 384 \times 384 \times 3 \times 3 / 2 = 663,552 $$

$$ 256 \times 384 \times 3 \times 3 / 2 = 442,368 $$

$$ 4096 \times (256 \times 6 \times 6) = 37,738,736 $$

$$ 4096 \times 4096 = 16,777,216 $$

$$ 1000 \times 4096 = 4,096,000 $$

모든 레이어의 #parameters를 합치면 총 61M이다.


2.5.4 Model Size

model size는 weight가 동일한 bit width를 가진다면 간단히 구할 수 있다.

bit width가 다른 mixed precision model은 계산이 달라진다.

   📝 예제 2: AlexNet model size    

AlexNet이 #Parameters를 61M만큼 갖는다고 할 때, 각 조건에서의 model size를 구하라.

   🔍 풀이   

$$ 61M \times 4Byte = 224MB $$

$$ 61M \times 1Byte = 61MB $$

$1MB = 1 \times 10^6 Bytes$


2.5.5 Number of Activations (#Activations)

ResNet과 MobileNetV2를 비교해 보자.

ResNet vs MobileNet

이는 MobileNetV2의 특정 레이어에서 #activations이 memory bottleneck을 일으키는 구조이기 때문이다.

MCUNetV2: Memory-Efficient Patch-based Inference for Tiny Deep Learning 논문

MBV2 activation

또한 훈련 중에도 memory bottleneck의 주된 원인은 #Parameter가 아닌 #activations이다.

memory bottleneck in training

   📝 예제 3: AlexNet #Activations    

AlexNet의 (1) Total #Activations, (2) Peak #Activations를 구하라.

AlexNet example

   🔍 풀이   

$$ 3 \times 224 \times 224 = 150,528 $$

$$ 96 \times 55 \times 55 = 290,400 $$

$$ 96 \times 27 \times 27 = 69,984 $$

...

$$ 256 \times 6 \times 6 = 9,216 $$

$$ 4096 = 4096 $$

$$ 4096 = 4096 $$

$$ 1000 = 1000 $$

따라서 Total #Activations, Peak #Activations은 다음과 같다.

$$ = 932,264 $$

$$\approx input \ activations + output \ activations $$

$$= 150,528 + 290,400 = 440,928$$


2.5.6 MACs

computation efficiency를 표현하는 대표적인 지표인 MAC(Multiply-Accumulate) operations를 살펴보자.(MAC 연산은 CNN 연산의 대부분을 차지한다.)

우선 Multiply-Accumulate operation(MAC)은 다음과 같이 곱셈과 덧셈으로 이루어진 연산을 의미한다.

$$ a \leftarrow a + b \cdot c $$

두 가지 대표적인 연산에서 MACs를 구해보자.

Matrix-Vector Multiplication 연산에서 MACs는 다음과 같이 계산할 수 있다.

![Matrix-Vector](https://raw.githubusercontent.com/erectbranch/MIT-Efficient-AI/master/2022/lec02/summary02/images/Matrix-Vector.png)

$$ MACs = m \cdot n $$

Matrix-Matrix 연산에서 MACs는 다음과 같이 계산할 수 있다.

![General Matrix-Matrix](https://raw.githubusercontent.com/erectbranch/MIT-Efficient-AI/master/2022/lec02/summary02/images/General_Matrix-Matrix.png)

$$ MACs = m \cdot n \cdot k $$

이번에는 여러 레이어 종류별로 계산해 보자.(batch size n=1로 가정)

linear layer

$$ MACs = c_o \cdot c_i $$

2D convolution

$$ MACs = c_i \cdot k_h \cdot k_w \cdot c_o \cdot w_o \cdot c_o $$

$w_o, c_o$ 계산에 주의.

다시 말해 output activation 각 픽셀마다 $c_i \cdot k_h \cdot k_w \cdot c_o$ 만큼의 MACs를 갖는다는 뜻이기도 하다.

grouped convolution

$$ MACs = c_i/g \cdot k_h \cdot k_w \cdot c_o \cdot w_o \cdot c_o $$

depthwise convolution

$$ MACs = k_h \cdot k_w \cdot c_o \cdot w_o \cdot c_o $$

   📝 예제 4: AlexNet #MACs    

AlexNet의 #MACs를 구하라.

AlexNet example

   🔍 풀이   

$$ 96 \times 3 \times 11 \times 11 \times 55 \times 55 = 105,415,200 $$

$$ 256 \times 96 \times 5 \times 5 \times 27 \times 27 / 2 = 223,948,800 $$

...

$$ 4096 \times (256 \times 6 \times 6) = 37,748,736 $$

$$ 4096 \times 4096 = 16,777,216 $$

$$ 1000 \times 4096 = 4,096,000 $$

따라서 총 MACs는 724M이다.


2.5.7 FLOP

MAC과 마찬가지로 computation과 관련된 대표적인 지표로 FLOP(Floating Point Operations)이 있다.

processor의 성능 지표인 FLOPS(Floating Point Operation Per Second)와 구분할 것

만약 operations이 다음과 같은 data type이라면, 1 MAC = 2 FLOP이다.

   📝 예제 5: AlexNet #FLOP    

AlexNet의 FLOPs를 구하라. AlexNet은 총 MACs를 724M개를 갖는다고 한다.

   🔍 풀이   

$$ 724M \times 2 = 1.4G $$