3

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."

PeterBe
  • 700
  • 1
  • 17
  • 37
  • Can you please add the details of your operating system? – Laassairi Abdellah Apr 25 '23 at 07:28
  • @LaassairiAbdellah: Thanks for your comment Laassa. My operating system is Windows 10 Pro – PeterBe Apr 26 '23 at 04:03
  • Hi @PeterBe, Please ensure that you have not installed `TensorFlow > 2.10` which is not supported for GPU support in Windows Native. Please check the details in this [link](https://www.tensorflow.org/install/pip) and follow all the hardware/software requirements and step by step instructions mentioned to install TensorFlow with GPU support. –  May 05 '23 at 17:52
  • Can you add you TF version, cuda version (from within python/tensorflow), and nvidia-smi output. – Daraan Jul 11 '23 at 10:02
  • @Daraan: Thanks for your comment. My TF version is `tf.version.VERSION: 2.11.0`. I don't know how to get to know the cuda version and nvidia-smi output. I tried the following code for the cuda version ` cuda_version = sys_details["cuda_version"] print(f"cuda_version: {cuda_version}")` found here (https://www.gcptutorials.com/article/how-to-check-cuda-version-in-tensorflow) but this leads to the key error `KeyError: 'cuda_version'` – PeterBe Jul 11 '23 at 14:57
  • @PeterBe Can you confirm that you are no longer on `tf.version.VERSION: 2.11.0` OR using the alternate solutions in this thread: https://discuss.tensorflow.org/t/tensorflow-gpu-not-working-on-windows/13120 – Morrison Chang Jul 12 '23 at 07:26
  • @MorrisonChang: Thanks Morrison for your comment. Actually I am suing `tf.version.VERSION: 2.11.0` which, according to the package manager in PyCharm, is the current version. The link you gave me is about Anaconda and Jupyter which I am not using. Is there any newer version available from TensorFlow? – PeterBe Jul 12 '23 at 09:00
  • 1
    @PeterBe Based on https://github.com/tensorflow/tensorflow/issues/60650 no Windows native support exists past 2.10. You should probably install [WSL2 per the documentation](https://www.tensorflow.org/install/pip#windows-native) – Morrison Chang Jul 12 '23 at 17:07
  • @PeterBe If your concern is about using PyCharm Windows native with WSL2, see: https://blog.jetbrains.com/pycharm/2021/03/pycharm-2021-1-eap-4/ and https://youtrack.jetbrains.com/issue/PY-46143/Using-PYCHARM-professonal-with-WSL2-as-python-interpreter-does-not-have-access-to-some-packages – Morrison Chang Jul 12 '23 at 17:15
  • @MorrisonChang: Thanks a lot for the comments Morrison. What do you mean by install WSL2 per documentation? I have no clue what this is and how this can be done. Unforuantely the link you posted is about anaconda, which I am not using. I just have the PyCharm editor for Windows. – PeterBe Jul 12 '23 at 17:57
  • I'm not sure what else to say other than learn/be patient enough to read the links provided. You have two choices 1) Don't want to use Anaconda/WSL2 - to use Nvidia GPU with Tensorflow on Windows Native you MUST use TF ver 2.10 OR 2) Want to use latest Tensorflow version on Windows, install WSL2, follow [Nvidia instructions](https://docs.nvidia.com/cuda/wsl-user-guide/index.html) and Anaconda/Miniconda. Not wanting to install Anaconda/Miniconda means that most of your time would be spent building/supporting TF and related tools/libraries from scratch rather than using TF. – Morrison Chang Jul 13 '23 at 19:38
  • @MorrisonChang: Thanks Morrison for your comment. I definitely don't want to install Anaconda. I have had terrible experience with it (for me one of the worst and buggiest tools that I have ever used). So the only option is to use TF v 2.10 (downgrade)?. For me this is kind of strange. Why does Nvidia not support higher Tensorflow versions? Altough having paid so much for the GPU, you either have to use an old Tensorflow version that does not have all features or you have to use a low-quality product like Anaconda? – PeterBe Jul 14 '23 at 07:06
  • 1
    Related Tensorflow issue ticket: [Please bring back native Windows CUDA support! #59918](https://github.com/tensorflow/tensorflow/issues/59918), key comment: [`While native GPU support on Windows will bring back the 5% perf increase and will support a few more users, please note that the total number of users of TF on Windows is very small compared to the other usecases and that there is almost no Windows expertise at Google to maintain this build. `](https://github.com/tensorflow/tensorflow/issues/59918#issuecomment-1513386150) – Morrison Chang Jul 14 '23 at 07:59
  • @MorrisonChang: Thanks a lot for your effort. Do you know if I can use WSL2 without Anaconda on Windows? I think the effort for using WSL2 is extremely high such that I don't know if I want to use. – PeterBe Jul 14 '23 at 09:28
  • Realize that Tensorflow is expecting a Linux/POSIX environment. Following the TF docs you are installing the Linux version of miniconda NOT Windows. You can search the TF project issue tickets for related problems. Docker may be a option but based on [this thread](https://www.reddit.com/r/tensorflow/comments/13bdjta/comment/jjolytt/?utm_source=share&utm_medium=web2x&context=3) may have setup time/stability issues. – Morrison Chang Jul 14 '23 at 15:31
  • @MorrisonChang: Thanks Morrison for your answer and effort. Unfortunately it does not look good, as I don't want to bother with Linux, Anaconda or Docker. I paid so much for this GPU which was a big mistake :-( – PeterBe Jul 14 '23 at 20:49
  • Not wanting to deal with Linux which even Micro$oft had to adopt with WSL is a odd thing given Linux's place in professional envs. Alternatively as you appear to be learning ML you may want to look at other frameworks like PyTorch for enabling GPU capabilities while restricting your tooling. – Morrison Chang Jul 15 '23 at 04:47
  • @MorrisonChang: Linux makes things unnecessarily complex according to my experience. Whenever easy solutions are possible, Linux wants extremely complex and overengineered ways to do something easy. This is why I don't want to use it. Unfortunately I don't want to use PyTorch for my current project, as I have already implemented 2000 lines of code for my application and I don't want to change it. But in future I might consider using it. – PeterBe Jul 15 '23 at 13:08
  • Seems CUDA related based on [Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation](https://stackoverflow.com/q/59823283/295004), `If, on the other hand, you installed tensorflow and wanted GPU acceleration, check your CUDA installation (TF 2.1 requires CUDA 10.1, not 10.2 or 10.0).` And checking the TF site, 2.10 has `conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0` so you'll need to downgrade to `cudatoolkit=11.2 cudnn=8.1.0` for compatbility. – Morrison Chang Jul 15 '23 at 18:21
  • @MorrisonChang: Thanks for your answer. My cuda version is "Cuda Version: 64_112" `from tensorflow.python.platform import build_info as build print(f"Cuda Version: {build.build_info['cuda_version']}")`. How can I downgrade the cudatoolkit and cudnn? I can't find them in the package manager? I just find nvidia-cudnn-cu110 (with different numbers but no 8.1.0) – PeterBe Jul 17 '23 at 13:29
  • See: [Why can't I install cudatookkit=10.1 using pip but can using conda?](https://stackoverflow.com/q/67912832/295004) so you'll have download from https://developer.nvidia.com/cuda-toolkit-archive and https://developer.nvidia.com/rdp/cudnn-archive given your no `conda` requirement. Once working feel free to self answer with correct steps. Good luck. – Morrison Chang Jul 17 '23 at 20:29

4 Answers4

1
  1. There are a few things, first of all, sometimes pycharm fails to detect installed packages. It Happens to work after restarting the pc.

  2. Next you can check if you have the path variable for cuda setup correctly. [it should be like this][1] [1]: https://i.stack.imgur.com/kEVK0.png

  3. Next if that still doesn't work, you can check the compatibility of tensorflow version with the cuda version. for reference- https://www.tensorflow.org/install/sourcehttps://www.tensorflow.org/install/source

  4. Also check compatibility with tensorflow-gpu. if there is some problem with them, after resolving the issue, recommend restarting pycharm.

  5. Check TensorFlow GPU Support: TensorFlow needs to be built with GPU support. You can verify this by running the following code:

    import tensorflow as tf

    print(tf.test.is_built_with_cuda())

these are the version i am using for tensorflow and tensorflow-gpu and my cuda version is 10.1

and whenever working on these projects, its better to be working on a venv, as different projects need different version compatibility.

Naman Raj
  • 11
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '23 at 21:01
0

You can try pip install tensorflow-gpu==2.10.0 and check if it works. It should uninstall old tensorflow package and install the gpu one of version==2.10.0

YadneshD
  • 396
  • 2
  • 12
  • Thanks for the answer. I installed tensorflow 2.10. not via pip because this led to an error message, but via the PyCharm package manager. Unfortunately, I still get another error message and the GPU is still not found. I wrote an update on the question. – PeterBe Jul 15 '23 at 13:21
  • @PeterBe Its easiest if you use conda. Pip is slightly more complicated. I never tried pycharm package manager but i guess it to be more complicated than conda. It might be possible that your current environment is all messed up making things more difficult. I would recommend you create a new conda environment and follow steps mentioned by other people, then error might get easily solved. You can use miniconda if you find conda too heavy for your PC. – YadneshD Jul 16 '23 at 09:23
  • @Thanks YadneshD for your answer. Actually, I have very bad experience with Anaconda (for me it is one of the worst tools that I have ever used). I think it is buggy. This is why I don't want to mess around with Anaconda – PeterBe Jul 17 '23 at 08:51
  • Thanks for your comment. Just for your information: I personally did not downvote any answer (and I think it is rude that someone downvotes without giving any explanation) – PeterBe Jul 17 '23 at 08:57
  • @PeterBe Full version of conda is very heavy and even I found it messy at first. I personally prefer miniconda which is lightweight and easy to remove. Miniconda has all major functionalities of conda and is easy to use from command line. – YadneshD Jul 17 '23 at 15:10
  • Thanks for your answer. I upvoted it. – PeterBe Jul 26 '23 at 08:26
0

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!

mhenning
  • 426
  • 4
  • 8
  • Why do I (and everyone else) get a downvote on their answer? I'd be helpful to know if I did something wrong.. – mhenning Jul 17 '23 at 08:53
  • Thanks for your comment. I personally did not downvote any answer (and I think it is rude that someone downvotes without giving any explanation) – PeterBe Jul 17 '23 at 08:55
  • My Cuda Version is 64_112 – PeterBe Jul 17 '23 at 13:29
  • Ah well seems I can't read my own links right. The linked guide says CUDA 11.2, not CUDA 11.8. I'll fix it in the answer. Maybe explains the downvote.. As for your version, it seems right. Sounds like CUDA 11.2. Does the error you edited in your question persists? What does it says if you type in `nvcc -V` in your console? – mhenning Jul 17 '23 at 13:56
  • Thanks for your answer. I downloaded Cuda Toolkit 11.8 and cudnn-11.2-windows-x64-v8.1.0.77. as a zip file. When using the command `nvcc -V` in the Python Console of PyCharm I get "PyDev console: starting. Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32 nvcc -V Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.1\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode coro = func() File "", line 1, in NameError: name 'nvcc' is not defined" – PeterBe Jul 17 '23 at 14:01
  • Just for your information: I awarded the bounty to you altough the question is still not answered. I hope that your suggested approach will solve the issue. – PeterBe Jul 17 '23 at 14:05
  • Please enter the `nvcc -V` command in the comand line (terminal tab in PyCharm), not the Python console. Sorry for not being clear about that. – mhenning Jul 17 '23 at 14:14
  • Ah okay, not I get an output which is `nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2023 NVIDIA Corporation Built on Mon_Apr__3_17:36:15_Pacific_Daylight_Time_2023 Cuda compilation tools, release 12.1, V12.1.105 Build cuda_12.1.r12.1/compiler.32688072_0 ` When I try to install Cuda Toolkit 11.2 I get an error message stating that I already have a newer version of it. – PeterBe Jul 17 '23 at 14:39
  • It states that you have indeed CUDA 12.1 installed. Try to uninstall it first via the `windows control panel -> programs and features?` and then install CUDA 11.2. – mhenning Jul 17 '23 at 14:57
  • @PeterBe you need to match versions of cudatoolkit=11.2 cudnn=8.1.0 for tensorflow-gpu==2.10.0. Install cudatoolkit=11.2 and cudnn=8.1.0 first, ensure that they are installed properly and then try installing tensorflow/tensorflow-gpu==2.10.0 again. – YadneshD Jul 17 '23 at 15:24
  • I have now installed Cuda toolkit 11.2 and copied the relevant files from cudatoolkit=11.2 into the cuda folder. I also have tensorflow 2.10.0. Still I get the same output `tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found` and `No GPUs detected tf.version.VERSION: 2.10.0 Cuda Version: 64_112` – PeterBe Jul 18 '23 at 13:45
  • Ah but now the error is different. It seems now Cuda is recognized, but no cudnn files were found (indicated by `cudnn64_8.dll` errors). Did you download and copy the cudnn files according to https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#installcuda-windows? You can find the files here: https://developer.nvidia.com/rdp/cudnn-archive. According to the cudnn site, you could also use the `8.9.3` version for CUDA 11.x. – mhenning Jul 18 '23 at 14:20
  • @mhenning: Thanks for your comment. In the link it says "Copy bin\cudnn*.dll to C:\Program Files\NVIDIA\CUDNN\v8.x\bin". There is no such thing as a `NVIDIA\CUDNN` directory on my disk. There is only `C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA` and `C:\Program Files\NVIDIA Corporation` which contains a lot of folders but no cudnn. I copied the 3 relevant files into `C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2` but I still get the error message. Also I can't set the environment variable to `Value to Add: C:\Program Files\NVIDIA\CUDNN\v8.x\bin` as this folder does not exist – PeterBe Jul 19 '23 at 08:41
  • Try to set the environment variables like there: https://stackoverflow.com/a/65987613/15019223. I'm not sure why the guide has different path, and I can't check it myself as I have no cuda in windows. – mhenning Jul 19 '23 at 11:25
  • @mhenning: Thanks hennig for your comment and effort. I really appreciate it. I did what you suggested and it might be working know. I get the output "GPUs detected: PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')". Is that a valid output for a GPU? I have to admit that I don't notice any significant speed-up in the training process (but that might be a topic for another question). I just want to know if this output indicates that Tensorflow is not able to find and use the available GPU? – PeterBe Jul 21 '23 at 08:38
  • Yes that is a valid output for one GPU found. TensorFlow will automatically use it. It should mention the name of the GPU at the start of the training somewhere. As for the training speed, that is indeed another question but can depend on many things. Maybe your network is too small, or you load your data inefficently? https://www.tensorflow.org/guide/data_performance is a nice helpful guide on that. – mhenning Jul 21 '23 at 10:29
  • Thanks mhenning for your answer and effort. I get the following output when I run the code now `2023-07-23 15:22:42.875381: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. ` – PeterBe Jul 23 '23 at 13:24
  • `2023-07-23 15:22:43.402807: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 21659 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:65:00.0, compute capability: 8.6 Epoch 1/30 2023-07-23 15:22:45.421800: I tensorflow/stream_executor/cuda/cuda_blas.cc:1614] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.` --> However, there is no speed up at all, altought the GPU should be really powerful. Maybe this should be asked in another question – PeterBe Jul 23 '23 at 13:25
  • Yes I think we far exceeded the scope of the question (and answer) with the length of the comments. I think you should ask this in another question, and try google first of course. – mhenning Jul 24 '23 at 09:32
  • Thanks a lot mhenning for your answer and effort. I really appreciate it. I accepted and upvoted it (and I had already awarded the bounty to you). Thanks for the tremendous help. – PeterBe Jul 26 '23 at 08:27
-1

I am also faced this issue. I am using anaconda. In my case problem was i installed tensorflow instead of tensorflow-gpu. so I created new env in anaconda and then installed the tensorflow-gpu. so now it using my gpu Gtx 1060

#CREATE THE ENV conda create --name ENVNAME -y

#ACTIVATE THE eNV conda activate ENVNAME

#INSTALLING CUDA DRIVERS conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0 -y

#INSTALLING TENSORFLOW conda install tensorflow-gpu -y

conda install -c anaconda ipykernel -y

conda install ipykernel -y

#ADDING ENV TO JUPYTER LIST python3 -m ipykernel install --user --name=ENVNAME

#'VERIFY GPU SUPPORT' python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

enter image description here

Avinash
  • 359
  • 3
  • 5
  • Thanks for your answer. I am not using Anaconda so I don't think that I can use your suggested approach – PeterBe Jul 11 '23 at 09:50
  • Thanks for your comment. Just for your information: I personally did not downvote any answer (and I think it is rude that someone downvotes without giving any explanation) – PeterBe Jul 17 '23 at 08:57
  • who is donwvoting and why? – Avinash Jul 21 '23 at 08:38