昇思MindSpore源码编译安装

news/2025/1/11 14:05:38/

一、参考资料

MindSpore源码阅读系列(三):统一调试MindSpore Python与C++
MindSpore源码阅读系列(二):编译并调试MindSpore源码(对阅读源码很有帮助)

二、源码编译MindSpore(Ascend910版本)

源码编译方式安装MindSpore Ascend 910版本

1. 重要说明

  • 建议使用Anaconda虚拟环境,防止破坏系统中的Ascend环境。
  • MindSpore与CANN版本版本对齐,否则编译得到MindSpore软件包无法使用。
  • 当默认路径存在安装包的时候,LD_LIBRARY_PATH环境变量不起作用;默认路径优先级别为:/usr/local/Ascend/nnae 高于 /usr/loacl/Ascend/ascend-toolkit。原因是MindSpore采用DT_RPATH方式支持无环境变量启动,减少用户设置;DT_RPATH优先级比LD_LIBRARY_PATH环境变量高。

2. 准备工作

2.1 安装CANN和固件驱动

昇腾软件包提供商用版和社区版两种下载途径:

  • 商用版下载需要申请权限,下载链接与安装方式请参考Ascend Data Center Solution 22.0.RC3安装指引文档。
  • 社区版下载不受限制,下载链接请前往CANN社区版,选择6.0.RC1.alpha005版本,以及在固件与驱动链接中获取对应的固件和驱动安装包,安装包的选择与安装方式请参照上述的商用版安装指引文档。

详细安装步骤,请参考:install/mindspore_ascend_install_source.md · MindSpore/docs - Gitee.com

2.2 安装昇腾AI处理器配套软件包

如果之前已经安装过昇腾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

3. 下载源码

以MindSpore1.9版本为例。

git clone https://gitee.com/mindspore/mindspore.git -b r1.9

4. 编译

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

4.1 CPU版本

源码编译方式安装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

4.2 Ascend910版本

# 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

5. 安装

在联网状态下,安装whl包时会自动下载MindSpore安装包的依赖项(依赖项详情参见setup.py中的required_package),其余情况需自行安装。运行模型时,需要根据ModelZoo中不同模型指定的requirements.txt安装额外依赖,常见依赖可以参考requirements.txt。

pip install output/mindspore-*.whl -i https://pypi.tuna.tsinghua.edu.cn/simple

6. 配置环境变量

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二进制包所在位置的绝对路径。

7. 验证是否成功安装

7.1 方法一

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!

7.2 方法二

import numpy as np
import mindspore as ms
import mindspore.ops as opsms.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.]]]]

8. 本地源码编译升级

在源码根目录下执行编译脚本build.sh成功后,在output目录下找到编译生成的whl安装包,然后执行命令进行升级。

pip install --upgrade output/mindspore-*.whl

三、源码编译MindSpore(Ascend310版本)

源码编译方式安装mindspore-ascend-310版本

整体步骤与Ascend910一致,这里仅记录差异的地方,需要特别注意。

1. 编译

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

2. 安装

由于Ascend310是推理卡,源码编译Ascend310版本的MindSpore软件包只能用于推理,用于真实生产环境

tar -zxf output/mindspore_ascend-*.tar.gz

3. 配置环境变量

...
...
# 其他环境变量配置,与Ascend910一致
# Set path to extracted MindSpore accordingly
export LD_LIBRARY_PATH={mindspore_path}:${LD_LIBRARY_PATH}

解释说明

  • {mindspore_path}表示MindSpore二进制包所在位置的绝对路径。

4. 验证是否安装成功

创建目录放置样例代码工程,例如/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

http://www.ppmy.cn/news/727926.html

相关文章

理解和使用蓝牙BlueZ中的FTP与PBAP协议

目录(?)[-] 相关说明 相关网站资源 工作环境 基本工作原理 代码框架 大致工作流程 Server端启动流程 Client端调用流程 具体使用方法 PBAP相关函数接口的使用 作者:刘旭晖 Raymond转载请注明出处 Email:colorant163.com BLOG:http://blog.…

Java使用ffmpeg进行音频格式转换

ffmpeg相关文章网址(用于记录一些好的文章地址~) ffmpeg命令详解 https://blog.csdn.net/zzcchunter/article/details/68060989 java使用ffmpeg进行视频转换 https://blog.csdn.net/zhengdesheng19930211/article/details/64443620 java使用ffmpeg插…

redux-saga

redux-saga 官网:About | Redux-Saga 中文网:自述 Redux-Saga redux-saga 是一个用于管理 异步获取数据(副作用) 的redux中间件;它的目标是让副作用管理更容易,执行更高效,测试更简单,处理故障时更容易… …

【学员作品】我的电商类App 3.2.1居然被拒了,怎么破?(酷课堂iOS交流群问答整理201812期)

酷课堂iOS移动开发交流群问答精华整理(201812期) 以下内容由客服妹子爬楼整理,限于篇幅,无法就所有问题做详细说明,感兴趣的小伙伴,可以添加小课QQ:3068527144,申请加入交流小群。希…

MindSpore:测试mindspore的ascend用例编译失败

问题描述: 测试mindspore的ascend用例编译失败(MindSpore官网 ) (base) xlingxling-MS-7916:~$ cd /home/xling/codes/ascend_projects/docs/tutorials/tutorial_code/ascend310_single_op_sample (base) xlingxling-MS-7916:~/codes/asce…

BQB pts测试

测试BQB的pts dongle是在蓝牙SIG官网买的,链接是 https://store.bluetooth.com/12210888/orders/d3b63cfd9d3d5a22c2e08ad9711a91c4 pts dongle最新的测试结果 ACS-BV-07-I/ACS-BV-12-I/ACS-BI-13-I 这三个是接完电话就fail ICA-BV-02-I/TCA-BV-01-I 这两个是挂不了电话 ICR-B…

SAP Fiori应用索引大全

官网地址:https://fioriappslibrary.hana.ondemand.com/sap/fix/externalViewer/index.html 顾名思义,这是SAP官方发布的一个Fiori应用检索工具,所有标准发布的Fiori应用的明细都能在这个工具里查到。 截至到Jerry写这篇文章的时候&#xff…

Unity 打包安卓APK到上架各大应用商店(超详细)

一、Unity下载 1.Unity官网下载Hub,中国官网,国际版官网 2.下载Hub,安装---安装 (建议安装LTS版本) 3.添加Android 打包环境,现在新版本直接下载这个就可以直接打包了。 二、Unity打包APK 1.File--Build Settings…