Random_device could not be read

humza: <p><strong>Traceback (most recent call last):</strong><br>
**  File “test.py”, line 55, in **<br>
**    from torchvision.transforms import transforms as T**<br>
**  File “/home/manager/env/lib/python3.8/site-packages/torchvision/<strong>init</strong>.py”, line 4, in **<br>
**    from .extension import _HAS_OPS**<br>
**  File “/home/manager/env/lib/python3.8/site-packages/torchvision/extension.py”, line 101, in **<br>
**    _check_cuda_version()**<br>
**  File “/home/manager/env/lib/python3.8/site-packages/torchvision/extension.py”, line 80, in _check_cuda_version**<br>
**    _version = torch.ops.torchvision._cuda_version()**<br>
<strong>RuntimeError: random_device could not be read</strong></p>
<p>Not sure whats wrong, whenever I try to run my model in a script on cuda device I get this error.<br>
If I import “from torchvision.transforms import transforms as T” in a console it runs fine.</p>
<p>I have tried installing latest to historic versions of pytorch but no luck.</p>
<p>My system has ubuntu 20.04 installed. I am also using tensorflow along with pytorch in the script. I have started facing this issue after upgrading to 20.04 from 16.04.</p>

ptrblck: <aside class="quote no-group" data-username="humza" data-post="1" data-topic="138697">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/h/f19dbf/48.png" class="avatar"> humza:</div>
<blockquote>
<p>If I import “from torchvision.transforms import transforms as T” in a console it runs fine.</p>
</blockquote>
</aside>
<p>This would point to a broken installation in the other environment you are using (I don’t know how you are executing the script in the failing case).</p>

AlbertZeyer: <p>I just want to write that I see a very similar problem. I reported it also here:</p><aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/102360">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/102360" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/102360" target="_blank" rel="noopener nofollow ugc">PyTorch hangs at import when used together with TensorFlow</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2023-05-26" data-time="08:58:13" data-timezone="UTC">08:58AM - 26 May 23 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/albertz" target="_blank" rel="noopener nofollow ugc">
          <img alt="albertz" src="https://avatars.githubusercontent.com/u/59132?v=4" class="onebox-avatar-inline" width="20" height="20">
          albertz
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🐛 Describe the bug

Code:
```python
import tensorflow
import torch
```<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">
This hangs in some cases in the `import torch`.

Importing it the other way around, or also just importing Torch does not hang. However, I'm reporting this here because the stacktrace looks still suspicious.

Specifically, on my system, Ubuntu 22.04, using the distrib Python 3.10, I have TensorFlow 2.12 and PyTorch 2.0.1. Same also with Python 3.11.

The stacktrace of the hang:
```
0x00007ffff7d51992 in __GI___libc_read (fd=0, buf=0x7fffffffa6f7, nbytes=1) at ../sysdeps/unix/sysv/linux/read.c:26                                              
26      ../sysdeps/unix/sysv/linux/read.c: No such file or directory.                                                                                                           
(gdb) bt                                                                                
#0  0x00007ffff7d51992 in __GI___libc_read (fd=0, buf=0x7fffffffa6f7, nbytes=1) at ../sysdeps/unix/sysv/linux/read.c:26                                                         
#1  0x00007ffff43af518 in std::random_device::_M_getval() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007fff5d55a6ef in _GLOBAL__sub_I_IpcFabricConfigClient.cpp () from /u/zeyer/.local/lib/python3.10/site-packages/torch/lib/libtorch_cpu.so
#3  0x00007ffff7fc947e in call_init (l=&lt;optimized out&gt;, argc=argc@entry=3, argv=argv@entry=0x7fffffffd1c8, env=env@entry=0x555557198d40) at ./elf/dl-init.c:70
#4  0x00007ffff7fc9568 in call_init (env=0x555557198d40, argv=0x7fffffffd1c8, argc=3, l=&lt;optimized out&gt;) at ./elf/dl-init.c:33
...
```

The `fd=0`, coming from `std::random_device::_M_getval` looks very suspicious to me. It looks like the `std::random_device` is not properly initialized? Code [here](https://github.com/gcc-mirror/gcc/blob/d156c6054200237b707f9fb44ae9958d926b0745/libstdc%2B%2B-v3/src/c%2B%2B11/random.cc#L582) and [here](https://github.com/pytorch/kineto/blob/6bf8eeb9955a2e975e494ae8dffd2259680e771b/libkineto/src/IpcFabricConfigClient.cpp#L26).

In other cases, I have also seen the error "random_device could not be read". This seems to be very related, maybe it got another uninitialized `_M_fd` value.

I also reported this here: https://github.com/rwth-i6/returnn/issues/1339

Some related issues:
https://discuss.pytorch.org/t/random-device-could-not-be-read/138697 (very related)
https://github.com/JohnSnowLabs/spark-nlp/issues/5943
https://discuss.tensorflow.org/t/tensorflow-linux-wheels-are-being-upgraded-to-manylinux2014/8339
https://github.com/h2oai/datatable/issues/2453
https://github.com/robjinman/pro_office_calc/issues/5
https://github.com/boostorg/fiber/issues/249
https://github.com/h2oai/datatable/issues/2453
https://github.com/microsoft/LightGBM/issues/1516


### Versions

PyTorch version: 2.0.1+cu117                                                            
Is debug build: False                                                                   
CUDA used to build PyTorch: 11.7                                                        
ROCM used to build PyTorch: N/A                                                         
                                                                                        
OS: Ubuntu 22.04.2 LTS (x86_64)                                                         
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0                                                                                                                              
Clang version: 15.0.7                                                                                                                                                           
CMake version: version 3.26.3                                                           
Libc version: glibc-2.35                                                                
                                                                                        
Python version: 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.15.0-46-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: GPU 0: NVIDIA GeForce GTX 980
Nvidia driver version: 530.41.03
cuDNN version: Could not collect
HIP runtime version: N/A 
MIOpen runtime version: N/A        
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   39 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          6
On-line CPU(s) list:             0-5
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Core(TM) i5-8500 CPU @ 3.00GHz
CPU family:                      6
Model:                           158
Thread(s) per core:              1
Core(s) per socket:              6
Socket(s):                       1
Stepping:                        10
CPU(s) scaling MHz:              42%
CPU max MHz:                     4100.0000
CPU min MHz:                     800.0000
BogoMIPS:                        6000.00
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 sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d arch_capabilities
L1d cache:                       192 KiB (6 instances)
L1i cache:                       192 KiB (6 instances)
L2 cache:                        1.5 MiB (6 instances)
L3 cache:                        9 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-5
Vulnerability Itlb multihit:     KVM: Mitigation: VMX unsupported
Vulnerability L1tf:              Mitigation; PTE Inversion
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT disabled
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT disabled
Vulnerability Retbleed:          Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; IBRS, IBPB conditional, RSB filling
Vulnerability Srbds:             Mitigation; Microcode
Vulnerability Tsx async abort:   Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] flake8==4.0.1
[pip3] numpy==1.23.5
[pip3] torch==2.0.1
[pip3] torchaudio==2.0.2
[pip3] torchdata==0.6.1
[pip3] triton==2.0.0
[conda] Could not collect</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<aside class="onebox githubissue" data-onebox-src="https://github.com/rwth-i6/returnn/issues/1339">
  <header class="source">

      <a href="https://github.com/rwth-i6/returnn/issues/1339" target="_blank" rel="noopener nofollow ugc">github.com/rwth-i6/returnn</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/rwth-i6/returnn/issues/1339" target="_blank" rel="noopener nofollow ugc">pytest tests/test_rf_array.py crash in _GLOBAL__sub_I_IpcFabricConfigClient.cpp</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2023-05-25" data-time="13:41:16" data-timezone="UTC">01:41PM - 25 May 23 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/albertz" target="_blank" rel="noopener nofollow ugc">
          <img alt="albertz" src="https://avatars.githubusercontent.com/u/59132?v=4" class="onebox-avatar-inline" width="20" height="20">
          albertz
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">File `IpcFabricConfigClient.cpp`.

```
$ gdb --args python3 -m pytest tests/t<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">est_rf_array.py
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04) 12.1
...
Reading symbols from python3...
(gdb) r
Starting program: /work/tools/users/zeyer/linuxbrew/bin/python3 -m pytest tests/test_rf_array.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
================================================================== test session starts ==================================================================
platform linux -- Python 3.11.2, pytest-7.3.1, pluggy-1.0.0
rootdir: /u/zeyer/code/returnn
configfile: pytest.ini
collecting ... [Detaching after vfork from child process 2130305]
[Detaching after vfork from child process 2130306]
[Detaching after vfork from child process 2130307]
warning: File "/u/zeyer/.linuxbrew-homefs/Cellar/gcc/12.2.0/lib/gcc/current/libstdc++.so.6.0.30-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /u/zeyer/.linuxbrew-homefs/Cellar/gcc/12.2.0/lib/gcc/current/libstdc++.so.6.0.30-gdb.py
line to your configuration file "/u/zeyer/.config/gdb/gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/u/zeyer/.config/gdb/gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"
[Detaching after vfork from child process 2130356]
[Detaching after vfork from child process 2130357]
[Detaching after vfork from child process 2130360]

Program received signal SIGABRT, Aborted.
__pthread_kill_implementation (threadid=&lt;optimized out&gt;, signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
44      pthread_kill.c: No such file or directory.
(gdb) bt
#0  __pthread_kill_implementation (threadid=&lt;optimized out&gt;, signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
#1  0x00007ffff77ececf in __pthread_kill_internal (signo=6, threadid=&lt;optimized out&gt;) at pthread_kill.c:78
#2  0x00007ffff77a2ea2 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#3  0x00007ffff778e45c in __GI_abort () at abort.c:79
#4  0x00007fffeefd48d9 in ?? () from /work/tools/users/zeyer/linuxbrew/opt/gcc/lib/gcc/current/libstdc++.so.6
#5  0x00007fffeefdff0a in ?? () from /work/tools/users/zeyer/linuxbrew/opt/gcc/lib/gcc/current/libstdc++.so.6
#6  0x00007fffeefdff75 in std::terminate() () from /work/tools/users/zeyer/linuxbrew/opt/gcc/lib/gcc/current/libstdc++.so.6
#7  0x00007fffeefe01c7 in __cxa_throw () from /work/tools/users/zeyer/linuxbrew/opt/gcc/lib/gcc/current/libstdc++.so.6
#8  0x00007fffeefd7253 in std::__throw_runtime_error(char const*) () from /work/tools/users/zeyer/linuxbrew/opt/gcc/lib/gcc/current/libstdc++.so.6
#9  0x00007fffef00acc3 in std::random_device::_M_getval() () from /work/tools/users/zeyer/linuxbrew/opt/gcc/lib/gcc/current/libstdc++.so.6
#10 0x00007fff5f04169f in _GLOBAL__sub_I_IpcFabricConfigClient.cpp () from /u/zeyer/.local/lib/python3.11/site-packages/torch/lib/libtorch_cpu.so
#11 0x00007ffff7fcad6e in call_init (env=0x4cdc7e0, argv=0x7fffffffd668, argc=4, l=&lt;optimized out&gt;) at dl-init.c:70
#12 call_init (l=&lt;optimized out&gt;, argc=4, argv=0x7fffffffd668, env=0x4cdc7e0) at dl-init.c:26
#13 0x00007ffff7fcae54 in _dl_init (main_map=0x4fb9c80, argc=4, argv=0x7fffffffd668, env=0x4cdc7e0) at dl-init.c:117
#14 0x00007ffff78ae4f5 in __GI__dl_catch_exception (exception=&lt;optimized out&gt;, operate=&lt;optimized out&gt;, args=&lt;optimized out&gt;) at dl-error-skeleton.c:182
#15 0x00007ffff7fd1b76 in dl_open_worker (a=0x7fffffff9570) at dl-open.c:808
#16 dl_open_worker (a=a@entry=0x7fffffff9570) at dl-open.c:771
#17 0x00007ffff78ae4a9 in __GI__dl_catch_exception (exception=&lt;optimized out&gt;, operate=&lt;optimized out&gt;, args=&lt;optimized out&gt;) at dl-error-skeleton.c:208
#18 0x00007ffff7fd1d3a in _dl_open (file=0x7fffb5b1f9d0 "/u/zeyer/.local/lib/python3.11/site-packages/torch/_C.cpython-311-x86_64-linux-gnu.so", 
    mode=&lt;optimized out&gt;, caller_dlopen=0x7ffff7cf8a5f &lt;_PyImport_FindSharedFuncptr+127&gt;, nsid=-2, argc=4, argv=0x7fffffffd668, env=0x4cdc7e0)
    at dl-open.c:883
#19 0x00007ffff77e75f8 in dlopen_doit (a=a@entry=0x7fffffff97e0) at dlopen.c:56
#20 0x00007ffff78ae4a9 in __GI__dl_catch_exception (exception=exception@entry=0x7fffffff9740, operate=&lt;optimized out&gt;, args=&lt;optimized out&gt;)
    at dl-error-skeleton.c:208
#21 0x00007ffff78ae54f in __GI__dl_catch_error (objname=0x7fffffff97a0, errstring=0x7fffffff97a8, mallocedp=0x7fffffff979f, operate=&lt;optimized out&gt;, 
    args=&lt;optimized out&gt;) at dl-error-skeleton.c:227
#22 0x00007ffff77e7176 in _dlerror_run (operate=operate@entry=0x7ffff77e75a0 &lt;dlopen_doit&gt;, args=args@entry=0x7fffffff97e0) at dlerror.c:138
#23 0x00007ffff77e7676 in dlopen_implementation (dl_caller=&lt;optimized out&gt;, mode=&lt;optimized out&gt;, file=&lt;optimized out&gt;) at dlopen.c:71
#24 ___dlopen (file=&lt;optimized out&gt;, mode=&lt;optimized out&gt;) at dlopen.c:81
#25 0x00007ffff7cf8a5f in _PyImport_FindSharedFuncptr () from /work/tools/users/zeyer/linuxbrew/lib/libpython3.11.so.1.0
#26 0x00007ffff7ce6a10 in _PyImport_LoadDynamicModuleWithSpec () from /work/tools/users/zeyer/linuxbrew/lib/libpython3.11.so.1.0
#27 0x00007ffff7ce66b8 in _imp_create_dynamic () from /work/tools/users/zeyer/linuxbrew/lib/libpython3.11.so.1.0
#28 0x00007ffff7c3d13e in cfunction_vectorcall_FASTCALL () from /work/tools/users/zeyer/linuxbrew/lib/libpython3.11.so.1.0
#29 0x00007ffff7c6962d in _PyEval_EvalFrameDefault () from /work/tools/users/zeyer/linuxbrew/lib/libpython3.11.so.1.0
...
#124 0x00007ffff7cf8cf8 in Py_RunMain () from /work/tools/users/zeyer/linuxbrew/lib/libpython3.11.so.1.0
#125 0x00007ffff7cf8ab9 in Py_BytesMain () from /work/tools/users/zeyer/linuxbrew/lib/libpython3.11.so.1.0
#126 0x00007ffff778f1b7 in __libc_start_call_main (main=main@entry=0x401040 &lt;main&gt;, argc=argc@entry=4, argv=argv@entry=0x7fffffffd668) at ../sysdeps/nptl/libc_start_call_main.h:58
#127 0x00007ffff778f26c in __libc_start_main_impl (main=0x401040 &lt;main&gt;, argc=4, argv=0x7fffffffd668, init=&lt;optimized out&gt;, fini=&lt;optimized out&gt;, rtld_fini=&lt;optimized out&gt;, stack_end=0x7fffffffd658) at ../csu/libc-start.c:392
#128 0x0000000000401071 in _start () at ../sysdeps/x86_64/start.S:115
```

I'm posting this because I have not found such stacktrace on Google at all, or any stacktrace related to IpcFabricConfigClient.</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


AlbertZeyer: <p>What TensorFlow version did you use? I noticed that I get the hang in PyTorch when I use TensorFlow 2.12, but not when I use TensorFlow 2.10. But different CUDA versions/setups might also have influenced this observation.</p>

univanxx: <p>For me importing torchvision package (or other packages using it) <strong>before</strong> importing tensorflow solved the problem. Wonder why is this happening…</p>

humza: <p>Some versions of TensorFlow with PyTorch cause this problem. You can try downgrading or upgrading your TensorFlow. For me, it worked by downgrading the TensorFlow</p>
<p><img width="1" height="1" src="http://post.spmailtechn.com/q/crEh3A3I9kC0wsWtXCEopw~~/AAIPyQA~/RgRmtiH4PVcDc3BjQgpk03ju1GS1VsTMUhpodW16YW5hdmVlZEByb2NrZXRtYWlsLmNvbVgEAAAAAA~~" alt=""></p>
<blockquote>
<p><img src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/univanxx/45/62478_2.png" title="univanxx" width="45" height="45"> | <a href="http://post.spmailtechn.com/f/a/H38ctDxF72EVQ5fH01zuPA~~/AAIPyQA~/RgRmtiH4P0QmaHR0cHM6Ly9kaXNjdXNzLnB5dG9yY2gub3JnL3UvdW5pdmFueHhXA3NwY0IKZNN47tRktVbEzFIaaHVtemFuYXZlZWRAcm9ja2V0bWFpbC5jb21YBAAAAAA~" rel="noopener nofollow ugc">univanxx</a> Ivan<br>August 10 |</p>
<ul>
<li>| - |</li>
</ul>
</blockquote>
<p>For me importing torchvision package (or other packages using it) <strong>before</strong> importing tensorflow solved the problem. Wonder why is this happening…</p>

yyyuhan: <p>I had the same issue recently. I commented the cause in this thread: <a href="https://github.com/pytorch/pytorch/issues/102360" class="inline-onebox" rel="noopener nofollow ugc">PyTorch hangs at import when used together with TensorFlow · Issue #102360 · pytorch/pytorch · GitHub</a></p>
<p>Hope that would be helpful.</p>