Incredibly long torch loading

thistlillo: <p>I have recently configured a new Windows 11 (Pro) PC, that is quite powerful. For sure, it is more powerful than the old machine I had where I did not encounter the problem I am about to describe.</p>
<p>I installed miniconda, then created an environment shared several machine I have access to via a yaml file.</p>
<p>When I load torch within this environment for the first time, the loading takes approximately 1 minute to complete. Thinking it was due to this <code>ai</code> env, I created another environment named <code>torch</code> where I installed torch using the command on the homepage:</p>
<pre><code class="lang-auto">conda install pytorch torchvision torchaudio pytorch-cuda=11.6 -c pytorch -c nvidia
</code></pre>
<p>The following commands were executed within the same “Anaconda prompt” shell running within Windows Terminal.</p>
<pre><code class="lang-auto">(base) C:\Users\user&gt;conda activate ai
(ai) C:\Users\user&gt;python
Python 3.9.15 (main, Nov 24 2022, 14:39:17) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit("import torch", number=1)
53.3811931
&gt;&gt;&gt; timeit.timeit("import torch", number=1)
3.1999999947629476e-06
&gt;&gt;&gt; exit()

(ai) C:\Users\user&gt;python
Python 3.9.15 (main, Nov 24 2022, 14:39:17) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit("import torch", number=1)
0.9886645000000005

(ai) C:\Users\user&gt;conda deactivate

(base) C:\Users\user&gt;conda activate torch

(torch) C:\Users\user&gt;python
Python 3.9.15 (main, Nov 24 2022, 14:39:17) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit("import torch", number=1)
21.7602496
&gt;&gt;&gt; timeit.timeit("import torch", number=1)
2.8999999983625457e-06
&gt;&gt;&gt;
</code></pre>
<p>As you can see, the first time I run <code>import torch</code> it takes almost a minute, then, if I stay in the same environment, the loading is fast. If I change environment, the first loading is still slow.</p>
<p>If I open a new Anacoda prompt within the same Windows Terminal, the loading is fast (as here after the first load). If I close and re-open Windows Terminal, the loading is fast.</p>
<p>When I reboot the machine, I am back at the very slow loading.</p>
<p>I have found other questions here similar to mine, some answers suggested it could be related to the JIT compiler. None had a solution.</p>
<p>This Windows 11 Pro installation is fresh: there are only two environment <code>ai</code> and the test one <code>torch</code>. I have been using conda and pytorch for years and I have never had this problem.</p>
<p>Any suggestions on what to look at?</p>

ptrblck: <p>JIT compilation is often mentioned as a potential root cause of a slow startup time, but note that you won’t be able to JIT compile PyTorch in under a minute, so I doubt that’s the case.<br>
However, you could check if any processes are created during the import and see if this could point towards the issue.<br>
Also, since you’ve never encountered the issue before: what changed in your current setup? Is this just a new Windows machine using the same setup which works fine on an older Windows setup?</p>

Kaslanarian: <p>Hey,  I have the same problem! I have tried on non-conda environments and conda environments with different Python versions. I find this problem not only happens on pytorch, other packages, like numpy, pandas, and sklearn also have this problem. But there are no such problems on my desktop computer, which is also Windows 11. So have you ever found out what’s the problem? Thank you.</p>

Bo_Bo: <p>Have you fixed this problem? I face the same situation as you</p>

crissallan: <p>I meet the same issue as you did! hope to see the solution for this.</p>

intlsy: <p>Is your partition running on network-based file systems like NFS? When you import things, it needs to visit a lot of small files on the disk. If your filesystem has a low IOPs, it may be a bottleneck.</p>

Zarxrax: <p>I’m seeing the exact same issue. I was previously on windows 10 and did not experience any problems. I recently upgraded to Windows 11 pro (clean install) and now it’s taking over a minute to import pytorch!</p>
<p>I have read some comments saying it could be windows defender, but I have realtime protection turned off. I do see some CPU and disk access while its importing, but it doesn’t seem like anything unusually high.</p>

Zarxrax: <p>I have resolved the problem. Windows 11 has a feature called Smart App Control. While this was enabled, it was scanning all the huge dll files that pytorch uses, taking 1-2 minutes. After disabling it, everything is back at full speed.</p>
<p>Please note that if you disable Smart App Control, it can not be turned back on.</p>