Incredibly long torch loading

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.

I installed miniconda, then created an environment shared several machine I have access to via a yaml file.

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 ai env, I created another environment named torch where I installed torch using the command on the homepage:

conda install pytorch torchvision torchaudio pytorch-cuda=11.6 -c pytorch -c nvidia

The following commands were executed within the same “Anaconda prompt” shell running within Windows Terminal.

(base) C:\Users\user>conda activate ai
(ai) C:\Users\user>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.
>>> import timeit
>>> timeit.timeit("import torch", number=1)
53.3811931
>>> timeit.timeit("import torch", number=1)
3.1999999947629476e-06
>>> exit()

(ai) C:\Users\user>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.
>>> import timeit
>>> timeit.timeit("import torch", number=1)
0.9886645000000005

(ai) C:\Users\user>conda deactivate

(base) C:\Users\user>conda activate torch

(torch) C:\Users\user>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.
>>> import timeit
>>> timeit.timeit("import torch", number=1)
21.7602496
>>> timeit.timeit("import torch", number=1)
2.8999999983625457e-06
>>>

As you can see, the first time I run import torch 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.

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.

When I reboot the machine, I am back at the very slow loading.

I have found other questions here similar to mine, some answers suggested it could be related to the JIT compiler. None had a solution.

This Windows 11 Pro installation is fresh: there are only two environment ai and the test one torch. I have been using conda and pytorch for years and I have never had this problem.

Any suggestions on what to look at?

2 Likes

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.
However, you could check if any processes are created during the import and see if this could point towards the issue.
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?

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.

1 Like

Have you fixed this problem? I face the same situation as you

I meet the same issue as you did! hope to see the solution for this.

1 Like