Skip to main content

Tensors and Dynamic neural networks in Python

Project description

PyTorch for RISC-V Architecture

PyTorch is a popular open-source machine learning framework that provides a wide range of tools for building neural networks. It is widely used in academic and industry research, and is also used in production applications.

The following is the steps to build and install PyTorch for RISC-V architecture on OpenEuler 24.03.

It is more convenient to view the tutorial in GitHub and Gitee repository, which can be accessed through the following links:

Introduction

The RISC-V software ecosystem is still under construction, and it is common to encounter difficulties when trying to use popular software packages. This article provides a guide on how to install PyTorch in a RISC-V environment.

The system environment is shown in the image below: System Environment

The CPU model is SOPHON SG2042, RV64GC architecture, with V extension, 64 cores, and a clock speed of 2.0GHz. The toolchain versions are as shown: Toolchain Versions

Steps

Step 1: Install Packages

Use OpenEuler's dnf package manager to install the necessary packages with the following command:

sudo dnf install python3-{hypothesis,psutil,pyyaml,requests,sympy,filelock,networkx,jinja2,fsspec,packaging,numpy,venv}

Step 2: Create a Virtual Environment

Create a virtual environment with the following command:

cd; python3 -m venv --system-site-packages venv

The --system-site-packages option allows the virtual environment to inherit the system Python environment, thus using the global PyPI packages.

Activate the created environment with:

source ~/venv/bin/activate

Alternatively, add this command to ~/.bashrc to activate the environment automatically upon login.

Step 3: Install Other Dependencies

Activate the venv environment.

Install with pip

  • Now if you use riscv64 as the platform, you can install PyTorch with the following command:
pip install pytorch-riscv64
  • And then skip directly to the next step.
  • If you would like to install PyTorch with other options, you can see the steps below.

Install from PyPI Wheel

Download the:

After downloading the wheel file, install it with the following command:

source ~/venv/bin/activate
pip install torch-2.3.0a1+gitunknown-cp311-cp311-linux_riscv64.whl

The following is the steps to install PyTorch from source code.

If you have already installed PyTorch from PyPI, you can skip this step.

Install from Source

We recommend installing PyTorch version 2.3.0. First, download the source code (this process can be done locally and then uploaded to the server):

wget https://github.com/pytorch/pytorch/releases/download/v2.3.0/pytorch-v2.3.0.tar.gz

Extract it:

tar xvf pytorch-v2.3.0.tar.gz
cd pytorch-v2.3.0/

Update cpuinfo by executing the following:

cd third_party/
rm -rf cpuinfo/
git clone https://github.com/sophgo/cpuinfo.git
cd ..

Then, make the following changes:

  1. In aten/src/ATen/CMakeLists.txt, replace the line:

    if(NOT MSVC AND NOT EMSCRIPTEN AND NOT INTERN_BUILD_MOBILE)
    

    with:

    if(FALSE)
    
  2. In caffe2/CMakeLists.txt, replace the line:

    target_link_libraries(${test_name}_${CPU_CAPABILITY} c10 sleef gtest_main)
    

    with:

    target_link_libraries(${test_name}_${CPU_CAPABILITY} c10 gtest_main)
    
  3. In test/cpp/api/CMakeLists.txt, add the following line after:

    add_executable(test_api ${TORCH_API_TEST_SOURCES})
    
    target_compile_options(test_api PUBLIC -Wno-nonnull)
    

Save the changes. If the modifications were done locally, upload the modified source code to the server.

Create a build script:

#!/bin/bash
source ~/venv/bin/activate
export USE_CUDA=0 # CUDA is not available on RISC-V architecture servers
export USE_DISTRIBUTED=0 # Distributed support is not available
export USE_MKLDNN=0 # MKL is not supported as it is not an Intel processor
export MAX_JOBS=5 # Number of compile processes, adjust as needed
python3 setup.py develop --cmake

Save it as build.sh in the pytorch-v2.3.0/ directory on the server. Execute the script with:

bash build.sh

This will automatically complete the build process. The build time is quite long; in my case, using 5 processes, it took two to three hours. Consider using tmux or screen for a persistent session. If needed, install these tools using the dnf package manager.

Step 4: Verify Installation

If no errors occurred during the installation, the process is complete. The following shows a verification test: Verification

If you get the corresponding output as shown in the image, PyTorch has been successfully installed.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pytorch_riscv64-2.3.0a1-cp311-none-any.whl (93.7 MB view details)

Uploaded CPython 3.11

File details

Details for the file pytorch_riscv64-2.3.0a1-cp311-none-any.whl.

File metadata

File hashes

Hashes for pytorch_riscv64-2.3.0a1-cp311-none-any.whl
Algorithm Hash digest
SHA256 ef8c1c6de94f98fb67dfc86308e28ac52ba413779723f23c19e1277e3866975e
MD5 585064c4432a8bb49d167acb793d22e6
BLAKE2b-256 6cc53cb69a9fc791ed71782e874f9ddb43b1911f3d05bdd142ed432ef99fbcef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page