인공지능(AI)/TensorFlow

TensorFlow GPU Memory error

starcell 2020. 3. 9. 17:55

텐서플로우를 이용하여 GPU에서 트레이닝이나 추론을 할 때 메모리 에러를 만났다.

매 번 어려움을 겪어서 조금 정리해 본다.

에러 환경 : 

TensorFlow, GPU, CNN 을 조합으로 사용할 때 매 번 에러를 만났던 것 같다.

원인은 대부분 Out Of Memory 문제 였던 것 같은 데, 아래 링크에 비교적 잘 정리가 되어 있으니 참고. 

https://datamasters.co.kr/33

https://goodtogreate.tistory.com/entry/TensorFlow%EB%A5%BC-%EA%B3%B5%EC%9A%A9-GPU%EC%97%90%EC%84%9C-%EC%82%AC%EC%9A%A9-%ED%95%A0-%EB%95%8C-%EB%A9%94%EB%AA%A8%EB%A6%AC-%EC%A0%88%EC%95%BD-%EB%B0%A9%EB%B2%95

 

해결 방법은 대략 다음과 같다.

(아래의 방법은 ver 2.x와 1.x 모두 사용 가능)

방법 1. : 아래와 같은 코드를 추가하여 설정에서 GPU의 메모리를 탄력적으로 사용하도록 한다.

(자신의 소스에서 session config 설정이 어떻게 되어 있는냐에 따라 응용하여 적용)

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)

방법 2. : GPU 메모리의 몇 퍼센트를 사용할 지 지정한다.(아래 예는 90%를 지정)

config = tf.ConfigProto() 
config.gpu_options.per_process_gpu_memory_fraction = 0.9
session = tf.Session(config=config, ...)

--> 내 경우 방법 2. 를 사용했을 때 동일한 문제가 발생 되었다.(0.9값이 너무 컸나?)

그래서 그냥 두개 다 대 버렸다. 아래처럼.

(방법 1번만 해도 에러는 발생하지 않았지만 두 개 다 했을 때 에러 없이 트레이닝이 잘 되었기에 동일한 조건에서 하고 비교 하려고 계속 2개를 다 설정했다.)

config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.9

 

error log 일부 : 주로 아래와 같은 에러가 나온다. --------------------------------------------------------

2020-03-09 17:39:12.843393: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudnn.so.7
2020-03-09 17:39:13.352333: E tensorflow/stream_executor/cuda/cuda_dnn.cc:329] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
2020-03-09 17:39:13.366280: E tensorflow/stream_executor/cuda/cuda_dnn.cc:329] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
I0309 17:39:13.417196 140077791434496 coordinator.py:219] Error reported to Coordinator: 2 root error(s) found.