Open heojae opened 3 years ago
저는 학습
이 아닌 inference
관점에서, 분석하기 위해 원본 깃 레포 에서 필요한 부분만 빼내어서 변형하여 실험 하였습니다.
이걸 현재 레포에 추가할까 고민했지만, 레포와는 별개의 실험이니, 요기에 정리하기로 하였습니다.
article_implem.py
utils
- memory.py
- plot.py
article_implem.py
plot.py
memory.py
두 경우 모두, model
과 input
의 사이즈는 동일하게 들어 갑니다. 하지만, model
의 layer
들의 output
을 저장하느냐에 따라서, 아래와 같이 다른 결과들을 낼 수 있었다고 판단됩니다.
동일한 시작
시작할 때에는 각 값에, 동일한 메모리가 할당 된것을 볼 수 있습니다.
Allocated: 44.7MB, 'Max Allocated: 44.7MB, Cached: 64.0MB, Max Cached: 64.0 MB, NVIDIA_SMI: 539 MiB MB
------------------------------------ after upload input to gpu ---------------------------------------------
Allocated: 45.3MB, 'Max Allocated: 45.3MB, Cached: 66.0MB, Max Cached: 66.0 MB, NVIDIA_SMI: 541 MiB MB
------------------------------------ 1 Epoch ----------------------------------
Allocated: 45.3MB, 'Max Allocated: 45.3MB, Cached: 66.0MB, Max Cached: 66.0 MB, NVIDIA_SMI: 541 MiB MB
train mode
아래의 경우를 자세히 살펴보면, 1,2,3 epoch
모두 동일한 그래프를 그리고 있는 것을 확인할 수 있습니다.
그러나, model의 layer
들을 거치면서, Allocated memory
가 계속 증가하는 것을 확인할 수 있었으며,
NVIDIA SMI
결과 값 또한 증가하는 것을 볼 수 있었습니다.
마지막 결과 값으로, 그래프 상 약 20MB
정도, Allocated Memory
가 증가한 것을 볼 수 있었습니다.
inference mode
아래의 경우를 자세히 살펴보면, 1,2,3 epoch
모두 동일한 그래프를 그리고 있는 것을 확인할 수 있습니다.
또한, 모델의 용량
과 input 용량
을 제외하면, 그래프 상으로는 6MB
정도이고, 터미널에 log 된 값에 따르면 20MB
순간 증가하였으나, 바로 감소하였습니다.
그리고, 도중에 갑자기 증가하고 감소하는 것이 있는데, 이는 model layer
각각의 output
연산이 끝날 때를 의미하며, output
이 GPU memory
상에 계속 쌓이지 않는다는 것을 의미합니다.
마지막 결과 값으로, 그래프 상 Allocated Memory
가 거의 증가하지 않았습니다.
두 경우 모두, model
과 input
의 사이즈는 동일하게 들어 갑니다. 하지만, model
의 layer
들의 output
을 저장하느냐에 따라서, 아래와 같이 다른 결과들을 낼 수 있었다고 판단됩니다.
동일한 시작
시작할 때에는 각 값에, 동일한 메모리가 할당 된것을 볼 수 있습니다.
------------------------------------ after upload model to gpu ---------------------------------------------
Allocated: 44.7MB, 'Max Allocated: 44.7MB, Cached: 64.0MB, Max Cached: 64.0 MB, NVIDIA_SMI: 539 MiB MB
------------------------------------ after upload input to gpu ---------------------------------------------
Allocated: 118.7MB, 'Max Allocated: 118.7MB, Cached: 138.0MB, Max Cached: 138.0 MB, NVIDIA_SMI: 613 MiB MB
------------------------------------ 1 Epoch ----------------------------------
Allocated: 118.7MB, 'Max Allocated: 118.7MB, Cached: 138.0MB, Max Cached: 138.0 MB, NVIDIA_SMI: 613 MiB MB
train mode
아래의 경우를 자세히 살펴보면, 1,2,3 epoch
모두 동일한 그래프를 그리고 있는 것을 확인할 수 있습니다.
그러나, model의 layer
들을 거치면서, Allocated memory
가 계속 증가하는 것을 확인할 수 있었으며,
NVIDIA SMI
결과 값 또한 증가하는 것을 볼 수 있었습니다.
마지막 결과 값으로, 그래프 상 약 2700MB
정도, Allocated Memory
가 증가한 것을 볼 수 있었습니다.
또한, NVIDIA_SMI
결과 값또한, 3400MB
인것을 확인할 수 있습니다.
inference mode
아래의 경우를 자세히 살펴보면, 1,2,3 epoch
모두 동일한 그래프를 그리고 있는 것을 확인할 수 있습니다.
또한, 모델의 용량
과 input 용량
을 제외하면, 그래프 상으로는 800MB
정도이고,
그리고, 도중에 갑자기 증가하고 감소하는 것이 있는데, 이는 model layer
의 output
을 의미하며, output
이 GPU memory
상에 계속 쌓이지 않는다는 것을 의미합니다.
마지막 결과 값으로, 그래프 상 Allocated Memory
가 거의 증가하지 않았습니다.
(사진)
두 경우 모두, model
과 input
의 사이즈는 동일하게 들어 갑니다. 하지만, model
의 layer
들의 output
을 저장하느냐에 따라서, 아래와 같이 다른 결과들을 낼 수 있었다고 판단됩니다.
동일한 시작
시작할 때에는 각 값에, 동일한 메모리가 할당 된것을 볼 수 있습니다.
------------------------------------ after upload model to gpu ---------------------------------------------
Allocated: 44.7MB, 'Max Allocated: 44.7MB, Cached: 64.0MB, Max Cached: 64.0 MB, NVIDIA_SMI: 539 MiB MB
------------------------------------ after upload input to gpu ---------------------------------------------
Allocated: 338.7MB, 'Max Allocated: 338.7MB, Cached: 358.0MB, Max Cached: 358.0 MB, NVIDIA_SMI: 833 MiB MB
------------------------------------ 1 Epoch ----------------------------------
Allocated: 338.7MB, 'Max Allocated: 338.7MB, Cached: 358.0MB, Max Cached: 358.0 MB, NVIDIA_SMI: 833 MiB MB
train mode
아래의 경우를 자세히 살펴보면, 1,2,3 epoch
모두 동일한 그래프를 그리고 있는 것을 확인할 수 있습니다.
그러나, model의 layer
들을 거치면서, Allocated memory
가 계속 증가하는 것을 확인할 수 있었으며,
NVIDIA SMI
결과 값 또한 증가하는 것을 볼 수 있었습니다.
마지막 결과 값으로, 그래프 상 약 10876MB
정도, Allocated Memory
가 증가한 것을 볼 수 있었습니다.
또한, NVIDIA_SMI
결과 값또한, 12165MB
인것을 확인할 수 있습니다.
inference mode
아래의 경우를 자세히 살펴보면, 1,2,3 epoch
모두 동일한 그래프를 그리고 있는 것을 확인할 수 있습니다.
또한, 모델의 용량
과 input 용량
을 제외하면, 그래프 상으로는 3200MB
정도이고,
그리고, 도중에 갑자기 증가하고 감소하는 것이 있는데, 이는 model layer
의 output
을 의미하며, output
이 GPU memory
상에 계속 쌓이지 않는다는 것을 의미합니다.
마지막 결과 값으로, 그래프 상 Allocated Memory
가 거의 증가하지 않았습니다.
위의 결과를 기반으로 정리를 하면 아래와 같이 정리할 수 있을거 같으며,
inference mode
가 train mode
에 비해서, 확실히 memory
가 적게 든다.
inference mode
의 경우, GPU memory
에 model layer
의 output
이 쌓이지 않는 다는 것을 확인 할 수 있었으며,
512
와 같은 큰 batch size
의 경우, Memory Allocated
가 3배 정도 차이가 나는 것을 알 수 있다.
위의 정보를 활용하여, inference
를 할 때, 보다 GPU
의 batch
활용에 도움이 될 수 있을 것이라 생각합니다.
with torch.no_grad()
를 통해서, 여러 글들을 통해서,inference
를 할때 더 효율적이라는 것은 알고있었지만,위와 같이 실험을 통해서, 알게 되고,
또한 아래와 같이 추가로 더
torch
의GPU memory
할당에 대해서 공부할 수 있어 좋았습니다.
또한, 이를 공부하다 여러가지 더 깊게 정리할 수 있었습니다.
Memory Management
https://pytorch.org/docs/stable/notes/cuda.html#cuda-memory-management
torch.cuda.memory_summary()
위 함수를 활용해서, 아래와 같이 정리된 표로 나타낼 수 있으며,
https://discuss.pytorch.org/t/does-cuda-cache-memory-for-future-usage/87680
https://discuss.pytorch.org/t/clearing-the-gpu-is-a-headache/84762/4
https://github.com/pytorch/pytorch/issues/37250
이 자료들을 참고하여, torch
와 GPU memory
사이의 연관성에 대해서 좀더 깊게 이해할 수 있었으며,
아래 대사를 통해서, total allocated memory
+ ~700MB
정도가 NVIDIA_SMI
를 통해 나오는 용량이라는 것 또한 알아낼 수 있었습니다.
The total allocated memory is shown in
memory_summary()
as ~10GB, which seems to fit thenvidia-smi
output. Note that the CUDA context will use additional memory, which will be visible in the memory usage innvidia-smi
(that's where the ~700MB are most likely coming from).
그래서, 이를 확인을 해보았고, 아래와 같이 비슷한 범위내에서 만족을 하고 있는 것 또한 확인할 수 있었습니다.
참고자료
https://www.sicara.ai/blog/2019-28-10-deep-learning-memory-usage-and-pytorch-optimization-tricks
위 글을 보면서,
학습
을 할 때의GPU
상에memory
가 쌓이는 순서 및release
되는 방향에 대해서, 수치적으로 이해를 할 수 있었습니다.하지만, 실제로
torch
에서는cache
에 대해서도 고려를 해주어야할 필요도 있고,with torch.no_grad()
를 통해서,inference
를 할 때 또한, 생각을 해주어야 할 필요가 있습니다.https://pytorch.org/docs/stable/notes/cuda.html#cuda-memory-management
그렇기에, 이를 참고하여, 실제로
train
과inference
를 할 때,GPU memory
와torch memory
에서는 어떠하게 다르며 이를 분석하고자, 아래와 같은 실험을 하게되었습니다. 대부분의model infernce
API
는with torch.no_grad()
를 사용을 많이하게 되어있는 데, 이를 수치적으로 어떠한 만큼 효율을 낼 수 있는지 보여주고 싶습니다.