Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `import torch`, without using any `torch` module, take up to 5 seconds?

I have realised that whenever I import torch in any of my python files, the import is lagging, i.e. takes up to 5 or 6 seconds, even though I am only printing hello world and not using any modules from torch. So, running this simple programme from my terminal takes around 5 seconds every time I run it:

import torch
print('hello world')

Could it be that the interpreter must find the torch module first or what could cause this lag? Also, running the script diretly from VS Code with the run button does not work, it says there is no module named 'torch', even though I have selected the right interpreter.

In VS Code, if I want to select an interpreter, Python 3.8.3 is the base, then I have Python 3.8.15 and my virtual environment with 3.11.4. I am working on a MacBook Pro (2019) with Sonoma14.0.

EDIT:

Whenever I run a python script from the terminal, I get a pop up from xcode saying, the Python3 command requires the command line developer tools, asking if I want to install it. I did already, but I get this pop-up every time.

like image 527
tanasr Avatar asked Oct 25 '25 07:10

tanasr


1 Answers

To profile the import process:

python -X importtime -c 'import torch' 2> torch-import.prof
uvx tuna torch-import.prof

The resulting chart shows that torch._C and torch._meta_registrations are the main reasons that import torch is slow.

tuna chart

Further discussions on how it could be improved:

  • https://github.com/pytorch/pytorch/issues/97106
  • https://dev-discuss.pytorch.org/t/delving-into-what-happens-when-you-import-torch/1589
  • https://discuss.pytorch.org/t/making-torch-load-faster/210991
like image 104
Daniel Darabos Avatar answered Oct 26 '25 23:10

Daniel Darabos