PyTorch for Jetson

Managed to compile 1.7.0 on Jetson Nano after upgrade to R32.4.4 !

python3 --version
Python 3.9.0
python3 -c ‘import torch, torchvision; print(f"PyTorch {torch.version} TorchVision {torchvision.version}")’
PyTorch 1.7.0 TorchVision 0.8.1
python3 -c ‘import torch; print(torch.config.show())’
PyTorch built with:
- GCC 7.5
- C++ Version: 201402
- OpenMP 201511 (a.k.a. OpenMP 4.5)
- NNPACK is enabled
- CPU capability usage: NO AVX
- CUDA Runtime 10.2
- NVCC architecture flags: -gencode;arch=compute_53,code=sm_53
- CuDNN 8.0
- Build settings: BLAS=MKL, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_XNNPACK -DUSE_V
ULKAN_WRAPPER -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wn
o-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasi
ng -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color
=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -DMISSING_ARM_VST1 -DMISSING_ARM_VLD1
-Wno-stringop-overflow, USE_CUDA=ON, USE_EIGEN_FOR_BLAS=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=OFF, USE_MKLDNN=OFF, USE_MPI=OFF, USE_NCCL=0,
USE_NNPACK=ON, USE_OPENMP=ON,

2 Likes

python3 -c ‘import torch; print(torch.cuda.is_available())’
True

Hi! I am installing in a virtualenvironment as such:

wget https://nvidia.box.com/shared/static/wa34qwrwtk9njtyarwt5nvo6imenfy26.whl -O torch-1.7.0-cp36-cp36m-linux_aarch64.whl
sudo apt-get install python3-pip libopenblas-base libopenmpi-dev 
pip3 install Cython
pip3 install numpy torch-1.7.0-cp36-cp36m-linux_aarch64.whl
sudo apt-get install libjpeg-dev zlib1g-dev
git clone --branch v0.8.1 https://github.com/pytorch/vision torchvision
cd torchvision
export BUILD_VERSION=0.8.0
sudo python3 setup.py install

but got the error No module named torch although I am able to import torch in python.

Please advise!

Okay I think I solved this with:
sudo pip3 install numpy torch-1.7.0-cp36-cp36m-linux_aarch64.whl

1 Like

If you are using virtual environment, run last command without sudo. It helps me.

UPD. Sorry, it helps me with torchvision, not torch

Hi this gives permission error for me.

Although sudo pip3 install numpy torch-1.7.0-cp36-cp36m-linux_aarch64.whl has allowed me to sudo python3 setup.py install, it failed with this error:

Building wheel torchvision-0.8.0a0+45f960c
PNG found: False
Running build on conda-build: False
Running build on conda: False
JPEG found: True
Building torchvision with JPEG image support
FFmpeg found: True
ffmpeg include path: /usr/include
ffmpeg library_dir: /usr/lib
running install
running bdist_egg
running egg_info
writing torchvision.egg-info/PKG-INFO
writing dependency_links to torchvision.egg-info/dependency_links.txt
writing requirements to torchvision.egg-info/requires.txt
writing top-level names to torchvision.egg-info/top_level.txt
/usr/local/lib/python3.6/dist-packages/torch/utils/cpp_extension.py:339: UserWarning: Attempted to use ninja as the BuildExtension backend but we could not find ninja.. Falling back to using the slow distutils backend.
  warnings.warn(msg.format('we could not find ninja.'))
reading manifest file 'torchvision.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
writing manifest file 'torchvision.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-aarch64/egg
running install_lib
running build_py
copying torchvision/version.py -> build/lib.linux-aarch64-3.6/torchvision
running build_ext
building 'torchvision.video_reader' extension
aarch64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/aim-1/torchvision/torchvision/csrc/cpu/decoder -I/home/aim-1/torchvision/torchvision/csrc/cpu/video_reader -I/home/aim-1/torchvision/torchvision/csrc/cpu/video -I/usr/include -I/home/aim-1/torchvision/torchvision/csrc -I/usr/local/lib/python3.6/dist-packages/torch/include -I/usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/api/include -I/usr/local/lib/python3.6/dist-packages/torch/include/TH -I/usr/local/lib/python3.6/dist-packages/torch/include/THC -I/usr/include/python3.6m -c /home/aim-1/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.cpp -o build/temp.linux-aarch64-3.6/home/aim-1/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.o -std=c++14 -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=video_reader -D_GLIBCXX_USE_CXX11_ABI=1
In file included from /home/aim-1/torchvision/torchvision/csrc/cpu/decoder/memory_buffer.h:3:0,
                 from /home/aim-1/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.cpp:6:
/home/aim-1/torchvision/torchvision/csrc/cpu/decoder/defs.h:12:10: fatal error: libavcodec/avcodec.h: No such file or directory
 #include <libavcodec/avcodec.h>
          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'aarch64-linux-gnu-gcc' failed with exit status 1

Hi, dear support team.

I’m trying to run YOLOv5 on Jetson Nano. When i tried it under Venv with Python v3.6 I managed to install torch and torchvision successfully. But i discovered, that Yolov5 needs Python v3.8. I recreate my Venv and bumped into

ERROR: torch-1.7.0-cp36-cp36m-linux_aarch64.whl is not a supported wheel on this platform.

I manage to download whl with substitution of 38 instead of 36, but result was the same.
ERROR: torch-1.7.0-cp38-cp38m-linux_aarch64.whl is not a supported wheel on this platform.

Please, share the know! As i see user student5487 manage to install it with Python v3.9

If you have ffmpeg installed when trying to build torchvision, then also run sudo apt-get install libavcodec-dev first. For more info, please see this post: https://github.com/dusty-nv/jetson-inference/issues/521#issuecomment-637771596

These wheels were built for Python 3.6, so they won’t work on Python 3.8. You would need to recompile PyTorch wheel against Python 3.8, which I believe is what the other user did.

Hey @dusty_nv ,
I am getting the following error after trying to install torchvision 0.8.1 for Pytorch 1.7.0 on a Jetson AGX Xavier with JetPack 4.4.1 :

FAILED: /home/nvidia/Desktop/torchvision/build/temp.linux-aarch64-3.6/home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.o
c++ -MMD -MF /home/nvidia/Desktop/torchvision/build/temp.linux-aarch64-3.6/home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.o.d -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/decoder -I/home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/video_reader -I/home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/video -I/usr/include -I/home/nvidia/Desktop/torchvision/torchvision/csrc -I/home/nvidia/.local/lib/python3.6/site-packages/torch/include -I/home/nvidia/.local/lib/python3.6/site-packages/torch/include/torch/csrc/api/include -I/home/nvidia/.local/lib/python3.6/site-packages/torch/include/TH -I/home/nvidia/.local/lib/python3.6/site-packages/torch/include/THC -I/usr/include/python3.6m -c -c /home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.cpp -o /home/nvidia/Desktop/torchvision/build/temp.linux-aarch64-3.6/home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.o -std=c++14 -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=video_reader -D_GLIBCXX_USE_CXX11_ABI=1
In file included from /home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/decoder/memory_buffer.h:3:0,
from /home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.cpp:6:
/home/nvidia/Desktop/torchvision/torchvision/csrc/cpu/decoder/defs.h:13:10: fatal error: libavformat/avformat.h: No such file or directory
#include <libavformat/avformat.h>
^~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
File “/home/nvidia/.local/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1522, in _run_ninja_build
env=env)
File “/usr/lib/python3.6/subprocess.py”, line 438, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command ‘[‘ninja’, ‘-v’]’ returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “setup.py”, line 424, in
‘clean’: clean,

File “/home/nvidia/.local/lib/python3.6/site-packages/torch/utils/cpp_extension.py”, line 1538, in _run_ninja_build
raise RuntimeError(message) from e
RuntimeError: Error compiling objects for extension

Any ideas, what the problem could be?

Tried Installing PyTorch v1.7.0
Am able to install PyTorch but while running this command “sudo python3 setup.py install” ti install torchvision, I get the below mentiond error:

from /home/nano/Documents/installer/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.cpp:6:
/home/nano/Documents/installer/torchvision/torchvision/csrc/cpu/decoder/defs.h:12:10: fatal error: libavcodec/avcodec.h: No such file or directory
 #include <libavcodec/avcodec.h>
          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command 'aarch64-linux-gnu-gcc' failed with exit status 1

Kindly suggest the solution. I’m using Jetson nano, python3.6 (used JetPack 4.4.1).

Hi @david_1, if you have ffmpeg installed when trying to build torchvision, then also run sudo apt-get install libavcodec-dev first. For more info, please see this post: https://github.com/dusty-nv/jetson-inference/issues/521#issuecomment-637771596

@vinayrraj, can you try the same? Do you know if you had installed ffmpeg utility previously, or perhaps another package installed it?

Thanks for the quick response @dusty_nv .

I am getting the same error after installing libavcodec-dev.

OK - can you also try sudo apt-get install libavformat-dev?

Alternatively, you can rename the ffmpeg binary (this is what torchvision detections) before installing torchvision, and then restore ffmpeg after:

$ sudo mv /usr/bin/ffmpeg /usr/bin/ffmpeg_bak
$ sudo python3 setup.py install
$ sudo mv /usr/bin/ffmpeg_bak /usr/bin/ffmpeg
1 Like

Installing libavformat-dev did not work, but renaming the ffmpeg binary solved it.

Thanks a lot!

@dusty_nv, I performed the task mentionedabove, check my sequnce of commands:

wget https://nvidia.box.com/shared/static/wa34qwrwtk9njtyarwt5nvo6imenfy26.whl -O torch-1.7.0-cp36-cp36m-linux_aarch64.whl
sudo apt-get install python3-pip libopenblas-base libopenmpi-dev 
pip3 install Cython
sudo apt-get install libavcodec-dev
sudo apt-get update
sudo apt-get upgrade
cd Documents/installer/
pip3 install numpy torch-1.7.0-cp36-cp36m-linux_aarch64.whl
sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev libavcodec-dev
git clone --branch v0.8.1 https://github.com/pytorch/vision torchvision
export BUILD_VERSION=0.8.0
cd torchvision/
sudo python3 setup.py install

And the final output after trying to install torchvision is (error output):

from /home/nano/Documents/installer/torchvision/torchvision/csrc/cpu/video_reader/VideoReader.cpp:6:
/home/nano/Documents/installer/torchvision/torchvision/csrc/cpu/decoder/defs.h:13:10: fatal error: libavformat/avformat.h: No such file or directory
include <libavformat/avformat.h>
^~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command ‘aarch64-linux-gnu-gcc’ failed with exit status 1

I’ve not specifically installed “ffmpeg”. “ffmpeg -version” gives the following output:

ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 7 (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04)
configuration: --prefix=/usr --extra-version=0ubuntu0.2 --toolchain=hardened --libdir=/usr/lib/aarch64-linux-gnu --incdir=/usr/include/aarch64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100

Resolved issue, reinstalled libavformat and libswscale with following command before executing the above command:
sudo apt-get install --reinstall libavformat-dev libswscale-dev

1 Like

Thanks @vinayrraj and @david_1 - I have updated the install instructions with these packages. It seems like ffmpeg executable is not installed by default, but the libraries that torchvision wants to use are not.

1 Like

Hi I have pytorch for Jetson working on my Jetpack 4.4 but unfortunately it gives this error when trying to run the notebooks on the fastai course:


namely:
RuntimeError: solve: MAGMA library not found in compilation. Please rebuild with MAGMA.

How can I built Pytorch for Jetson Nano with MAGMA library?

You would first need to compile/install MAGMA, and then re-build PyTorch by following the Build from Source instructions in the first post of this topic.

However, what may be easier is to just change that line of code to the following so it doesn’t try to use MAGMA:

return torch.solve(B.cpu(), A.cpu())[0]   # you may also need [0].cuda() - I'm not sure