本文包括了更换cuda版本,以及安装mindspore环境。
MindSpore 对CUDA版本有要求,否则会报warning,所以建议使用官方推荐的CUDA版本。另外注意,MindSpore的GPU版本只支持Linux,Windows上只可以使用CPU版本,所以想在windows上使用MindSpore的的小伙伴只能用CPU版本了。
那么对于Linux,MindSpore对环境的要求也要比pytorch多一些,并且最好版本一致,否则很可能出现warning,也不知道最终会不会影响结果,为了安全起见,还是按照官方要求来吧。
MindSpore的下载地址以及选项大家可以参考官网。
这里要注意的是,它支持的硬件平台是CUDA 11.1,所以首先我们需要看一下服务器中CUDA的版本,通过nvcc -V指令可以方便的看到,比如,我的就是11.2版本,显然不符。
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Sun_Feb_14_21:12:58_PST_2021
Cuda compilation tools, release 11.2, V11.2.152
Build cuda_11.2.r11.2/compiler.29618528_0
如果这种情况下使用import mindspore,就会报warning。
import mindspore
[WARNING] ME(879553:140253901357888,MainProcess):2022-10-07-02:08:42.471.54 [mindspore/run_check/_check_version.py:141] MindSpore version 1.8.1 and cuda version 11.2.72 does not match, please refer to the installation guide for version matching information: https://www.mindspore.cn/install
为了防止未知错误,所以我还是换成了11.1。
首先是卸载,我使用官方提供的方法卸载试试,应该成功了。有人会卸载不成功,但再cuda路径下bin文件中有uninstall。
apt-get --purge remove "*cublas*" "*cufft*" "*curand*" \
"*cusolver*" "*cusparse*" "*npp*" "*nvjpeg*" "cuda*" "nsight*"
之后去官网找到适合自己系统的CUDA
CUDA Toolkit 11.1 Update 1 Downloads | NVIDIA Developer
我选择了runfile模式安装,一共两行指令,下载和运行
wget https://developer.download.nvidia.com/compute/cuda/11.1.1/local_installers/cuda_11.1.1_455.32.00_linux.run
sudo sh cuda_11.1.1_455.32.00_linux.run
安装时候报错,因为安装选项中选择了安装driver,后来将要driver的选项去掉之后安装成功。这里一般已经安装了显卡的driver,并且版本影响不大,如果不需要的话,可以不进行安装;如果要安装的话,出现错误,可以去参考其它人的博客,看起来比较麻烦,这里就不再尝试了。
Finished with code: 256
[ERROR]: Install of driver component failed.
[ERROR]: Install of 455.32.00 failed, quitting
没有安装driver在安装完毕后也会抛出一个warning,告诉我,显卡驱动的版本必须高于455.0。因为我已经安装了驱动,所以可以忽略。
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 455.00 is required for CUDA 11.1 functionality to work.
再次执行nvcc -V,发现已经完成了CUDA版本的替换
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Mon_Oct_12_20:09:46_PDT_2020
Cuda compilation tools, release 11.1, V11.1.105
Build cuda_11.1.TC455_06.29190527_0
之后如果想要安装pytorch的话,可以使用以下指令,版本号可以根据自己的需要修改。
conda install pytorch==1.7.0 torchvision==0.8.0 torchaudio==0.7.0 cudatoolkit=11.0 -c pytorch
之后就是安装mindspore了,我们使用conda创建一个新的虚拟环境,然后按照官网的指令进行安装,我首先选择了cuda指令,但是报错,我使用的清华源,但找不到包。
conda create -n mindspore python=3.8
conda activate mindspore
conda install mindspore-gpu=1.8.1 cudatoolkit=11.1 -c mindspore -c conda-forge
Solving environment: failed
PackagesNotFoundError: The following packages are not available from current channels:
- cudatoolkit=11.1
- __glibc[version='>=2.17,<3.0.a0']
换成pip的安装方法,成功
pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.8.1/MindSpore/gpu/x86_64/cuda-11.1/mindspore_gpu-1.8.1-cp38-cp38-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple
Successfully installed asttokens-2.0.8 astunparse-1.6.3 mindspore-gpu-1.8.1 numpy-1.23.3 packaging-21.3 pillow-9.2.0 protobuf-4.21.7 psutil-5.9.2 pyparsing-3.0.9 scipy-1.9.1 six-1.16.0
进行测试,使用官方提供的方法,如果第一次测试,显示完版本号完全卡死,不如强行结束,等一会儿再试一下,就可能好了。可能刚刚安装成功不适应?。。。。
python -c "import mindspore;mindspore.run_check()"
MindSpore version: 1.8.1
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="GPU")
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.]]]]
至此,mindspore的安装结束。使用pycharm连上远程的环境,可以开始调代码了。
后面随着使用的深入,可能需要用到mindvision工具,MindVision是一个基于MindSpore的计算机视觉开源工具箱,可以使用pip直接进行安装。
pip install mindvision