Search code examples
pythontensorflowgpu

Tensorflow does not detect GPU altough the GPU driver and Cuda are installed


I have an Nvidia GPU (Geforce RTX 3090) and the driver is displayed in Nvidia Control Panel. I also have installed the latest version of Cuda. However, when using the following code in Python with TensorFlow:

gpus = tf.config.list_physical_devices('GPU')
if not gpus:
    print("No GPUs detected")
else:
    print("GPUs detected:")
    for gpu in gpus:
        print(gpu)

It always shows me, that no GPU is detected. Can you tell me what I have to do in order to make Tensorflow use the GPU?

EDIT: I am using PyCharm and downloaded Python directly (so I don't use something like Anaconda).

Update: Here is the nvidia-smi output from the cmd:

U:\>nvidia-smi
Wed Jul 12 09:13:40 2023
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 531.14                 Driver Version: 531.14       CUDA Version: 12.1     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                      TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf            Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce RTX 3090       WDDM | 00000000:65:00.0  On |                  N/A |
|  0%   36C    P8               13W / 350W|   2085MiB / 24576MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A      3252    C+G   ...m Files\Mozilla Firefox\firefox.exe    N/A      |
|    0   N/A  N/A      4364    C+G   ...h2txyewy\InputApp\TextInputHost.exe    N/A      |
|    0   N/A  N/A     11312    C+G   ...soft Office\root\Office16\EXCEL.EXE    N/A      |
|    0   N/A  N/A     19072    C+G   ...5n1h2txyewy\ShellExperienceHost.exe    N/A      |
|    0   N/A  N/A     21476    C+G   ...m Files\Mozilla Firefox\firefox.exe    N/A      |
|    0   N/A  N/A     23832    C+G   ....Search_cw5n1h2txyewy\SearchApp.exe    N/A      |
|    0   N/A  N/A     24544    C+G   ..._8wekyb3d8bbwe\Microsoft.Photos.exe    N/A      |
|    0   N/A  N/A     25932    C+G   ...x64__8wekyb3d8bbwe\ScreenSketch.exe    N/A      |
|    0   N/A  N/A     33528    C+G   ...ekyb3d8bbwe\PhoneExperienceHost.exe    N/A      |
|    0   N/A  N/A     36580    C+G   ...on 2022.3.1\jbr\bin\jcef_helper.exe    N/A      |
|    0   N/A  N/A     42128    C+G   ...cal\Microsoft\OneDrive\OneDrive.exe    N/A      |
+---------------------------------------------------------------------------------------+

Update: I downgraded to tensorflow 2.10 and get some new error messages:"2023-07-15 15:15:23.440924: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2023-07-15 15:15:23.441186: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine."


Solution

  • According to this guide you should use CUDA 11.8 11.2 (with CuDNN 8.1.0) for Windows and TF 2.10. You wrote that you installed "the latest version of CUDA", which is 12.2 right now. Can you verify that? To check your installed cuda version you can try to run

    from tensorflow.python.platform import build_info as build
    print(build.build_info['cuda_version'])
    

    or for a comand-line one-liner:

    python3 -c "from tensorflow.python.platform import build_info as build;print(build.build_info['cuda_version'])"
    

    But given that TensorFlow can't find your GPU, I'm not sure that works. You can also look at C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA which version the subfolder name is.

    The error Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll indicates that TensorFlow looks for libraries with the version 11.

    To install the right TensorFlow version, run

    pip install -U "tensorflow==2.10.*"
    

    Download CUDA 11.8 11.2 from https://developer.nvidia.com/cuda-11-2-0-download-archive
    If you want to have CuDNN, download and install it according to this guide. Be careful to choose version 8.1.0!