这是你没见过的 MindSpore 2.0.0 for Windows GPU 版

本文分享自华为云社区《MindSpore 2.0.0 for Windows GPU泄漏版尝鲜》,作者:张辉 。
在看了 MindSpore 架构师王磊老师的帖子( https://zhuanlan.zhihu.com/p/574507930 )之后,本来张小白要源码编译 MindSpore for Windows GPU 版的,尽管已经安装了 CUDA 11.1 和配套的 cuDNN 8.6.0 for CUDA 11.X( https://bbs.huaweicloud.com/blogs/381666 ),但是由于各种意外,编译的进度停滞了。然而,凭一点点小道消息,张小白发现了别人编译好的 pip 安装包:

嗟来之食张小白也可以吃的,毕竟饿了很久了,慌不择路饥不择食也是情有可原的。
于是,开干!
从上面的文件名 mindspore_gpu-2.0.0-cp37-cp37m-win_amd64.whl 可以看出,这个 whl 包应该是用 Python 3.7 编译的。
首先打开 Windows 的命令行,查看下当前的 Python 版本:

正好对应了。所以如果文件名是 xxx_cp39_cp39m_xxxx.whl,张小白会怎么办?显然是要先准备一个 Anaconda 的 Python 3.9 环境再安装。这个思路不知道读者们 Get 到了没有。
不过,这个时候必须要解决一下 安装了 CUDA 和 cuDNN 之后的一个小 BUG(这其实是源码编译发现的问题,等张小白真的源码编译成功后再仔细解释这件事儿)
就是在 CUDA 11.1 和配套的 cuDNN 8.6.0 for CUDA 11.X 安装手册( https://bbs.huaweicloud.com/blogs/381666 )中,简单的将 cuDNN 解压是不行的:

因为 MindSpore 会直接到 $CUDNN_HOME 的 lib 下找文件,而不是到 lib/x64 目录下找。
所以,自然需要将上图左边的文件拷贝到 $CUDNN_HOME 的 lib 目录下。
好了,这个小插曲解决了之后,我们再来安装 MindSpore 2.0.0 for Windows GPU 版吧!
pip install mindspore_gpu-2.0.0-cp37-cp37m-win_amd64.whl

这就装好了?!张小白有点不相信自己的眼睛(后来的事实表明,确实没那么简单)
再安装 MindVision:
pip install mindvision

...

下载 mindspore_quick_start.py
python mindspore_quick_start.py --device_target=GPU

奇怪,刚才不是装过了 mindvision 了吗?怎么还会报找不到 mindvision 的错?难道 pip 被装到了不该装的地方?
那只有一个办法:使用 python -m pip install xxx 这种安装法了。
python -m pip install mindspore_gpu-2.0.0-cp37-cp37m-win_amd64.whl

python -m pip install mindvision


下载验证脚本:
下载可以获得 mindspore_quick_start.py 文件。

GPU 的第一次验证:
python mindspore_quick_start.py --device_target=GPU

好像有点卡:
不过居然走下去了:

从目录结构可以看出,它自动创建了 lenet 和 mnist 两个目录,然后下载了 mnist 数据集,然后开始例行的训练、评估、推理流程:

虽然 10 轮 epoch 都已经做完了,但是 python 代码迟迟没有退出:

恐怕真的有点什么问题。
张小白强行中断了当前脚本的执行。
先用以前在 ubuntu 的 GPU 环境中常用的 test.py 脚本试试:
编辑 test.py

执行 python test.py

奇怪,这倒是 OK 的。应该说明 MindSpore GPU 版应该是没问题的。
再来试试:
python mindspore_quick_start.py --device_target=GPU

。。。


关闭图形的小窗口后:

居然打印出来了训练的结果:预测:126133,实际 126133。嗯,孺子可教也!
不过张小白总觉得 GPU 训练的时间有点慢,不如用 CPU 方式训练比较一下:
python mindspore_quick_start.py --device_target=CPU



CPU 训练后居然会出现有个图片推理结果错误了。
但是这也许不重要。
我们看下训练的速度比较:

看来不能光靠感觉,得靠数字说话,虽然 LeNet 是个最简单的网络,但是张小白这台 2019 年的笔记本的 GPU 的训练速度还是比 CPU 略快的。
版权声明: 本文为 InfoQ 作者【华为云开发者联盟】的原创文章。
原文链接:【http://xie.infoq.cn/article/55d766e75f4e009b04d89e079】。文章转载请联系作者。
评论