CUDA error: invalid device ordinal on linux

Aseem: <p>I am getting the following error on linux:</p>
<p>(skoots-env2) [akas0018@m3t103 skoots]$ skoots-train --config-file “config.yaml”<br>
/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/skoots/train/<strong>main</strong>.py:70: UserWarning: Could not find file at path: /home/chris/Dropbox (Partners HealthCare)/trainMitochondriaSegmentation/models/Oct20_11-54-51_CHRISUBUNTU.trch. Model has not been loaded.<br>
warnings.warn(f’Could not find file at path: {cfg.TRAIN.PRETRAINED_MODEL_PATH[0]}. Model has not been loaded.')<br>
setting up rank=1 (with world_size=2)<br>
MASTER_ADDR=‘127.0.0.1’<br>
port=‘41819’<br>
backend=‘nccl’<br>
setting up rank=0 (with world_size=2)<br>
MASTER_ADDR=‘127.0.0.1’<br>
port=‘41819’<br>
backend=‘nccl’<br>
 → done setting up rank=1<br>
 → done setting up rank=0<br>
W1016 02:28:45.823000 139689245087552 torch/multiprocessing/spawn.py:146] Terminating process 1788769 via signal SIGTERM<br>
Traceback (most recent call last):<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/bin/skoots-train”, line 8, in <br>
sys.exit(main())<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/skoots/train/<strong>main</strong>.py”, line 75, in main<br>
mp.spawn(train, args=(port, world_size, model, cfg, args.log), nprocs=world_size, join=True)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/multiprocessing/spawn.py”, line 282, in spawn<br>
return start_processes(fn, args, nprocs, join, daemon, start_method=“spawn”)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/multiprocessing/spawn.py”, line 238, in start_processes<br>
while not context.join():<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/multiprocessing/spawn.py”, line 189, in join<br>
raise ProcessRaisedException(msg, error_index, failed_process.pid)<br>
torch.multiprocessing.spawn.ProcessRaisedException:</p>
<p>– Process 1 terminated with the following error:<br>
Traceback (most recent call last):<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/multiprocessing/spawn.py”, line 76, in _wrap<br>
fn(i, *args)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/skoots/train/engine.py”, line 71, in train<br>
base_model = base_model.to(device)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1174, in to<br>
return self._apply(convert)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 780, in _apply<br>
module._apply(fn)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 780, in _apply<br>
module._apply(fn)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 780, in _apply<br>
module._apply(fn)<br>
[Previous line repeated 1 more time]<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 805, in _apply<br>
param_applied = fn(param)<br>
File “/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/site-packages/torch/nn/modules/module.py”, line 1160, in convert<br>
return t.to(<br>
RuntimeError: CUDA error: invalid device ordinal<br>
Compile with <code>TORCH_USE_CUDA_DSA</code> to enable device-side assertions.</p>
<p>Below is the configuration on my environment<br>
(skoots-env2) [akas0018@m3t103 skoots]$ python -m torch.utils.collect_env<br>
/scratch/cm10/akas0018/miniconda/conda/envs/skoots-env2/lib/python3.10/runpy.py:126: RuntimeWarning: ‘torch.utils.collect_env’ found in sys.modules after import of package ‘torch.utils’, but prior to execution of ‘torch.utils.collect_env’; this may result in unpredictable behaviour<br>
warn(RuntimeWarning(msg))<br>
Collecting environment information…<br>
PyTorch version: 2.4.1+cu118<br>
Is debug build: False<br>
CUDA used to build PyTorch: 11.8<br>
ROCM used to build PyTorch: N/A</p>
<p>OS: Rocky Linux 9.2 (Blue Onyx) (x86_64)<br>
GCC version: (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)<br>
Clang version: 15.0.7 (Red Hat 15.0.7-2.el9)<br>
CMake version: version 3.20.2<br>
Libc version: glibc-2.34</p>
<p>Python version: 3.10.15 (main, Oct  3 2024, 07:27:34) [GCC 11.2.0] (64-bit runtime)<br>
Python platform: Linux-5.14.0-284.25.1.el9_2.x86_64-x86_64-with-glibc2.34<br>
Is CUDA available: True<br>
CUDA runtime version: Could not collect<br>
CUDA_MODULE_LOADING set to: LAZY<br>
GPU models and configuration: GPU 0: Tesla T4<br>
Nvidia driver version: 535.104.05<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:                   46 bits physical, 48 bits virtual<br>
Byte Order:                      Little Endian<br>
CPU(s):                          52<br>
On-line CPU(s) list:             0-51<br>
Vendor ID:                       GenuineIntel<br>
Model name:                      Intel(R) Xeon(R) Gold 6230R CPU @ 2.10GHz<br>
CPU family:                      6<br>
Model:                           85<br>
Thread(s) per core:              1<br>
Core(s) per socket:              26<br>
Socket(s):                       2<br>
Stepping:                        7<br>
CPU max MHz:                     4000.0000<br>
CPU min MHz:                     1000.0000<br>
BogoMIPS:                        4200.00<br>
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities<br>
L1d cache:                       1.6 MiB (52 instances)<br>
L1i cache:                       1.6 MiB (52 instances)<br>
L2 cache:                        52 MiB (52 instances)<br>
L3 cache:                        71.5 MiB (2 instances)<br>
NUMA node(s):                    2<br>
NUMA node0 CPU(s):               0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50<br>
NUMA node1 CPU(s):               1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51<br>
Vulnerability Itlb multihit:     KVM: Mitigation: VMX unsupported<br>
Vulnerability L1tf:              Not affected<br>
Vulnerability Mds:               Not affected<br>
Vulnerability Meltdown:          Not affected<br>
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT disabled<br>
Vulnerability Retbleed:          Mitigation; Enhanced IBRS<br>
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl<br>
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization<br>
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence<br>
Vulnerability Srbds:             Not affected<br>
Vulnerability Tsx async abort:   Mitigation; TSX disabled</p>
<p>Versions of relevant libraries:<br>
[pip3] mypy-extensions==1.0.0<br>
[pip3] numpy==2.0.1<br>
[pip3] torch==2.4.1+cu118<br>
[pip3] torchaudio==2.4.1+cu118<br>
[pip3] torchvision==0.19.1+cu118<br>
[pip3] triton==3.0.0<br>
[pip3] triton-library==1.0.0rc4<br>
[conda] blas                      1.0                         mkl<br>
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch<br>
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch<br>
[conda] mkl                       2023.1.0         h213fc3f_46344<br>
[conda] mkl-fft                   1.3.10                   pypi_0    pypi<br>
[conda] mkl-random                1.2.7                    pypi_0    pypi<br>
[conda] mkl-service               2.4.0                    pypi_0    pypi<br>
[conda] mkl_fft                   1.3.10          py310h5eee18b_0<br>
[conda] mkl_random                1.2.7           py310h1128e8f_0<br>
[conda] numpy                     2.0.1                    pypi_0    pypi<br>
[conda] numpy-base                2.0.1           py310hb5e798b_1<br>
[conda] pytorch-mutex             1.0                         cpu    pytorch<br>
[conda] torch                     2.4.1+cu118              pypi_0    pypi<br>
[conda] torchaudio                2.4.1+cu118              pypi_0    pypi<br>
[conda] torchvision               0.19.1+cu118             pypi_0    pypi<br>
[conda] triton                    3.0.0                    pypi_0    pypi<br>
[conda] triton-library            1.0.0rc4                 pypi_0    pypi</p>
<p>I would be really grateful for any help. Thanks in advance</p>

ptrblck: <p>Check which <code>device</code> is used in the <code>base_model.to(device)</code> operation and make sure it’s in a valid range <code>[0, torch.cuda.device_count()-1]</code>.</p>

Aseem: <p>Thanks a lot for your comment. This is the code where that error was being generated from -</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/8/1829e947cc39742582f2608ecf01ac37dd33e92f.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1829e947cc39742582f2608ecf01ac37dd33e92f" title="Screenshot 2024-10-16 at 4.33.57 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/8/1829e947cc39742582f2608ecf01ac37dd33e92f_2_690x246.jpeg" alt="Screenshot 2024-10-16 at 4.33.57 AM" data-base62-sha1="3rLfJlPhMp5rfElrIkAU2UOCGS3" width="690" height="246" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/8/1829e947cc39742582f2608ecf01ac37dd33e92f_2_690x246.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/1/8/1829e947cc39742582f2608ecf01ac37dd33e92f_2_1035x369.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/8/1829e947cc39742582f2608ecf01ac37dd33e92f_2_1380x492.jpeg 2x" data-dominant-color="F9F9F9"><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">Screenshot 2024-10-16 at 4.33.57 AM</span><span class="informations">1858×664 99.9 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>
<p>Below is the cuda availability -<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/c/7cbcebc42abcd0f986ee007d31e6734fea58a35e.png" data-download-href="https://discuss.pytorch.org/uploads/default/7cbcebc42abcd0f986ee007d31e6734fea58a35e" title="Screenshot 2024-10-16 at 3.51.26 AM"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/c/7cbcebc42abcd0f986ee007d31e6734fea58a35e.png" alt="Screenshot 2024-10-16 at 3.51.26 AM" data-base62-sha1="hNtXZzl3CoHl86mEcMGqvHyuMNM" width="690" height="183" data-dominant-color="0C0C0C"><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">Screenshot 2024-10-16 at 3.51.26 AM</span><span class="informations">752×200 21.7 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>
<p>I therefore changed <strong>base_model.to(device)</strong> to <strong>base_model.to(“cuda:0”)</strong></p>
<p>Now I am getting the following error</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/2/f286e17707b232d2dc60cf84a4e674eca3b77178.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/f286e17707b232d2dc60cf84a4e674eca3b77178" title="Screenshot 2024-10-16 at 4.41.14 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/2/f286e17707b232d2dc60cf84a4e674eca3b77178_2_690x326.jpeg" alt="Screenshot 2024-10-16 at 4.41.14 AM" data-base62-sha1="yBuwLpJKg3wAhXTbw8fbZOwjHZu" width="690" height="326" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/2/f286e17707b232d2dc60cf84a4e674eca3b77178_2_690x326.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/2/f286e17707b232d2dc60cf84a4e674eca3b77178_2_1035x489.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/2/f286e17707b232d2dc60cf84a4e674eca3b77178_2_1380x652.jpeg 2x" data-dominant-color="0D0D0D"><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">Screenshot 2024-10-16 at 4.41.14 AM</span><span class="informations">1920×908 158 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>
<p>Not able to understand how it is detecting two GPUs when I only see one</p>

ptrblck: <p>Hard-coding the device to <code>cuda:0</code> fails as you are now reusing the same device on different ranks. You thus should stick to using the right <code>device</code> ID but you would have to make sure the actually assigned IDs are in the valid range. I.e. if you have 2 GPUs on the system, the <code>device</code> variable should have the values <code>0</code> and <code>1</code> only.</p>