# Install PyTorch on PYNQ

**URL:** <https://discuss.pynq.io/t/install-pytorch-on-pynq/5499>\
**Category:** Learn\
**Created:** [April 20, 2023, 6:54am UTC](https://discuss.pynq.io/t/install-pytorch-on-pynq/5499 "2023-04-20T06:54:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![haipnh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/haipnh/32/4439_2.png) [@haipnh](https://discuss.pynq.io/u/haipnh)\
**Post date:** [April 20, 2023, 6:54am UTC](https://discuss.pynq.io/t/install-pytorch-on-pynq/5499/1 "2023-04-20T06:54:27Z")

</div>

Hello all,

I’d like to share my way how to install PyTorch on PYNQ.  
Tested:

- PYNQ-ZU board
- PYNQ image 3.0.1

At first, I tried the latest torch==2.0.0 but it caused core dumped every time I imported it. So torch==1.13.1 was selected.

You can install a specific version for your demand:  
(1) Go to [https://pypi.org/project/torch/#history](https://pypi.org/project/torch/#history).  
(2) Choose your version of choice, i.e. 1.13.1.  
(3) Go to `Download files` to check if a \*\_aarch64.whl was released, i.e.  
torch-1.13.1-cp310-cp310-manylinux2014\_aarch64.whl. Otherwise, you might need to compile it from source.

**Note :** PYNQ 3.0 uses Python 3.10. You can check the [PYNQ Release Notes](https://github.com/Xilinx/PYNQ/releases) for each version.

**INSTALLATION**

1. Open a new Terminal on Jupyter, or Open a new Terminal over SSH/Serial then `su -s` to switch to root account.
2. Activate PYNQ environment

```auto
source /etc/profile.d/pynq_venv.sh

```

1. Install

```auto
pip install --user torch==1.13.1

```

**VERIFICATION**  
(1) Execute the following Python code snippet to verify the installtion.  
(2) If you want to run it on Jupyter, then `Kernel -> Restart` to restart the IPython kernel.

```python
import torch
x = torch.rand(5, 3)
print(x)

```

Ref: [PyTorch linux-verification](https://pytorch.org/get-started/locally/#linux-verification)

Details on my board:

```auto
root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# lscpu
Architecture: **aarch64**
  CPU op-mode(s): 32-bit, 64-bit
  Byte Order: Little Endian
CPU(s): 4
  On-line CPU(s) list: 0-3
Vendor ID: ARM
  Model name: Cortex-A53
    Model: 4
    Thread(s) per core: 1
    Core(s) per cluster: 4
...
root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# source /etc/profile.d/pynq_venv.sh
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# pip install --user torch==1.7.1 torchstat torchvision==0.8.2ERROR: Could not find a version that satisfies the requirement torch==1.7.1 (from versions: 1.10.2, 1.11.0, 1.12.0, 1.12.1, 1.13.0, 1.13.1, 2.0.0)
ERROR: No matching distribution found for torch==1.7.1
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# pip install --user torch torchstat torchvisionCollecting torch
  Downloading torch-2.0.0-1-cp310-cp310-manylinux2014_aarch64.whl (74.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 74.3/74.3 MB 474.4 kB/s eta 0:00:00
Collecting torchstat
  Downloading torchstat-0.0.7-py3-none-any.whl (11 kB)
Collecting torchvision
  Downloading torchvision-0.15.1-cp310-cp310-manylinux2014_aarch64.whl (1.2 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 1.9 MB/s eta 0:00:00
Requirement already satisfied: typing-extensions in /usr/local/share/pynq-venv/lib/python3.10/site-packages (from torch) (4.3.0)
Collecting filelock
  Downloading filelock-3.12.0-py3-none-any.whl (10 kB)
Requirement already satisfied: networkx in /usr/lib/python3/dist-packages (from torch) (2.4)
Requirement already satisfied: sympy in /usr/lib/python3/dist-packages (from torch) (1.9)
Requirement already satisfied: jinja2 in /usr/lib/python3/dist-packages (from torch) (3.0.3)
Requirement already satisfied: pandas in /usr/lib/python3/dist-packages (from torchstat) (1.3.5)
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from torchstat) (1.21.5)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/lib/python3/dist-packages (from torchvision) (9.0.1)
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from torchvision) (2.25.1)
Installing collected packages: filelock, torch, torchvision, torchstat
  WARNING: The scripts convert-caffe2-to-onnx, convert-onnx-to-caffe2 and torchrun are installed in '/root/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script torchstat is installed in '/root/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed filelock-3.12.0 torch-2.0.0 torchstat-0.0.7 torchvision-0.15.1
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# python3
Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Illegal instruction (core dumped)
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# x = torch.rand(5, 3)
bash: syntax error near unexpected token `('
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# print(x)
bash: syntax error near unexpected token `x'
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# python3
Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Illegal instruction (core dumped)
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# pip install --user torch==1.13.1 torchstat torchvision
Collecting torch==1.13.1
  Downloading torch-1.13.1-cp310-cp310-manylinux2014_aarch64.whl (60.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 60.5/60.5 MB 349.4 kB/s eta 0:00:00
Requirement already satisfied: torchstat in /root/.local/lib/python3.10/site-packages (0.0.7)
Requirement already satisfied: torchvision in /root/.local/lib/python3.10/site-packages (0.15.1)
Requirement already satisfied: typing-extensions in /usr/local/share/pynq-venv/lib/python3.10/site-packages (from torch==1.13.1) (4.3.0)
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from torchstat) (1.21.5)
Requirement already satisfied: pandas in /usr/lib/python3/dist-packages (from torchstat) (1.3.5)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/lib/python3/dist-packages (from torchvision) (9.0.1)
Requirement already satisfied: requests in /usr/lib/python3/dist-packages (from torchvision) (2.25.1)
Collecting torchvision
  Downloading torchvision-0.14.1-cp310-cp310-manylinux2014_aarch64.whl (760 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 760.3/760.3 KB 965.9 kB/s eta 0:00:00
Installing collected packages: torch, torchvision
  Attempting uninstall: torch
    Found existing installation: torch 2.0.0
    Not uninstalling torch at /root/.local/lib/python3.10/site-packages, outside environment /usr/local/share/pynq-venv
    Can't uninstall 'torch'. No files were found to uninstall.
  WARNING: The scripts convert-caffe2-to-onnx, convert-onnx-to-caffe2 and torchrun are installed in '/root/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  Attempting uninstall: torchvision
    Found existing installation: torchvision 0.15.1
    Not uninstalling torchvision at /root/.local/lib/python3.10/site-packages, outside environment /usr/local/share/pynq-venv
    Can't uninstall 'torchvision'. No files were found to uninstall.
Successfully installed torch-2.0.0 torchvision-0.15.1
(pynq-venv) root@pynq:/home/xilinx/jupyter_notebooks/pynq-dpu# python3
Python 3.10.4 (main, Apr 2 2022, 09:04:19) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> x = torch.rand(5, 3)
<stdin>:1: UserWarning: Failed to initialize NumPy: module compiled against API version 0xf but this version of numpy is 0xe (Triggered internally at /root/pytorch/torch/csrc/utils/tensor_numpy.cpp:77.)
>>> x
tensor([[0.0751, 0.7460, 0.6827],
        [0.5740, 0.5532, 0.5004],
        [0.3974, 0.7535, 0.4684],
        [0.3987, 0.3578, 0.2454],
        [0.2679, 0.2589, 0.8194]])

```

Happy coding!
