2021. 3. 30. 14:46ㆍDeep Learning/framework
yolov5 의 detection.py코드를 보면 이러한 부분이 있다.
import torch.backends.cudnn as cudnn
.
.
.
cudnn.benchmark = True # set True to speed up constant image size inference
일정한 이미지 사이즈의 inference의 속도를 높이려면 True로 setting하라
torch.backends.cudnn.benchmark는 True/False로 설정할 수 있습니다.
discuss.pytorch.org/t/what-does-torch-backends-cudnn-benchmark-do/5936/9
What does torch.backends.cudnn.benchmark do?
I have a question. e.g. I have a net1 whose input sizes don’t vary. And I have other nn.Module named net_Loss whose input sizes vary. I only need to optimeze net1’s parameters. So should I use the cudnn.benchmark True or False? Thank you !
discuss.pytorch.org
cudnn에서의 benchmark mode를 활성화할 것인가?!
- cudnn auto tuner를 활성화하여, hardware에 맞게 사용할 optimal 알고리즘을 찾는다.(tensor size, gpu memory따라 효율적인 conv algorithm을 찾아줌)
- 입력 이미지 크기가 자주 변하지 않는다면, 초기 시간이 소요되지만 일반적으로 더 빠른 런타임의 효과를 볼 수 있다.
- 그러나, 각 iteration마다 입력 이미지 크기가 변경된다면 런타임성능이 오히려 저하될 수 있다.
그렇다면,, False로 설정하였을 때의 default conv algorithm은 무엇인지,,, True일 때 pytorch가 선택하는 conv알고리즘의 선택지는 어디까지인지,,, 궁금해서 좀 찾아봤습니다.
www.programmersought.com/article/29992204468/
torch.backends.cudnn.benchmark ?! - Programmer Sought
All the time in training depth learning model, often use the GPU to accelerate training of the network. But talking abouttorch.backends.cudnn.benchmark The GPU-related flag, some people may feel unfamiliar. In general scenarios, simply PyTorch program at t
www.programmersought.com
가장 간단한 구현은 output for each channel이 될 각 입력 이미지에 대해 multi-layer nested loop를 사용하는 것이다. 각 입력 채널에 대해, convolution operation을 위해 convolution kernel로 지정된 영역을 선택한 다음, 전체 이미지가 처리될 때까지, 이 방법을 일반적으로 direct method이라고 한다. 방법은 간단하지만, 그렇게 많은 사이클을 보기 위해, --> 일반적으로 효율성이 그리 높지 않다 (가장 기본적인 conv연산을 말하는 듯 하다)
convolution layer를 기반으로 하는 알고리즘은 GEMM(General Matrix Multiple) 기반, Winograd algorithm 등이며, 각 알고리즘은 FFT의 일부 변형을 기반으로 하는 자체 알고리즘을 가지고 있다. open-source C ++ library triNNity에서 전자는 컨볼루션 알고리즘을 확산하기 위해 거의 80종이나 실현된다!
각 conv 알고리즘은 각자의 고유한 장점을 가지고 있고, conv kernel이 큰 몇몇 알고리즘은 매우 빠릅니다.
몇몇 case에서 몇몇 알고리즘은 상대적으로 memory 사용량을 적게 가져갑니다.
resnet-101같은 cnn이 주어졌을 때 , 주어진 hardware platform에서, network가 input image size를 사용하는 가장 쉬움 방법은 모든 레이어에 대해 같은 conv알고리즘을 사용하는 것입니다. (direct algorithm같은) 그러나 이 연산은 optimal하지 않을 것 입니다. 더 좋은 접근은, 사전에 간단한 optimization test를 진행하고, 각 layer에서 가장 적절한 conv알고리즘을 선택할 수 있게 하는 것이다. 그 후에 전체 network를 실행했을 때, 효율성은 아주 많이 향상될 것.
항상 같은 이미지로 network를 training시키지 않는데, 즉, input network가 계속 변하는데, 어떻게 미리 optimal 알고리즘을 지정하고 적용시킬 수 있을까?
-> 이유는, 주어진 input에 대해 이 input이 어떤 값을 가지고 있느냐는 conv의 running time에 영향을 미치지 않고, 오직 input 크기만이 영향을 미치기 때문이다.
(For example, we are just fixed input size (8, 64, 224, 224), i.e. batch_size 8, the input channel 64, the width and height is 224, then the running time is almost constant convolution layer , whether each particular pixel value is 1000 or 0.1.)
cudnn.benchmark가 몇몇 case에서는 오히려 runtime을 늘릴 수도 있는 것이 사실이다.
그래서 convolution layer running time에 어떤 요인이 영향을 미치는지 아는 것이 중요하다.
1. convolution layer 의 parameters( kernel size, stride, dilation, padding등 모두 포함하여)
2. relevant input parameters( input의 w, h, 개수, input과 output의 채널 수 등)
3. hardware platform, input과output의 accuracy, layout등의 다른 요인들
이러한 것들을 고려하여 cudnn.benchmark가 효율적일지, 아닐지를 판단하여 사용하면 될 것 같습니다 : )
'Deep Learning > framework' 카테고리의 다른 글
[Pytorch] torch.no_grad (0) | 2021.05.22 |
---|---|
[PyTorch] tensor 부등호 연산 (?), list와 차이점 (0) | 2021.05.13 |
[Pytorch] gpu가 인식이 안 되는 오류 - Linux 에서 Nvidia driver설치 (0) | 2021.04.05 |
[PyTorch] model.half() (0) | 2021.03.26 |
yaml (+ xml, json) (0) | 2021.02.02 |