Problem
I've compiled PyTorch 1.8.0 with Python 3.7.12, but when I try to install torchvision, I encounter an error when importing the torch library. I'm probably missing linking torch to the Python virtual environment, but honestly, I don't know how I should do it or where the PyTorch installation is located since it has been compiled from the source.
Due to working with the Jetson TX2 NX platform, I have been following the instructions from both Nvidia and PyTorch. For version 3.7.12, it appears appropriate to install PyTorch 1.8.0, and the corresponding version of torchvision is 0.9.0.
Following the instructions provided in this Nvidia forum, I have executed the following commands and also applied the patch for 1.8.0 (Instructions > Build from the source > Apply Patch > 1.8.0 modifications)
I created a virtual environment with Python version 3.7.12 and executed the following:
sudo nvpmodel -m 0
sudo jetson_clocks
git clone --recursive --branch v1.8.0 http://github.com/pytorch/pytorch pytorch
cd pytorch
# At this moment, I have added all the manual modifications.
export USE_NCCL=0
export USE_DISTRIBUTED=0
export USE_QNNPACK=0
export USE_PYTORCH_QNNPACK=0
export TORCH_CUDA_ARCH_LIST="5.3;6.2;7.2"
export PYTORCH_BUILD_VERSION=1.8.0
export PYTORCH_BUILD_NUMBER=1
pip3 install -r requirements.txt
pip3 install scikit-build
pip3 install ninja
python setup.py bdist_wheel
The compilation completed without errors.
Update here! Solution!
Inside pytorch directory (cloned from github), the file torch-1.8.0a0+37c1f4a-cp37-cp37m-linux_aarch64.whl had been generated within the /dist/ folder. All I had to do was navigate to the folder and install it using pip install torch-1.8.0a0+37c1f4a-cp37-cp37m-linux_aarch64.whl.
So:
cd dist/
pip install torch-1.8.0a0+37c1f4a-cp37-cp37m-linux_aarch64.whl
But when I tried to install torchvision,
git clone --branch v0.9.0 https://github.com/pytorch/vision torchvision
cd torchvision
export BUILD_VERSION=0.9.0
python setup.py install
I encountered the following error when importing torch:
import torch
ModuleNotFoundError: No module named 'torch'
(venv37) [19-01-24 19:57:36] aaeon@aaeon-desktop:~/torchvision$
Solved this error, i found that i cant install torchvision 0.9.0 because of a lzma.py error. I've just only need to modify the file /usr/local/lib/python3.7/lzma.py. I saw it here
try:
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
from backports.lzma import *
from backports.lzma import _encode_filter_properties, _decode_filter_properties
What i tried (Obsolete)
1)I have opened the interpreter and imported torch, but it throws an error
$ python
Python 3.7.12 (default, Jan 16 2024, 16:47:04)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
2)I looked for the installation location on the device and tried adding it to the PYTHONPATH for recognition, but unfortunately, it didn't work. :-(
find / -name "torch"
/home/aaeon/pytorch/build/lib.linux-aarch64-cpython-37/torch/home/aaeon/pytorch/torch/lib/python3.7/site-packages/caffe2/torch
export PYTHONPATH=/home/aaeon/pytorch/torch/lib/python3.7/site-packages/caffe2/torch:$PYTHONPATH
Considering that PyTorch has been installed successfully, how can I make it work within the virtual environment?
PS: To address my issue, I might consider trying higher versions of Python beyond 3.7.12, but I cannot downgrade to Python 3.6.9. This is because I ultimately need to export models under TensorRT, and there are incompatibilities with ONNX. But before investing more time and effort into installing other versions of Python, PyTorch, torchvision, ... I would like to know if I can find a solution to my problem with Python 3.7.12. Thank you very much.
PS2: Also asked about it on unofficial Pytorch discord
Update (Obsolete)
I have detected an error.
I ran the command:
sudo find / -type d -name "torch";
And I found the path: /home/aaeon/pytorch/torch/lib/python3.7/site-packages/caffe2/torch
For this reason, I added the path to the bash using export.
However, within the virtual environment path of python3.7.12, the site-packages directory does not contain torch, which means it has not been installed correctly within the environment.
Running pip list does indicate that torch 1.8.0 is installed.
I've decided to re-compile pytorch again: python setup.py bdist_wheel