参与到 Ascend/pytorch项目里,首先就是如何安装。
前提条件
安装配套版本的CANN软件,并使用其脚本
1
| source . {xxx}/ascend-toolkit/set_env.sh
|
安装PyTorch框架
已有conda环境t00906153_bindCore
中包含torch 2.1.0
编译安装
如果需要调试torch_npu,需要删除torch相关的验证代码,所以需要手动编译torch
这是torch会校验DEBUG情况,但是PTA的值为2。 为此需要修改torch包,删除gen_variable_type.py
的对应行
1 2
| if ( xxx not in DONT_ENFORCE_STORAGE_IMPL_xxx): stmts_after_call += []
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git clone -b v2.1.0 https://github.com/pytorch/pytorch pt210 cd pt210 git submodule sync
git submodule update --init --recursive
conda activate XXX
export CC=gcc export CXX=g++ export USE_XNNPACK=0 export _GLIBCXX_USE_CXX11_ABI=0 // 与PTA的保持对齐 DEBUG=1 python setup.py bdist_wheel // 生成whl DEBUG=1 python setup.py install // 直接安装
|
通过源码安装torch_npu插件
参考文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| yum install -y patch libjpeg-turbo-devel dos2unix openblas git
git clone -b v2.1.0-6.0.rc1 https://gitee.com/ascend/pytorch.git cd pytorch
conda activate XXX pip3 install pyyaml -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com pip3 install setuptools -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com
pip3 install torch==2.1.0 -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com
pip3 install torch==2.1.0+cpu --index-url https://download.pytorch.org/whl/cpu -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host=mirrors.aliyun.com
source . {xxx}/ascend-toolkit/set_env.sh bash ci/build.sh --python=3.8
pip3 install --upgrade dist/torch_npu-2.1.0.post3-cp38-cp38-linux_aarch64.whl
|
- 运行
setup.py
前 export DEBUG=1
即可。
- DEBUG的PTA刚需DEBUG的pytorch包, 原因是前面的
use_count != 1
- 惯性思维:不严谨,想当然了,clone了master来运行
- 文档阅读不细致,就急于尝试:公司里有人写wiki是很难得的,如果有wiki可以参考,一定先看完,可以避免很多坑。
- 编译时缺头文件,如果不在项目代码里,就很有可能是相应的系统包没有安装。
torchrun
torchrun
是 PyTorch 中用于分布式训练的命令行工具,以下是参数的解析:
--nnodes=1
:指定节点数量,这里设置为 1 表示只使用一个节点。
--nproc_per_node=8
:每个节点上使用的进程数,这里设置为 8,通常对应于 GPU 数量。
--master-port 61888
:指定主节点的端口号,用于进程间通信。
结合这些参数,命令会在一个节点上启动 8 个进程进行训练,主节点通过指定的端口进行通信。
sudo netstat -tuln | grep -E ':(61888|61889|619[0-9]{2}|62000)'
参考文献