MindSpore cpu版本源码编译失败

1 系统环境

硬件环境(Ascend/GPU/CPU): CPU
MindSpore版本: 2.0.0
执行模式(PyNative/ Graph): 不限
Python版本: 3.9
操作系统平台:Ubuntu 20.04

2 报错信息

2.1 问题描述

直接将Tensor从布尔值转换为浮点数,导致错误的结果。在类型转换前更改为numpy后,结果正确。
在github codespace上拉取最新分支后,直接通过vscode 的cmake插件进行build编译安装:

/usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug

2.2 报错信息

[cmake] CMake Error at mindspore/ccsrc/CMakeLists.txt:440 (add_library):
[cmake]   Target "mindspore_backend" links to target "mindspore::ssl" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?
[cmake]
[cmake]
[cmake] CMake Error at mindspore/ccsrc/CMakeLists.txt:440 (add_library):
[cmake]   Target "mindspore_backend" links to target "mindspore::crypto" but the
[cmake]   target was not found.  Perhaps a find_package() call is missing for an
[cmake]   IMPORTED target, or an ALIAS target is missing?
[cmake]
[cmake]
[cmake] CMake Error at mindspore/core/CMakeLists.txt:91 (add_library):
[cmake]   Target "mindspore_core" links to target "mindspore::crypto" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?

2.3 脚本代码

import mindspore as ms

data = ms.Tensor(np.array([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]), ms.bool_)
print(data)
print(~data)
new_data = ms.Tensor(~data, ms.float32)
print(new_data)
result = ops.ReduceSum()(new_data, 0)
print(result)

3 根因分析

[cmake] CMake Error at mindspore/ccsrc/CMakeLists.txt:440 (add_library):
[cmake]   Target "mindspore_backend" links to target "mindspore::ssl" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED

从报错信息来看,是由于使用系统vscode自带的CMake进行编译导致的,主要原因是使用系统CMake编译的过程中,找不到openssl等编译所需相关依赖库。
接下来也是由于缺少编译所需的相关库库导致找不到crypto等。

[cmake] CMake Error at mindspore/ccsrc/CMakeLists.txt:440 (add_library):
[cmake]   Target "mindspore_backend" links to target "mindspore::crypto" but the
[cmake]   target was not found.  Perhaps a find_package() call is missing for an
[cmake]   IMPORTED target, or an ALIAS target is missing?
[cmake]
[cmake]
[cmake] CMake Error at mindspore/core/CMakeLists.txt:91 (add_library):
[cmake]   Target "mindspore_core" links to target "mindspore::crypto" but the target
[cmake]   was not found.  Perhaps a find_package() call is missing for an IMPORTED
[cmake]   target, or an ALIAS target is missing?

查看日志可以发现,使用build.sh脚本安装则会自动安装相关依赖库。

pkg name:openssl,openssl
openssl config hash: 35bb35a37f90f3a65347c55514f4c6bb6d2b2372d4a87613e269714b151d0ae7
_FIND:/home/xt/mindspore/build/mindspore/.mslib/openssl_1.1.1k_35bb35a37f90f3a65347c55514f4c6bb6d2b2372d4a87613e269714b151d0ae7
not find ssl in path: /home/xt/mindspore/build/mindspore/.mslib/openssl_1.1.1k_35bb35a37f90f3a65347c55514f4c6bb6d2b2372d4a87613e269714b151d0ae7/lib
download:  , openssl , https://gitee.com/mirrors/openssl/repository/archive/OpenSSL_1_1_1k.tar.gz
-- Populating openssl
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xt/mindspore/build/mindspore/_deps/openssl-subbuild
Configuring OpenSSL version 1.1.1k (0x101010bfL) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL file first)         ***
***                                                                ***
**********************************************************************

4 解决方案

(1) 按照官网安装所需工具:Python、GCC等相关依赖工具

(2) 下载最新版2.0代码块

(3) 执行脚本,编译MindSpore

cd mindspore
conda activate py39_ms20
bash build.sh -e cpu -j4 -S on
  • 输出一下日志,则编译成功

success building mindspore project!

---------------- MindSpore: build end ---------------- (4) 安装源码

pip install output/mindspore-*.whl -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 安装成功输出一下日志

Installing collected packages: six, psutil, protobuf, pillow, packaging, numpy, scipy, astunparse, asttokens, mindspore Successfully installed asttokens-2.2.1 astunparse-1.6.3 mindspore-2.0.0 numpy-1.24.2 packaging-23.0 pillow-9.4.0 protobuf-4.22.0 psutil-5.9.4 scipy-1.10.1 six-1.16.0

(5) 验证版本

python -c "import mindspore;mindspore.run_check()"

输出 MindSpore version: 2.0.0 The result of multiplication calculation is correct, MindSpore has been installed successfully!

至此,源码安装MindSpore2.0完成

示例代码测试

(1) 分析源代码缺少相关依赖包引入,补全如下

import mindspore as ms
import numpy as np
from mindspore import ops

(2) 运行源码报错如下

[ERROR] DEVICE(1510258,7f148dd97180,python):2023-02-28-08:35:02.149.178 [mindspore/ccsrc/plugin/device/cpu/hal/device/cpu_device_address.cc:166] SyncDeviceToHost] Types not match. Device type: Bool, host type: Float32!
Traceback (most recent call last):
  File "/home/xt/mindspore/test.py", line 9, in <module>
    print(new_data)
  File "/home/xt/anaconda3/envs/py39_ms20/lib/python3.9/site-packages/mindspore/common/tensor.py", line 444, in __str__
    return str(self.asnumpy())
  File "/home/xt/anaconda3/envs/py39_ms20/lib/python3.9/site-packages/mindspore/common/tensor.py", line 714, in asnumpy
    return Tensor_.asnumpy(self)
RuntimeError: SyncDeviceToHost failed.
  • 从日志来看,是由于数据类型导致,在创建新的Tensor 前进行数据格式转换,修改如下:
import mindspore as ms
import numpy as np
from mindspore import ops

data = ms.Tensor(np.array([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]), ms.bool_)
print(data)
print(~data)
new_data = ms.Tensor(~data, ms.float32)
print(new_data)
result = ops.ReduceSum()(new_data, 0)
print(result)
  • 成功输出
    [[False False False False False False]
     [ True  True  True  True  True  True]
     [ True  True  True  True  True  True]]
    [[ True  True  True  True  True  True]
     [False False False False False False]
     [False False False False False False]]
    [[1. 1. 1. 1. 1. 1.]
     [0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0.]]
    [1. 1. 1. 1. 1. 1.]
  • 至此,完成编译和示例测试