35 KiB
PyTorch Installation Guide
Overview
When setting up the environment for PyTorch model development and running, you can manually build and install the modules adapted to the PyTorch framework on a server.
Figure 1 Environment setup process
Manual Build and Installation
Prerequisites
Prerequisites
- The development or operating environment of CANN has been installed. For details, see the CANN Software Installation Guide.
- CMake 3.12.0 or later has been installed. For details about how to install CMake, see Installing CMake.
- GCC 7.3.0 or later has been installed. For details about how to install and use GCC 7.3.0, see How Do I Install GCC 7.3.0?.
- Python 3.7.5 or 3.8 has been installed.
- The Patch and Git tools have been installed in the environment. To install the tools for Ubuntu and CentOS, run the following commands:
-
Ubuntu
apt-get install patch apt-get install git -
CentOS
yum install patch yum install git
-
Installing the PyTorch Framework
Installation Process
-
Log in to the server as the root user or a non-root user.
-
Run the following commands in sequence to install the PyTorch dependencies.
If you install Python and its dependencies as a non-root user, add --user at the end of each command in this step. Example command: pip3.7 install pyyaml --user.
pip3 install pyyaml pip3 install wheel -
Obtain the PyTorch source code.
-
Run the following command to obtain the PyTorch source code adapted to Ascend AI Processors:
git clone https://gitee.com/ascend/pytorch.gitThe directory structure of the downloaded source code is as follows:
├── patch # Directory of the patch adapted to Ascend AI Processors │ ├── pytorch1.5.0_npu.patch # Patch for PyTorch 1.5.0 │ └── pytorch1.8.1_npu.patch # Patch for PyTorch 1.8.1 ├── pytorch1.5.0 # Source code and test directory for PyTorch 1.5.0 │ ├── access_control_test.py │ ├── src # Source code directory │ └── test # Directory for storing test cases ├── pytorch1.8.1 # Source code and test directory for PyTorch 1.8.1 │ ├── access_control_test.py │ ├── src # Source code directory │ └── test # Directory for storing test cases └── scripts # Build and create a directory. -
Obtain the native PyTorch source code from the root directory /pytorch of the current repository.
-
If PyTorch 1.5.0 is installed, run the following command:
git clone -b v1.5.0 --depth=1 https://github.com/pytorch/pytorch.git -
If PyTorch 1.8.1 is installed, run the following command:
git clone -b v1.8.1 --depth=1 https://github.com/pytorch/pytorch.git
-
-
Run the following commands to go to the native PyTorch code directory pytorch and obtain the PyTorch passive dependency code:
cd pytorch git submodule sync git submodule update --init --recursive
NOTE:
Due to network fluctuation, it may take a long time to obtain the source code. If no error is reported after the download is complete, the PyTorch and third-party code on which PyTorch depends are generated. -
-
Generate the PyTorch installation package adapted to Ascend AI Processors.
-
Go to the pytorch/scripts directory and run the conversion script to generate full code adapted to Ascend AI Processors.
cd ../scripts # If PyTorch 1.5.0 is installed: bash gen.sh # If PyTorch 1.8.1 is installed: bash gen.sh -v 1.8.1The full code adapted to Ascend AI Processors is generated in the pytorch/pytorch directory.
-
Go to the pytorch/pytorch/ directory and install the dependency library.
cd ../pytorch pip3 install -r requirements.txt -
Build and generate the binary installation package of PyTorch.
bash build.sh --python=3.7 or bash build.sh --python=3.8Specify the Python version in the environment for build. The generated binary package is stored in the current dist directory pytorch/pytorch/dist.
-
-
Go to the pytorch/pytorch/dist directory and run the following command to install PyTorch:
pip3 install --upgrade torch-1.5.0+ascend.post3-cp37-cp37m-linux_{arch}.whl{arch} indicates the architecture information. The value can be aarch64 or x86_64.
NOTE:
To upgrade PyTorch in the environment, uninstall the PyTorch software package installed in the environment and then perform Step 5 Install PyTorch. Run the following command to check whether PyTorch has been installed:
pip3 list | grep torch
Configuring Environment Variables
After the software packages are installed, configure environment variables to use Ascend PyTorch. For details about the environment variables, see Table 1.
-
Configure the operating environment variables by running the following command in the root directory of the PyTorch source code adapted to Ascend AI Processors:
source pytorch/env.sh -
Select a proper HCCL initialization mode based on the actual scenario and configure the corresponding environment variables.
# Scenario 1: Single-node scenario export HCCL_WHITELIST_DISABLE=1 # Disable the HCCL trustlist. # Scenario 2: Multi-node scenario export HCCL_WHITELIST_DISABLE=1 # Disable the HCCL trustlist. export HCCL_IF_IP="1.1.1.1" # 1.1.1.1 is the NIC IP address of the host. Change it based on the site requirements. Ensure that the NIC IP addresses in use can communicate with each other in the cluster. -
(Optional) Configure function or performance environment variables in the NPU scenario. The variables are disabled by default.
export DYNAMIC_COMPILE_ENABLE=1 # (Optional) Dynamic shape feature, which is used when the shape changes. To enable this function, set the value to 1. (PyTorch 1.8.1 does not support this environment variable.) export COMBINED_ENABLE=1 # (Optional) Optimizes the scenario where two inconsecutive operators are combined. To enable this function, set the value to 1. export TRI_COMBINED_ENABLE=1 # (Optional) Optimizes the scenario where three inconsecutive operators are combined. To enable this function, set the value to 1. export ACL_DUMP_DATA=1 # (Optional) Operator data dump function, which is used for debugging. To enable this function, set the value to 1. export DYNAMIC_OP="ADD#MUL" # (Optional) Operator implementation. The performance of the ADD and MUL operators varies depending on the scenarios. -
(Optional) If the system is openEuler or its inherited OS, such as UOS, run the following command to cancel CPU core binding.
# unset GOMP_CPU_AFFINITY
Table 1 Description of environment variables
Dynamic library search path. Set this variable based on the preceding example. If you need to upgrade GCC in OSs such as CentOS, Debian, and BC-Linux, add ${install_path}/lib64 to the LD_LIBRARY_PATH variable of the dynamic library search path. Replace {install_path} with the GCC installation path. For details, see 5. |
|
Python search path. Set this variable based on the preceding example. |
|
Executable program search path. Set this variable based on the preceding example. |
|
Operator package (OPP) root directory. Set this variable based on the preceding example. |
|
Whether to asynchronously deliver tasks and call the ACL APIs. You are advised to set this parameter to 1 to enable this function. |
|
Whether to enable the communication trustlist when the HCCL is used.
The default value is 0, indicating that the trustlist is enabled by default. |
|
IP address of the NIC for initializing communication in the HCCL.
By default, the host communication NICs are selected in the following sequence: NICs other than Docker/local NICs (in ascending alphabetical order of NIC names) > Docker NICs > local NICs. |
|
Use the PTCopy operator mode to accelerate the operation of converting non-contiguous tensors to contiguous tensors and copy process. You are advised to set the value to 1 to enable this function. |
|
(Optional) Whether to enable the log printing function. |
|
Sets the global log level of app logs. |
|
Whether to enable event logging for apps. |
|
(Optional) Dynamic shape feature, which is used when the shape changes. To enable this function, set the value to 1. (PyTorch 1.8.1 does not support this environment variable.) |
|
(Optional) Optimizes the scenario where two inconsecutive operators are combined. To enable this function, set the value to 1. |
|
(Optional) Optimizes the scenario where three inconsecutive operators are combined. To enable this function, set the value to 1. |
|
(Optional) Operator data dump function, which is used for debugging. To enable this function, set the value to 1. |
|
(Optional) Operator implementation. The performance of the ADD and MUL operators varies depending on the scenarios. This parameter is not set by default. |
|
(Optional) If the system is openEuler or its inherited OS, such as UOS, run this command to cancel CPU core binding. |
Installing the Mixed Precision Module
Prerequisites
- Ensure that the PyTorch framework adapted to Ascend AI Processors in the operating environment can be used properly.
- Before building and installing Apex, you have configured the environment variables on which the build depends. See Configuring Environment Variables.
Installation Process
-
Log in to the server as the root user or a non-root user.
-
Obtain the Apex source code.
-
Run the following command to obtain the Apex source code adapted to Ascend AI Processors:
git clone https://gitee.com/ascend/apex.gitThe directory structure of the downloaded source code is as follows:
apex │ ├─patch # Directory of the patch adapted to Ascend AI Processors │ ├─npu.patch │ ├─scripts # Build and create a directory. │ ├─gen.sh │ ├─src # Source code directory │ ├─tests # Directory for storing test cases -
Run the following commands to go to the apex directory and obtain the native Apex source code:
cd apex git clone https://github.com/NVIDIA/apex.gitAfter the native Apex source code is downloaded, the main directory structure of the code is as follows:
apex │ ├─apex # Directory for storing the native Apex code │ ├─patch # Directory of the patch adapted to Ascend AI Processors │ ├─npu.patch │ ├─scripts # Build and create a directory. │ ├─gen.sh │ ├─src # Source code directory │ ├─tests # Directory for storing test cases -
Go to the native Apex code directory, that is, apex/apex. Switch to the code branch whose commit ID is 4ef930c1c884fdca5f472ab2ce7cb9b505d26c1a.
cd apex git checkout 4ef930c1c884fdca5f472ab2ce7cb9b505d26c1a
NOTE:
Due to network fluctuation, it may take a long time to obtain the source code. -
-
Generate the Apex installation package adapted to Ascend AI Processors.
-
Go to the apex/scripts directory and run the conversion script to generate full code adapted to Ascend AI Processors.
cd ../scripts bash gen.shThe full code adapted to Ascend AI Processors is generated in the apex/apex directory.
-
Go to the full code directory apex/apex, and compile and generate the binary installation package of Apex.
cd ../apex python3 setup.py --cpp_ext --npu_float_status bdist_wheelThe Python version must be the same as that used by PyTorch. The generated binary package is stored in the current dist directory, that is, apex/apex/dist.
-
-
Go to the apex/apex/dist directory and run the following command to install Apex:
pip3 install --upgrade apex-0.1+ascend-cp37-cp37m-linux_{arch}.whl{arch} indicates the architecture information. The value can be aarch64 or x86_64.
NOTE:
To upgrade PyTorch in the environment, uninstall the PyTorch software package installed in the environment and then perform Step 4 Install Apex. Run the following command to check whether PyTorch has been installed:
pip3 list | grep apex
References
Installing CMake
Procedure for upgrading CMake to 3.12.1
-
Obtain the CMake software package.
wget https://cmake.org/files/v3.12/cmake-3.12.1.tar.gz --no-check-certificate -
Decompress the package and go to the software package directory.
tar -xf cmake-3.12.1.tar.gz cd cmake-3.12.1/ -
Run the configuration, build, and installation commands.
./configure --prefix=/usr/local/cmake make && make install -
Set the soft link.
ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake -
Run the following command to check whether CMake has been installed:
cmake --versionIf the message "cmake version 3.12.1" is displayed, the installation is successful.
How Do I Install GCC 7.3.0?
Perform the following steps as the root user.
-
Download gcc-7.3.0.tar.gz from https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz.
-
GCC installation requires adequate temporary space. Run the following command to clear the /tmp directory in advance:
sudo rm -rf /tmp/* -
Install the dependency package. (CentOS and Ubuntu are used as examples.)
-
For CentOS, run the following command:
yum install bzip2 -
For Ubuntu, run the following command:
apt-get install bzip2
-
-
Build and install GCC.
-
Go to the directory where the source package gcc-7.3.0.tar.gz is located and run the following command to decompress it:
tar -zxvf gcc-7.3.0.tar.gz -
Go to the extracted directory and run the following command to download the GCC dependency packages:
cd gcc-7.3.0 ./contrib/download_prerequisitesIf an error is reported during the command execution, run the following commands in the gcc-7.3.0/ directory to download the dependency packages:
wget http://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2 wget http://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2 wget http://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz wget http://gcc.gnu.org/pub/gcc/infrastructure/isl-0.16.1.tar.bz2After the preceding dependencies are downloaded, run the following command again:
./contrib/download_prerequisitesIf the validation fails, check whether the dependency packages are repeatedly downloaded. The packages should be downloaded at a time.
-
Run the configuration, build, and installation commands.
./configure --enable-languages=c,c++ --disable-multilib --with-system-zlib --prefix=/usr/local/linux_gcc7.3.0 make -j15 # Check the number of CPUs by running grep -w processor /proc/cpuinfo|wc -l. In this example, the number is 15. make install
NOTICE:
The --prefix option is used to specify the linux_gcc7.3.0 installation path, which is configurable. Do not set it to /usr/local or /usr, which is the default installation path for the GCC installed by using the software source. Otherwise, a conflict occurs and the original GCC compilation environment of the system is damaged. In this example, the installation path is set to /usr/local/linux_gcc7.3.0.
-
-
Set the environment variable.
Training must be performed in the compilation environment with GCC upgraded. If you want to run training, configure the following environment variable in your training script:
export LD_LIBRARY_PATH=${install_path}/lib64:${LD_LIBRARY_PATH}${install_path} indicates the GCC 7.3.0 installation path configured in 3. In this example, the GCC 7.3.0 installation path is /usr/local/gcc7.3.0/.
NOTE:
Skip this step if you do not need to use the compilation environment with GCC upgraded.
What Do I Do If "torch 1.5.0xxxx" and "torchvision" Do Not Match When torch-\*.whl Is Installed?
Symptom
During the installation of torch-*.whl, the message "ERROR: torchvision 0.6.0 has requirement torch==1.5.0, but you'll have torch 1.5.0a0+1977093 which is incompatible" " is displayed.
Possible Causes
When the PyTorch is installed, the version check is automatically triggered. The version of the torchvision installed in the environment is 0.6.0. During the check, it is found that the version of the torch-*.whl is inconsistent with the required version 1.5.0. As a result, an error message is displayed, but the installation is successful.
Solution
This problem has no impact on the actual result, and no action is required.

