troubleshooting2025-01-10·9·235/348

TensorFlow GPU 사용 설정 에러

Troubleshooting TensorFlow GPU configuration — from CUDA toolkit mismatches to memory allocation errors and driver compatibility.

TensorFlow GPU 사용 설정 에러

Introduction

Setting up TensorFlow with GPU support is one of those tasks that should be simple but often devolves into hours of debugging version mismatches, missing libraries, and cryptic error messages. Every major TensorFlow release changes its CUDA requirements, and the documentation is sometimes behind or incomplete.

I set up a TensorFlow GPU environment for a machine learning project in Lisbon to run time-series prediction models on financial data. The setup process on my NVIDIA RTX 4070 involved multiple rounds of debugging CUDA and cuDNN compatibility.

Environment

OS: Ubuntu 22.04 LTS
GPU: NVIDIA RTX 4070 (12GB VRAM)
CUDA Toolkit: 12.2
cuDNN: 8.9.7
TensorFlow: 2.15.0
Python: 3.11.5
Driver: 535.129.03

Problem

Error 1: Could not load dynamic library

2025-01-10 14:32:15.123456: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2211] 
Cannot dlopen some GPU libraries: libcudart.so.12: cannot open shared object file: 
No such file or directory; libcudnn.so.8: cannot open shared object file: 
No such file or directory
Skipping visible GPU devices: 0

Error 2: CUDA out of memory

2025-01-10 14:33:22.654321: E tensorflow/core/common_runtime/gpu/gpu_device.cc:957] 
The device was not registered because it seems like the device ordinal is already in use 
by a different process. (Ignore this error if you are using CUDA memory allocation 
features)

Or:

tensorflow.python.framework.errors_impl.ResourceExhaustedError: 
OOM when allocating tensor with shape[32,2048,2048] and type float on 
/job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc

Error 3: No GPU detected

>>> import tensorflow as tf
>>> print(tf.config.list_physical_devices('GPU'))
[]  # Empty list — no GPU found

Error 4: Version mismatch

ImportError: This version of TensorFlow requires CUDA toolkit 12.x but found 
CUDA toolkit 11.8. Please install CUDA toolkit 12.x or use TensorFlow 2.13.x.

Analysis

TensorFlow GPU errors almost always fall into one of four categories.

Category 1: CUDA/cuDNN version mismatch. Each TensorFlow version requires specific CUDA and cuDNN versions. TensorFlow 2.15 requires CUDA 12.2 and cuDNN 8.9. TensorFlow 2.14 requires CUDA 11.8 and cuDNN 8.7.

Category 2: Missing environment variables. TensorFlow looks for CUDA libraries in specific paths defined by LD_LIBRARY_PATH and CUDA_PATH.

Category 3: GPU memory exhaustion. TensorFlow by default tries to allocate all GPU memory at startup. If another process is using the GPU, this fails.

Category 4: Driver compatibility. NVIDIA driver version must be compatible with the CUDA toolkit version.

Check your current setup:

# Check NVIDIA driver
nvidia-smi

# Check CUDA version
nvcc --version

# Check if TensorFlow can see GPU
python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

Solution

Fix 1: Install the correct CUDA and cuDNN versions

# For TensorFlow 2.15 (CUDA 12.2)
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get install cuda-toolkit-12-2

# Install cuDNN 8.9.7
sudo apt-get install libcudnn8=8.9.7.29-1+cuda12.2
sudo apt-get install libcudnn8-dev=8.9.7.29-1+cuda12.2

Fix 2: Set environment variables

# Add to ~/.bashrc
export CUDA_HOME=/usr/local/cuda-12.2
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH

# Reload
source ~/.bashrc

Fix 3: Prevent TensorFlow from using all GPU memory

import tensorflow as tf

# Option 1: Allow memory growth
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    for gpu in gpus:
        tf.config.experimental.set_memory_growth(gpu, True)

# Option 2: Set a memory limit
tf.config.set_logical_device_configuration(
    gpus[0],
    [tf.config.LogicalDeviceConfiguration(memory_limit=4096)]  # 4GB limit
)

Fix 4: Check for GPU detection

import tensorflow as tf

print("TensorFlow version:", tf.__version__)
print("Built with CUDA:", tf.test.is_built_with_cuda())
print("GPUs:", tf.config.list_physical_devices('GPU'))
print("All devices:", tf.config.list_physical_devices())

Fix 5: For multi-GPU setups

import tensorflow as tf

# Use specific GPU
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"  # Use only GPU 0

# Or use mirrored strategy for multi-GPU
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
    model = tf.keras.Sequential([
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(10)
    ])

Lessons Learned

  • Always check nvidia-smi output first to confirm the driver is working and CUDA is available.
  • Match TensorFlow to CUDA/cuDNN versions exactly. The TensorFlow website has a compatibility matrix — use it.
  • Set memory_growth=True early in your script to prevent TensorFlow from grabbing all GPU memory.
  • Use CUDA_VISIBLE_DEVICES to control which GPUs TensorFlow can use.
  • If using Docker, use the official NVIDIA TensorFlow images to avoid host-level CUDA installation issues.

This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.