安装PyTorch
安装PyTorch环境依赖
pip3 install wheel pyyaml typing_extensions setuptools
PyTorch版本 |
Python版本 |
---|---|
PyTorch1.11.0 |
Python3.7.x(3.7.5及以上)、Python3.8.x、Python3.9.x、Python3.10.x |
PyTorch2.1.0 |
Python3.8.x、Python3.9.x、Python3.10.x |
PyTorch2.2.0 |
Python3.8.x、Python3.9.x、Python3.10.x |
安装PyTorch
用户应根据所在组织的安全规定,选择符合要求的版本,或者bugfix、补丁版本、升级版本等进行安装(使用以下操作步骤中提供的安装包或官方发布包,或采用自行源码编译的方式进行安装)。
此安装方式为二进制包安装。若用户采用源码编译,请参见源码安装PyTorch安装PyTorch。
- 安装torch包。
- PyTorch 1.11.0
- Python3.7.x(3.7.5及以上)
wget https://download.pytorch.org/whl/torch-1.11.0-cp37-cp37m-manylinux2014_aarch64.whl pip3 install torch-1.11.0-cp37-cp37m-manylinux2014_aarch64.whl
- Python3.8.x
wget https://download.pytorch.org/whl/torch-1.11.0-cp38-cp38-manylinux2014_aarch64.whl pip3 install torch-1.11.0-cp38-cp38-manylinux2014_aarch64.whl
- Python3.9.x
wget https://download.pytorch.org/whl/torch-1.11.0-cp39-cp39-manylinux2014_aarch64.whl pip3 install torch-1.11.0-cp39-cp39-manylinux2014_aarch64.whl
- Python3.10.x
wget https://download.pytorch.org/whl/torch-1.11.0-cp310-cp310-manylinux2014_aarch64.whl pip3 install torch-1.11.0-cp310-cp310-manylinux2014_aarch64.whl
- Python3.7.x(3.7.5及以上)
- PyTorch 2.1.0
- Python3.8.x
wget https://download.pytorch.org/whl/cpu/torch-2.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch-2.1.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.9.x
wget https://download.pytorch.org/whl/cpu/torch-2.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch-2.1.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.10.x
wget https://download.pytorch.org/whl/cpu/torch-2.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch-2.1.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.8.x
- PyTorch 2.2.0
- Python3.8.x
wget https://download.pytorch.org/whl/cpu/torch-2.2.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch-2.2.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.9.x
wget https://download.pytorch.org/whl/cpu/torch-2.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch-2.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.10.x
wget https://download.pytorch.org/whl/cpu/torch-2.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch-2.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.8.x
- PyTorch 1.11.0
- 安装PyTorch插件torch_npu。
- PyTorch 1.11.0
- Python3.7.x(3.7.5及以上)
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch1.11.0/torch_npu-1.11.0.post11-cp37-cp37m-linux_aarch64.whl pip3 install torch_npu-1.11.0.post11-cp37-cp37m-linux_aarch64.whl
- Python3.8.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch1.11.0/torch_npu-1.11.0.post11-cp38-cp38-linux_aarch64.whl pip3 install torch_npu-1.11.0.post11-cp38-cp38-linux_aarch64.whl
- Python3.9.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch1.11.0/torch_npu-1.11.0.post11-cp39-cp39-linux_aarch64.whl pip3 install torch_npu-1.11.0.post11-cp39-cp39-linux_aarch64.whl
- Python3.10.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch1.11.0/torch_npu-1.11.0.post11-cp310-cp310-linux_aarch64.whl pip3 install torch_npu-1.11.0.post11-cp310-cp310-linux_aarch64.whl
- Python3.7.x(3.7.5及以上)
- PyTorch 2.1.0
- Python3.8.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch2.1.0/torch_npu-2.1.0.post3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch_npu-2.1.0.post3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.9.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch2.1.0/torch_npu-2.1.0.post3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch_npu-2.1.0.post3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.10.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch2.1.0/torch_npu-2.1.0.post3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch_npu-2.1.0.post3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.8.x
- PyTorch 2.2.0
- Python3.8.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch2.2.0/torch_npu-2.2.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch_npu-2.2.0-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.9.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch2.2.0/torch_npu-2.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch_npu-2.2.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.10.x
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch2.2.0/torch_npu-2.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl pip3 install torch_npu-2.2.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Python3.8.x
如果使用wget命令下载whl包时出现ERROR: cannot verify gitee.com's certificate报错,可在下载命令后加上--no-check-certificate参数避免此问题。
命令示例如下。
wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc1-pytorch1.11.0/torch_npu-1.11.0.post11-cp37-cp37m-linux_aarch64.whl --no-check-certificate
- PyTorch 1.11.0
- 执行如下命令,若返回True则说明安装成功。
python3 -c "import torch;import torch_npu;print(torch_npu.npu.is_available())"
若PyTorch版本为2.1.0,出现“找不到google或者protobuf或者protobuf版本过高”报错时,需执行如下命令:
pip3 install protobuf==3.20
- 安装对应框架版本的torchvision,PyTorch 1.11.0需安装0.12.0版本,PyTorch 2.1.0版本需安装0.16.0版本,PyTorch 2.2.0版本需安装0.17.0版本。
pip3 install torchvision==0.12.0
安装APEX混合精度模块
混合精度训练是在训练时混合使用单精度(float32)与半精度(float16)数据类型,将两者结合在一起,并使用相同的超参数实现了与float32几乎相同的精度。用户需要开启混合精度,可以提升模型的性能。APEX混合精度模块是一个集优化性能、精度收敛于一身的综合优化库,可以提供不同场景下的混合精度训练支持。
编译源码包安装APEX模块步骤如下。
- 执行如下命令安装git工具和相关依赖。以CentOS与Ubuntu操作系统为例。
- Ubuntu
apt-get install -y patch build-essential libbz2-dev libreadline-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev liblzma-dev m4 dos2unix libopenblas-dev git
- CentOS
yum install -y patch libjpeg-turbo-devel dos2unix openblas git
若出现“ModuleNotFoundError: No module named 'dnf'”报错信息,请参见No module named 'dnf'解决。
- Ubuntu
- 请确保setuptools版本小于等于65.7.0,使用pip3 show setuptools命令查询版本,若版本不符合条件,可使用以下命令安装。
pip3 install setuptools==65.7.0
- 获取昇腾适配的APEX源码。
git clone -b master https://gitee.com/ascend/apex.git
如果返回类似以下报错信息:
fatal: unable to access 'https://gitee.com/ascend/apex.git/': SSL certificate problem: self signed certificate in certificate chain
则执行如下命令关闭证书检验后,再执行上述命令获取源码。
git config --global http.sslVerify "false"
- 进入昇腾适配的APEX源码目录,执行命令编译生成二进制安装包(支持Python3.7-3.10,以Python3.7为例)。
cd apex bash scripts/build.sh --python=3.7
- 请确保NPU版本的PyTorch可以正常使用,否则会影响APEX的编译。
- 过程中会自动拉取apex官方源码,请保证服务器连接网络,生成的二进制包在“apex/dist”目录下。
- 执行如下命令安装。
cd apex/dist pip3 install apex-0.1_ascend-*.whl