[error] torch.cuda.OutOfMemoryError: CUDA out of memory.

펭귄은 mmdetection을 사용해서 object detection model을 학습하던 중 아래와 같은 에러를 만났다.

torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 128.00 MiB. GPU 0 has a total capacity of 11.73 GiB of which 13.38 MiB is free. Including non-PyTorch memory, this process has 11.70 GiB memory in use. Of the allocated memory 11.31 GiB is allocated by PyTorch, and 165.17 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)

 

위 에러가 발생한 원인은 아래와 같다.

  • 가용한 GPU 메모리 용량보다 더 큰 크기의 모델을 GPU에 올리려고 시도했다

펭귄은 인공지능 개발을 위해 'RTX 4070 Super 12G' GPU 1장을 가지고 있다.

 

가용한 GPU 자원은 nvidia-smi 명령어를 통해 확인할 수 있다.

 

nvidia-smi

 

image
nvidia-smi

 

위 에러는 아래와 같은 방법으로 해결할 수 있다.

  • GPU를 증설한다
  • 더 작은 크기의 모델을 사용한다
  • 학습 데이터의 batch size를 줄인다
  • 학습 데이터의 크기(image size 등)를 줄인다

 

GPU 증설은 비용과 시간이 소요되므로, 펭귄은 모델의 크기와 학습 데이터의 batch size를 조절하여 위 에러를 해결했다.

 


 

출처

  • Penguin by Ruby from "https://thenounproject.com/browse/icons/term/penguin/" Noun Project (CC BY 3.0)