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는 다음 수식으로 계산할 수 있다.

Latencymax(Tcomputation,Tmemory)

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

Number of Operations of NNNumber of Opeartions that Processor can Process Per Second Tdata movement of activations+Tdata movement of weights

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

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

Model sizeMemory Bandwidth of Processor Input activation size+Output activation sizeMemory Bandwidth of Processor

2.5.2 Energy Consumption

Energy Consumption

(생략)


2.5.3 Number of Parameters (#Parameters)

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

WT

: 입력 채널 수( ci

)와 출력 채널 수( co

)를 곱하면 된다.

linear layer

co·ci

filters

co·ci·kh·kw

grouped convolution

co/g·ci/g·kh·kw·g =co·ci·kh·kw/g

depthwise convolution

=co·kh·kw

   📝 예제 1: AlexNet #Parameters    

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

AlexNet example

   🔍 풀이   

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

96×3×11×11=24,848 256×96×5×52=307,200 384×256×3×3=884,736 384×384×3×3/2=663,552 256×384×3×3/2=442,368 4096×(256×6×6)=37,738,736 4096×4096=16,777,216 1000×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×4Byte=224MB 61M×1Byte=61MB

1MB=1×106Bytes


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×224×224=150,528 96×55×55=290,400 96×27×27=69,984

...

256×6×6=9,216 4096=4096 4096=4096 1000=1000

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

=932,264 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)은 다음과 같이 곱셈과 덧셈으로 이루어진 연산을 의미한다.

aa+b·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·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·n·k

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

linear layer

MACs=co·ci

2D convolution

MACs=ci·kh·kw·co·wo·co

wo,co 계산에 주의.

다시 말해 output activation 각 픽셀마다 ci·kh·kw·co 만큼의 MACs를 갖는다는 뜻이기도 하다.

grouped convolution

MACs=ci/g·kh·kw·co·wo·co

depthwise convolution

MACs=kh·kw·co·wo·co

   📝 예제 4: AlexNet #MACs    

AlexNet의 #MACs를 구하라.

AlexNet example

   🔍 풀이   

96×3×11×11×55×55=105,415,200 256×96×5×5×27×27/2=223,948,800

...

4096×(256×6×6)=37,748,736 4096×4096=16,777,216 1000×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×2=1.4G