注:本教程仅供摩米实验室内部学习适用
MLU270外形类似一张显卡,需要插到主板的显卡插槽上,供电有点特殊,需要把两个2×4pin的电源插口通过一个二合一的转接线,转接成一个2×4pin的插口,再插到270上。
1、到ubuntu官网下载系统镜像,ubuntu16.04下载地址,下载64-bit PC (AMD64) desktop image
2、使用UltraISO软件写入u盘以制作系统盘,Ultraiso下载地址
3、将系统盘插到主机上,开机狂按F12进入bios,选择以usb方式启动,开始装系统
4、装好系统进入分盘时,对根目录尽量分配超过40GB的空间,因为后面装pytorch很多文件直接需要装到根目录下。否则到时候重新分盘有点麻烦。参考Ubuntu分区扩充
1、v1.6.0文件是提供的软件包,v1.6.0文件夹下提供如下文件:
caffe cncl cnml cnnl cnplugin cntoolkit driver pytorch tensorflow models
datasets
将这些文件夹放到/home/目录下
2、安装驱动
安装前需要检查能不能读取到270
lspci -vvv -d:0270
检查是否输出以下信息:
65:00.0 Processing accelerators: Device cabc:0270
Subsystem: Device cabc:0018
Control: I/O- Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErrStepping- SERR+ FastB2B- DisINTx+
Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort-
<TAbort- <MAbort- >SERR- <PERR- INTxLatency: 0, Cache Line Size: 32 bytes
Interrupt: pin A routed to IRQ 33
Region 0: Memory at 380080000000 (64-bit, prefetchable) [size=256M]
Region 2: Memory at 380090000000 (64-bit, prefetchable) [size=64M]
Region 4: Memory at 380094000000 (64-bit, prefetchable) [size=64M]
安装两个依赖
apt install dkms
apt install docker.io
下一步,安装驱动
cd driver
dpkg -i neuware-mlu270-driver-dkms_4.8.0_all.deb
ls /dev/cambricon*
显示黄色的/dev/cambricon_ctl /dev/cambricon_dev0则表示安装成功
3、环境准备
解压模型和数据集,到dataset文件夹下:
cat Cambricon-MLU270-datasets.tar.gz0* >Cambricon-MLU270-datasets.tar.gz
tar -zxvf Cambricon-MLU270-datasets.tar.gz
到model文件夹下
cat Cambricon-MLU270-datasets.tar.gz0* >Cambricon-MLU270-datasets.tar.gz
tar -zxvf Cambricon-MLU270-datasets.tar.gz
4、安装PyTorch环境
(1)导入PyTorch镜像
cd pytorch/docker
cat pytorch-0.11.1-ubuntu16.04-py2.7.12.tar.gz0* >pytorch-0.11.1-
ubuntu16.04-py2.7.12.tar.gz
tar -zxvf pytorch-0.11.1-ubuntu16.04-py2.7.12.tar.gz
docker load <pytorch-0.11.1-ubuntu16.04-py2.7.12.tar
(2)查看镜像是否导入成功
docker images
查看是否显示yellow.hub.cambricon.com/pytorch/pytorch:0.14.0-ubuntu16.04-
py2.7.12的镜像
(3)安装库(容器中已经安装,不需要操作)
cd cntoolkit
dpkg -i cntoolkit_1.6.1-1.ubuntu16.04_amd64.deb
cd /var/cntoolkit-1.6.1/
dpkg -i *.deb
cd cnml
dpkg -i cnml_7.9.2-1.ubuntu16.04_amd64.deb
cd cnnl
dpkg -i cnnl_1.0.3-1.ubuntu16.04_amd64.deb
cd cnplugin
dpkg -i cnplugin_1.11.1-1.ubuntu16.04_amd64.deb
(4)安装pytorch(容器中已经安装,不需要操作)
cd pytorch/wheel
pip install torch-1.3.0-cp27-cp27mu-linux_x86_64.whl
pip install torch_mlu-0.11.0.post1-cp27-cp27mu-linux_x86_64.whl
pip install torchvision-0.2.1-cp27-cp27mu-linux_x86_64.whl
(5)验证pytorch是否正常
python
import torch_mlu
1、解压pytorch/src下主目录:tar -xzvf pytorch-v0.11.1.tar.gz
解压得到一个文件夹cambricon_pytorch,里面有些文件。将整个文件夹拷贝到/home/下
再将前面解压好的datasets和pytorch_models拷贝到如下路径里
/home/cambricon_pytorch/pytorch/models
/home/cambricon_pytorch/datasets
2、把run-cambricon-docker.sh放到/home/路径下,如果找不到这个文件可以自己新建一个
#!/bin/bash
export MY_CONTAINER="pytorch-v1.7.0-test"
num=`sudo docker ps -a|grep "$MY_CONTAINER"|wc -l`
echo $num
echo $MY_CONTAINER
if [ 0 -eq $num ]; then
sudo xhost +
sudo docker run \
--device /dev/cambricon_ctl \
--net=host --pid=host \
-v /etc/localtime:/etc/localtime:ro \
-it --privileged --name $MY_CONTAINER \
-v /opt/cambricon:/opt/cambricon \
-v $PWD/cambricon_pytorch:/home/cambricon_pytorch \
-w /home/cambricon_pytorch \
091ff0c21629 \
/bin/bash
else
sudo docker start $MY_CONTAINER
#sudo docker attach $MY_CONTAINER
sudo docker exec -w /home/cambricon_pytorch -ti $MY_CONTAINER /bin/bash
fi
然后修改如下
MY_CONTAINER=“”:容器名称,可以自定义
29af2f162dab:是刚才导入的镜像ID(sudo docker iamges 查看 images id)
3、启动脚本(此时还没进入容器)
./run-cambricon-docker.sh
于是进入了容器
4、检查驱动是否映射到容器中
ls /dev/cam*
5、检查高性能库文件是否存在
ls /usr/local/neuware/lib64/
激活环境变量
source env_pytorch.sh
pip install virtualenv
编译:
./configure_pytorch.sh 0
激活pytorch环境(在/home/cambricon_pytorch目录下执行):
source ./pytorch/src/catch/venv/pytorch/bin/activate
后续进入docker后均需执行:
source env_pytorch.sh
source ./pytorch/src/catch/venv/pytorch/bin/activate
/home/cambricon_pytorch/pytorch/src/catch/examples/online/yolov3
修改模型加载路径:
/home/cambricon_pytorch/pytorch/src/catch/venv/pytorch/lib/python2.7/site-packages/torchvision/models/object_detection/yolov3/models.py
416行修改如下
#model.load_state_dict(model_zoo.load_url(model_urls[‘yolov3’])) 修改为
model.load_state_dict(torch.load("/home/cambricon_pytorch/pytorch/models/pytorch_models/origin/checkpoints/yolov3.pth"), strict=False)
运行命令:
python test.py --mlu false --jit false --save_pt false --offline_mode false --batch_size 1 --core_number 1 --ann_dir $COCO_PATH_PYTORCH/COCO --image_number 10 --half_input 1 --quantized_mode 1 --coco_path $COCO_PATH_PYTORCH/COCO --quantization false --input_channel_order 0
python test.py --mlu false --jit false --save_pt false --offline_mode false --batch_size 1 --core_number 1 --ann_dir $COCO_PATH_PYTORCH/COCO --image_number 10 --half_input 1 --quantized_mode 1 --coco_path $COCO_PATH_PYTORCH/COCO --quantization true --input_channel_order 0
在当前目录生成量化后的yolov3.pth文件
/home/cambricon_pytorch/pytorch/src/catch/examples/online/yolov3/yolov3.pth
修改模型路径:
vim /home/cambricon_pytorch/pytorch/src/catch/venv/pytorch/lib/python2.7/site-packages/torchvision/models/quantization/object_detection/yolov3/models.py
python test.py --mlu true --jit true --save_pt false --offline_mode false --batch_size 1 --core_number 1 --ann_dir $COCO_PATH_PYTORCH/COCO --image_number 10 --half_input 1 --quantized_mode 1 --coco_path $COCO_PATH_PYTORCH/COCO --quantization false --input_channel_order 0
cd /home/cambricon_pytorch/pytorch/src/catch/examples/offline/yolov3
运行命令:
./run_all_offline_mc.sh 1 1