1 분 소요

이 글은 WINDOWS10 환경에서 CUDA 11.0 기준으로 GPU를 사용하기 위한 확인과 설정 방법을 정리한다. 텐서플로가 GPU를 인식하는지 확인하는 방법부터 장치 할당 로깅, GPU 메모리 제한 설정까지 다룬다.

사전 작업

CUDA 11.0 소프트웨어 스택과 GPU 드라이버가 올바르게 연동되어 있어야 텐서플로의 GPU 가속 런타임이 정상 동작한다[^cuda-guide].

  • GPU 사용 환경을 아직 구성하지 않았다면 다음 설치 안내를 참고한다.
  • [WINDOWS10 환경] 딥러닝을 위한 텐서플로우(tensorflow) GPU 설치 방법 : https://ingu627.github.io/tips/install_cuda/ (본 가이드는 CUDA 11.0 및 cuDNN 8.x 환경 기준이다.)

image

  • nvidia-smi 명령은 사용 가능한 GPU 카드와 함께 각 카드에서 실행 중인 프로세스를 보여준다.

텐서플로의 GPU 인식 상태 확인

import tensorflow as tf

print(tf.test.is_gpu_available())

print(tf.test.gpu_device_name())

print(tf.config.experimental.list_physical_devices(device_type='GPU'))

image


if tf.config.experimental.list_physical_devices("GPU"):
    with tf.device("gpu:0"):
        print("GPU 사용 가능")
        v = tf.Variable(tf.random.normal([1000, 1000]))
        v = None  # v는 더이상 GPU 메모리를 사용하지 않음

image


장치 할당 로깅

연산과 텐서가 어느 장치(CPU 또는 GPU)에 배치되는지 확인하려면 장치 배치 로깅을 활성화한다[^tf-gpu-guide].

import tensorflow as tf

tf.debugging.set_log_device_placement(True)

가시적 GPU 장치 제한

특정 GPU만 프로세스에 노출하려면 set_visible_devices를 사용한다[^tf-gpu-guide].

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
  # 텐서플로가 첫 번째 GPU만 사용하도록 제한
  try:
    tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
  except RuntimeError as e:
    # 프로그램 시작 시점에 접근 가능한 장치가 설정되어야 함
    print(e)

점진적 GPU 메모리 할당 (Memory Growth)

텐서플로는 기본적으로 전체 VRAM을 선점하지만, set_memory_growth를 활성화하면 필요한 시점에만 메모리를 점진적으로 할당한다[^tf-gpu-guide].

for gpu in tf.config.experimental.list_physical_devices("GPU"):
    tf.config.experimental.set_memory_growth(gpu, True)

특정 GPU에 2048MB 메모리만 할당하도록 제한

가상 장치 구성을 통해 프로세스가 사용할 수 있는 GPU 메모리 상한을 2048MB(2GB)로 고정한다[^tf-gpu-guide].

import tensorflow as tf

gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    # 특정 GPU에 2048MB(2GB) 메모리만 할당하도록 제한 (memory_limit 단위: MB)
    try:
        tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
        tf.config.experimental.set_virtual_device_configuration(
            gpus[0],
            [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)])
    except RuntimeError as e:
        # 프로그램 시작 시점에 가상 장치가 설정되어야 함
        print(e)

한계와 주의사항

  • Windows 환경 네이티브 GPU 지원 범위: TensorFlow는 2.10 버전을 마지막으로 Windows 네이티브 환경의 GPU(CUDA) 지원을 종료했다[^tf-win]. TensorFlow 2.11 이상 버전에서 GPU 가속을 유지하려면 Windows Subsystem for Linux 2(WSL2) 환경을 구축해야 한다.
  • 초기화 시점 제약 (RuntimeError): set_visible_devices, set_memory_growth, set_virtual_device_configuration 등 모든 런타임 장치 설정은 텐서 생성이나 연산이 실행되기 전 프로그램 최상단에서 호출되어야 한다[^tf-gpu-guide]. 한 번이라도 장치가 초기화된 뒤 호출하면 RuntimeError가 발생한다.
  • 메모리 반환 한계 및 단편화: memory_growth 설정은 필요한 시점에 VRAM을 점진적으로 할당하지만, 텐서플로의 BFC(Best-Fit with Coalescing) 할당자 특성상 한 번 확보된 VRAM은 프로세스가 종료되기 전까지 운영체제로 반환되지 않는다[^tf-bfc].
  • CUDA/cuDNN 버전 호환성 제약: CUDA 11.0 툴킷과 cuDNN 8.0, 텐서플로 2.4.0 빌드 간의 마이너 버전 불일치가 발생하면 cudart64_110.dll 동적 링크 라이브러리를 로드하지 못해 GPU 가속이 비활성화되고 CPU로 폴백된다[^tf-install-gpu].

    References

  • Hands-On Machine Learning with Scikit-Learn, Keras & TensorFlow(핸즈온 머신러닝)
  • 텐서플로우 - GPU 사용하기 [^cuda-guide]: NVIDIA CUDA C++ Programming Guide (CUDA 11.0 Archive), Section 1.2: CUDA Software Environment: https://docs.nvidia.com/cuda/archive/11.0/cuda-c-programming-guide/ [^tf-gpu-guide]: TensorFlow Core Guide: Use a GPU, Virtual Devices and Memory Management: https://www.tensorflow.org/guide/gpu [^tf-win]: TensorFlow Install: Software requirements for GPU support on Windows: https://www.tensorflow.org/install/pip#windows-native [^tf-bfc]: TensorFlow Internals: BFC (Best-Fit with Coalescing) Allocator and GPU Memory Management: https://github.com/tensorflow/tensorflow/blob/v2.4.0/tensorflow/core/common_runtime/bfc_allocator.h [^tf-install-gpu]: TensorFlow Hardware and Software Support Matrix: https://www.tensorflow.org/install/source#gpu

댓글남기기