본문 바로가기

프로그래밍 관련/[Python] 간단 해결

[Python] Tensorflow 실행 시 CUBLAS_STATUS_ALLOC_FAILED 등의 그래픽카드 메모리 문제 해결

- 증상

Tensorflow 실행 시 

1) CUDA_OUT_OF_MEMORY
2) CUBLAS_STATUS_ALLOC_FAILED
3) Blas GEMM launch failed 등의 에러 발생

 

- 원인

- Tensorflow는 실행 시 기본적으로 프로세서에 GPU의 메모리 전부를 미리 할당

 

- 해결

import tensorflow as tf

'''
1) tf.__version__ <= 2.0.0
'''

config = tf.ConfigProto()

# 1-1) 메모리 수동 할당 (ex] 0.4 = 40% )
config.gpu_options.per_process_gpu_memory_fraction = 0.4

# 1-2) 메모리 자동 할당 (사전 메모리 할당 비활성화 > 사용량에 따라 점차적 증가)
config.gpu_options.allow_growth = True

sess = tf.Session(config=config)


'''
2) tf.__version__ >= 2.0.0
'''

gpu_devices = tf.config.experimental.list_physical_devices('GPU')
for device in gpu_devices:
    tf.config.experimental.set_memory_growth(device, True)