Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a8e6f8c86 | ||
|
|
0db8c607b6 | ||
|
|
5e0fc864a2 | ||
|
|
accb0ba603 | ||
|
|
39849cf72d | ||
|
|
129f42c911 | ||
|
|
7977e5737e | ||
|
|
dae2415d1e | ||
|
|
118987cbb1 | ||
|
|
1fcf3a003c | ||
|
|
4fb3c0c667 | ||
|
|
67c590693e | ||
|
|
b2c0e583d8 | ||
|
|
8757692989 | ||
|
|
df5ed40e13 | ||
|
|
2d65cb41ed | ||
|
|
471afd84a8 | ||
|
|
f43426a4c5 | ||
|
|
bb7f740c87 |
@@ -13,4 +13,5 @@ reviewers:
|
||||
- wangyu0516
|
||||
- yskhhh
|
||||
- lvpeiyuan
|
||||
- xchu42
|
||||
- xchu42
|
||||
- weili10
|
||||
@@ -24,14 +24,11 @@ yum install -y cmake zlib-devel libffi-devel openssl-devel libjpeg-turbo-devel g
|
||||
apt-get install -y gcc g++ make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev m4 cmake dos2unix libopenblas-dev
|
||||
|
||||
|
||||
# python依赖库
|
||||
# Ascend配套软件
|
||||
|
||||
|
||||
依赖库安装:
|
||||
|
||||
```python3
|
||||
pip3 install -r requirements.txt
|
||||
```
|
||||
| AscendPyTorch版本 | CANN版本 | 支持PyTorch版本 |
|
||||
| :------------ | :----------- | :----------- |
|
||||
| 2.0.2 | CANN 5.0.2 | 1.5.0 |
|
||||
|
||||
|
||||
# 使用方式 --生成全量代码并编译
|
||||
@@ -63,6 +60,16 @@ bash gen.sh
|
||||
|
||||
会在pytorch/pytorch/目录中生成npu适配全量代码
|
||||
|
||||
|
||||
## python依赖库
|
||||
|
||||
进入到pytorch/pytorch/目录,依赖库安装:
|
||||
|
||||
```python3
|
||||
pip3 install -r requirements.txt
|
||||
```
|
||||
|
||||
|
||||
## 编译torch的二进制包
|
||||
|
||||
进入到pytorch/pytorch/目录,执行
|
||||
|
||||
|
Before Width: | Height: | Size: 580 B After Width: | Height: | Size: 580 B |
|
Before Width: | Height: | Size: 580 B After Width: | Height: | Size: 580 B |
|
Before Width: | Height: | Size: 394 B After Width: | Height: | Size: 394 B |
|
Before Width: | Height: | Size: 406 B After Width: | Height: | Size: 406 B |
|
Before Width: | Height: | Size: 253 B After Width: | Height: | Size: 253 B |
|
Before Width: | Height: | Size: 580 B After Width: | Height: | Size: 580 B |
@@ -0,0 +1,568 @@
|
||||
# FrameworkPTAdapter 2.0.2 PyTorch Installation Guide
|
||||
- [Overview](#overview)
|
||||
- [Manual Build and Installation](#manual-build-and-installation)
|
||||
- [Prerequisites](#prerequisites)
|
||||
- [Installing the PyTorch Framework](#installing-the-pytorch-framework)
|
||||
- [Configuring Environment Variables](#configuring-environment-variables)
|
||||
- [Installing the Mixed Precision Module](#installing-the-mixed-precision-module)
|
||||
- [Using the Ascend Hub Image](#using-the-ascend-hub-image)
|
||||
- [Obtaining the PyTorch Image from the Ascend Hub](#obtaining-the-pytorch-image-from-the-ascend-hub)
|
||||
- [Configuring Environment Variables](#configuring-environment-variables-0)
|
||||
- [References](#references)
|
||||
- [Installing CMake](#installing-cmake)
|
||||
- [How Do I Install GCC 7.3.0?](#how-do-i-install-gcc-7-3-0)
|
||||
- [What Do I Do If "torch 1.5.0xxxx" and "torchvision" Do Not Match When torch-\*.whl Is Installed?](#what-do-i-do-if-torch-1-5-0xxxx-and-torchvision-do-not-match-when-torch--whl-is-installed)
|
||||
<h2 id="overview">Overview</h2>
|
||||
|
||||
When setting up the environment for PyTorch model porting and training, you can manually build and install the modules adapted to the PyTorch framework on a training server, or use the base image provided by the Ascend Hub image center \(the PyTorch module and mixed precision module have been installed in the image\).
|
||||
|
||||
**Figure 1** Environment setup process<a name="en-us_topic_0000001119176876_fig1938918396117"></a>
|
||||

|
||||
|
||||
<h2 id="manual-build-and-installation">Manual Build and Installation</h2>
|
||||
|
||||
- **[Prerequisites](#prerequisites)**
|
||||
|
||||
- **[Installing the PyTorch Framework](#installing-the-pytorch-framework)**
|
||||
|
||||
- **[Configuring Environment Variables](#configuring-environment-variables)**
|
||||
|
||||
- **[Installing the Mixed Precision Module](#installing-the-mixed-precision-module)**
|
||||
|
||||
|
||||
<h2 id="prerequisites">Prerequisites</h2>
|
||||
|
||||
## Prerequisites<a name="en-us_topic_0000001105856382_en-us_topic_0275872734_section108914373254"></a>
|
||||
|
||||
- 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](#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?](#how-do-i-install-gcc-7-3-0).
|
||||
- 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
|
||||
```
|
||||
|
||||
|
||||
|
||||
<h2 id="installing-the-pytorch-framework">Installing the PyTorch Framework</h2>
|
||||
|
||||
## Installation Process<a name="en-us_topic_0000001152776301_section1611810384557"></a>
|
||||
|
||||
1. Log in to the server as the **root** user or a non-root user.
|
||||
2. 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
|
||||
```
|
||||
|
||||
3. Obtain the PyTorch source code.
|
||||
|
||||
1. Run the following command to obtain the PyTorch source code adapted to Ascend AI Processors:
|
||||
|
||||
```
|
||||
git clone https://gitee.com/ascend/pytorch.git
|
||||
```
|
||||
|
||||
The directory structure of the downloaded source code is as follows:
|
||||
|
||||
```
|
||||
pytorch
|
||||
│ ├─patch # Directory of the patch adapted to Ascend AI Processors
|
||||
│ ├─npu.patch
|
||||
│ ├─scripts # Build and create a directory.
|
||||
│ ├─gen.sh
|
||||
│ ├─src # Source code directory
|
||||
│ ├─test # Directory for storing test cases
|
||||
│ ├─README.md
|
||||
```
|
||||
|
||||
2. Run the following commands to go to the **pytorch** directory and obtain the native PyTorch source code:
|
||||
|
||||
```
|
||||
cd pytorch
|
||||
git clone -b v1.5.0 --depth=1 https://github.com/pytorch/pytorch.git
|
||||
```
|
||||
|
||||
After the native PyTorch source code is downloaded, the main directory structure of the code is as follows:
|
||||
|
||||
```
|
||||
pytorch
|
||||
│ ├─patch # Directory of the patch adapted to Ascend AI Processors
|
||||
│ ├─npu.patch
|
||||
│ ├─pytorch # Directory for storing the native PyTorch code
|
||||
│ ├─scripts # Build and create a directory.
|
||||
│ ├─gen.sh
|
||||
│ ├─src # Source code directory
|
||||
│ ├─test # Directory for storing test cases
|
||||
│ ├─README.md
|
||||
```
|
||||
|
||||
3. 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.
|
||||
|
||||
4. Generate the PyTorch installation package adapted to Ascend AI Processors.
|
||||
1. Go to the **pytorch/scripts** directory and run the conversion script to generate full code adapted to Ascend AI Processors.
|
||||
|
||||
```
|
||||
cd ../scripts
|
||||
bash gen.sh
|
||||
```
|
||||
|
||||
The full code adapted to Ascend AI Processors is generated in the **pytorch/pytorch** directory.
|
||||
|
||||
2. Go to the full code directory **pytorch/pytorch**, and compile and generate the binary installation package of PyTorch.
|
||||
|
||||
```
|
||||
cd ../pytorch
|
||||
bash build.sh
|
||||
```
|
||||
|
||||
The generated binary package is stored in the current dist directory **pytorch/pytorch/dist**.
|
||||
|
||||
5. <a name="en-us_topic_0000001152776301_li49671667141"></a>Install PyTorch.
|
||||
|
||||
Go to the **pytorch/pytorch/dist** directory and run the following command to install PyTorch:
|
||||
|
||||
```
|
||||
pip3 install --upgrade torch-1.5.0+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 5 Install PyTorch](#en-us_topic_0000001152776301_li49671667141). Run the following command to check whether PyTorch has been installed:
|
||||
>**pip3 list | grep torch**
|
||||
|
||||
|
||||
<h2 id="configuring-environment-variables">Configuring Environment Variables</h2>
|
||||
|
||||
After the software packages are installed, configure environment variables to use Ascend PyTorch. You are advised to build a startup script, for example, the **set\_env.sh** script, and run **source set\_env.sh** to configure the environment variables. The content of the **set\_env.sh** script is as follows \(the **root** user is used as the installation user and the default installation path is used\):
|
||||
|
||||
```
|
||||
cpu_type=$(echo $HOSTTYPE)
|
||||
|
||||
if [ x"${cpu_type}" == x"x86_64" ];then
|
||||
cpu_type=x86_64-linux
|
||||
else
|
||||
cpu_type=arm64-linux
|
||||
fi
|
||||
if [ -d /usr/local/Ascend/nnae/latest ];then
|
||||
export LD_LIBRARY_PATH=/usr/local/:/usr/local/python3.7.5/lib/:/usr/local/openblas/lib:/usr/local/lib/:/usr/lib64/:/usr/lib/:/usr/local/Ascend/nnae/latest/fwkacllib/lib64/:/usr/local/Ascend/driver/lib64/common/:/usr/local/Ascend/driver/lib64/driver/:/usr/local/Ascend/add-ons/:/usr/lib/aarch64_64-linux-gnu:$LD_LIBRARY_PATH
|
||||
export PATH=$PATH:/usr/local/Ascend/nnae/latest/fwkacllib/ccec_compiler/bin/:/usr/local/Ascend/nnae/latest/toolkit/tools/ide_daemon/bin/
|
||||
export ASCEND_OPP_PATH=/usr/local/Ascend/nnae/latest/opp/
|
||||
export OPTION_EXEC_EXTERN_PLUGIN_PATH=/usr/local/Ascend/nnae/latest/fwkacllib/lib64/plugin/opskernel/libfe.so:/usr/local/Ascend/nnae/latest/fwkacllib/lib64/plugin/opskernel/libaicpu_engine.so:/usr/local/Ascend/nnae/latest/fwkacllib/lib64/plugin/opskernel/libge_local_engine.so
|
||||
export PYTHONPATH=/usr/local/Ascend/nnae/latest/fwkacllib/python/site-packages/:/usr/local/Ascend/nnae/latest/fwkacllib/python/site-packages/auto_tune.egg/auto_tune:/usr/local/Ascend/nnae/latest/fwkacllib/python/site-packages/schedule_search.egg:$PYTHONPATH
|
||||
export ASCEND_AICPU_PATH=/usr/local/Ascend/nnae/latest/
|
||||
else
|
||||
export LD_LIBRARY_PATH=/usr/local/:/usr/local/lib/:/usr/lib64/:/usr/lib/:/usr/local/python3.7.5/lib/:/usr/local/openblas/lib:/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/lib64/:/usr/local/Ascend/driver/lib64/common/:/usr/local/Ascend/driver/lib64/driver/:/usr/local/Ascend/add-ons/:/usr/lib/aarch64-linux-gnu:$LD_LIBRARY_PATH
|
||||
export PATH=$PATH:/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin/:/usr/local/Ascend/ascend-toolkit/latest/toolkit/tools/ide_daemon/bin/
|
||||
export ASCEND_OPP_PATH=/usr/local/Ascend/ascend-toolkit/latest/opp/
|
||||
export OPTION_EXEC_EXTERN_PLUGIN_PATH=/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/lib64/plugin/opskernel/libfe.so:/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/lib64/plugin/opskernel/libaicpu_engine.so:/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/lib64/plugin/opskernel/libge_local_engine.so
|
||||
export PYTHONPATH=/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/python/site-packages/:/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/python/site-packages/auto_tune.egg/auto_tune:/usr/local/Ascend/ascend-toolkit/latest/fwkacllib/python/site-packages/schedule_search.egg:$PYTHONPATH
|
||||
export ASCEND_AICPU_PATH=/usr/local/Ascend/ascend-toolkit/latest/${cpu_type}
|
||||
fi
|
||||
path_lib=$(python3.7 -c """
|
||||
import sys
|
||||
import re
|
||||
result=''
|
||||
for index in range(len(sys.path)):
|
||||
match_sit = re.search('-packages', sys.path[index])
|
||||
if match_sit is not None:
|
||||
match_lib = re.search('lib', sys.path[index])
|
||||
if match_lib is not None:
|
||||
end=match_lib.span()[1]
|
||||
result += sys.path[index][0:end] + ':'
|
||||
result+=sys.path[index] + '/torch/lib:'
|
||||
print(result)"""
|
||||
)
|
||||
export LD_LIBRARY_PATH=/usr/local/python3.7.5/lib/:${path_lib}:$LD_LIBRARY_PATH
|
||||
export TASK_QUEUE_ENABLE=1
|
||||
|
||||
# (Optional) If the system is openEuler, run this command to cancel CPU core binding.
|
||||
# unset GOMP_CPU_AFFINITY
|
||||
|
||||
# Select an HCCL initialization method and configure the corresponding environment variables in the following scenarios. The details are as follows:
|
||||
# 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 used can communicate with each other in the cluster.
|
||||
```
|
||||
|
||||
[Table 1](#en-us_topic_0000001152616261_table42017516135) describes related variables.
|
||||
|
||||
**Table 1** Description of environment variables
|
||||
|
||||
<a name="en-us_topic_0000001152616261_table42017516135"></a>
|
||||
<table><thead align="left"><tr id="en-us_topic_0000001152616261_row16198951191317"><th class="cellrowborder" valign="top" width="55.48%" id="mcps1.2.3.1.1"><p id="en-us_topic_0000001152616261_p51981251161315"><a name="en-us_topic_0000001152616261_p51981251161315"></a><a name="en-us_topic_0000001152616261_p51981251161315"></a>Environment Variable</p>
|
||||
</th>
|
||||
<th class="cellrowborder" valign="top" width="44.519999999999996%" id="mcps1.2.3.1.2"><p id="en-us_topic_0000001152616261_p9198135114133"><a name="en-us_topic_0000001152616261_p9198135114133"></a><a name="en-us_topic_0000001152616261_p9198135114133"></a>Description</p>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody><tr id="en-us_topic_0000001152616261_row6882121917329"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p688241953218"><a name="en-us_topic_0000001152616261_p688241953218"></a><a name="en-us_topic_0000001152616261_p688241953218"></a>LD_LIBRARY_PATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p1888291915322"><a name="en-us_topic_0000001152616261_p1888291915322"></a><a name="en-us_topic_0000001152616261_p1888291915322"></a>Dynamic library search path. Set this variable based on the preceding example.</p>
|
||||
<p id="p1292181892120"><a name="p1292181892120"></a><a name="p1292181892120"></a>If you need to upgrade GCC in OSs such as CentOS, Debian, and BC-Linux, add <strong id="b6163826603"><a name="b6163826603"></a><a name="b6163826603"></a><em id="i161631926505"><a name="i161631926505"></a><a name="i161631926505"></a>${install_path}</em>/lib64</strong> to the <span class="parmname" id="parmname161637265015"><a name="parmname161637265015"></a><a name="parmname161637265015"></a><b>LD_LIBRARY_PATH</b></span> variable of the dynamic library search path. Replace <em id="i01649261704"><a name="i01649261704"></a><a name="i01649261704"></a><strong id="b1216317261109"><a name="b1216317261109"></a><a name="b1216317261109"></a>{install_path}</strong></em> with the GCC installation path. For details, see <a href="#how-do-i-install-gcc-7-3-0#en-us_topic_0000001135347812_en-us_topic_0000001173199577_en-us_topic_0000001172534867_en-us_topic_0276688294_li9745165315131">5</a>.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row16194175523010"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p16195185523019"><a name="en-us_topic_0000001152616261_p16195185523019"></a><a name="en-us_topic_0000001152616261_p16195185523019"></a>PYTHONPATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p19637083322"><a name="en-us_topic_0000001152616261_p19637083322"></a><a name="en-us_topic_0000001152616261_p19637083322"></a>Python search path. Set this variable based on the preceding example.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row2954102119329"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p195452113218"><a name="en-us_topic_0000001152616261_p195452113218"></a><a name="en-us_topic_0000001152616261_p195452113218"></a>PATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p964914893211"><a name="en-us_topic_0000001152616261_p964914893211"></a><a name="en-us_topic_0000001152616261_p964914893211"></a>Executable program search path. Set this variable based on the preceding example.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row58592816294"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p1886016892913"><a name="en-us_topic_0000001152616261_p1886016892913"></a><a name="en-us_topic_0000001152616261_p1886016892913"></a>ASCEND_OPP_PATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p28608892915"><a name="en-us_topic_0000001152616261_p28608892915"></a><a name="en-us_topic_0000001152616261_p28608892915"></a>Operator package (OPP) root directory. Set this variable based on the preceding example.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row144592037903"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p104601373014"><a name="en-us_topic_0000001152616261_p104601373014"></a><a name="en-us_topic_0000001152616261_p104601373014"></a>OPTION_EXEC_EXTERN_PLUGIN_PATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p1046013716017"><a name="en-us_topic_0000001152616261_p1046013716017"></a><a name="en-us_topic_0000001152616261_p1046013716017"></a>Path of the operator information library.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row16184379493"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p131851873492"><a name="en-us_topic_0000001152616261_p131851873492"></a><a name="en-us_topic_0000001152616261_p131851873492"></a>ASCEND_AICPU_PATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p181851575497"><a name="en-us_topic_0000001152616261_p181851575497"></a><a name="en-us_topic_0000001152616261_p181851575497"></a>Path of the AI CPU operator package.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row234714854615"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p2034724894619"><a name="en-us_topic_0000001152616261_p2034724894619"></a><a name="en-us_topic_0000001152616261_p2034724894619"></a>TASK_QUEUE_ENABLE</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p53477489462"><a name="en-us_topic_0000001152616261_p53477489462"></a><a name="en-us_topic_0000001152616261_p53477489462"></a>Whether to asynchronously deliver tasks and call the ACL APIs. You are advised to set this parameter to <strong id="en-us_topic_0000001152616261_b186701714141819"><a name="en-us_topic_0000001152616261_b186701714141819"></a><a name="en-us_topic_0000001152616261_b186701714141819"></a>1</strong> to enable this function.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row1680820246202"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p4809112415207"><a name="en-us_topic_0000001152616261_p4809112415207"></a><a name="en-us_topic_0000001152616261_p4809112415207"></a>HCCL_WHITELIST_DISABLE</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p952814428206"><a name="en-us_topic_0000001152616261_p952814428206"></a><a name="en-us_topic_0000001152616261_p952814428206"></a>Whether to enable the communication trustlist when the HCCL is used.</p>
|
||||
<a name="ul928845132310"></a><a name="ul928845132310"></a><ul id="ul928845132310"><li><strong id="b12793231525"><a name="b12793231525"></a><a name="b12793231525"></a>0</strong>: enable the trustlist. The HCCL communication trustlist does not need to be verified.</li><li><strong id="b1146142619212"><a name="b1146142619212"></a><a name="b1146142619212"></a>1</strong>: disable the trustlist. The HCCL communication trustlist needs to be verified.</li></ul>
|
||||
<p id="en-us_topic_0000001152616261_p5809162416201"><a name="en-us_topic_0000001152616261_p5809162416201"></a><a name="en-us_topic_0000001152616261_p5809162416201"></a>The default value is <strong id="en-us_topic_0000001152616261_b1270332516435"><a name="en-us_topic_0000001152616261_b1270332516435"></a><a name="en-us_topic_0000001152616261_b1270332516435"></a>0</strong>, indicating that the trustlist is enabled by default.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row0671137162115"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p4671203792114"><a name="en-us_topic_0000001152616261_p4671203792114"></a><a name="en-us_topic_0000001152616261_p4671203792114"></a>HCCL_IF_IP</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p1822165982114"><a name="en-us_topic_0000001152616261_p1822165982114"></a><a name="en-us_topic_0000001152616261_p1822165982114"></a>IP address of the NIC for initializing communication in the HCCL.</p>
|
||||
<a name="ul2676102292415"></a><a name="ul2676102292415"></a><ul id="ul2676102292415"><li>The IP address is in dotted decimal notation.</li><li>Currently, only the host NIC is supported.</li></ul>
|
||||
<p id="en-us_topic_0000001152616261_p1167163719217"><a name="en-us_topic_0000001152616261_p1167163719217"></a><a name="en-us_topic_0000001152616261_p1167163719217"></a>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.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0000001152616261_row1371356152313"><td class="cellrowborder" valign="top" width="55.48%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0000001152616261_p16711563237"><a name="en-us_topic_0000001152616261_p16711563237"></a><a name="en-us_topic_0000001152616261_p16711563237"></a>unset GOMP_CPU_AFFINITY</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="44.519999999999996%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0000001152616261_p0711356152317"><a name="en-us_topic_0000001152616261_p0711356152317"></a><a name="en-us_topic_0000001152616261_p0711356152317"></a>(Optional) If the system is openEuler, run this command to cancel CPU core binding.</p>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2 id="installing-the-mixed-precision-module">Installing the Mixed Precision Module</h2>
|
||||
|
||||
## Prerequisites<a name="en-us_topic_0000001106176190_section3225481020"></a>
|
||||
|
||||
1. Ensure that the PyTorch framework adapted to Ascend AI Processors in the operating environment can be used properly.
|
||||
2. Before building and installing Apex, you have configured the environment variables on which the build depends. See [Configuring Environment Variables](#configuring-environment-variables).
|
||||
|
||||
## Installation Process<a name="en-us_topic_0000001106176190_section11880164819567"></a>
|
||||
|
||||
1. Log in to the server as the **root** user or a non-root user.
|
||||
2. Obtain the Apex source code.
|
||||
|
||||
1. Run the following command to obtain the Apex source code adapted to Ascend AI Processors:
|
||||
|
||||
```
|
||||
git clone -b 2.0.2.tr5 https://gitee.com/ascend/apex.git
|
||||
```
|
||||
|
||||
The 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
|
||||
│ ├─README.md
|
||||
```
|
||||
|
||||
2. 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.git
|
||||
```
|
||||
|
||||
After 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
|
||||
│ ├─README.md
|
||||
```
|
||||
|
||||
3. 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
|
||||
cd ..
|
||||
```
|
||||
|
||||
> **NOTE:**
|
||||
>Due to network fluctuation, it may take a long time to obtain the source code.
|
||||
|
||||
3. Generate the Apex installation package adapted to Ascend AI Processors.
|
||||
1. Go to the **apex/scripts** directory and run the conversion script to generate full code adapted to Ascend AI Processors.
|
||||
|
||||
```
|
||||
cd ../scripts
|
||||
bash gen.sh
|
||||
```
|
||||
|
||||
The full code adapted to Ascend AI Processors is generated in the **apex/apex** directory.
|
||||
|
||||
2. 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_wheel
|
||||
```
|
||||
|
||||
The generated binary package is stored in the current dist directory **apex/apex/dist**.
|
||||
|
||||
4. <a name="en-us_topic_0000001106176190_li425495374416"></a>Install Apex.
|
||||
|
||||
Go to the **apex/apex/dist** directory and run the following command to install Apex:
|
||||
|
||||
```
|
||||
pip3.7 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](#en-us_topic_0000001106176190_li425495374416). Run the following command to check whether PyTorch has been installed:
|
||||
>**pip3 list | grep apex**
|
||||
|
||||
|
||||
<h2 id="using-the-ascend-hub-image">Using the Ascend Hub Image</h2>
|
||||
|
||||
- **[Obtaining the PyTorch Image from the Ascend Hub](#obtaining-the-pytorch-image-from-the-ascend-hub)**
|
||||
|
||||
- **[Configuring Environment Variables](#configuring-environment-variables-0)**
|
||||
|
||||
|
||||
<h2 id="obtaining-the-pytorch-image-from-the-ascend-hub">Obtaining the PyTorch Image from the Ascend Hub</h2>
|
||||
|
||||
## Prerequisites<a name="en-us_topic_0000001118701830_en-us_topic_0275872734_section108914373254"></a>
|
||||
|
||||
- The development or operating environment of CANN has been installed. For details, see the _CANN Software Installation Guide_.
|
||||
- Docker has been installed on the host.
|
||||
|
||||
## Obtaining and Using an Image<a name="en-us_topic_0000001118701830_section108941734162613"></a>
|
||||
|
||||
Log in to the [Ascend Hub](https://ascendhub.huawei.com/#/home) to obtain the required image. \(Activate the account when applying for an image for the first time.\)
|
||||
|
||||
[Table 1](#en-us_topic_0000001118701830_en-us_topic_0000001074498056_table1519011227314) lists the supported images. Select an image that you want.
|
||||
|
||||
**Table 1** Image list
|
||||
|
||||
<a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_table1519011227314"></a>
|
||||
<table><thead align="left"><tr id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_row0190152218319"><th class="cellrowborder" valign="top" width="55.00000000000001%" id="mcps1.2.4.1.1"><p id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1419132211315"><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1419132211315"></a><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1419132211315"></a>Image Name</p>
|
||||
</th>
|
||||
<th class="cellrowborder" valign="top" width="20%" id="mcps1.2.4.1.2"><p id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p75071327115313"><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p75071327115313"></a><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p75071327115313"></a>Image Version</p>
|
||||
</th>
|
||||
<th class="cellrowborder" valign="top" width="25%" id="mcps1.2.4.1.3"><p id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1024411406234"><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1024411406234"></a><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1024411406234"></a>CANN Version</p>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody><tr id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_row71915221134"><td class="cellrowborder" valign="top" width="55.00000000000001%" headers="mcps1.2.4.1.1 "><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_ul81691515131910"></a><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_ul81691515131910"></a><ul id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_ul81691515131910"><li>ARM architecture: <a href="https://ascendhub.huawei.com/#/detail?name=ascend-pytorch-arm" target="_blank" rel="noopener noreferrer">ascend-pytorch-arm</a></li><li>x86 architecture: <a href="https://ascendhub.huawei.com/#/detail?name=ascend-pytorch-x86" target="_blank" rel="noopener noreferrer">ascend-pytorch-x86</a></li></ul>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.4.1.2 "><p id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p14648161414516"><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p14648161414516"></a><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p14648161414516"></a>21.0.2</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.4.1.3 "><p id="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1264815147514"><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1264815147514"></a><a name="en-us_topic_0000001118701830_en-us_topic_0000001074498056_p1264815147514"></a><a href="https://support.huawei.com/enterprise/zh/ascend-computing/cann-pid-251168373" target="_blank" rel="noopener noreferrer">5.0.2</a></p>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h2 id="configuring-environment-variables-0">Configuring Environment Variables</h2>
|
||||
|
||||
After starting and entering the image container, configure the environment variables on which model training depends by referring to [Configuring Environment Variables](#configuring-environment-variables).
|
||||
|
||||
<h2 id="references">References</h2>
|
||||
|
||||
- **[Installing CMake](#installing-cmake)**
|
||||
|
||||
- **[How Do I Install GCC 7.3.0?](#how-do-i-install-gcc-7-3-0)**
|
||||
|
||||
- **[What Do I Do If "torch 1.5.0xxxx" and "torchvision" Do Not Match When torch-\*.whl Is Installed?](#what-do-i-do-if-torch-1-5-0xxxx-and-torchvision-do-not-match-when-torch--whl-is-installed)**
|
||||
|
||||
|
||||
<h2 id="installing-cmake">Installing CMake</h2>
|
||||
|
||||
Procedure for upgrading CMake to 3.12.1
|
||||
|
||||
1. Obtain the CMake software package.
|
||||
|
||||
```
|
||||
wget https://cmake.org/files/v3.12/cmake-3.12.1.tar.gz --no-check-certificate
|
||||
```
|
||||
|
||||
2. Decompress the package and go to the software package directory.
|
||||
|
||||
```
|
||||
tar -xf cmake-3.12.1.tar.gz
|
||||
cd cmake-3.12.1/
|
||||
```
|
||||
|
||||
3. Run the configuration, build, and installation commands.
|
||||
|
||||
```
|
||||
./configure --prefix=/usr/local/cmake
|
||||
make && make install
|
||||
```
|
||||
|
||||
4. Set the soft link.
|
||||
|
||||
```
|
||||
ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake
|
||||
```
|
||||
|
||||
5. Run the following command to check whether CMake has been installed:
|
||||
|
||||
```
|
||||
cmake --version
|
||||
```
|
||||
|
||||
If the message "cmake version 3.12.1" is displayed, the installation is successful.
|
||||
|
||||
|
||||
<h2 id="how-do-i-install-gcc-7-3-0">How Do I Install GCC 7.3.0?</h2>
|
||||
|
||||
Perform the following steps as the **root** user.
|
||||
|
||||
1. 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](https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz).
|
||||
2. GCC installation requires adequate temporary space. Run the following command to clear the **/tmp** directory in advance:
|
||||
|
||||
```
|
||||
sudo rm -rf /tmp/*
|
||||
```
|
||||
|
||||
3. 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
|
||||
```
|
||||
|
||||
4. Build and install GCC.
|
||||
1. 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
|
||||
```
|
||||
|
||||
2. Go to the extracted directory and run the following command to download the GCC dependency packages:
|
||||
|
||||
```
|
||||
cd gcc-7.3.0
|
||||
./contrib/download_prerequisites
|
||||
```
|
||||
|
||||
If 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.bz2
|
||||
```
|
||||
|
||||
After the preceding dependencies are downloaded, run the following command again:
|
||||
|
||||
```
|
||||
./contrib/download_prerequisites
|
||||
```
|
||||
|
||||
If the validation fails, check whether the dependency packages are repeatedly downloaded. The packages should be downloaded at a time.
|
||||
|
||||
3. <a name="en-us_topic_0000001135347812_en-us_topic_0000001173199577_en-us_topic_0000001172534867_en-us_topic_0276688294_li1649343041310"></a>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**.
|
||||
|
||||
|
||||
5. 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](#en-us_topic_0000001135347812_en-us_topic_0000001173199577_en-us_topic_0000001172534867_en-us_topic_0276688294_li1649343041310). 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.
|
||||
|
||||
|
||||
<h2 id="what-do-i-do-if-torch-1-5-0xxxx-and-torchvision-do-not-match-when-torch--whl-is-installed">What Do I Do If "torch 1.5.0xxxx" and "torchvision" Do Not Match When torch-\*.whl Is Installed?</h2>
|
||||
|
||||
## Symptom<a name="en-us_topic_0000001105856364_en-us_topic_0175549220_section197270431505"></a>
|
||||
|
||||
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<a name="en-us_topic_0000001105856364_en-us_topic_0175549220_section169499490501"></a>
|
||||
|
||||
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<a name="en-us_topic_0000001105856364_section108142031907"></a>
|
||||
|
||||
This problem has no impact on the actual result, and no action is required.
|
||||
|
||||
|
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 394 B |
|
After Width: | Height: | Size: 406 B |
|
After Width: | Height: | Size: 253 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 356 B |
|
After Width: | Height: | Size: 9.2 KiB |
|
After Width: | Height: | Size: 946 B |
|
After Width: | Height: | Size: 946 B |
|
After Width: | Height: | Size: 356 B |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 73 KiB |
|
After Width: | Height: | Size: 126 KiB |
|
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 607 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 51 KiB |
|
After Width: | Height: | Size: 57 KiB |
|
After Width: | Height: | Size: 19 KiB |
|
After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 7.8 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 66 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 126 KiB |
|
After Width: | Height: | Size: 56 KiB |
|
After Width: | Height: | Size: 59 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 59 KiB |
|
After Width: | Height: | Size: 101 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 26 KiB |
|
After Width: | Height: | Size: 76 KiB |
|
After Width: | Height: | Size: 7.2 KiB |
|
After Width: | Height: | Size: 7.2 KiB |
|
After Width: | Height: | Size: 69 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 17 KiB |
|
After Width: | Height: | Size: 58 KiB |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 394 B |
|
After Width: | Height: | Size: 406 B |
|
After Width: | Height: | Size: 253 B |
|
After Width: | Height: | Size: 580 B |
@@ -0,0 +1,599 @@
|
||||
# PyTorch Online Inference Guide
|
||||
- [Application Scenario](#application-scenario)
|
||||
- [Basic Workflow](#basic-workflow)
|
||||
- [Prerequisites](#prerequisites)
|
||||
- [Online Inference Process](#online-inference-process)
|
||||
- [Environment Variable Configuration](#environment-variable-configuration)
|
||||
- [Sample Reference](#sample-reference)
|
||||
- [Special Topics](#special-topics)
|
||||
- [Mixed Precision](#mixed-precision)
|
||||
- [How Do I Install GCC 7.3.0?](#how-do-i-install-gcc-7-3-0)
|
||||
<h2 id="application-scenario">Application Scenario</h2>
|
||||
|
||||
Online inference, unlike offline inference, allows developers to perform inference directly with PyTorch models using the **model.eval\(\)** method.
|
||||
|
||||
In this way, PyTorch-based inference apps can be directly ported to the Ascend AI Processor, which is especially useful in the data center inference scenarios.
|
||||
|
||||
## Supported Processors<a name="section15598658104219"></a>
|
||||
|
||||
Ascend 910 AI Processor
|
||||
|
||||
Ascend 710 AI Processor
|
||||
|
||||
<h2 id="basic-workflow">Basic Workflow</h2>
|
||||
|
||||
- **[Prerequisites](#prerequisites)**
|
||||
|
||||
- **[Online Inference Process](#online-inference-process)**
|
||||
|
||||
- **[Environment Variable Configuration](#environment-variable-configuration)**
|
||||
|
||||
- **[Sample Reference](#sample-reference)**
|
||||
|
||||
|
||||
<h2 id="prerequisites">Prerequisites</h2>
|
||||
|
||||
The PyTorch framework and mixed precision module have been installed. For details, see the .
|
||||
|
||||
<h2 id="online-inference-process">Online Inference Process</h2>
|
||||
|
||||
[Figure 1](#fig13802941161818) shows the online inference process.
|
||||
|
||||
**Figure 1** Online inference process<a name="fig13802941161818"></a>
|
||||

|
||||
|
||||
<h2 id="environment-variable-configuration">Environment Variable Configuration</h2>
|
||||
|
||||
The following are the environment variables required for starting the inference process on PyTorch:
|
||||
|
||||
```
|
||||
# Set the environment variable for the installation path of the infrastructure software on which online inference depends. The following assumes that the installation user is HwHiAiUser, and the default installation path is used.
|
||||
# Method 1: Install Ascend-CANN-Toolkit for inference on an Ascend AI device, which serves as the development environment.
|
||||
. /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh
|
||||
# Method 2: Install Ascend-CANN-NNAE on an Ascend AI device.
|
||||
. /home/HwHiAiUser/Ascend/nnae/set_env.sh
|
||||
|
||||
# If multiple Python 3 versions exist in the operating environment, configure the installation path of Python 3.7.5 in environment variables.
|
||||
export PATH=/usr/local/python3.7.5/bin:$PATH
|
||||
export LD_LIBRARY_PATH=/usr/local/python3.7.5/lib:$LD_LIBRARY_PATH
|
||||
|
||||
# Sets the logical ID of a processor.
|
||||
export ASCEND_DEVICE_ID=0
|
||||
|
||||
# Output log information. Replace it as required.
|
||||
export ASCEND_SLOG_PRINT_TO_STDOUT=1
|
||||
export ASCEND_GLOBAL_LOG_LEVEL=0
|
||||
|
||||
# Task issued with multiple threads
|
||||
export TASK_QUEUE_ENABLE=0
|
||||
```
|
||||
|
||||
**Table 1** Description of environment variables
|
||||
|
||||
<a name="table2072027194819"></a>
|
||||
<table><thead align="left"><tr id="row1173527114812"><th class="cellrowborder" valign="top" width="46.75467546754676%" id="mcps1.2.4.1.1"><p id="p073027114816"><a name="p073027114816"></a><a name="p073027114816"></a>Environment Variable</p>
|
||||
</th>
|
||||
<th class="cellrowborder" valign="top" width="43.18431843184319%" id="mcps1.2.4.1.2"><p id="p157362712480"><a name="p157362712480"></a><a name="p157362712480"></a>Description</p>
|
||||
</th>
|
||||
<th class="cellrowborder" valign="top" width="10.061006100610062%" id="mcps1.2.4.1.3"><p id="p187392714485"><a name="p187392714485"></a><a name="p187392714485"></a>Required/Optional</p>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody><tr id="row2732273481"><td class="cellrowborder" valign="top" width="46.75467546754676%" headers="mcps1.2.4.1.1 "><p id="p57382719481"><a name="p57382719481"></a><a name="p57382719481"></a>LD_LIBRARY_PATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="43.18431843184319%" headers="mcps1.2.4.1.2 "><p id="p07319271480"><a name="p07319271480"></a><a name="p07319271480"></a>Dynamic library search path. Set this variable based on the preceding example.</p>
|
||||
<div class="note" id="note119943144912"><a name="note119943144912"></a><a name="note119943144912"></a><span class="notetitle"> NOTE: </span><div class="notebody"><p id="p49933104915"><a name="p49933104915"></a><a name="p49933104915"></a>If GCC 7.3.0 is installed in OSs such as CentOS 7.6, Debian, and BCLinux, configure the related environment variable. For details, see <a href="#how-do-i-install-gcc-7-3-0.md#en-us_topic_0000001146754749_en-us_topic_0000001072593337_l5e5c9395407d46439788b12f0e6342bc">5</a>.</p>
|
||||
</div></div>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="10.061006100610062%" headers="mcps1.2.4.1.3 "><p id="p187352744817"><a name="p187352744817"></a><a name="p187352744817"></a>Required</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="row17342744812"><td class="cellrowborder" valign="top" width="46.75467546754676%" headers="mcps1.2.4.1.1 "><p id="p4731227184818"><a name="p4731227184818"></a><a name="p4731227184818"></a>PATH</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="43.18431843184319%" headers="mcps1.2.4.1.2 "><p id="p97310276484"><a name="p97310276484"></a><a name="p97310276484"></a>Executable program search path. Set this variable based on the preceding example.</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="10.061006100610062%" headers="mcps1.2.4.1.3 "><p id="p1573142711483"><a name="p1573142711483"></a><a name="p1573142711483"></a>Required</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="row95241840165613"><td class="cellrowborder" valign="top" width="46.75467546754676%" headers="mcps1.2.4.1.1 "><p id="p15242404566"><a name="p15242404566"></a><a name="p15242404566"></a>ASCEND_DEVICE_ID</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="43.18431843184319%" headers="mcps1.2.4.1.2 "><p id="p688447526"><a name="p688447526"></a><a name="p688447526"></a>Sets the logical ID of a processor.</p>
|
||||
<p id="p178824712212"><a name="p178824712212"></a><a name="p178824712212"></a>The value range is [0, <em id="i18812110304"><a name="i18812110304"></a><a name="i18812110304"></a>N</em> – 1] and the default value is<strong id="b1381717104019"><a name="b1381717104019"></a><a name="b1381717104019"></a> 0</strong>. <em id="i1156411211703"><a name="i1156411211703"></a><a name="i1156411211703"></a>N</em> indicates the device count in the physical machine, VM, or container.</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="10.061006100610062%" headers="mcps1.2.4.1.3 "><p id="p105245400562"><a name="p105245400562"></a><a name="p105245400562"></a>Optional</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="row203705529819"><td class="cellrowborder" valign="top" width="46.75467546754676%" headers="mcps1.2.4.1.1 "><p id="p53705522810"><a name="p53705522810"></a><a name="p53705522810"></a>ASCEND_SLOG_PRINT_TO_STDOUT</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="43.18431843184319%" headers="mcps1.2.4.1.2 "><p id="en-us_topic_0000001073203258_p763102116322"><a name="en-us_topic_0000001073203258_p763102116322"></a><a name="en-us_topic_0000001073203258_p763102116322"></a>Enables or disables log printing. Values:</p>
|
||||
<a name="en-us_topic_0000001073203258_ul95291245104113"></a><a name="en-us_topic_0000001073203258_ul95291245104113"></a><ul id="en-us_topic_0000001073203258_ul95291245104113"><li><strong id="b1877191971104041"><a name="b1877191971104041"></a><a name="b1877191971104041"></a>0</strong> or not configured: disabled.</li><li><strong id="b737654378104041"><a name="b737654378104041"></a><a name="b737654378104041"></a>1</strong>: enabled.</li></ul>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="10.061006100610062%" headers="mcps1.2.4.1.3 "><p id="p6370125217818"><a name="p6370125217818"></a><a name="p6370125217818"></a>Optional</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="row188405280914"><td class="cellrowborder" valign="top" width="46.75467546754676%" headers="mcps1.2.4.1.1 "><p id="p5840928594"><a name="p5840928594"></a><a name="p5840928594"></a>ASCEND_GLOBAL_LOG_LEVEL</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="43.18431843184319%" headers="mcps1.2.4.1.2 "><p id="en-us_topic_0000001073521184_p763102116322"><a name="en-us_topic_0000001073521184_p763102116322"></a><a name="en-us_topic_0000001073521184_p763102116322"></a>Sets the global log level. Values:</p>
|
||||
<a name="en-us_topic_0000001073521184_u4d735da6e0494e55abbf5eb6aefe988d"></a><a name="en-us_topic_0000001073521184_u4d735da6e0494e55abbf5eb6aefe988d"></a><ul id="en-us_topic_0000001073521184_u4d735da6e0494e55abbf5eb6aefe988d"><li><strong id="b1731786453104041"><a name="b1731786453104041"></a><a name="b1731786453104041"></a>0</strong>: DEBUG</li><li><strong id="b758139838104041"><a name="b758139838104041"></a><a name="b758139838104041"></a>1</strong>: INFO</li><li><strong id="b539270545104041"><a name="b539270545104041"></a><a name="b539270545104041"></a>2</strong>: WARNING</li><li><strong id="b444623067104041"><a name="b444623067104041"></a><a name="b444623067104041"></a>3</strong>: ERROR</li><li><strong id="b1157662181104041"><a name="b1157662181104041"></a><a name="b1157662181104041"></a>4</strong>: NULL (no log output)</li><li>Other values: invalid</li></ul>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="10.061006100610062%" headers="mcps1.2.4.1.3 "><p id="p6841928598"><a name="p6841928598"></a><a name="p6841928598"></a>Optional</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="row17638114219104"><td class="cellrowborder" valign="top" width="46.75467546754676%" headers="mcps1.2.4.1.1 "><p id="p20638342191018"><a name="p20638342191018"></a><a name="p20638342191018"></a>TASK_QUEUE_ENABLE</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="43.18431843184319%" headers="mcps1.2.4.1.2 "><p id="p126381842151019"><a name="p126381842151019"></a><a name="p126381842151019"></a>Enables or disables the task delivery in multi-thread mode. When this function is enabled, the training performance of the entire network is improved in most cases. Values:</p>
|
||||
<a name="ul6194152711118"></a><a name="ul6194152711118"></a><ul id="ul6194152711118"><li><strong id="b166811333191713"><a name="b166811333191713"></a><a name="b166811333191713"></a>0</strong> or not configured: disabled</li><li><strong id="b3865153321914"><a name="b3865153321914"></a><a name="b3865153321914"></a>1</strong>: enabled</li></ul>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="10.061006100610062%" headers="mcps1.2.4.1.3 "><p id="p86386423103"><a name="p86386423103"></a><a name="p86386423103"></a>Optional</p>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
> **NOTE:**
|
||||
>For more log information, see the _CANN Log Reference_.
|
||||
|
||||
<h2 id="sample-reference">Sample Reference</h2>
|
||||
|
||||
## Sample Code<a name="section2017032418274"></a>
|
||||
|
||||
Try to minimize the initialization frequency across the app lifetime during inference. The inference mode is set using the **model.eval\(\)** method, and the inference process must run under the code branch **with torch.no\_grad\(\):**.
|
||||
|
||||
The following uses Python code of the ResNet-50 network as an example for description.
|
||||
|
||||
**resnet50\_infer\_for\_pytorch.py** sample code:
|
||||
|
||||
```
|
||||
import argparse
|
||||
import os
|
||||
import time
|
||||
import torch
|
||||
import torch.nn.parallel
|
||||
import torch.optim
|
||||
import torch.utils.data
|
||||
import torch.utils.data.distributed
|
||||
import torchvision.transforms as transforms
|
||||
import torchvision.datasets as datasets
|
||||
import torchvision.models as models
|
||||
|
||||
import torch.npu
|
||||
from apex import amp
|
||||
|
||||
model_names = sorted(name for name in models.__dict__
|
||||
if name.islower() and not name.startswith("__")
|
||||
and callable(models.__dict__[name]))
|
||||
|
||||
|
||||
def parse_args():
|
||||
""" User-defined dataset path and model path """
|
||||
parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
|
||||
parser.add_argument('--data', metavar='DIR', default="/data/imagenet",
|
||||
help='path to dataset')
|
||||
parser.add_argument('-a', '--arch', metavar='ARCH', default='resnet50',
|
||||
choices=model_names,
|
||||
help='model architecture: ' +
|
||||
' | '.join(model_names) +
|
||||
' (default: resnet18)')
|
||||
|
||||
parser.add_argument('--epochs', default=100, type=int, metavar='N',
|
||||
help='number of total epochs to run')
|
||||
|
||||
parser.add_argument('-b', '--batch_size', default=512, type=int,
|
||||
metavar='N',
|
||||
help='mini-batch size (default: 256), this is the total '
|
||||
'batch size of all GPUs on the current node when '
|
||||
'using Data Parallel or Distributed Data Parallel')
|
||||
|
||||
parser.add_argument('--resume', default='', type=str, metavar='PATH',
|
||||
help='path to latest checkpoint (default: none)')
|
||||
|
||||
parser.add_argument('--pretrained', dest='pretrained', action='store_true',
|
||||
help='use pre-trained model')
|
||||
|
||||
parser.add_argument('--npu', default=None, type=int,
|
||||
help='NPU id to use.')
|
||||
|
||||
parser.add_argument('-j', '--workers', default=32, type=int, metavar='N',
|
||||
help='number of data loading workers (default: 8)')
|
||||
|
||||
parser.add_argument('--lr', '--learning_rate', default=0.1, type=float,
|
||||
metavar='LR', help='initial learning rate', dest='lr')
|
||||
parser.add_argument('--wd', '--weight_decay', default=1e-4, type=float,
|
||||
metavar='W', help='weight decay (default: 1e-4)',
|
||||
dest='weight_decay')
|
||||
|
||||
args, unknown_args = parser.parse_known_args()
|
||||
if len(unknown_args) > 0:
|
||||
for bad_arg in unknown_args:
|
||||
print("ERROR: Unknown command line arg: %s" % bad_arg)
|
||||
raise ValueError("Invalid command line arg(s)")
|
||||
|
||||
return args
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# Main function entry
|
||||
# =========================================================================
|
||||
def main():
|
||||
args = parse_args()
|
||||
if args.npu is None:
|
||||
args.npu = 0
|
||||
global CALCULATE_DEVICE
|
||||
CALCULATE_DEVICE = "npu:{}".format(args.npu)
|
||||
torch.npu.set_device(CALCULATE_DEVICE)
|
||||
print("use ", CALCULATE_DEVICE)
|
||||
main_worker(args.npu, args)
|
||||
|
||||
|
||||
def main_worker(npu, args):
|
||||
global best_acc1
|
||||
args.npu = npu
|
||||
|
||||
# =========================================================================
|
||||
# Create a model.
|
||||
# =========================================================================
|
||||
print("=> creating model '{}'".format(args.arch))
|
||||
model = models.__dict__[args.arch](zero_init_residual=True)
|
||||
|
||||
# Copy the model data to the Ascend AI Processor.
|
||||
model = model.to(CALCULATE_DEVICE)
|
||||
|
||||
optimizer = torch.optim.SGD([
|
||||
{'params': [param for name, param in model.named_parameters() if name[-4:] == 'bias'], 'weight_decay': 0.0},
|
||||
{'params': [param for name, param in model.named_parameters() if name[-4:] != 'bias'],
|
||||
'weight_decay': args.weight_decay}],
|
||||
args.lr)
|
||||
|
||||
# =========================================================================
|
||||
# Initialize the mixed precision model. After the model is used, the calculation can be accelerated, but the accuracy may slightly decrease. Determine whether to use the model based on the actual scenario.
|
||||
# =========================================================================
|
||||
model, optimizer = amp.initialize(model, optimizer, opt_level="O2", loss_scale=1024, verbosity=1)
|
||||
|
||||
# =========================================================================
|
||||
# Load the trained model parameters.
|
||||
# =========================================================================
|
||||
# Restore model parameters from the model file.
|
||||
if os.path.isfile(args.resume):
|
||||
print("=> loading checkpoint '{}'".format(args.resume))
|
||||
checkpoint = torch.load(args.resume)
|
||||
|
||||
best_acc1 = checkpoint['best_acc1']
|
||||
best_acc1 = best_acc1.to("npu:{}".format(args.npu))
|
||||
|
||||
model.load_state_dict(checkpoint['state_dict'])
|
||||
print("=> loaded checkpoint '{}' (epoch {})".format(args.resume, checkpoint['epoch']))
|
||||
|
||||
else:
|
||||
print("=> no checkpoint found at '{}'".format(args.resume))
|
||||
|
||||
# =========================================================================
|
||||
# Initialize the database.
|
||||
# =========================================================================
|
||||
# Load and preprocess image data.
|
||||
valdir = os.path.join(args.data, 'val')
|
||||
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
||||
std=[0.229, 0.224, 0.225])
|
||||
|
||||
val_loader = torch.utils.data.DataLoader(
|
||||
datasets.ImageFolder(valdir, transforms.Compose([
|
||||
transforms.Resize(256),
|
||||
transforms.CenterCrop(224),
|
||||
transforms.ToTensor(),
|
||||
normalize,
|
||||
])),
|
||||
batch_size=args.batch_size, shuffle=True,
|
||||
num_workers=args.workers, pin_memory=True)
|
||||
|
||||
# =========================================================================
|
||||
# Run the validate command.
|
||||
# =========================================================================
|
||||
validate(val_loader, model, args)
|
||||
|
||||
|
||||
# =========================================================================
|
||||
# Implement the sample API for online inference.
|
||||
# =========================================================================
|
||||
def validate(val_loader, model, args):
|
||||
batch_time = AverageMeter('Time', ':6.3f')
|
||||
top1 = AverageMeter('Acc@1', ':6.2f')
|
||||
top5 = AverageMeter('Acc@5', ':6.2f')
|
||||
progress = ProgressMeter(
|
||||
len(val_loader),
|
||||
[batch_time, top1, top5],
|
||||
prefix='Test: ')
|
||||
|
||||
# =========================================================================
|
||||
# Switch to the inference mode.
|
||||
# =========================================================================
|
||||
model.eval()
|
||||
|
||||
# =========================================================================
|
||||
# Execute the model forward propagation under the torch.no_grad(): branch.
|
||||
# =========================================================================
|
||||
with torch.no_grad():
|
||||
end = time.time()
|
||||
for i, (images, target) in enumerate(val_loader):
|
||||
|
||||
# Place the image data on the NPU.
|
||||
images = images.to(CALCULATE_DEVICE, non_blocking=True)
|
||||
target = target.to(torch.int32).to(CALCULATE_DEVICE, non_blocking=True)
|
||||
|
||||
# Calculate the output.
|
||||
output = model(images)
|
||||
|
||||
# Collect statistics on result precisions.
|
||||
acc1, acc5 = accuracy(output, target, topk=(1, 5))
|
||||
top1.update(acc1[0], images.size(0))
|
||||
top5.update(acc5[0], images.size(0))
|
||||
|
||||
# Measure the running time.
|
||||
batch_time.update(time.time() - end)
|
||||
end = time.time()
|
||||
|
||||
# Print inference logs.
|
||||
progress.display(i)
|
||||
|
||||
print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'.format(top1=top1, top5=top5))
|
||||
|
||||
return top1.avg
|
||||
|
||||
|
||||
class AverageMeter(object):
|
||||
"""Calculate and store the average value and current value."""
|
||||
def __init__(self, name, fmt=':f'):
|
||||
self.name = name
|
||||
self.fmt = fmt
|
||||
self.reset()
|
||||
self.start_count_index = 10
|
||||
|
||||
def reset(self):
|
||||
self.val = 0
|
||||
self.avg = 0
|
||||
self.sum = 0
|
||||
self.count = 0
|
||||
|
||||
def update(self, val, n=1):
|
||||
if self.count == 0:
|
||||
self.batchsize = n
|
||||
|
||||
self.val = val
|
||||
self.count += n
|
||||
if self.count > (self.start_count_index * self.batchsize):
|
||||
self.sum += val * n
|
||||
self.avg = self.sum / (self.count - self.start_count_index * self.batchsize)
|
||||
|
||||
def __str__(self):
|
||||
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
|
||||
return fmtstr.format(**self.__dict__)
|
||||
|
||||
|
||||
class ProgressMeter(object):
|
||||
"""Record model computing information."""
|
||||
def __init__(self, num_batches, meters, prefix=""):
|
||||
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
|
||||
self.meters = meters
|
||||
self.prefix = prefix
|
||||
|
||||
def display(self, batch):
|
||||
entries = [self.prefix + self.batch_fmtstr.format(batch)]
|
||||
entries += [str(meter) for meter in self.meters]
|
||||
print('\t'.join(entries))
|
||||
|
||||
def _get_batch_fmtstr(self, num_batches):
|
||||
num_digits = len(str(num_batches // 1))
|
||||
fmt = '{:' + str(num_digits) + 'd}'
|
||||
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
|
||||
|
||||
|
||||
def accuracy(output, target, topk=(1,)):
|
||||
"""Calculate the precision of k top predictions based on the specified value k."""
|
||||
with torch.no_grad():
|
||||
maxk = max(topk)
|
||||
batch_size = target.size(0)
|
||||
|
||||
_, pred = output.topk(maxk, 1, True, True)
|
||||
pred = pred.t()
|
||||
correct = pred.eq(target.view(1, -1).expand_as(pred))
|
||||
|
||||
res = []
|
||||
for k in topk:
|
||||
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
|
||||
res.append(correct_k.mul_(100.0 / batch_size))
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
```
|
||||
|
||||
## Sample Running<a name="section340764716270"></a>
|
||||
|
||||
The following uses the ResNet-50 model as an example to describe how to perform online inference.
|
||||
|
||||
1. Download a pre-trained model.
|
||||
|
||||
Visit [Ascend ModelZoo](https://www.hiascend.com/en/software/modelzoo) and click **Download Model** to download a pre-trained ResNet-50 model.
|
||||
|
||||
2. Edit the inference script.
|
||||
|
||||
Create a model script file **resnet50\_infer\_for\_pytorch.py** and write code by referring to [Sample Code]().
|
||||
|
||||
3. Run inference.
|
||||
|
||||
Set environment variables by referring to [Environment Variable Configuration](#environment-variable-configuration) and then run the following command:
|
||||
|
||||
```
|
||||
python3 pytorch-resnet50-apex.py --data /data/imagenet \
|
||||
--npu 7 \
|
||||
--epochs 90 \
|
||||
--resume ./checkpoint.pth.tar # ./checkpoint.pth.tar is the path of the sample pre-trained model file.
|
||||
```
|
||||
|
||||
> **NOTE:**
|
||||
>The preceding command is an example only. Modify the arguments as needed.
|
||||
|
||||
|
||||
<h2 id="special-topics">Special Topics</h2>
|
||||
|
||||
- **[Mixed Precision](#mixed-precision)**
|
||||
|
||||
|
||||
<h2 id="mixed-precision">Mixed Precision</h2>
|
||||
|
||||
## Overview<a name="section14734122962615"></a>
|
||||
|
||||
Based on the architecture features of the NPU, mixed precision is involved in the model computing, that is, the scenario where the float16 and float32 data types are used together. Replacing float32 with float16 has the following advantages:
|
||||
|
||||
- The memory usage of intermediate variables is reduced.
|
||||
- The data transfer time decreases because the memory usage is reduced.
|
||||
- The compute units of float16 provide better computing performance.
|
||||
|
||||
However, the mixed precision training is limited by the precision range expressed by float16. If float32 is converted into float16, the training convergence is affected. To use float16 for acceleration in some computations and ensure training convergence, the mixed precision module Apex is used. The mixed precision module Apex is a comprehensive optimization library that features high optimization performance and precision.
|
||||
|
||||
## Supported Features<a name="section175201458521"></a>
|
||||
|
||||
[Table 1](#en-us_topic_0278765773_table10717173813332) describes the functions and optimization of the mixed precision module.
|
||||
|
||||
**Table 1** Functions of the mixed precision module
|
||||
|
||||
<a name="en-us_topic_0278765773_table10717173813332"></a>
|
||||
<table><thead align="left"><tr id="en-us_topic_0278765773_row371716385333"><th class="cellrowborder" valign="top" width="32.269999999999996%" id="mcps1.2.3.1.1"><p id="en-us_topic_0278765773_p13717163815333"><a name="en-us_topic_0278765773_p13717163815333"></a><a name="en-us_topic_0278765773_p13717163815333"></a>Function</p>
|
||||
</th>
|
||||
<th class="cellrowborder" valign="top" width="67.73%" id="mcps1.2.3.1.2"><p id="en-us_topic_0278765773_p14400173910345"><a name="en-us_topic_0278765773_p14400173910345"></a><a name="en-us_topic_0278765773_p14400173910345"></a>Description</p>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody><tr id="en-us_topic_0278765773_row1571763813334"><td class="cellrowborder" valign="top" width="32.269999999999996%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0278765773_p4502732153412"><a name="en-us_topic_0278765773_p4502732153412"></a><a name="en-us_topic_0278765773_p4502732153412"></a>O1 configuration</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="67.73%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0278765773_p640053920348"><a name="en-us_topic_0278765773_p640053920348"></a><a name="en-us_topic_0278765773_p640053920348"></a>Conv and Matmul use float16 for computing, and Softmax and BN use float32.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0278765773_row3717173817336"><td class="cellrowborder" valign="top" width="32.269999999999996%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0278765773_p11503103210344"><a name="en-us_topic_0278765773_p11503103210344"></a><a name="en-us_topic_0278765773_p11503103210344"></a>O2 configuration</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="67.73%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0278765773_p164001639143419"><a name="en-us_topic_0278765773_p164001639143419"></a><a name="en-us_topic_0278765773_p164001639143419"></a>BN uses float32, and others use float16.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0278765773_row14717193815334"><td class="cellrowborder" valign="top" width="32.269999999999996%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0278765773_p1950318328349"><a name="en-us_topic_0278765773_p1950318328349"></a><a name="en-us_topic_0278765773_p1950318328349"></a>Static loss scale</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="67.73%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0278765773_p1440033983418"><a name="en-us_topic_0278765773_p1440033983418"></a><a name="en-us_topic_0278765773_p1440033983418"></a>Parameters are statically set to ensure the convergence of mixed precision training.</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="en-us_topic_0278765773_row871733813317"><td class="cellrowborder" valign="top" width="32.269999999999996%" headers="mcps1.2.3.1.1 "><p id="en-us_topic_0278765773_p1550303243417"><a name="en-us_topic_0278765773_p1550303243417"></a><a name="en-us_topic_0278765773_p1550303243417"></a>Dynamic loss scale</p>
|
||||
</td>
|
||||
<td class="cellrowborder" valign="top" width="67.73%" headers="mcps1.2.3.1.2 "><p id="en-us_topic_0278765773_p15400143963412"><a name="en-us_topic_0278765773_p15400143963412"></a><a name="en-us_topic_0278765773_p15400143963412"></a>The loss scale value is dynamically calculated to determine whether overflow occurs.</p>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
> **NOTE:**
|
||||
>In the current version, Apex is implemented using Python and does not support AscendCL or CUDA optimization.
|
||||
|
||||
## Initializing the Mixed Precision Model<a name="section18178125518268"></a>
|
||||
|
||||
1. To use the mixed precision module Apex, you need to import the amp module from the Apex library as follows:
|
||||
|
||||
```
|
||||
from apex import amp
|
||||
```
|
||||
|
||||
2. After the amp module is imported, you need to initialize it so that it can modify the model, optimizer, and PyTorch internal functions. The initialization code is as follows:
|
||||
|
||||
```
|
||||
model, optimizer = amp.initialize(model, optimizer)
|
||||
```
|
||||
|
||||
For details, see "Initialize the mixed precision model."# in [Sample Code](#sample-reference).
|
||||
|
||||
```
|
||||
model, optimizer = amp.initialize(model, optimizer, opt_level="O2", loss_scale=1024, verbosity=1)
|
||||
```
|
||||
|
||||
|
||||
## Mixed Precision Inference<a name="section818071513288"></a>
|
||||
|
||||
After the mixed precision model is initialized, perform model forward propagation.
|
||||
|
||||
Sample code: For details, see the implementation of **validate\(val\_loader, model, args\)** in [Sample Code](#sample-reference).
|
||||
|
||||
<h2 id="how-do-i-install-gcc-7-3-0">How Do I Install GCC 7.3.0?</h2>
|
||||
|
||||
Perform the following steps as the **root** user.
|
||||
|
||||
1. 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](https://mirrors.tuna.tsinghua.edu.cn/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz).
|
||||
2. To install GCC, you need to reserve adequate temporary space. Run the following command to clear the **/tmp** directory before the installation:
|
||||
|
||||
```
|
||||
sudo rm -rf /tmp/*
|
||||
```
|
||||
|
||||
3. 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
|
||||
```
|
||||
|
||||
4. Build and install GCC.
|
||||
1. Go to the directory where the source code package **gcc-7.3.0.tar.gz** is located and run the following command to extract it:
|
||||
|
||||
```
|
||||
tar -zxvf gcc-7.3.0.tar.gz
|
||||
```
|
||||
|
||||
2. Go to the extracted directory and run the following command to download the GCC dependency packages:
|
||||
|
||||
```
|
||||
cd gcc-7.3.0
|
||||
./contrib/download_prerequisites
|
||||
```
|
||||
|
||||
If 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.bz2
|
||||
```
|
||||
|
||||
After downloading the preceding dependency packages, run the following command:
|
||||
|
||||
```
|
||||
./contrib/download_prerequisites
|
||||
```
|
||||
|
||||
If the verification fails, check whether the dependency package is repeatedly downloaded. The package should be downloaded at a time.
|
||||
|
||||
3. <a name="en-us_topic_0000001146754749_en-us_topic_0000001072593337_l75d31a2874534a2092e80a5f865b46f0"></a>Run the following commands for configuration, build, and installation.
|
||||
|
||||
```
|
||||
./configure --enable-languages=c,c++ --disable-multilib --with-system-zlib --prefix=/usr/local/linux_gcc7.3.0
|
||||
make -j15 # The value 15 indicates the number of CPUs, which is configurable and can be queried by running grep -w processor /proc/cpuinfo|wc -l.
|
||||
make install
|
||||
```
|
||||
|
||||
> **NOTICE:**
|
||||
>The **--prefix** parameter 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**.
|
||||
|
||||
|
||||
5. Set the environment variable.
|
||||
|
||||
The build environment after GCC upgrade is required for training. Therefore, you need to configure the following environment variable in the 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.](#en-us_topic_0000001146754749_en-us_topic_0000001072593337_l75d31a2874534a2092e80a5f865b46f0). In this example, the GCC 7.3.0 installation path is **/usr/local/linux\_gcc7.3.0/**.
|
||||
|
||||
> **NOTE:**
|
||||
>The environment variable needs to be configured only when you need to use the build environment after the GCC upgrade.
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 394 B |
|
After Width: | Height: | Size: 406 B |
|
After Width: | Height: | Size: 253 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 43 KiB |
|
After Width: | Height: | Size: 7.6 KiB |
|
After Width: | Height: | Size: 50 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 31 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 394 B |
|
After Width: | Height: | Size: 406 B |
|
After Width: | Height: | Size: 253 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 580 B |
|
After Width: | Height: | Size: 394 B |
|
After Width: | Height: | Size: 406 B |
|
After Width: | Height: | Size: 253 B |
|
After Width: | Height: | Size: 580 B |