Pytorch not recognizing GPU -- CUDA initialization: CUDA driver initialization failed, you might not have a CUDA gpu

ml-py: <p>Hello,</p>
<p>I’m having issues getting pytorch to recognize my GPU as whenever I run “torch.cuda.is_available()”, it says the CUDA driver failed to initialize.</p>
<p>Pytorch is installed using pip and I have tried reinstalling different versions of the NVIDIA driver / CUDA toolkit and pytorch version but I always get the same error.</p>
<p>Any help would be appreciated.</p>
<p><strong>Here is the output from nvidia-smi:</strong><br>
<img src="/uploads/default/original/3X/f/f/ffb29176058b10ed97de7a4c1d0e2808bd70394e.png" alt="image" data-base62-sha1="Au0kNZ1cGCwMVupDhl3BOBRihJ4" width="688" height="500"></p>
<p><strong>Here is the output from nvcc -V</strong><br>
nvcc: NVIDIA (R) Cuda compiler driver<br>
Copyright (c) 2005-2022 NVIDIA Corporation<br>
Built on Wed_Sep_21_10:33:58_PDT_2022<br>
Cuda compilation tools, release 11.8, V11.8.89<br>
Build cuda_11.8.r11.8/compiler.31833905_0</p>
<p><strong>Here is the output from torch envirionment:</strong><br>
PyTorch version: 2.1.2+cu121<br>
Is debug build: False<br>
CUDA used to build PyTorch: 12.1<br>
ROCM used to build PyTorch: N/A</p>
<p>OS: Ubuntu 22.04.3 LTS (x86_64)<br>
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0<br>
Clang version: Could not collect<br>
CMake version: Could not collect<br>
Libc version: glibc-2.35</p>
<p>Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (64-bit runtime)<br>
Python platform: Linux-5.15.0-91-generic-x86_64-with-glibc2.35<br>
Is CUDA available: False<br>
CUDA runtime version: Could not collect<br>
CUDA_MODULE_LOADING set to: N/A<br>
GPU models and configuration:<br>
GPU 0: NVIDIA A100 80GB PCIe<br>
GPU 1: NVIDIA A100 80GB PCIe<br>
GPU 2: NVIDIA A100 80GB PCIe<br>
GPU 3: NVIDIA A100 80GB PCIe</p>
<p>Nvidia driver version: 530.30.02<br>
cuDNN version: Could not collect<br>
HIP runtime version: N/A<br>
MIOpen runtime version: N/A<br>
Is XNNPACK available: True</p>
<p>CPU:<br>
Architecture:                       x86_64<br>
CPU op-mode(s):                     32-bit, 64-bit<br>
Address sizes:                      43 bits physical, 48 bits virtual<br>
Byte Order:                         Little Endian<br>
CPU(s):                             64<br>
On-line CPU(s) list:                0-63<br>
Vendor ID:                          AuthenticAMD<br>
Model name:                         AMD Ryzen Threadripper PRO 3975WX 32-Cores<br>
CPU family:                         23<br>
Model:                              49<br>
Thread(s) per core:                 2<br>
Core(s) per socket:                 32<br>
Socket(s):                          1<br>
Stepping:                           0<br>
Frequency boost:                    enabled<br>
CPU max MHz:                        4368.1641<br>
CPU min MHz:                        2200.0000<br>
BogoMIPS:                           6986.40<br>
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip rdpid overflow_recov succor smca sme sev sev_es<br>
Virtualization:                     AMD-V<br>
L1d cache:                          1 MiB (32 instances)<br>
L1i cache:                          1 MiB (32 instances)<br>
L2 cache:                           16 MiB (32 instances)<br>
L3 cache:                           128 MiB (8 instances)<br>
NUMA node(s):                       1<br>
NUMA node0 CPU(s):                  0-63<br>
Vulnerability Gather data sampling: Not affected<br>
Vulnerability Itlb multihit:        Not affected<br>
Vulnerability L1tf:                 Not affected<br>
Vulnerability Mds:                  Not affected<br>
Vulnerability Meltdown:             Not affected<br>
Vulnerability Mmio stale data:      Not affected<br>
Vulnerability Retbleed:             Mitigation; untrained return thunk; SMT enabled with STIBP protection<br>
Vulnerability Spec rstack overflow: Mitigation; safe RET<br>
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp<br>
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization<br>
Vulnerability Spectre v2:           Mitigation; Retpolines, IBPB conditional, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected<br>
Vulnerability Srbds:                Not affected<br>
Vulnerability Tsx async abort:      Not affected</p>
<p>Versions of relevant libraries:<br>
[pip3] numpy==1.21.5<br>
[pip3] torch==2.1.2<br>
[pip3] torchaudio==2.1.2<br>
[pip3] torchvision==0.16.2<br>
[pip3] triton==2.1.0<br>
[conda] Could not collect</p>

ptrblck: <p>Are you able to run any other CUDA application on this system?<br>
Installing the NVIDIA driver and installing the PyTorch binaries should be enough to run PyTorch workloads. A locally installed CUDA toolkit won’t be needed unless you build PyTorch from source or a custom CUDA extension.</p>

ml-py: <p>Yes- I can run other CUDA apps on the system without issues.</p>
<p>I still get this error if I try to validate the CUDA/GPU config with pytorch<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/e/0ebea2b628324e5e9de105fee3fb4adbbab873df.png" data-download-href="https://discuss.pytorch.org/uploads/default/0ebea2b628324e5e9de105fee3fb4adbbab873df" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/e/0ebea2b628324e5e9de105fee3fb4adbbab873df.png" alt="image" data-base62-sha1="26r7pcihkiyPbLoyzOvyFKuJF9l" width="690" height="84" data-dominant-color="1A1A1A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1251×153 7.43 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>I cannot reproduce any issue using the latest wheels on an A100 server:</p>
<pre data-code-wrap="python"><code class="lang-python">nvidia-smi
Thu Jan 18 19:02:49 2024       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.105.17   Driver Version: 525.105.17   CUDA Version: 12.3     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA A100-SXM...  Off  | 00000000:07:00.0 Off |                    0 |
| N/A   30C    P0    60W / 400W |      0MiB / 81920MiB |      0%      Default |
|                               |                      |             Disabled |
+-------------------------------+----------------------+----------------------+
|   1  NVIDIA A100-SXM...  Off  | 00000000:0F:00.0 Off |                    0 |
| N/A   30C    P0    58W / 400W |      0MiB / 81920MiB |      0%      Default |
|                               |                      |             Disabled |
+-------------------------------+----------------------+----------------------+
...
python -c "import torch; print(torch.__version__); print(torch.cuda.is_available()); print(torch.randn(1).cuda())"
2.1.2+cu121
True
tensor([1.4417], device='cuda:0')
</code></pre>

ml-py: <p>This is odd.</p>
<p>What driver packages and pytorch did you install?<br>
I would like to mimic on mine to see if there any changes.</p>

ptrblck: <p>NVIDIA driver <code>525.105.17</code> with a simple <code>pip install torch</code> installing the latest stable <code>2.1.2+cu121</code> binary.</p>

ml-py: <p>No luck.<br>
Are there any other commands or logs I can review to see why it isn’t detecting?</p>

ptrblck: <p>You could check your environment as we’ve seen issues in the past reported here where users were unaware of e.g. <code>CUDA_VISIBLE_DEVICES</code> being set to an invalid value, thus blocking the GPU usage. This would of course not explain why standalone CUDA applications work, but still checking env variables as well as <code>dmesg</code> might give you a clue. Additionally, you could also pull docker containers with PyTorch installed to check if these would work.</p>

aberoham: <p>I encountered this same problem over the weekend on a machine with multiple A100’s and the missing piece was to make sure the system was running nvidia-fabricmanager <a href="https://docs.nvidia.com/datacenter/tesla/fabric-manager-user-guide/index.html" rel="noopener nofollow ugc">https://docs.nvidia.com/datacenter/tesla/fabric-manager-user-guide/index.html</a> –</p>
<pre><code class="lang-auto"># systemctl status nvidia-fabricmanager
● nvidia-fabricmanager.service - NVIDIA fabric manager service
     Loaded: loaded (/lib/systemd/system/nvidia-fabricmanager.service; disabled; vendor preset: enabled)
     Active: active (running) since Sat 2025-01-11 17:50:50 UTC; 1 day 21h ago
   Main PID: 20978 (nv-fabricmanage)
      Tasks: 19 (limit: 231678)
     Memory: 13.5M
        CPU: 1min 31.736s
     CGroup: /system.slice/nvidia-fabricmanager.service
             └─20978 /usr/bin/nv-fabricmanager -c /usr/share/nvidia/nvswitch/fabricmanager.cfg

Jan 11 17:50:38 dltb-5209 systemd[1]: Starting NVIDIA fabric manager service...
Jan 11 17:50:39 dltb-5209 nv-fabricmanager[20978]: Connected to 1 node.
Jan 11 17:50:50 dltb-5209 nv-fabricmanager[20978]: Successfully configured all the available GPUs and NVSwitches to route NVLink traffic.
Jan 11 17:50:50 dltb-5209 systemd[1]: Started NVIDIA fabric manager service.
</code></pre>

ravpatil: <p>I encountered same problem with single L40S Nvidia device on physical system. The nvidia-smi detects one GPU and when tested within python environment, it fails with CUDA driver initialization failed.</p>
<pre><code class="lang-auto">python -c "import torch; print(torch.version.cuda); print(torch.cuda.is_available())"
12.4
/opt/app-root/lib64/python3.11/site-packages/torch/cuda/__init__.py:129: UserWarning: CUDA initialization: CUDA driver initialization failed, you might not have a CUDA gpu. (Triggered internally at /mnt/work-dir/torch-2.6.0/torch-2.6.0/c10/cuda/CUDAFunctions.cpp:109.)
  return torch._C._cuda_getDeviceCount() &gt; 0
False
</code></pre>
<p>Verified that kernel modules for nvidia exists</p>
<pre><code class="lang-auto"># lsmod | grep nvidia

nvidia_drm            114688  0
nvidia_modeset       1449984  1 nvidia_drm
nvidia_uvm           5767168  0
nvidia               8224768  7 nvidia_uvm,nvidia_modeset
video                  73728  1 nvidia_modeset
drm_kms_helper        245760  4 ast,nvidia_drm
drm                   741376  6 drm_kms_helper,ast,drm_shmem_helper,nvidia,nvidia_drm
</code></pre>
<p>As this is single GPU, so nvidia-fabricmanager service is not required here.</p>
<p>The nvidia-smi is able to detect the single GPU Nvidia L40S, but python environment or any container on the system is unable to detect the GPU driver and fails with CUDA initialization failure as above.</p>