MindSpore源码阅读系列(三):统一调试MindSpore Python与C++
MindSpore源码阅读系列(二):编译并调试MindSpore源码(对阅读源码很有帮助)
源码编译方式安装MindSpore Ascend 910版本
/usr/local/Ascend/nnae
高于 /usr/loacl/Ascend/ascend-toolkit
。原因是MindSpore采用DT_RPATH方式支持无环境变量启动,减少用户设置;DT_RPATH优先级比LD_LIBRARY_PATH环境变量高。昇腾软件包提供商用版和社区版两种下载途径:
6.0.RC1.alpha005
版本,以及在固件与驱动链接中获取对应的固件和驱动安装包,安装包的选择与安装方式请参照上述的商用版安装指引文档。详细安装步骤,请参考:install/mindspore_ascend_install_source.md · MindSpore/docs - Gitee.com
如果之前已经安装过昇腾AI处理器配套软件包,需要先卸载对应的whl包。
pip uninstall te topi hccl -y
重新安装昇腾AI处理器配套软件whl包
pip install sympy
pip install /usr/local/Ascend/ascend-toolkit/latest/lib64/topi-*-py3-none-any.whl
pip install /usr/local/Ascend/ascend-toolkit/latest/lib64/te-*-py3-none-any.whl
pip install /usr/local/Ascend/ascend-toolkit/latest/lib64/hccl-*-py3-none-any.whl
以MindSpore1.9版本为例。
git clone https://gitee.com/mindspore/mindspore.git -b r1.9
cd mindspore
# 编译Ascend910版本
bash build.sh -e ascend -S on
# 编译CPU版本
bash build.sh -e cpu -S on
time bash build.sh -d -t ut -S on
源码编译方式安装MindSpore CPU版本
源码编译方式安装MindSpore CPU版本(含第三方依赖)
# time bash build.sh -e cpu -S on
...
...
...
adding 'mindspore-1.9.1.dist-info/METADATA'
adding 'mindspore-1.9.1.dist-info/WHEEL'
adding 'mindspore-1.9.1.dist-info/entry_points.txt'
adding 'mindspore-1.9.1.dist-info/top_level.txt'
adding 'mindspore-1.9.1.dist-info/RECORD'
removing build/bdist.linux-aarch64/wheel
CPack: - package: /data/YOYOFile/mindspore/build/mindspore/mindspore generated.
success building mindspore project!
---------------- MindSpore: build end ----------------
real 109m37.807s
user 822m49.398s
sys 54m14.307s
输出
output/mindspore-1.9.1-cp39-cp39-linux_aarch64.whl
output/mindspore-1.9.1-cp39-cp39-linux_aarch64.whl.sha256
# time bash build.sh -e ascend -S on
...
...
...
adding 'mindspore_ascend-1.9.1.dist-info/METADATA'
adding 'mindspore_ascend-1.9.1.dist-info/WHEEL'
adding 'mindspore_ascend-1.9.1.dist-info/entry_points.txt'
adding 'mindspore_ascend-1.9.1.dist-info/top_level.txt'
adding 'mindspore_ascend-1.9.1.dist-info/RECORD'
removing build/bdist.linux-aarch64/wheel
CPack: - package: /data/YOYOFile/mindspore/build/mindspore/mindspore generated.
success building mindspore project!
---------------- MindSpore: build end ----------------
real 128m6.095s
user 925m10.682s
sys 56m32.674s
# time bash build.sh -e ascend -S on
...
...
...
[100%] Built target _mindspore_frontend_parallel_obj
[100%] Linking CXX static library libmindspore.a
[100%] Built target mindspore
Consolidate compiler generated dependencies of target _c_expression
[100%] Linking CXX shared library _c_dataengine.cpython-39-aarch64-linux-gnu.so
[100%] Building CXX object mindspore/ccsrc/CMakeFiles/_c_expression.dir/pipeline/jit/init.cc.o
[100%] Built target _c_dataengine
Consolidate compiler generated dependencies of target cache_admin
Consolidate compiler generated dependencies of target cache_server
[100%] Building CXX object mindspore/ccsrc/minddata/dataset/engine/cache/CMakeFiles/cache_server.dir/cache_main.cc.o
[100%] Building CXX object mindspore/ccsrc/minddata/dataset/engine/cache/CMakeFiles/cache_admin.dir/cache_admin.cc.o
[100%] Building CXX object mindspore/ccsrc/minddata/dataset/engine/cache/CMakeFiles/cache_admin.dir/cache_admin_arg.cc.o
[100%] Linking CXX executable cache_server
[100%] Built target cache_server
[100%] Linking CXX executable cache_admin
[100%] Built target cache_admin
[100%] Linking CXX shared module _c_expression.cpython-39-aarch64-linux-gnu.so
[100%] Built target _c_expression
Run CPack packaging tool...
CPack: Create package using External
CPack: Install projects
CPack: - Clean temporary : /data/YOYOFile/mindspore/build//package/mindspore
CPack: - Install project: MindSpore []
CPack: Create package
...
...
...
adding 'mindspore_ascend-1.9.1.dist-info/METADATA'
adding 'mindspore_ascend-1.9.1.dist-info/WHEEL'
adding 'mindspore_ascend-1.9.1.dist-info/entry_points.txt'
adding 'mindspore_ascend-1.9.1.dist-info/top_level.txt'
adding 'mindspore_ascend-1.9.1.dist-info/RECORD'
removing build/bdist.linux-aarch64/wheel
CPack: - package: /data/YOYOFile/mindspore/build/mindspore/mindspore generated.
success building mindspore project!
---------------- MindSpore: build end ----------------
real 127m5.367s
user 911m16.674s
sys 48m24.587s
输出
output/mindspore_ascend-1.9.1-cp39-cp39-linux_aarch64.whl
output/mindspore_ascend-1.9.1-cp39-cp39-linux_aarch64.whl.sha256
在联网状态下,安装whl包时会自动下载MindSpore安装包的依赖项(依赖项详情参见setup.py中的required_package),其余情况需自行安装。运行模型时,需要根据ModelZoo中不同模型指定的requirements.txt安装额外依赖,常见依赖可以参考requirements.txt。
pip install output/mindspore-*.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
LOCAL_ASCEND=/usr/local/Ascend
是昇腾AI处理器配套软件包默认安装路径。
# control log level. 0-DEBUG, 1-INFO, 2-WARNING, 3-ERROR, 4-CRITICAL, default level is WARNING.
export GLOG_v=2
# Conda environmental options
LOCAL_ASCEND=/usr/local/Ascend # the root directory of run package
# lib libraries that the run package depends on
export LD_LIBRARY_PATH=${LOCAL_ASCEND}/ascend-toolkit/latest/lib64:${LOCAL_ASCEND}/driver/lib64:${LOCAL_ASCEND}/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe/op_tiling:${LD_LIBRARY_PATH}
# Environment variables that must be configured
## TBE operator implementation tool path
export TBE_IMPL_PATH=${LOCAL_ASCEND}/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe
## OPP path
export ASCEND_OPP_PATH=${LOCAL_ASCEND}/ascend-toolkit/latest/opp
## AICPU path
export ASCEND_AICPU_PATH=${ASCEND_OPP_PATH}/..
## TBE operator compilation tool path
export PATH=${LOCAL_ASCEND}/ascend-toolkit/latest/compiler/ccec_compiler/bin/:${PATH}
## Python library that TBE implementation depends on
export PYTHONPATH=${TBE_IMPL_PATH}:${PYTHONPATH}
# Set path to extracted MindSpore accordingly
export LD_LIBRARY_PATH={mindspore_path}:${LD_LIBRARY_PATH}
参数解释
{mindspore_path}
表示MindSpore二进制包所在位置的绝对路径。python -c "import mindspore;mindspore.run_check()"
MindSpore version: 版本号
The result of multiplication calculation is correct, MindSpore has been installed successfully!
(ms19) root@80f9a288c6ba:~# python -c "import mindspore;mindspore.run_check()"
MindSpore version: 1.9.0
The result of multiplication calculation is correct, MindSpore has been installed successfully!
import numpy as np
import mindspore as ms
import mindspore.ops as ops
ms.set_context(device_target="Ascend")
x = ms.Tensor(np.ones([1,3,3,4]).astype(np.float32))
y = ms.Tensor(np.ones([1,3,3,4]).astype(np.float32))
print(ops.add(x, y))
[[[[2. 2. 2. 2.]
[2. 2. 2. 2.]
[2. 2. 2. 2.]]
[[2. 2. 2. 2.]
[2. 2. 2. 2.]
[2. 2. 2. 2.]]
[[2. 2. 2. 2.]
[2. 2. 2. 2.]
[2. 2. 2. 2.]]]]
在源码根目录下执行编译脚本build.sh
成功后,在output
目录下找到编译生成的whl安装包,然后执行命令进行升级。
pip install --upgrade output/mindspore-*.whl
源码编译方式安装mindspore-ascend-310版本
整体步骤与Ascend910一致,这里仅记录差异的地方,需要特别注意。
cd mindspore
# 编译Ascend310版本
bash build.sh -e ascend -V 310 -S on
# time bash build.sh -e ascend -V 310 -S on
...
...
...
Run CPack packaging tool...
CPack: Create package using TGZ
CPack: Install projects
CPack: - Run preinstall target for: MindSpore
CPack: - Install project: MindSpore []
CPack: Create package
CPack: - package: /data/YOYOFile/mindspore/output/mindspore_ascend-1.9.1-linux_aarch64.tar.gz generated.
CPack: - checksum file: /data/YOYOFile/mindspore/output/mindspore_ascend-1.9.1-linux_aarch64.tar.gz.sha256 generated.
success building mindspore project!
acl mode, skipping deploy phase
---------------- MindSpore: build end ----------------
real 68m39.293s
user 511m36.950s
sys 25m28.390s
输出
output/mindspore_ascend-1.9.1-linux_aarch64.tar.gz
output/mindspore_ascend-1.9.1-linux_aarch64.tar.gz.sha256
由于Ascend310是推理卡,源码编译Ascend310版本的MindSpore软件包只能用于推理,用于真实生产环境。
tar -zxf output/mindspore_ascend-*.tar.gz
...
...
# 其他环境变量配置,与Ascend910一致
# Set path to extracted MindSpore accordingly
export LD_LIBRARY_PATH={mindspore_path}:${LD_LIBRARY_PATH}
解释说明
{mindspore_path}
表示MindSpore二进制包所在位置的绝对路径。创建目录放置样例代码工程,例如/home/HwHiAiUser/Ascend/ascend-toolkit/20.0.RC1/acllib_linux.arm64/sample/acl_execute_model/ascend310_single_op_sample
,代码可以从官网示例下载获取,这是一个[1, 2, 3, 4]
与[2, 3, 4, 5]
相加的简单样例,代码工程目录结构如下:
└─ascend310_single_op_sample
├── CMakeLists.txt // 编译脚本
├── README.md // 使用说明
├── main.cc // 主函数
└── tensor_add.mindir // MindIR模型文件
# 进入样例工程目录,按照实际情况修改路径路径
cd /home/HwHiAiUser/Ascend/ascend-toolkit/20.0.RC1/acllib_linux.arm64/sample/acl_execute_model/ascend310_single_op_sample
# 编译
# {mindspore_path}表示MindSpore二进制包所在位置的绝对路径
cmake . -DMINDSPORE_PATH={mindspore_path}
make
# 执行
./tensor_add_sample
root@44ff3bb0ad1d:/data/YOYOFile/Downloads/ascend310_single_op_sample# cmake . -DMINDSPORE_PATH=/data/YOYOFile/Downloads/mindspore_ascend-1.6.2-linux_aarch64
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /data/YOYOFile/Downloads/ascend310_single_op_sample
root@44ff3bb0ad1d:/data/YOYOFile/Downloads/ascend310_single_op_sample# make -j4
Consolidate compiler generated dependencies of target tensor_add_sample
[ 50%] Building CXX object CMakeFiles/tensor_add_sample.dir/main.cc.o
[100%] Linking CXX executable tensor_add_sample
[100%] Built target tensor_add_sample
root@44ff3bb0ad1d:/data/YOYOFile/Downloads/ascend310_single_op_sample# ./tensor_add_sample
3
5
7
9