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.
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.

Further discussions on how it could be improved:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With