项⽬名称:
基于MindSpore的量化感知训练,扩展混合bit量化功能
⽅案描述:
基于MindSpore的量化感知训练,采⽤搜索算法(例如进化算法/强化学习等)⾃动根据⽹络结构设置4/8bit混合量化策略。
时间规划:
具体开发时间⻅项⽬总结-⽅案进度
7.1 - 7.10 搭建环境,数据集,浮点模型,运⾏全8位量化感知训练,查阅相关api⽀持情况,决定位宽选取⽅案。
7.10 - 7.25 开发位宽选取部分,⽐如量化敏感度求解器,APQ框架或者HAWQ框架
7.25 - 8.25 训练mobilenet v2,调试相关参数,达到项⽬效果
8.26 - 9.20 进⼀步优化,增加⼀些技巧等,增加⼀些额外功能;⼀些其他的⽹络
9.21 - 9.30 代码规范,整理结果
项目背景
混合位宽量化
昇思mindspore量化简介
训练后量化(Post-training Quantization)的功能实现主要在mindspore lite
,主要支持权重量化,全量化,并且现在已经支持混合位宽量化。激活值量化方法支持KL散度,MAX_MIN或REMOVEAL_OUTLIER方法;另外有bias_correction
统计量修正方法。https://www.mindspore.cn/lite/docs/zh-CN/r1.5/use/post_training_quantization.html
感知量化训练(Quantization Aware Training)主要在mindspore
依靠伪量化节点实现。伪量化节点,是指感知量化训练中插入的节点,用以寻找网络数据分布,并反馈损失精度,模拟量化为低比特时的精度损失,把该损失作用到网络模型中,传递给损失函数,让优化器在训练过程中对该损失值进行优化。
而在训练方式上,支持传统QAT和可学习步长QAT(LSQ)两种方式。
相关工作
主要用于训练后量化(PTQ)中,比如ZeroQ[4]等,也有和进化搜索结合的用于混合QAT中,如EvoQ[5]
基于黑森矩阵的混合位宽
量化误差和黑森矩阵有关,计算黑森矩阵可以指导量化位宽的选取,比如HAWQ[6],HAWQ-V2[7],HAWQ-V3[8]
基于强化学习,预测器的混合位宽
搜索问题一种解决方法即强化学习,可以训练ddpg agent等做位宽决策,比如AMC[9],HAQ[10],APQ[11]等。其中对精度的获取可以用预测器,或者实测得到。
项目方案
数据集划分
imagenet训练集原有120万张左右图⽚(modelzoo⾥很多都写错了
)
验证集5万张
训练验证
可以指定gpuid,位宽配置,预训练模型位置等参数;
输出在小数据集上训练后验证的精度结果
src/mobilenetv2_mix/train_and_eval.py
搜索算法
Input:search rounds iters,population N,Randomly generate small training set T and validation set VRandomly generate initial population Pfor i=1...iters doquantize model with candidate configurations in P, train and validate with T and V. sort P with validation accuracy in descending orderP = P[:N // 2]P_crossover = Crossover(P[:N // 4])P_mutation = Mutation(P[:N // 4])P = P + P_crossover + P_mutationOutput: Candidate with best accuracy
混合qat和验证
official/cv/mobilenetv2_quant,添加混合位宽设置即可
network = quantizer.quantize(network)quantizer._set_mixed_bits(network, bits_array)
实验结果
进化搜索实验结果
cpu Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz 72核⼼
内存 512GB
GPU NVidia Tesla V100 32GB * 8 (功耗墙200W)
约16小时



方案进度

后续工作安排
优化训练的pipeline
增加更多位宽和限制下搜索结果
受设备和时间限制,现有结果主要有{4,8}
可选位宽在{60%,70%,80%,90%}
模型限制下的搜索位宽结果和精度,{2,4,6,8}
可选位宽在{60%}
模型限制下的搜索位宽结果和精度及{4,8}
随机选择位宽在{80%}
模型限制下精度平均结果,可以初步说明算法的正确性和有效性。
下一步将在更多位宽和限制下实验,并扩展到其他网络
代码规范 补充说明文档 提交合入请求
遇到的问题及解决方案
centos⽤⼾权限编译安装mindspore
解决⽅案:
⽬标机上缺少⼯具链,而且没有权限装⼯具链⽐较⿇烦,考虑在有权限的相同环境上编译出安装包
使⽤了nvidia/cuda:10.1-cudnn7-devel-centos7
docker镜像搭建容器编译安装包,注意后⾯tag需要有cudnn且有devel(当时ms只⽀持cuda10.1,现在已经⽀持cuda11),容器要安装nccl
编译⼯具链使⽤gcc7.3.0,python使⽤conda的3.7.5,其他⼯具和编译步骤参考
编译完在容器内可以运行
LD_LIBRARY_PATHldd libgpu_collective.so查看,如果有些rpath找不到运行就会有问题
比如我这里的mpi和nccl都是在用户态安装的
libmpi.so.40 => /home/S/chenyihui/usr/lib/libmpi.so.40 (0x00007f9dc06b1000)libnccl.so.2 => /home/S/chenyihui/usr/lib64/libnccl.so.2 (0x00007f9dbb6df000)
mindspore_gpu-1.3.0-cp37-cp37m-linux_x86_64.whl复制到目标机使用
pip安装运行即可(安装包不要乱改名字,后面的部分pip会做检查)
项目完成质量
最开始不太熟悉ms的使⽤,修改参数,跑训练任务遇到很多小问题,在导师帮助下解 决了;
[1] Choi, Jungwook, et al. "Pact: Parameterized clipping activation for quantized neural networks." arXiv preprint arXiv:1805.06085 (2018).
[2] Esser, Steven K., et al. "Learned step size quantization." arXiv preprint arXiv:1902.08153 (2019).
[3] Bhalgat, Yash, et al. "Lsq+: Improving low-bit quantization through learnable offsets and better initialization." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition Workshops. 2020.
[4] Cai, Yaohui, et al. "Zeroq: A novel zero shot quantization framework." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2020.
[5] Yuan, Yong, et al. "Evoq: Mixed precision quantization of dnns via sensitivity guided evolutionary search." 2020 International Joint Conference on Neural Networks (IJCNN). IEEE, 2020.
[6] Dong, Zhen, et al. "Hawq: Hessian aware quantization of neural networks with mixed-precision." Proceedings of the IEEE/CVF International Conference on Computer Vision. 2019.
[7] Dong, Zhen, et al. "Hawq-v2: Hessian aware trace-weighted quantization of neural networks." arXiv preprint arXiv:1911.03852 (2019).
[8] Yao, Zhewei, et al. "HAWQ-V3: Dyadic Neural Network Quantization." International Conference on Machine Learning. PMLR, 2021.
[9] He, Yihui, et al. "Amc: Automl for model compression and acceleration on mobile devices." Proceedings of the European conference on computer vision (ECCV). 2018.
[10] Wang, Kuan, et al. "Haq: Hardware-aware automated quantization with mixed precision." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2019.
[11] Wang, Tianzhe, et al. "Apq: Joint searc for network architecture, pruning and quantization policy." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition. 2020.
/本文为中国科学技术大学陈翊辉同学原创文章 欢迎投稿/





