PyTorch for Jetson - version 1.11 now available

Hi @fabian.groh , you can check your installed-torch path using pip3 show torch and I guess your libtorch.so in /usr/local/lib/python3.6/dist-packages/torch/lib/ .

Thanks for the quick reply @dusty_nv and @mheriyanto !

The info that the patch also works up to version 4.5.1 is what I have needed. In the sticky front post only JP4.4 is mentioned on the build from source patches. Should have looked a bit deeper into the content! I will try it the next days.

Thanks again!

Hello. Can anyone suggest? I try install torchvision 0.9.0 on my jetson xavier agx according to instructions. git clone, cd torchvision, export BUILD_VERSION=0.9.0, python3 setup.py install --user. But i get error:

In file included from /home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/…/decoder/decoder.h:6:0,
from /home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/…/decoder/sync_decoder.h:4,
from /home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/video_reader.cpp:6:
/home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/…/decoder/stream.h:52:30: error: ‘findCodec’ declared as a ‘virtual’ field
virtual AVCodec* findCodec(AVCodecParameters* params);
^~~~~~~~~~~~~~~~~
/home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/…/decoder/stream.h:52:20: error: expected ‘;’ at end of member declaration
virtual AVCodec* findCodec(AVCodecParameters* params);
^~~~~~~~~
/home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/…/decoder/stream.h:52:47: error: expected ‘)’ before ‘’ token
virtual AVCodec
findCodec(AVCodecParameters* params);
^
/home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/video_reader.cpp: In function ‘size_t vision::video_reader::{anonymous}::fillTensor(std::vectorffmpeg::DecoderOutputMessage&, at::Tensor&, at::Tensor&, int64_t, int64_t)’:
/home/paluhin/Downloads/torchvision/torchvision/csrc/io/video_reader/video_reader.cpp:98:23: error: there are no arguments to ‘av_rescale_q’ that depend on a template parameter, so a declaration of ‘av_rescale_q’ must be available [-fpermissive]
framePtsData[i] = av_rescale_q(msg.header.pts, timeBaseQ, avr);

Hi @thegood, it appears to be some issue with ffmpeg. Have you tried installing these dependencies?

sudo apt-get install libjpeg-dev zlib1g-dev libpython3-dev libavcodec-dev libavformat-dev libswscale-dev

If that is already done, you may want to try disabling ffmpeg during the build. You can do this by temporarily re-naming the ffmpeg binary:

$ sudo mv /usr/bin/ffmpeg /usr/bin/ffmpeg_backup

Getting an error on python3 setup.py install:

Traceback (most recent call last):
  File "setup.py", line 91, in <module>
    zip_safe=False,
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/install.py", line 67, in run
    self.do_egg_install()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/install.py", line 109, in do_egg_install
    self.run_command('bdist_egg')
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/bdist_egg.py", line 164, in run
    cmd = self.call_command('install_lib', warn_dir=0)
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/bdist_egg.py", line 150, in call_command
    self.run_command(cmdname)
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/install_lib.py", line 11, in run
    self.build()
  File "/usr/lib/python3.6/distutils/command/install_lib.py", line 109, in build
    self.run_command('build_ext')
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/torchaudio/build_tools/setup_helpers/extension.py", line 52, in run
    super().run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/build_ext.py", line 79, in run
    _build_ext.run(self)
  File "/home/greatman/.local/lib/python3.6/site-packages/Cython/Distutils/old_build_ext.py", line 186, in run
    _build_ext.build_ext.run(self)
  File "/usr/lib/python3.6/distutils/command/build_ext.py", line 339, in run
    self.build_extensions()
  File "/home/greatman/.local/lib/python3.6/site-packages/Cython/Distutils/old_build_ext.py", line 195, in build_extensions
    _build_ext.build_ext.build_extensions(self)
  File "/usr/lib/python3.6/distutils/command/build_ext.py", line 448, in build_extensions
    self._build_extensions_serial()
  File "/usr/lib/python3.6/distutils/command/build_ext.py", line 473, in _build_extensions_serial
    self.build_extension(ext)
  File "/home/greatman/torchaudio/build_tools/setup_helpers/extension.py", line 97, in build_extension
    ["cmake", str(_ROOT_DIR)] + cmake_args, cwd=self.build_temp)
  File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '/home/greatman/torchaudio', '-DCMAKE_BUILD_TYPE=Release', '-DCMAKE_PREFIX_PATH=/home/greatman/.local/lib/python3.6/site-packages/torch/share/cmake', '-DCMAKE_INSTALL_PREFIX=/home/greatman/torchaudio/build/lib.linux-aarch64-3.6/torchaudio/', '-DCMAKE_VERBOSE_MAKEFILE=ON', '-DPython_INCLUDE_DIR=/usr/include/python3.6m', '-DBUILD_SOX:BOOL=OFF', '-DBUILD_KALDI:BOOL=ON', '-DBUILD_TRANSDUCER:BOOL=OFF', '-DBUILD_TORCHAUDIO_PYTHON_EXTENSION:BOOL=ON', '-DBUILD_LIBTORCHAUDIO:BOOL=OFF', '-GNinja']' returned non-zero exit status 1.

Is this for torchaudio? I think we would need to see more of the log to find the actual error.

You can see the process I use to install torchaudio in the l4t-pytorch container here:

https://github.com/dusty-nv/jetson-containers/blob/1e10908a104494a883f6855d1e9947827f2a17bc/Dockerfile.pytorch#L90

-- Building version 0.8.0a0+099d788
running install
running bdist_egg
running egg_info
creating torchaudio.egg-info
writing torchaudio.egg-info/PKG-INFO
writing dependency_links to torchaudio.egg-info/dependency_links.txt
writing requirements to torchaudio.egg-info/requires.txt
writing top-level names to torchaudio.egg-info/top_level.txt
writing manifest file 'torchaudio.egg-info/SOURCES.txt'
adding license file 'LICENSE' (matched pattern 'LICEN[CS]E*')
reading manifest file 'torchaudio.egg-info/SOURCES.txt'
writing manifest file 'torchaudio.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-aarch64/egg
running install_lib
running build_py
creating build
creating build/lib.linux-aarch64-3.6
creating build/lib.linux-aarch64-3.6/torchaudio
copying torchaudio/kaldi_io.py -> build/lib.linux-aarch64-3.6/torchaudio
copying torchaudio/transforms.py -> build/lib.linux-aarch64-3.6/torchaudio
copying torchaudio/version.py -> build/lib.linux-aarch64-3.6/torchaudio
copying torchaudio/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio
creating build/lib.linux-aarch64-3.6/torchaudio/sox_effects
copying torchaudio/sox_effects/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/sox_effects
copying torchaudio/sox_effects/sox_effects.py -> build/lib.linux-aarch64-3.6/torchaudio/sox_effects
creating build/lib.linux-aarch64-3.6/torchaudio/_internal
copying torchaudio/_internal/misc_ops.py -> build/lib.linux-aarch64-3.6/torchaudio/_internal
copying torchaudio/_internal/module_utils.py -> build/lib.linux-aarch64-3.6/torchaudio/_internal
copying torchaudio/_internal/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/_internal
copying torchaudio/_internal/fft.py -> build/lib.linux-aarch64-3.6/torchaudio/_internal
creating build/lib.linux-aarch64-3.6/torchaudio/utils
copying torchaudio/utils/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/utils
copying torchaudio/utils/sox_utils.py -> build/lib.linux-aarch64-3.6/torchaudio/utils
creating build/lib.linux-aarch64-3.6/torchaudio/extension
copying torchaudio/extension/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/extension
copying torchaudio/extension/extension.py -> build/lib.linux-aarch64-3.6/torchaudio/extension
creating build/lib.linux-aarch64-3.6/torchaudio/models
copying torchaudio/models/wav2letter.py -> build/lib.linux-aarch64-3.6/torchaudio/models
copying torchaudio/models/wavernn.py -> build/lib.linux-aarch64-3.6/torchaudio/models
copying torchaudio/models/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/models
copying torchaudio/models/conv_tasnet.py -> build/lib.linux-aarch64-3.6/torchaudio/models
creating build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/librispeech.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/utils.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/yesno.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/gtzan.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/speechcommands.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/commonvoice.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/libritts.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/ljspeech.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/tedlium.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/cmuarctic.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
copying torchaudio/datasets/vctk.py -> build/lib.linux-aarch64-3.6/torchaudio/datasets
creating build/lib.linux-aarch64-3.6/torchaudio/functional
copying torchaudio/functional/filtering.py -> build/lib.linux-aarch64-3.6/torchaudio/functional
copying torchaudio/functional/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/functional
copying torchaudio/functional/functional.py -> build/lib.linux-aarch64-3.6/torchaudio/functional
creating build/lib.linux-aarch64-3.6/torchaudio/compliance
copying torchaudio/compliance/kaldi.py -> build/lib.linux-aarch64-3.6/torchaudio/compliance
copying torchaudio/compliance/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/compliance
creating build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/sox_io_backend.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/utils.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/no_backend.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/__init__.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/soundfile_backend.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/_soundfile_backend.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/sox_backend.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
copying torchaudio/backend/common.py -> build/lib.linux-aarch64-3.6/torchaudio/backend
running build_ext
CMake Error: CMake was unable to find a build program corresponding to "Ninja".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "/home/greatman/torchaudio/build/temp.linux-aarch64-3.6/CMakeFiles/CMakeOutput.log".
Traceback (most recent call last):
  File "setup.py", line 91, in <module>
    zip_safe=False,
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/__init__.py", line 153, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/install.py", line 67, in run
    self.do_egg_install()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/install.py", line 109, in do_egg_install
    self.run_command('bdist_egg')
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/bdist_egg.py", line 164, in run
    cmd = self.call_command('install_lib', warn_dir=0)
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/bdist_egg.py", line 150, in call_command
    self.run_command(cmdname)
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/install_lib.py", line 11, in run
    self.build()
  File "/usr/lib/python3.6/distutils/command/install_lib.py", line 109, in build
    self.run_command('build_ext')
  File "/usr/lib/python3.6/distutils/cmd.py", line 313, in run_command
    self.distribution.run_command(command)
  File "/usr/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/home/greatman/torchaudio/build_tools/setup_helpers/extension.py", line 52, in run
    super().run()
  File "/home/greatman/.local/lib/python3.6/site-packages/setuptools/command/build_ext.py", line 79, in run
    _build_ext.run(self)
  File "/home/greatman/.local/lib/python3.6/site-packages/Cython/Distutils/old_build_ext.py", line 186, in run
    _build_ext.build_ext.run(self)
  File "/usr/lib/python3.6/distutils/command/build_ext.py", line 339, in run
    self.build_extensions()
  File "/home/greatman/.local/lib/python3.6/site-packages/Cython/Distutils/old_build_ext.py", line 195, in build_extensions
    _build_ext.build_ext.build_extensions(self)
  File "/usr/lib/python3.6/distutils/command/build_ext.py", line 448, in build_extensions
    self._build_extensions_serial()
  File "/usr/lib/python3.6/distutils/command/build_ext.py", line 473, in _build_extensions_serial
    self.build_extension(ext)
  File "/home/greatman/torchaudio/build_tools/setup_helpers/extension.py", line 97, in build_extension
    ["cmake", str(_ROOT_DIR)] + cmake_args, cwd=self.build_temp)
  File "/usr/lib/python3.6/subprocess.py", line 311, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['cmake', '/home/greatman/torchaudio', '-DCMAKE_BUILD_TYPE=Release', '-DCMAKE_PREFIX_PATH=/home/greatman/.local/lib/python3.6/site-packages/torch/share/cmake', '-DCMAKE_INSTALL_PREFIX=/home/greatman/torchaudio/build/lib.linux-aarch64-3.6/torchaudio/', '-DCMAKE_VERBOSE_MAKEFILE=ON', '-DPython_INCLUDE_DIR=/usr/include/python3.6m', '-DBUILD_SOX:BOOL=OFF', '-DBUILD_KALDI:BOOL=ON', '-DBUILD_TRANSDUCER:BOOL=OFF', '-DBUILD_TORCHAUDIO_PYTHON_EXTENSION:BOOL=ON', '-DBUILD_LIBTORCHAUDIO:BOOL=OFF', '-GNinja']' returned non-zero exit status 1.

Try running this first:

$ sudo apt-get install build-essential
$ pip3 install scikit-build
$ pip3 install ninja

If you continue having issues building torchvision, it is already built in the l4t-pytorch containers.

Am having issues building torchaudio, not torchvision.

Am having issues building torchaudio, not torchvision. Also, the suggested solution did not work

According to your torchaudio error log above, you may have needed those packages because that is what the initial errors you were getting. What error log are you getting now?

I haven’t built torchaudio outside of container before, hence my recommendation to use or build the l4t-pytorch container that already has it installed.

How would I build the l4t-PyTorch container? The error log is identical

You could use one of the existing l4t-pytorch container images on NGC (then you needn’t build it), or you can build it following this:

https://github.com/dusty-nv/jetson-containers

I’m looking to install an older version of pytorch, specifically 0.4.1 which is required by some code I’m trying to run (CenterNet) on the Jetson TX2. There is no pip wheel available for v0.4.1 and I am unable to build it from source. I tried git clone --recursive --branch v0.4.1 http://github.com/pytorch/pytorch however during the process it fails to find a third party repository. python setup.py install then fails because of the missing file, and asks me to run git submodule update --init but this command of course fails.

Anyone able to successfully install pytorch 0.4.1 on their Jetson Tx2? If so I could use some advice on how to go about this.

Hi @ian.blake, I got an error too when I just tried checking out PyTorch v0.4.1 - it appears that nervana submodule is now private/offline.

However there is source tarball on the releases page here: https://github.com/pytorch/pytorch/releases/tag/v0.4.1

Note that building older PyTorch might require an older JetPack as well that includes the versions of CUDA/cuDNN that are supported in that version of PyTorch.

Hi @dusty_nv, thanks for your reply. I grabbed the source tarball but it is also missing most third party submodules, including that same nervana submodule. I found a work around to just delete the submoudule here: How install old version pytorch 0.4.1 from source? · Issue #19457 · pytorch/pytorch · GitHub

I’m on a slightly older version of JetPack as well specifically to try and use this version of PyTorch, however it looks like I didn’t go back far enough. The install encounters an error, most likely related to the version of CUDA; I think I need to be running version 9.2 instead of 10.0. I’m looking to find which version of JetPack installs cuda 9.2, if you could recommend a specific version that would be helpful, for now I’ll try using 3.3.3.

OK gotcha, yea I think JetPack 3.3.x was the last release with CUDA 9.0. Then JetPack 4 started with CUDA 10.0. I don’t think there ever was a JetPack with CUDA 9.2 in particular.

Hello Dusty,
following your instructions I have been able to build a wheel for my Xavier NX for python3.8 and pytorch 1.8
I can import torch into python3.8
When I execute print(torch.cuda.is_available())

I get:
/home/max/.local/lib/python3.8/site-packages/torch/cuda/init.py:52: UserWarning: CUDA initialization: CUDA unknown error - this may be due to an incorrectly set up environment, e.g. changing env variable CUDA_VISIBLE_DEVICES after program start. Setting the available devices to be zero. (Triggered internally at …/c10/cuda/CUDAFunctions.cpp:109.)
return torch._C._cuda_getDeviceCount() > 0
False

Do you have any idea how to fix that?
(I have CUDA version 11.2)

You can solve the problem also downgrading numpy from 1.19.5 to 1.18.2 (in my case).

sudo pip3 install torch-1.8.0-cp36-cp36m-linux_aarch64.whl
it showed error:
ERROR: Exception:
Traceback (most recent call last):
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/cli/base_command.py”, line 180, in _main
status = self.run(options, args)
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/cli/req_command.py”, line 204, in wrapper
return func(self, options, args)
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/commands/install.py”, line 402, in run
pycompile=options.compile,
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/req/init.py”, line 85, in install_given_reqs
pycompile=pycompile,
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/req/req_install.py”, line 790, in install
requested=self.user_supplied,
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/operations/install/wheel.py”, line 818, in install_wheel
requested=requested,
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/operations/install/wheel.py”, line 653, in _install_wheel
file.save()
File “/home/testgpu/.local/lib/python3.6/site-packages/pip/_internal/operations/install/wheel.py”, line 420, in save
shutil.copyfileobj(f, dest)
File “/usr/lib/python3.6/shutil.py”, line 79, in copyfileobj
buf = fsrc.read(length)
File “/usr/lib/python3.6/zipfile.py”, line 872, in read
data = self._read1(n)
File “/usr/lib/python3.6/zipfile.py”, line 948, in _read1
data = self._decompressor.decompress(data, n)
zlib.error: Error -3 while decompressing data: invalid code lengths set