安装torch_npu插件
torch_npu插件有两种安装方式:
- 快速安装:通过wheel格式的二进制软件包直接安装。
- 源码编译安装:用户可以选择对应的分支自行编译torch_npu。编译安装适用于进行算子适配开发、CANN版本与PyTorch兼容适配场景下使用。
获取安装命令
PyTorch版本 |
torch_npu插件版本 |
Python版本 |
系统架构 |
CANN版本 |
安装方式 |
安装命令 |
---|---|---|---|---|---|---|
1.11.0 |
6.0.rc2 |
Python 3.7和Python 3.7m |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp37-cp37m-linux_aarch64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp37-cp37m-linux_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.7和Python 3.7m |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp37-cp37m-linux_x86_64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp37-cp37m-linux_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.8 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp38-cp38-linux_aarch64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp38-cp38-linux_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.8 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp38-cp38-linux_x86_64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp38-cp38-linux_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp39-cp39-linux_aarch64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp39-cp39-linux_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp39-cp39-linux_x86_64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp39-cp39-linux_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp310-cp310-linux_aarch64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp310-cp310-linux_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch1.11.0/torch_npu-1.11.0.post14-cp310-cp310-linux_x86_64.whl # 安装命令 pip3 install torch_npu-1.11.0.post14-cp310-cp310-linux_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
2.1.0 |
6.0.rc2 |
Python 3.8 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.1.0/torch_npu-2.1.0.post6-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.1.0.post6-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.8 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.1.0/torch_npu-2.1.0.post6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.1.0.post6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.1.0/torch_npu-2.1.0.post6-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.1.0.post6-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.1.0/torch_npu-2.1.0.post6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.1.0.post6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.1.0/torch_npu-2.1.0.post6-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.1.0.post6-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.1.0/torch_npu-2.1.0.post6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.1.0.post6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
2.2.0 |
6.0.rc2 |
Python 3.8 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.2.0/torch_npu-2.2.0.post2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.2.0.post2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.8 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.2.0/torch_npu-2.2.0.post2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.2.0.post2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.2.0/torch_npu-2.2.0.post2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.2.0.post2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.2.0/torch_npu-2.2.0.post2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.2.0.post2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.2.0/torch_npu-2.2.0.post2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.2.0.post2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.2.0/torch_npu-2.2.0.post2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.2.0.post2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
2.3.1 |
6.0.rc2 |
Python 3.8 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.3.1/torch_npu-2.3.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.3.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.8 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.3.1/torch_npu-2.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.3.1/torch_npu-2.3.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.3.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.9 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.3.1/torch_npu-2.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
AArch64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.3.1/torch_npu-2.3.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 安装命令 pip3 install torch_npu-2.3.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
|||||
6.0.rc2 |
Python 3.10 |
X86_64 |
8.0.RC2 |
Pip |
# 下载插件包 wget https://gitee.com/ascend/pytorch/releases/download/v6.0.rc2-pytorch2.3.1/torch_npu-2.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 安装命令 pip3 install torch_npu-2.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl # 根据“安装前必读”完成环境变量配置 |
|
Source |
# 参考下方“源码编译安装torch_npu插件” |
- 如果下载whl包时出现ERROR: cannot verify gitee.com's certificate报错,可在下载命令后加上--no-check-certificate参数避免此问题。样例代码如下所示。
wget https://gitee.com/ascend/pytorch/releases/download/插件包 --no-check-certificate
- 执行如下命令,若返回True则说明安装成功。
python3 -c "import torch;import torch_npu;print(torch_npu.npu.is_available())"
- 若Pytorch版本为2.1.0,出现“找不到google或者protobuf或者protobuf版本过高”报错时,需执行如下命令:
pip install protobuf==3.20
源码编译安装torch_npu插件
编译安装适用于进行算子适配开发、CANN版本与PyTorch兼容适配场景下。
以下操作步骤以安装PyTorch 2.1.0版本为例。
- 方式一(推荐):容器场景
- 下载torch_npu源码。
git clone https://gitee.com/ascend/pytorch.git -b v2.1.0-6.0.rc2 --depth 1
下载对应PyTorch版本分支代码,进入插件根目录,以v2.1.0-6.0.rc2为例,其他版本请参考《版本说明》中的“版本配套关系”章节下载对应PyTorch版本分支代码。
- 构建镜像。
cd pytorch/ci/docker/{arch} docker build -t manylinux-builder:v1 .
{arch}表示CPU架构(X86或ARM)。
- 进入Docker容器,并将torch_npu源代码挂载至容器内。
docker run -it -v /{code_path}/pytorch:/home/pytorch manylinux-builder:v1 bash
{code_path}表示torch_npu源代码路径,请根据实际情况进行替换。
- 编译生成二进制安装包。
cd /home/pytorch bash ci/build.sh --python=3.8
指定Python版本编包方式,以Python3.8为例,其他Python版本请使用 --python=3.9或--python3.10。
- 在运行环境中安装生成的插件torch_npu包,如果使用非root用户安装,需要在命令后加--user。
# 请用户根据实际情况更改命令中的torch_npu包名 pip3 install --upgrade dist/torch_npu-2.1.0.post6-cp38-cp38-linux_aarch64.whl
- 下载torch_npu源码。
- 方式二:物理机及虚拟机场景
-
选择编译安装方式安装时需要安装系统依赖。目前支持CentOS与Ubuntu操作系统。
- CentOS
yum install -y patch libjpeg-turbo-devel dos2unix openblas git yum install -y gcc==9.4.0 cmake==3.18.0
- 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 apt-get install -y gcc==9.4.0 cmake==3.18.0
gcc9.4.0版本及以上,安装2.x及以上版本PyTorch,则cmake需为3.18.0版本及以上;若安装1.11.0版本PyTorch,则cmake只需为3.12.0版本及以上。
- CentOS
- 编译生成torch_npu插件的二进制安装包。
- 下载对应PyTorch版本分支代码,进入插件根目录,以v2.1.0-6.0.rc2为例,其他版本请参考《版本说明》中的“版本配套关系”章节下载对应PyTorch版本分支代码。
git clone -b v2.1.0-6.0.rc2 https://gitee.com/ascend/pytorch.git cd pytorch
- (可选)如果不需要切换OpPlugin(CANN)的版本,请跳过此步骤;如需切换OpPlugin版本,如切换OpPlugin的master分支最新代码,则进行以下操作进行切换。
- 编译生成二进制安装包。
# 指定Python版本编包方式,以Python3.8为例,其他Python版本请使用 --python=3.9或--python3.10 bash ci/build.sh --python=3.8
- 下载对应PyTorch版本分支代码,进入插件根目录,以v2.1.0-6.0.rc2为例,其他版本请参考《版本说明》中的“版本配套关系”章节下载对应PyTorch版本分支代码。
- 安装pytorch/dist目录下生成的插件torch_npu包,如果使用非root用户安装,需要在命令后加--user。
# 请用户根据实际情况更改命令中的torch_npu包名 pip3 install --upgrade dist/torch_npu-2.1.0.post6-cp38-cp38-linux_aarch64.whl
-
验证是否成功安装
- 方法一
显示如下类似信息说明PyTorch框架与插件安装成功。
tensor([[-0.6066, 6.3385, 0.0379, 3.3356], [ 2.9243, 3.3134, -1.5465, 0.1916], [-2.1807, 0.2008, -1.1431, 2.1523]], device='npu:0')
- 方法二
1 2 3 4 5 6 7 8
import torch import torch_npu x = torch.randn(2, 2).npu() y = torch.randn(2, 2).npu() z = x.mm(y) print(z)
输出如下类似信息说明PyTorch框架与插件安装成功。
tensor([[-0.0515, 0.3664], [-0.1258, -0.5425]], device='npu:0')