[{"data":1,"prerenderedAt":1948},["ShallowReactive",2],{"content-query-62heJlgs1E":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":1942,"_id":1943,"_source":1944,"_file":1945,"_stem":1946,"_extension":1947},"/technology-blogs/zh/937","zh",false,"","MindSpore的量化压缩技术原理","以有效的降低模型计算强度、参数大小和内存消耗，但往往带来巨大的精度损失","2022-01-10","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/eeb03e4364d243be80023ee3c6964142.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":1917},"root",[17,25,34,45,57,63,69,76,79,84,89,96,101,106,111,118,129,139,144,152,157,162,167,172,177,185,190,195,203,208,217,222,227,234,239,247,252,259,264,271,276,281,288,293,300,315,320,328,333,340,345,352,369,376,381,388,397,405,410,415,422,427,434,439,446,451,462,467,475,480,485,490,498,503,510,515,522,527,534,539,544,551,556,564,569,576,581,588,593,600,605,612,620,629,634,641,649,654,659,666,671,678,683,690,695,702,711,716,724,729,736,741,748,753,760,765,772,777,784,789,796,801,808,816,821,826,831,838,843,850,855,862,867,872,879,884,889,896,901,908,913,921,926,931,941,948,963,970,985,992,1007,1014,1023,1030,1039,1046,1061,1068,1083,1090,1105,1112,1126,1133,1138,1143,1150,1155,1163,1167,1172,1179,1186,1200,1207,1220,1227,1235,1242,1249,1256,1269,1276,1281,1286,1293,1304,1309,1318,1323,1328,1337,1342,1347,1352,1376,1381,1386,1391,1404,1409,1414,1419,1427,1434,1442,1449,1457,1464,1473,1481,1486,1509,1517,1522,1545,1550,1555,1560,1565,1570,1578,1583,1594,1599,1606,1611,1618,1623,1632,1637,1646,1651,1656,1663,1686,1692,1697,1702,1709,1718,1725,1730,1737,1742,1751,1756,1763,1768,1775,1780,1785,1793,1811,1838,1845,1853,1861,1867,1874,1878,1890,1899,1908],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore的量化压缩技术原理",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":18,"tag":30,"props":31,"children":33},"img",{"alt":7,"src":32},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/0f2445a8892d47b89d7087bcb6f28025.gif",[],{"type":18,"tag":26,"props":35,"children":36},{},[37,39],{"type":24,"value":38},"作者：ZOMI酱 ｜",{"type":18,"tag":40,"props":41,"children":42},"strong",{},[43],{"type":24,"value":44},"来源：知乎",{"type":18,"tag":46,"props":47,"children":49},"h3",{"id":48},"神经网络模型量化概述",[50],{"type":18,"tag":40,"props":51,"children":52},{},[53],{"type":18,"tag":40,"props":54,"children":55},{},[56],{"type":24,"value":48},{"type":18,"tag":46,"props":58,"children":60},{"id":59},"随着深度学习的发展神经网络被广泛应用于各种领域模型性能的提高同时也引入了巨大的参数量和计算量模型量化是一种将浮点计算转成低比特定点计算的技术可以有效的降低模型计算强度参数大小和内存消耗但往往带来巨大的精度损失尤其是在极低比特4bit二值网络1bit甚至将梯度进行量化时带来的精度挑战更大",[61],{"type":24,"value":62},"随着深度学习的发展，神经网络被广泛应用于各种领域，模型性能的提高同时也引入了巨大的参数量和计算量。模型量化是一种将浮点计算转成低比特定点计算的技术，可以有效的降低模型计算强度、参数大小和内存消耗，但往往带来巨大的精度损失。尤其是在极低比特(\u003C4bit)、二值网络(1bit)、甚至将梯度进行量化时，带来的精度挑战更大。",{"type":18,"tag":46,"props":64,"children":66},{"id":65},"这篇文章比较详细当然啦除了非常多的文字这篇文章塞了59个公式涉及到量化在推理和训练的内容虽然可能看得很辛苦但是也希望可以多多支持zomi酱哈打公式不容易发现错误欢迎评论留言指正",[67],{"type":24,"value":68},"这篇文章比较详细，当然啦，除了非常多的文字，这篇文章塞了59个公式，涉及到量化在推理和训练的内容。虽然可能看得很辛苦，但是也希望可以多多支持ZOMI酱哈。打公式不容易，发现错误欢迎评论留言指正。",{"type":18,"tag":26,"props":70,"children":71},{},[72],{"type":18,"tag":30,"props":73,"children":75},{"alt":7,"src":74},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/02f5d78186a84c7ea8bf30cb03c86992.jpg",[],{"type":18,"tag":46,"props":77,"children":78},{"id":7},[],{"type":18,"tag":26,"props":80,"children":81},{},[82],{"type":24,"value":83},"与FP32类型相比，FP16、INT8、INT4的低精度类型所占用空间更小，因此对应的存储空间和传输时间都可以大幅下降。以手机为例，为了提供更人性和智能的服务，现在越来越多的OS和APP集成了深度学习的功能，自然需要包含大量的模型及权重文件。以经典的AlexNet为例，原始权重文件的大小已经超过了200MB，而最近出现的新模型正在往结构更复杂、参数更多的方向发展。显然，低精度类型的空间受益还是很明显的。低比特的计算性能也更高，INT8相对比FP32的加速比可达到3倍甚至更高，功耗上也对应有所减少。",{"type":18,"tag":26,"props":85,"children":86},{},[87],{"type":24,"value":88},"模型量化即以较低的推理精度损失将连续取值（或者大量可能的离散取值）的浮点型模型权重或流经模型的张量数据定点近似（通常为int8）为有限多个（或较少的）离散值的过程，它是以更少位数的数据类型用于近似表示32位有限范围浮点型数据的过程，而模型的输入输出依然是浮点型，从而达到减少模型尺寸大小、减少模型内存消耗及加快模型推理速度等目标。",{"type":18,"tag":26,"props":90,"children":91},{},[92],{"type":18,"tag":30,"props":93,"children":95},{"alt":7,"src":94},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/0c7505074b2b480fbb1fe4eeeb65936b.jpg",[],{"type":18,"tag":26,"props":97,"children":98},{},[99],{"type":24,"value":100},"首先量化会损失精度，这相当于给网络引入了噪声，但是神经网络一般对噪声是不太敏感的，只要控制好量化的程度，对高级任务精度影响可以做到很小。",{"type":18,"tag":26,"props":102,"children":103},{},[104],{"type":24,"value":105},"其次，传统的卷积操作都是使用FP32浮点，浮点运算时需要很多时间周期来完成，但是如果我们将权重参数和激活在输入各个层之前量化到INT8，位数少了乘法操作少了，而且此时做的卷积操作都是整型的乘加运算，比浮点快很多，运算结束后再将结果乘上scale_factor变回FP32，这整个过程就比传统卷积方式快多。",{"type":18,"tag":26,"props":107,"children":108},{},[109],{"type":24,"value":110},"提前从体系结构的考量角度思考量化带来的另一个好处是节能和芯片面积，怎么理解呢？每个数使用了更少的位数，做运算时需要搬运的数据量少了，减少了访存开销（节能），同时所需的乘法器数目也减少（减少芯片面积）。",{"type":18,"tag":26,"props":112,"children":113},{},[114],{"type":18,"tag":30,"props":115,"children":117},{"alt":7,"src":116},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/3d47ebaca668488dbf177fa20833b08e.jpg",[],{"type":18,"tag":46,"props":119,"children":121},{"id":120},"主要的量化方法",[122],{"type":18,"tag":40,"props":123,"children":124},{},[125],{"type":18,"tag":40,"props":126,"children":127},{},[128],{"type":24,"value":120},{"type":18,"tag":130,"props":131,"children":133},"h2",{"id":132},"_1量化方法介绍",[134],{"type":18,"tag":40,"props":135,"children":136},{},[137],{"type":24,"value":138},"1、量化方法介绍",{"type":18,"tag":26,"props":140,"children":141},{},[142],{"type":24,"value":143},"一边而言，量化方案主要分为两种：在线量化（On Quantization）和离线量化（Off Quantization），在线量化指的是感知训练量化（Aware Quantization），离线量化指的是训练后量化（Post Quantization）。训练量化根据名字的意思很好理解，其实就是在网络模型训练阶段采用量化方案进行量化。训练后量化中的量化方案跟训练并不相关，主要是在模型离线工具（模型转换工具的时候）采用量化方案进行量化。",{"type":18,"tag":26,"props":145,"children":146},{},[147],{"type":18,"tag":40,"props":148,"children":149},{},[150],{"type":24,"value":151},"（1）感知量化训练（Aware Quantization）",{"type":18,"tag":26,"props":153,"children":154},{},[155],{"type":24,"value":156},"实际上无论是Tensorflow、MindSpore、Pytroch的量化感知训练是一种伪量化的过程，它是在可识别的某些操作内嵌入伪量化节点（fake quantization op），用以统计训练时流经该节点数据的最大最小值，便于在使用端测转换工具（推理转换工具）的时候，转换成端侧需要的格式时进行量化使用。",{"type":18,"tag":26,"props":158,"children":159},{},[160],{"type":24,"value":161},"目的是减少精度损失，其参与模型训练的前向推理过程令模型获得量化损失的差值，但梯度更新需要在浮点下进行，因而其并不参与反向传播过程。",{"type":18,"tag":26,"props":163,"children":164},{},[165],{"type":24,"value":166},"某些操作无法添加伪量化节点，这时候就需要人为的去统计某些操作的最大最小值，但如果统计不准那么将会带来较大的精度损失，因而需要较谨慎检查哪些操作无法添加伪量化节点。",{"type":18,"tag":26,"props":168,"children":169},{},[170],{"type":24,"value":171},"值得注意的是，伪量化节点的意义在于统计流经数据的最大最小值，并参与前向传播，让损失函数的值增大，优化器感知到这个损失值得增加，并进行持续性地反向传播学习，进一步提高因为伪量化操作而引起的精度下降，从而提升精确度。",{"type":18,"tag":26,"props":173,"children":174},{},[175],{"type":24,"value":176},"值得注意的是，训练时候的原理与在端测推理的时候，其工作原理并不一致。",{"type":18,"tag":26,"props":178,"children":179},{},[180],{"type":18,"tag":40,"props":181,"children":182},{},[183],{"type":24,"value":184},"（2）训练后动态量化（Post Dynamic Quantization）",{"type":18,"tag":26,"props":186,"children":187},{},[188],{"type":24,"value":189},"训练后动态量化是针对已训练好的模型来说的，针对大部分已训练，未做任何量化处理的模型来说均可用此方法进行模型量化。",{"type":18,"tag":26,"props":191,"children":192},{},[193],{"type":24,"value":194},"其工作比较简单，在端测转换工具的时候，对网络模型的权重进行统计其每一层卷积的layer或者channel的最大值和最小值，然后通过量化公式对数据进行byte转换。这样得到的权重参数比以前小1/4。推理的时候，在内存初始化的时候对网络模型中的权重进行反量化操作变成float进行正常的推理。",{"type":18,"tag":26,"props":196,"children":197},{},[198],{"type":18,"tag":40,"props":199,"children":200},{},[201],{"type":24,"value":202},"（3）训练后校正量化（Post Calibration Quantization）",{"type":18,"tag":26,"props":204,"children":205},{},[206],{"type":24,"value":207},"训练后静态量化，同时也称为校正量化或者数据集量化。其原理是对于Mindspore在端测低比特推理的时候（Inference），需要生成一个校准表来量化模型。这个量化校准表的生成需要输入有代表性的数据集，对于分类任务建议输入五百张到一千张有代表性的图片，最好每个类都要包括（即数据无偏）。",{"type":18,"tag":130,"props":209,"children":211},{"id":210},"_2量化算法介绍",[212],{"type":18,"tag":40,"props":213,"children":214},{},[215],{"type":24,"value":216},"2、量化算法介绍",{"type":18,"tag":26,"props":218,"children":219},{},[220],{"type":24,"value":221},"一般而言，无论per channel还是per layer量化方案，对于weight权重的量化使用对称量化，对于activate激活的量化使用非对称量化。",{"type":18,"tag":26,"props":223,"children":224},{},[225],{"type":24,"value":226},"其原因是对于权重而言，其数据的分布为对称的如或，因此采用对称量化可以进一步有效降低计算量，使得数据分布更加符合其真实的分布；activate激活在网络中通常使用ReLU和ReLU6等作为网络模型的激活函数，其数据分布集中在或者，如果采用对称方案会加剧数据的离散程度，另外一个原理是会造成数据的浪费，如[-128,0]之间并不存储任何数据，只有[0,127]有数据分布。",{"type":18,"tag":26,"props":228,"children":229},{},[230],{"type":18,"tag":30,"props":231,"children":233},{"alt":7,"src":232},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/2248acae80ac4e3f9e861c424eac4f0e.jpg",[],{"type":18,"tag":26,"props":235,"children":236},{},[237],{"type":24,"value":238},"下面以int8作为量化的标准，介绍一下两种量化算法的主要细节。",{"type":18,"tag":26,"props":240,"children":241},{},[242],{"type":18,"tag":40,"props":243,"children":244},{},[245],{"type":24,"value":246},"（1）对称量化symmetry",{"type":18,"tag":26,"props":248,"children":249},{},[250],{"type":24,"value":251},"对称的量化算法原始浮点精度数据与量化后INT8数据的转换如下：",{"type":18,"tag":26,"props":253,"children":254},{},[255],{"type":18,"tag":30,"props":256,"children":258},{"alt":7,"src":257},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/8ce6b8202c924c6e93b2e13cfa0d4924.png",[],{"type":18,"tag":26,"props":260,"children":261},{},[262],{"type":24,"value":263},"其中，scale默认是float32浮点数，为了能够表示正数和负数，int采用signed int8的数值类型。通过将float32原始高精度数据转换到signed int8数据的操作如下，其中round为取整函数，量化算法需要确定的数值即为常数scale：",{"type":18,"tag":26,"props":265,"children":266},{},[267],{"type":18,"tag":30,"props":268,"children":270},{"alt":7,"src":269},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/04e0ecf6639b4a349afd39905e468ff9.png",[],{"type":18,"tag":26,"props":272,"children":273},{},[274],{"type":24,"value":275},"由于在神经网络的结构以层（Layer）来进行划分的，因此对权值和数据的量化可以层Layer为单位来进行，对每层Layer的参数和数据分别进行不同scale的量化。",{"type":18,"tag":26,"props":277,"children":278},{},[279],{"type":24,"value":280},"对权值和数据的量化可以归结为寻找scale的过程，由于int8为有符号数，要保证正负数值表示范围的对称性，因此对所有数据首先进行取绝对值的操作，使待量化数据的范围变换为，再来确定scale，其scale正确的计算公式为：",{"type":18,"tag":26,"props":282,"children":283},{},[284],{"type":18,"tag":30,"props":285,"children":287},{"alt":7,"src":286},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/1175cb0dc78e4b3fb45bb74c3daced9a.png",[],{"type":18,"tag":26,"props":289,"children":290},{},[291],{"type":24,"value":292},"另外对于offset的计算公式为：",{"type":18,"tag":26,"props":294,"children":295},{},[296],{"type":18,"tag":30,"props":297,"children":299},{"alt":7,"src":298},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/1f7b337fb5a248cea95bbbb897736922.png",[],{"type":18,"tag":26,"props":301,"children":302},{},[303,305,309,311],{"type":24,"value":304},"确定了scale之后，itn8数据对应的表示范围为 ",{"type":18,"tag":30,"props":306,"children":308},{"alt":7,"src":307},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/d8ec16f222774fecb9c2e157bdddcb77.png",[],{"type":24,"value":310},"，量化操作即为对待量化数据以",{"type":18,"tag":30,"props":312,"children":314},{"alt":7,"src":313},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/31d8639e15324379a56eb6fe6aa1873d.png",[],{"type":18,"tag":26,"props":316,"children":317},{},[318],{"type":24,"value":319},"进行饱和式截断，即超过范围的数据饱和到边界值，然后进行第一条公式所示量化计算即可。",{"type":18,"tag":26,"props":321,"children":322},{},[323],{"type":18,"tag":40,"props":324,"children":325},{},[326],{"type":24,"value":327},"（2）非对称asymmetric",{"type":18,"tag":26,"props":329,"children":330},{},[331],{"type":24,"value":332},"非对称的量化算法与对称的量化算法，其主要区别在于数据转换的方式不同，如下，同样需要确定scale与offset这两个常数：",{"type":18,"tag":26,"props":334,"children":335},{},[336],{"type":18,"tag":30,"props":337,"children":339},{"alt":7,"src":338},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/3eed0b154da0477e9cd0916261f58044.png",[],{"type":18,"tag":26,"props":341,"children":342},{},[343],{"type":24,"value":344},"确定后通过原始float32高精度数据计算得到uint8数据的转换即为如下公式所示:",{"type":18,"tag":26,"props":346,"children":347},{},[348],{"type":18,"tag":30,"props":349,"children":351},{"alt":7,"src":350},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/92edf20c901847e885ad781e639b4423.png",[],{"type":18,"tag":26,"props":353,"children":354},{},[355,357,361,363,367],{"type":24,"value":356},"其中，scale是float32浮点数，uint为unsigned INT8定点数，offset是int32定点数。其表示的数据范围为",{"type":18,"tag":30,"props":358,"children":360},{"alt":7,"src":359},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/dd229861dac54c9ab29e401673deeb7d.png",[],{"type":24,"value":362},"。若待量化数据的取值范围为",{"type":18,"tag":30,"props":364,"children":366},{"alt":7,"src":365},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/cb3941c49e5949d1b495fe7786877068.png",[],{"type":24,"value":368}," ，则scale的计算公式如下：",{"type":18,"tag":26,"props":370,"children":371},{},[372],{"type":18,"tag":30,"props":373,"children":375},{"alt":7,"src":374},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/826e68b6ae6e44deb83493f7a632f88c.png",[],{"type":18,"tag":26,"props":377,"children":378},{},[379],{"type":24,"value":380},"offset的计算方式如下：",{"type":18,"tag":26,"props":382,"children":383},{},[384],{"type":18,"tag":30,"props":385,"children":387},{"alt":7,"src":386},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b7caeb3d58d34fd09a5a21950560feac.png",[],{"type":18,"tag":26,"props":389,"children":390},{},[391,393],{"type":24,"value":392},"对于权值和数据的量化，都采用上述公式的方案进行量化，Xmin和Xmax为待量化参数的最小值和最大值，Qmax和Qmin为：",{"type":18,"tag":30,"props":394,"children":396},{"alt":7,"src":395},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/686c42627a704b6c8990faf575a6caaf.png",[],{"type":18,"tag":26,"props":398,"children":399},{},[400],{"type":18,"tag":40,"props":401,"children":402},{},[403],{"type":24,"value":404},"（3）量化初始化",{"type":18,"tag":26,"props":406,"children":407},{},[408],{"type":24,"value":409},"在一般的端测推理的时候，由于网络模型的输入并没有伪量化节点记录输入tensor的最大值和最小值，因为无法求得量化参数scale和offset，这个时候推理框架许可要根据输入数据的标准差deviation和均值mean进行计算，得到对应的量化参数。",{"type":18,"tag":26,"props":411,"children":412},{},[413],{"type":24,"value":414},"其中标准差Standard Deviation的计算公式为下：",{"type":18,"tag":26,"props":416,"children":417},{},[418],{"type":18,"tag":30,"props":419,"children":421},{"alt":7,"src":420},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/12b2711d10e04af184b2eac8cf6e2c14.png",[],{"type":18,"tag":26,"props":423,"children":424},{},[425],{"type":24,"value":426},"其对应的scale为：",{"type":18,"tag":26,"props":428,"children":429},{},[430],{"type":18,"tag":30,"props":431,"children":433},{"alt":7,"src":432},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/1b33764506fc410a9abf825381279af1.png",[],{"type":18,"tag":26,"props":435,"children":436},{},[437],{"type":24,"value":438},"Offset的计算为：",{"type":18,"tag":26,"props":440,"children":441},{},[442],{"type":18,"tag":30,"props":443,"children":445},{"alt":7,"src":444},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/47fea352ce83420db9234ddcb1228e60.png",[],{"type":18,"tag":26,"props":447,"children":448},{},[449],{"type":24,"value":450},"值得注意的是，由于在其他端测推理框架（如tflite）需要手工输入标准差deviation和均值mean，容易导致对数据分析、数据输入的错误和框架的割裂，因此感知量化训练的时候会对模型的输入和输出加上fakeQuant伪量化节点，记录数据min和max，从而求得Mindspore端测用到input tensor的scale和offset。",{"type":18,"tag":130,"props":452,"children":454},{"id":453},"感知量化训练",[455],{"type":18,"tag":40,"props":456,"children":457},{},[458],{"type":18,"tag":40,"props":459,"children":460},{},[461],{"type":24,"value":453},{"type":18,"tag":26,"props":463,"children":464},{},[465],{"type":24,"value":466},"上面讲解了基本的量化公式和量化的方法，下面来详细展开感知量化训练（Aware Quantization）模型中插入伪量化节点fake quant来模拟量化引入的误差。端测推理的时候折叠fake quant节点中的属性到tensor中，在端测推理的过程中直接使用tensor中带有的量化属性参数。",{"type":18,"tag":26,"props":468,"children":469},{},[470],{"type":18,"tag":40,"props":471,"children":472},{},[473],{"type":24,"value":474},"1、伪量化节点",{"type":18,"tag":26,"props":476,"children":477},{},[478],{"type":24,"value":479},"为量化节点（Fake Quant）的意义在于：",{"type":18,"tag":26,"props":481,"children":482},{},[483],{"type":24,"value":484},"1）找到输入数据的分布，即找到min和max值；",{"type":18,"tag":26,"props":486,"children":487},{},[488],{"type":24,"value":489},"2）模拟量化到低比特操作的时候的精度损失，把该损失作用到网络模型中，传递给损失函数，让优化器去在训练过程中对该损失值进行优化。",{"type":18,"tag":26,"props":491,"children":492},{},[493],{"type":18,"tag":40,"props":494,"children":495},{},[496],{"type":24,"value":497},"a. 基本原理",{"type":18,"tag":26,"props":499,"children":500},{},[501],{"type":24,"value":502},"低比特量化模型运行时，需要确定scale与offset两个量化参数：",{"type":18,"tag":26,"props":504,"children":505},{},[506],{"type":18,"tag":30,"props":507,"children":509},{"alt":7,"src":508},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/93dcc0c6562e4cdf98935a98f84f9b36.png",[],{"type":18,"tag":26,"props":511,"children":512},{},[513],{"type":24,"value":514},"其中，是float32浮点数，uint8为unsigned int定点数，offset是int32定点数。其表示的数据范围为：",{"type":18,"tag":26,"props":516,"children":517},{},[518],{"type":18,"tag":30,"props":519,"children":521},{"alt":7,"src":520},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/31a2c273b49b417d9daf70a700b27f4e.png",[],{"type":18,"tag":26,"props":523,"children":524},{},[525],{"type":24,"value":526},"若待量化数据的取值范围为[a,b]，则scale和offset的计算方式如下：",{"type":18,"tag":26,"props":528,"children":529},{},[530],{"type":18,"tag":30,"props":531,"children":533},{"alt":7,"src":532},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/e0c7b2c0ef3440c5a3f3dd51eafabd41.png",[],{"type":18,"tag":26,"props":535,"children":536},{},[537],{"type":24,"value":538},"对于权值和数据的量化，都采用上述公式的方案进行量化，min和max为待量化参数的最小值和最大值。",{"type":18,"tag":26,"props":540,"children":541},{},[542],{"type":24,"value":543},"前面呢，曾经提到一般而言，权重数据是对称，为了可以进一步减少数据的计算，较少额外的存储数据，因此推荐使用对称的量化算法。但是在对mobieNetV2，mobileNetV3的实际测试开发的过程中，发现数据大部分是有偏的，例如mobileNetV2中：",{"type":18,"tag":26,"props":545,"children":546},{},[547],{"type":18,"tag":30,"props":548,"children":550},{"alt":7,"src":549},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/caac5b4ec7fb4ca9acbc9cbb673fc22d.jpg",[],{"type":18,"tag":26,"props":552,"children":553},{},[554],{"type":24,"value":555},"此时权重对应的数据并不是以0为中心点对称，featture.child8.conv.child3.weight的权重数据分布范围是，为了更好地拟合更多的数据，对更多的数据具有更好的泛化性，因此统一使用了非对称的量化算法。",{"type":18,"tag":26,"props":557,"children":558},{},[559],{"type":18,"tag":40,"props":560,"children":561},{},[562],{"type":24,"value":563},"b. 正向传播",{"type":18,"tag":26,"props":565,"children":566},{},[567],{"type":24,"value":568},"为了求得网络模型tensor数据精确的Min和Max值，因此在模型训练的时候插入伪量化节点来模拟引入的误差，得到数据的分布。对于每一个算子，量化参数通过下面的方式得到：",{"type":18,"tag":26,"props":570,"children":571},{},[572],{"type":18,"tag":30,"props":573,"children":575},{"alt":7,"src":574},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/4af26f10dc3b495b8ec150b76c31ff0d.png",[],{"type":18,"tag":26,"props":577,"children":578},{},[579],{"type":24,"value":580},"Fake quant节点的具体计算公式为：",{"type":18,"tag":26,"props":582,"children":583},{},[584],{"type":18,"tag":30,"props":585,"children":587},{"alt":7,"src":586},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/2cdacf618e3d4affa2f4cb6625c21c77.png",[],{"type":18,"tag":26,"props":589,"children":590},{},[591],{"type":24,"value":592},"为了更好地表示上面公式的作用，正向传播的时候fake quant节点对数据进行了模拟量化规约的过程，如下图所示：",{"type":18,"tag":26,"props":594,"children":595},{},[596],{"type":18,"tag":30,"props":597,"children":599},{"alt":7,"src":598},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/d3412515e4634d34bb09872c8b6aba25.jpg",[],{"type":18,"tag":26,"props":601,"children":602},{},[603],{"type":24,"value":604},"首先对数据根据min和max值进行截断，然后把float数据转换成为int数据，之后再把int数据重新转换成为float数据，该操作会丢失掉float1与float2转换之间的精度差。对于量化感知训练，通过模拟量化操作时候对数据进行规约操作，所以网络模型中每一个tensor数据可以根据下面公式进行封装成为op，得到：",{"type":18,"tag":26,"props":606,"children":607},{},[608],{"type":18,"tag":30,"props":609,"children":611},{"alt":7,"src":610},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/dfb3888cf3244bddb790b11e76909570.png",[],{"type":18,"tag":26,"props":613,"children":614},{},[615],{"type":18,"tag":40,"props":616,"children":617},{},[618],{"type":24,"value":619},"c. 反向传播",{"type":18,"tag":26,"props":621,"children":622},{},[623,625],{"type":24,"value":624},"按照正向传播的公式，如果方向传播的时候对其求导数会导致权重为0，因此反向传播的时候相当于一个直接估算器：",{"type":18,"tag":30,"props":626,"children":628},{"alt":7,"src":627},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/1bcbfb29bbc746b9a220d8b706b67756.png",[],{"type":18,"tag":26,"props":630,"children":631},{},[632],{"type":24,"value":633},"最终反向传播的时候fake quant节点对数据进行了截断式处理，如下图所示：",{"type":18,"tag":26,"props":635,"children":636},{},[637],{"type":18,"tag":30,"props":638,"children":640},{"alt":7,"src":639},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/4abd149d86eb46259609d2afdb5cd115.jpg",[],{"type":18,"tag":26,"props":642,"children":643},{},[644],{"type":18,"tag":40,"props":645,"children":646},{},[647],{"type":24,"value":648},"d. 更新Min和Max",{"type":18,"tag":26,"props":650,"children":651},{},[652],{"type":24,"value":653},"FakeQuant伪量化节点主要是根据找到的min和max值进行伪量化操作，更新min和max分别为running和moving，跟batch normal算子一样，这里不进行过多的讲述，具体在Mindspore框架中会自动根据网络模型的结构进行自动插入不同的形式，具体用户不感知。",{"type":18,"tag":26,"props":655,"children":656},{},[657],{"type":24,"value":658},"running minimum的计算公式为：",{"type":18,"tag":26,"props":660,"children":661},{},[662],{"type":18,"tag":30,"props":663,"children":665},{"alt":7,"src":664},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/f3b42efe83114b4c88624a79b98de21e.png",[],{"type":18,"tag":26,"props":667,"children":668},{},[669],{"type":24,"value":670},"Running maximum的计算公式为：",{"type":18,"tag":26,"props":672,"children":673},{},[674],{"type":18,"tag":30,"props":675,"children":677},{"alt":7,"src":676},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/5426d18e0f3c4f128c8cef70cab34189.png",[],{"type":18,"tag":26,"props":679,"children":680},{},[681],{"type":24,"value":682},"Moving minimum的计算公式为：",{"type":18,"tag":26,"props":684,"children":685},{},[686],{"type":18,"tag":30,"props":687,"children":689},{"alt":7,"src":688},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b1948461ad5744d4a987be30258c0832.png",[],{"type":18,"tag":26,"props":691,"children":692},{},[693],{"type":24,"value":694},"Moving maximum的计算公式为：",{"type":18,"tag":26,"props":696,"children":697},{},[698],{"type":18,"tag":30,"props":699,"children":701},{"alt":7,"src":700},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b597dd50b4ea4ea4b7e04d905148a4d5.png",[],{"type":18,"tag":46,"props":703,"children":705},{"id":704},"_2bn折叠",[706],{"type":18,"tag":40,"props":707,"children":708},{},[709],{"type":24,"value":710},"2、BN折叠",{"type":18,"tag":26,"props":712,"children":713},{},[714],{"type":24,"value":715},"初步实施方案中构图比较复杂的主要集中在如何拆分BN层，本节将会对拆分的原理进行细化，落实确定到每一个算子上面，包括每个算子的具体计算公式，控制原理。",{"type":18,"tag":26,"props":717,"children":718},{},[719],{"type":18,"tag":40,"props":720,"children":721},{},[722],{"type":24,"value":723},"a. BN Fold原理",{"type":18,"tag":26,"props":725,"children":726},{},[727],{"type":24,"value":728},"基本卷积操作为：",{"type":18,"tag":26,"props":730,"children":731},{},[732],{"type":18,"tag":30,"props":733,"children":735},{"alt":7,"src":734},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/fccd755a582b480e98b6d4b7b29a73f9.png",[],{"type":18,"tag":26,"props":737,"children":738},{},[739],{"type":24,"value":740},"经过BN层处理后：",{"type":18,"tag":26,"props":742,"children":743},{},[744],{"type":18,"tag":30,"props":745,"children":747},{"alt":7,"src":746},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/2b9f68133cf445608e44ef136c244e2e.png",[],{"type":18,"tag":26,"props":749,"children":750},{},[751],{"type":24,"value":752},"拆分上式得到：",{"type":18,"tag":26,"props":754,"children":755},{},[756],{"type":18,"tag":30,"props":757,"children":759},{"alt":7,"src":758},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/17ebb56494f143b3848ee2f0293bacf6.png",[],{"type":18,"tag":26,"props":761,"children":762},{},[763],{"type":24,"value":764},"定义：",{"type":18,"tag":26,"props":766,"children":767},{},[768],{"type":18,"tag":30,"props":769,"children":771},{"alt":7,"src":770},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/6502d9b3a69d4701845f7f5f7d94c50d.png",[],{"type":18,"tag":26,"props":773,"children":774},{},[775],{"type":24,"value":776},"因此conv+batch normal两个算子在推理的时候，合起来融合为一个conv算子：",{"type":18,"tag":26,"props":778,"children":779},{},[780],{"type":18,"tag":30,"props":781,"children":783},{"alt":7,"src":782},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/2f5c014daa1947fc807ad090c69299a5.png",[],{"type":18,"tag":26,"props":785,"children":786},{},[787],{"type":24,"value":788},"网络模型中经常使用BN层对数据进行规约，有效降低上下层之间的数据依赖性。然而大部分在推理框架当中都会对BN层和卷积操作进行融合，为了更好地模拟在推理的时候的算子融合操作，这里对BN层进行folding处理。下面左边图是(a)普通训练时模型，右边图是(b) 融合后推理时模型。",{"type":18,"tag":26,"props":790,"children":791},{},[792],{"type":18,"tag":30,"props":793,"children":795},{"alt":7,"src":794},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/1ed7f19cb47e4dd3b8f4e4d1f0e1fc70.jpg",[],{"type":18,"tag":26,"props":797,"children":798},{},[799],{"type":24,"value":800},"下面图中左边是BN折叠的BN folding训练模型，右边是BN folding感知量化训练模型。",{"type":18,"tag":26,"props":802,"children":803},{},[804],{"type":18,"tag":30,"props":805,"children":807},{"alt":7,"src":806},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/01de88f2e0534c4c8bb11a25b576098b.jpg",[],{"type":18,"tag":26,"props":809,"children":810},{},[811],{"type":18,"tag":40,"props":812,"children":813},{},[814],{"type":24,"value":815},"b. Correction数据矫正",{"type":18,"tag":26,"props":817,"children":818},{},[819],{"type":24,"value":820},"贝塞尔矫正（Bessel Correction）",{"type":18,"tag":26,"props":822,"children":823},{},[824],{"type":24,"value":825},"在计算方差的时候，论文并没有提及贝塞尔校正，因为贝塞尔校正是通用的统计学方法，用于校正方差。",{"type":18,"tag":26,"props":827,"children":828},{},[829],{"type":24,"value":830},"具体对应于样本标准差为：",{"type":18,"tag":26,"props":832,"children":833},{},[834],{"type":18,"tag":30,"props":835,"children":837},{"alt":7,"src":836},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/abf90c9c9ecc4e77b19c8b27428e39c7.png",[],{"type":18,"tag":26,"props":839,"children":840},{},[841],{"type":24,"value":842},"贝塞尔校正的样本标准差为：",{"type":18,"tag":26,"props":844,"children":845},{},[846],{"type":18,"tag":30,"props":847,"children":849},{"alt":7,"src":848},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/f1e8c574e5a243bebf19716780085e33.png",[],{"type":18,"tag":26,"props":851,"children":852},{},[853],{"type":24,"value":854},"因此对应贝塞尔因子为：",{"type":18,"tag":26,"props":856,"children":857},{},[858],{"type":18,"tag":30,"props":859,"children":861},{"alt":7,"src":860},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/f14b687ba2644703a1fdb500e9ee24cf.png",[],{"type":18,"tag":26,"props":863,"children":864},{},[865],{"type":24,"value":866},"Batch Normal矫正",{"type":18,"tag":26,"props":868,"children":869},{},[870],{"type":24,"value":871},"由于每次更新batch参数的delta的时候，都是使用当前batch的统计数据，这会使得权重参数连带着一起被当前的batch影响，为了避免当前权重数据w的抖动，加入correction的校正因子将权重缩放到长期batch统计数据中。",{"type":18,"tag":26,"props":873,"children":874},{},[875],{"type":18,"tag":30,"props":876,"children":878},{"alt":7,"src":877},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/f05c588f73b44e409362ec6cf1abee56.png",[],{"type":18,"tag":26,"props":880,"children":881},{},[882],{"type":24,"value":883},"模型开始训练的时候会在fold conv卷积的输出除以correction因子，这样的操作是为了让模型直接去学习正常的BN操作（根据当前的batch进行学习），在学习到一定程度之后，冻结当前的BN不再更新全局delta，然后fold conv卷积的操作中直接作用，并同时修改bias，作为correction_2更新到ADD算子中。",{"type":18,"tag":26,"props":885,"children":886},{},[887],{"type":24,"value":888},"Frozen前：",{"type":18,"tag":26,"props":890,"children":891},{},[892],{"type":18,"tag":30,"props":893,"children":895},{"alt":7,"src":894},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/c071311ec9a24a8da5f294672fdc4b91.png",[],{"type":18,"tag":26,"props":897,"children":898},{},[899],{"type":24,"value":900},"Frozen后：",{"type":18,"tag":26,"props":902,"children":903},{},[904],{"type":18,"tag":30,"props":905,"children":907},{"alt":7,"src":906},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/ff63d7c7be1743b0a3bceaca7cc58bbf.png",[],{"type":18,"tag":26,"props":909,"children":910},{},[911],{"type":24,"value":912},"加上correction能够有效地抑制在后期阶段weight的数据波动。",{"type":18,"tag":26,"props":914,"children":915},{},[916],{"type":18,"tag":40,"props":917,"children":918},{},[919],{"type":24,"value":920},"c. BN FOLD训练图",{"type":18,"tag":26,"props":922,"children":923},{},[924],{"type":24,"value":925},"算子",{"type":18,"tag":26,"props":927,"children":928},{},[929],{"type":24,"value":930},"组合模式ConvD+BN+ReLU拆分子图并加入FakeQuant算子，一共拆分为为以下几个算子：Conv2D、BatchNormFold、CorrectionMul、MulFold、FakeQuantWithMinMaxPreChannel、ConvMul、CorrectionAdd、AddFold、ReLU、FakeQuantWithMinMax。",{"type":18,"tag":932,"props":933,"children":934},"ol",{},[935],{"type":18,"tag":936,"props":937,"children":938},"li",{},[939],{"type":24,"value":940},"Conv2D (in: x, weight; out: y)：普通卷积，用于计算BatchNormFold的入参：",{"type":18,"tag":26,"props":942,"children":943},{},[944],{"type":18,"tag":30,"props":945,"children":947},{"alt":7,"src":946},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/212b06e08d7e4884a791f8cf220a4719.png",[],{"type":18,"tag":932,"props":949,"children":951},{"start":950},2,[952],{"type":18,"tag":936,"props":953,"children":954},{},[955,957,961],{"type":24,"value":956},"BatchNormFold ( ：FoldBN，用于计算当前 吗μB和啊σB，，并使用CMA算法更新下σn+1和μn+1，运行时使用 σn和μn 。该算子输出",{"type":18,"tag":30,"props":958,"children":960},{"alt":7,"src":959},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/87c296c8a64f49cf98aaf6463e0d1364.png",[],{"type":24,"value":962},"并不作用于网络，因此该算子实际计算公式为更新",{"type":18,"tag":26,"props":964,"children":965},{},[966],{"type":18,"tag":30,"props":967,"children":969},{"alt":7,"src":968},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/c3fb31877aa047708a0c1210427e9321.png",[],{"type":18,"tag":932,"props":971,"children":973},{"start":972},3,[974],{"type":18,"tag":936,"props":975,"children":976},{},[977,979,983],{"type":24,"value":978},"CorrectionMul (",{"type":18,"tag":30,"props":980,"children":982},{"alt":7,"src":981},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/f08b3cc8d20348aca8aa90f81ed9f37c.png",[],{"type":24,"value":984},")：对w乘以校正因子。由于每次更新batch参数的 σB的时候，都是使用当前batch的统计数据，这会使得权重参数连带着一起被当前的batch影响，为了避免当前权重数据w的抖动，加入correction校正因子将权重缩放到长期batch统计数据中：",{"type":18,"tag":26,"props":986,"children":987},{},[988],{"type":18,"tag":30,"props":989,"children":991},{"alt":7,"src":990},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/d22ed90a782a459b9b3f1e69f8ad0d23.png",[],{"type":18,"tag":932,"props":993,"children":995},{"start":994},4,[996],{"type":18,"tag":936,"props":997,"children":998},{},[999,1001,1005],{"type":24,"value":1000},"MulFold (",{"type":18,"tag":30,"props":1002,"children":1004},{"alt":7,"src":1003},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/921e8695894542b7b31130741ffae53f.png",[],{"type":24,"value":1006}," )：根据batch mean参数对w进行规约，模拟融合操作：",{"type":18,"tag":26,"props":1008,"children":1009},{},[1010],{"type":18,"tag":30,"props":1011,"children":1013},{"alt":7,"src":1012},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/0d220c46111645888c8e88b2f54a0a11.png",[],{"type":18,"tag":932,"props":1015,"children":1017},{"start":1016},5,[1018],{"type":18,"tag":936,"props":1019,"children":1020},{},[1021],{"type":24,"value":1022},"FakeQuantWithMinMaxPreChannel (in: w, delay; out: self)：控制流delay，训练模型中插入伪量化节点fake quant来模拟量化引入的误差，推荐权重按PerChannel模式。",{"type":18,"tag":26,"props":1024,"children":1025},{},[1026],{"type":18,"tag":30,"props":1027,"children":1029},{"alt":7,"src":1028},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/a1f92ae47aaa4f21a3c5c1d67adaf14a.png",[],{"type":18,"tag":932,"props":1031,"children":1033},{"start":1032},6,[1034],{"type":18,"tag":936,"props":1035,"children":1036},{},[1037],{"type":24,"value":1038},"Conv2D (in: w, x, out:x)：实际作用的卷积：",{"type":18,"tag":26,"props":1040,"children":1041},{},[1042],{"type":18,"tag":30,"props":1043,"children":1045},{"alt":7,"src":1044},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/cd61509699904d03975afc4af864da10.png",[],{"type":18,"tag":932,"props":1047,"children":1049},{"start":1048},7,[1050],{"type":18,"tag":936,"props":1051,"children":1052},{},[1053,1055,1059],{"type":24,"value":1054},"ConvMul ( ",{"type":18,"tag":30,"props":1056,"children":1058},{"alt":7,"src":1057},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/67001529dd39451da14885fbba186a5b.png",[],{"type":24,"value":1060}," )：控制流freeze，模型开始训练的时候会在fold conv卷积的输出除以correction校正因子，这样的操作是为了让模型直接去学习正常的BN操作（根据当前的batch进行学习）；在学习到一定程度之后，冻结当前的BN不再更新全局。",{"type":18,"tag":26,"props":1062,"children":1063},{},[1064],{"type":18,"tag":30,"props":1065,"children":1067},{"alt":7,"src":1066},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/cf7c092e201c454ea0df2c081ee271b8.png",[],{"type":18,"tag":932,"props":1069,"children":1071},{"start":1070},8,[1072],{"type":18,"tag":936,"props":1073,"children":1074},{},[1075,1077,1081],{"type":24,"value":1076},"CorrectionAdd ( ",{"type":18,"tag":30,"props":1078,"children":1080},{"alt":7,"src":1079},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/ea8c2b84e8d94bb98bd3ed7bf683a55c.png",[],{"type":24,"value":1082}," )：控制流freeze，在学习到一定程度之后冻结当前BN，校正因子修正bias偏置。",{"type":18,"tag":26,"props":1084,"children":1085},{},[1086],{"type":18,"tag":30,"props":1087,"children":1089},{"alt":7,"src":1088},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/5958e3e8030b4ef49806f2ae9fab3cbe.png",[],{"type":18,"tag":932,"props":1091,"children":1093},{"start":1092},9,[1094],{"type":18,"tag":936,"props":1095,"children":1096},{},[1097,1099,1103],{"type":24,"value":1098},"AddFold (",{"type":18,"tag":30,"props":1100,"children":1102},{"alt":7,"src":1101},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b6c06ee0da1c49d38d147e97ce5c21e3.png",[],{"type":24,"value":1104},")：作用于bias偏置，BN融合后剩余参数作用于bias偏置：",{"type":18,"tag":26,"props":1106,"children":1107},{},[1108],{"type":18,"tag":30,"props":1109,"children":1111},{"alt":7,"src":1110},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/3baadc4ce20548cbad7608d4b90ad154.png",[],{"type":18,"tag":932,"props":1113,"children":1115},{"start":1114},10,[1116,1121],{"type":18,"tag":936,"props":1117,"children":1118},{},[1119],{"type":24,"value":1120},"ReLU (in:x, out: x)：实际作用的激活函数；",{"type":18,"tag":936,"props":1122,"children":1123},{},[1124],{"type":24,"value":1125},"FakeQuantWithMinMax(in:x, min, max, delay; out: x)：控制流delay，训练模型中插入伪量化节点fake quant来模拟量化引入的误差，并用指数移动平均算法更新min和max：",{"type":18,"tag":26,"props":1127,"children":1128},{},[1129],{"type":18,"tag":30,"props":1130,"children":1132},{"alt":7,"src":1131},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/a9ba6a30a875498d85dc4227dc323a99.png",[],{"type":18,"tag":26,"props":1134,"children":1135},{},[1136],{"type":24,"value":1137},"正向图",{"type":18,"tag":26,"props":1139,"children":1140},{},[1141],{"type":24,"value":1142},"下面左边为原始设计bn折叠训练图，右边是实际Mindspore执行经过优化融合的训练图。",{"type":18,"tag":26,"props":1144,"children":1145},{},[1146],{"type":18,"tag":30,"props":1147,"children":1149},{"alt":7,"src":1148},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/162b8987f8b44ecdb75accc3e9b711f3.jpg",[],{"type":18,"tag":26,"props":1151,"children":1152},{},[1153],{"type":24,"value":1154},"图中主要注意的点为BN_Fold_1和BN_Fold_2，其中BN_Fold_1的作用有两个：拆分融合的BN层对weight的影响，和对权重数据进行校正；BN_Fold_2的作用有两个：拆分融合的BN层对bias的作用影响，和对bias数据进行校正。",{"type":18,"tag":26,"props":1156,"children":1157},{},[1158],{"type":18,"tag":40,"props":1159,"children":1160},{},[1161],{"type":24,"value":1162},"d. BN FOLD推理图",{"type":18,"tag":26,"props":1164,"children":1165},{},[1166],{"type":24,"value":925},{"type":18,"tag":26,"props":1168,"children":1169},{},[1170],{"type":24,"value":1171},"组合模式ConvD+BN+ReLU拆分子图并加入FakeQuant算子，一共拆分为以下个算子：Conv2D、BatchNormFold、MulFold、FakeQuantWithMinMaxPreChannel、AddFold。",{"type":18,"tag":932,"props":1173,"children":1174},{},[1175],{"type":18,"tag":936,"props":1176,"children":1177},{},[1178],{"type":24,"value":940},{"type":18,"tag":26,"props":1180,"children":1181},{},[1182],{"type":18,"tag":30,"props":1183,"children":1185},{"alt":7,"src":1184},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/cbef7b65504c4970aba5bd18f61daf9f.png",[],{"type":18,"tag":932,"props":1187,"children":1188},{"start":950},[1189],{"type":18,"tag":936,"props":1190,"children":1191},{},[1192,1194,1198],{"type":24,"value":1193},"BatchNormFold ( ",{"type":18,"tag":30,"props":1195,"children":1197},{"alt":7,"src":1196},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/307b51aee3e3474daf914dab36ea1c1d.png",[],{"type":24,"value":1199},")：开根号作用：",{"type":18,"tag":26,"props":1201,"children":1202},{},[1203],{"type":18,"tag":30,"props":1204,"children":1206},{"alt":7,"src":1205},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b2e56dd1d317478d95c5c4207529d7cf.png",[],{"type":18,"tag":932,"props":1208,"children":1209},{"start":972},[1210],{"type":18,"tag":936,"props":1211,"children":1212},{},[1213,1214,1218],{"type":24,"value":1000},{"type":18,"tag":30,"props":1215,"children":1217},{"alt":7,"src":1216},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/da3f1ee867424524931552425ff64dc6.png",[],{"type":24,"value":1219}," )：根据BN参数对w进行规约，模拟融合操作：",{"type":18,"tag":26,"props":1221,"children":1222},{},[1223],{"type":18,"tag":30,"props":1224,"children":1226},{"alt":7,"src":1225},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/8cbcbabc8003444e9eba6de018f1b6f1.png",[],{"type":18,"tag":932,"props":1228,"children":1229},{"start":994},[1230],{"type":18,"tag":936,"props":1231,"children":1232},{},[1233],{"type":24,"value":1234},"FakeQuantWithMinMaxPreChannel (in: w, delay; out: self)：训练模型中插入伪量化节点fake quant来模拟量化引入的误差，推荐权重按PerChannel模式。",{"type":18,"tag":26,"props":1236,"children":1237},{},[1238],{"type":18,"tag":30,"props":1239,"children":1241},{"alt":7,"src":1240},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/67b67c68c8c6493cabced6572ea7d5e9.png",[],{"type":18,"tag":932,"props":1243,"children":1244},{"start":1016},[1245],{"type":18,"tag":936,"props":1246,"children":1247},{},[1248],{"type":24,"value":1038},{"type":18,"tag":26,"props":1250,"children":1251},{},[1252],{"type":18,"tag":30,"props":1253,"children":1255},{"alt":7,"src":1254},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b14b8844882447df9d16a65cc9c4e309.png",[],{"type":18,"tag":932,"props":1257,"children":1258},{"start":1032},[1259],{"type":18,"tag":936,"props":1260,"children":1261},{},[1262,1263,1267],{"type":24,"value":1098},{"type":18,"tag":30,"props":1264,"children":1266},{"alt":7,"src":1265},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/83eebb3ea9f947ebab28e96f1d8099fa.png",[],{"type":24,"value":1268},"）：作用于bias偏置，BN融合后剩余参数作用于bias偏置：",{"type":18,"tag":26,"props":1270,"children":1271},{},[1272],{"type":18,"tag":30,"props":1273,"children":1275},{"alt":7,"src":1274},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/5919529e743d46e5822467688e11a3ec.png",[],{"type":18,"tag":26,"props":1277,"children":1278},{},[1279],{"type":24,"value":1280},"验证图示例",{"type":18,"tag":26,"props":1282,"children":1283},{},[1284],{"type":24,"value":1285},"正向推理验证图，上面的所有公式都会化为这个图的实际计算方式。",{"type":18,"tag":26,"props":1287,"children":1288},{},[1289],{"type":18,"tag":30,"props":1290,"children":1292},{"alt":7,"src":1291},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b5e9c2a8775d42728fdc7fa56c292d7e.jpg",[],{"type":18,"tag":130,"props":1294,"children":1296},{"id":1295},"训练后量化",[1297],{"type":18,"tag":40,"props":1298,"children":1299},{},[1300],{"type":18,"tag":40,"props":1301,"children":1302},{},[1303],{"type":24,"value":1295},{"type":18,"tag":26,"props":1305,"children":1306},{},[1307],{"type":24,"value":1308},"训练后静态量化（Post Calibration Quantization），同时也称为校正量化或者数据集量化。其原理是对于Mindspore在端测低比特推理的时候（Inference），需要生成一个校准表来量化模型。其核心是取得数据值的量化参数表。",{"type":18,"tag":46,"props":1310,"children":1312},{"id":1311},"_1-权值量化",[1313],{"type":18,"tag":40,"props":1314,"children":1315},{},[1316],{"type":24,"value":1317},"1、 权值量化",{"type":18,"tag":26,"props":1319,"children":1320},{},[1321],{"type":24,"value":1322},"权值在进行推理加速时均已确定，因此不需要对权值进行校准。如果使用对称量化方案，max使用权值的绝对值的最大值，对于非对称量化算法，max和min使用权值的最大值和最小值。",{"type":18,"tag":26,"props":1324,"children":1325},{},[1326],{"type":24,"value":1327},"根据算法验证结果，对于Convolution，每个卷积核按照per channel的方式采用一组独立的量化系数（scale和offset），量化后推理精度较高。因此，Convolution权值的量化根据卷积核数量分组进行，计算得到的scale和offset的数量与卷积核数量相同。Dense的权值使用一组scale和offset。",{"type":18,"tag":46,"props":1329,"children":1331},{"id":1330},"_2数据量化",[1332],{"type":18,"tag":40,"props":1333,"children":1334},{},[1335],{"type":24,"value":1336},"2、数据量化",{"type":18,"tag":26,"props":1338,"children":1339},{},[1340],{"type":24,"value":1341},"数据量化是对每个要量化的Operation的输入数据进行统计，每个Operation计算出最优的一组scale和offset。",{"type":18,"tag":26,"props":1343,"children":1344},{},[1345],{"type":24,"value":1346},"数据是推理计算的中间结果，其数据的范围与输入高度相关，需要使用一组参考输入作为激励，得到每个Operation的输入数据用于确定量化max和min。数据的范围与输入相关，为了使确定的min和max在网络接收不同输入时有更好的鲁棒性，因此提出基于统计分布确定min和max的方案。",{"type":18,"tag":26,"props":1348,"children":1349},{},[1350],{"type":24,"value":1351},"该方案的思路为最小化量化后数据的统计分布与原始高精度数据的统计分布差异性，操作流程如下：",{"type":18,"tag":1353,"props":1354,"children":1355},"ul",{},[1356,1361,1366,1371],{"type":18,"tag":936,"props":1357,"children":1358},{},[1359],{"type":24,"value":1360},"使用直方图统计的方式得到原始float32数据的直方图统计分布；",{"type":18,"tag":936,"props":1362,"children":1363},{},[1364],{"type":24,"value":1365},"在给定的min和max搜索空间中选取若干个和分别对待量化数据进行量化，分别得到量化后的数据；",{"type":18,"tag":936,"props":1367,"children":1368},{},[1369],{"type":24,"value":1370},"使用同样的直方图统计的方式得到n个的直方图统计分布；",{"type":18,"tag":936,"props":1372,"children":1373},{},[1374],{"type":24,"value":1375},"分别计算中每个与的统计分布差异性，找到差异性最低的一个对应的min和max作为确定的量化数值。",{"type":18,"tag":26,"props":1377,"children":1378},{},[1379],{"type":24,"value":1380},"在上述操作中，涉及的超参数包括进行直方图统计时选取的直方图bin个数、min和max的搜索空间、统计分布差异性的指标。",{"type":18,"tag":26,"props":1382,"children":1383},{},[1384],{"type":24,"value":1385},"对于直方图bin个数，该参数直接反应了直方图统计特征的分布数个数，由于数据经过量化后会集中到256个离散的点上，因此bin的个数不宜过大，否则绝大多数的bin都没有数值。",{"type":18,"tag":26,"props":1387,"children":1388},{},[1389],{"type":24,"value":1390},"max的搜索空间可以通过search_start_scale，search_end_scale与search_step来确定。",{"type":18,"tag":1353,"props":1392,"children":1393},{},[1394,1399],{"type":18,"tag":936,"props":1395,"children":1396},{},[1397],{"type":24,"value":1398},"search_start_scale为搜索空间起始点与search_value的比值。",{"type":18,"tag":936,"props":1400,"children":1401},{},[1402],{"type":24,"value":1403},"search_end_scale为搜索空间结束点与search_value的比值。",{"type":18,"tag":26,"props":1405,"children":1406},{},[1407],{"type":24,"value":1408},"search_step为搜索空间中每次搜索的值与seach_value的比值步进值。以max_candidate=100，search_start_scale=0.8，search_end_scale=1.2，search_step=0.01为例，对称量化算法下，其定义的max搜索空间为从100*0.8=80到100*1.2=120的范围，每次步进100*0.01=1，一共41个d_max搜索值；非对称量化算法下，搜索0.8*(max–min) ~ 1.2*(max–min)，确定最好的一个系数。",{"type":18,"tag":26,"props":1410,"children":1411},{},[1412],{"type":24,"value":1413},"继续举多一个例子， search_start_scale =0.3，search_step=0.01，search_end_scale==1.7，bin=150。需要在0.3*max~1.7*max这个范围中找一个最好的max，搜索步长是0.01max，因此需要搜索(1.7-0.3)/0.01 + 1 = 141个max数值。直方统计的bin个数也可以设置，假设当前是150。对于算法方案一，将0-2*max的数据分为150段，统计数据落在每段中的频率，使用数据的绝对值统计，对于算法方案二，在0.3*(max – min)~1.7*(max - min)这个范围中找一个最好的ratio，将0-2*(max - min)的数据分为150段，统计数据落在每段中的频率，使用data – min的值来统计频率。141个数值都要统计，因此一个量化算子需要存储141*150个数值。多个batch情况下对frequancy进行累加。",{"type":18,"tag":26,"props":1415,"children":1416},{},[1417],{"type":24,"value":1418},"统计分布差异性的指标为计算两个长度为n直方图、分布之间的信息差异度，选取的指标包括如下三种：",{"type":18,"tag":932,"props":1420,"children":1421},{},[1422],{"type":18,"tag":936,"props":1423,"children":1424},{},[1425],{"type":24,"value":1426},"Kullback-Leibler Divergence（KL散度）计算方式如下：",{"type":18,"tag":26,"props":1428,"children":1429},{},[1430],{"type":18,"tag":30,"props":1431,"children":1433},{"alt":7,"src":1432},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/9afec670f54a48cebdec0b5b1773688c.png",[],{"type":18,"tag":932,"props":1435,"children":1436},{"start":950},[1437],{"type":18,"tag":936,"props":1438,"children":1439},{},[1440],{"type":24,"value":1441},"Symmetric Kullback-Leibler Divergence（对称KL散度）：对于 Pf、Qq 两个分布，Pf相对Qq 与Qq相对PfKL Divergence是不同的，Symmetric KL Divergence的计算方式为 Pf 相对Qq与Qq相对的KL Divergence的均值，计算方式如下：",{"type":18,"tag":26,"props":1443,"children":1444},{},[1445],{"type":18,"tag":30,"props":1446,"children":1448},{"alt":7,"src":1447},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/08b4b81551384ff7a8d9b879b78d7a78.png",[],{"type":18,"tag":932,"props":1450,"children":1451},{"start":972},[1452],{"type":18,"tag":936,"props":1453,"children":1454},{},[1455],{"type":24,"value":1456},"Jensen-Shannon Divergence（JS散度）：首先生成一个新的分布M，为与的均值，JS Divergence为相对M的KL Divergence与相对M的KL Divergence的均值, 计算方式如下：",{"type":18,"tag":26,"props":1458,"children":1459},{},[1460],{"type":18,"tag":30,"props":1461,"children":1463},{"alt":7,"src":1462},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/ded916bde0494b318ae983516e958fed.png",[],{"type":18,"tag":46,"props":1465,"children":1467},{"id":1466},"_3calibration流程",[1468],{"type":18,"tag":40,"props":1469,"children":1470},{},[1471],{"type":24,"value":1472},"3、Calibration流程",{"type":18,"tag":26,"props":1474,"children":1475},{},[1476],{"type":18,"tag":40,"props":1477,"children":1478},{},[1479],{"type":24,"value":1480},"a. Calibration功能",{"type":18,"tag":26,"props":1482,"children":1483},{},[1484],{"type":24,"value":1485},"离线Calibration需要完成以下功能：",{"type":18,"tag":932,"props":1487,"children":1488},{},[1489,1494,1499,1504],{"type":18,"tag":936,"props":1490,"children":1491},{},[1492],{"type":24,"value":1493},"对算子的输入数据量化校准，计算出数据的最优scale和offset；",{"type":18,"tag":936,"props":1495,"children":1496},{},[1497],{"type":24,"value":1498},"将算子的权值量化为INT8，计算出权值的scale和offset。",{"type":18,"tag":936,"props":1500,"children":1501},{},[1502],{"type":24,"value":1503},"将算子的bias量化为INT32。",{"type":18,"tag":936,"props":1505,"children":1506},{},[1507],{"type":24,"value":1508},"由于算子输入数据在推理过程中才能产生，因此离线Calibration还要实现inference功能。与普通inference过程的不同之处在于对每个需要量化的op的输入数据和权值数据进行了量化和反量化过程。",{"type":18,"tag":26,"props":1510,"children":1511},{},[1512],{"type":18,"tag":40,"props":1513,"children":1514},{},[1515],{"type":24,"value":1516},"b. Calibration过程",{"type":18,"tag":26,"props":1518,"children":1519},{},[1520],{"type":24,"value":1521},"Calibration过程可以分为4步：",{"type":18,"tag":932,"props":1523,"children":1524},{},[1525,1530,1535,1540],{"type":18,"tag":936,"props":1526,"children":1527},{},[1528],{"type":24,"value":1529},"遍历graph中的node，对需要量化的算子的权重进行量化。",{"type":18,"tag":936,"props":1531,"children":1532},{},[1533],{"type":24,"value":1534},"第一次inference，搜索需要量化的算子的输入数据的max和min。",{"type":18,"tag":936,"props":1536,"children":1537},{},[1538],{"type":24,"value":1539},"第二次inference，进行数据的直方统计。",{"type":18,"tag":936,"props":1541,"children":1542},{},[1543],{"type":24,"value":1544},"计算分布差异性能指标（见4.2），根据指标选择最好的min和max，然后计算出数据的scale和offset，根据数据和权值的scale，进行bias量化。",{"type":18,"tag":26,"props":1546,"children":1547},{},[1548],{"type":24,"value":1549},"值得注意的是，min不一定是所有数据batch中的最小值，max也不一定是所有batch中的最大值，与min_percentile和max percentile的值相关。",{"type":18,"tag":26,"props":1551,"children":1552},{},[1553],{"type":24,"value":1554},"假设每个batch处理10张图片，共100个batch，某个op的1张图片输入数据是1000个数，max_percentile = 0.99999，数据总量是10*100*1000 = 1000000个数。1000000*0.99999 = 999990。把所有输入数据从大到小排序，max取第10个数。因此需要缓存10个数，对第一个batch的输入1000个数进行从大到小排序，取前10大的数进行缓存，第二个batch的输入1000个数进行从大到小排序，取前10个数，与缓存的10个数共20个数进行排序，取前10大的数，以此类推，最终获得所有batch数据中前10大的数。",{"type":18,"tag":26,"props":1556,"children":1557},{},[1558],{"type":24,"value":1559},"Inference有两种方式，第一种是数据不做量化进行推理，第二种是数据量化后进行推理。数据量化是指算子的当前batch输入数据使用当前batch的max和min进行量化，将数据量化为INT8，再由INT8转回FP32进行inference计算。",{"type":18,"tag":26,"props":1561,"children":1562},{},[1563],{"type":24,"value":1564},"第二次inference，进行数据的直方统计。根据第2步已经根据max_percentile找到需要的max的值。",{"type":18,"tag":26,"props":1566,"children":1567},{},[1568],{"type":24,"value":1569},"最后，计算分布差异指标，根据指标选择最好的min和max，然后计算出数据的scale和offset，根据数据和权值的scale，进行bias量化。",{"type":18,"tag":26,"props":1571,"children":1572},{},[1573],{"type":18,"tag":40,"props":1574,"children":1575},{},[1576],{"type":24,"value":1577},"c. 搜索空间",{"type":18,"tag":26,"props":1579,"children":1580},{},[1581],{"type":24,"value":1582},"影响搜索空间的参数可以分为两类：一类是增加搜索组合的数量的可以称之为配置参数，一类是影响单个模型结果的称之为超参数（不增加结果数量）。",{"type":18,"tag":130,"props":1584,"children":1586},{"id":1585},"端测量化推理",[1587],{"type":18,"tag":40,"props":1588,"children":1589},{},[1590],{"type":18,"tag":40,"props":1591,"children":1592},{},[1593],{"type":24,"value":1585},{"type":18,"tag":26,"props":1595,"children":1596},{},[1597],{"type":24,"value":1598},"端侧量化推理的结构方式主要由3种，分别是下图的(a) Fp32输入Fp32输出、(b) Fp32输入int8输出、(c) int8输入int32输出。",{"type":18,"tag":26,"props":1600,"children":1601},{},[1602],{"type":18,"tag":30,"props":1603,"children":1605},{"alt":7,"src":1604},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/b1b5ea7a1aaa48468562fe93c8c03920.jpg",[],{"type":18,"tag":26,"props":1607,"children":1608},{},[1609],{"type":24,"value":1610},"INT8卷积示意图，里面混合里三种不同的模式，因为不同的卷积通过不同的方式进行拼接。",{"type":18,"tag":26,"props":1612,"children":1613},{},[1614],{"type":18,"tag":30,"props":1615,"children":1617},{"alt":7,"src":1616},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/0764585cb8dc4decb6e4c63a41bdbb1b.jpg",[],{"type":18,"tag":26,"props":1619,"children":1620},{},[1621],{"type":24,"value":1622},"使用INT8进行inference时，由于数据是实时的，因此数据需要在线量化，量化的流程如图所示。数据量化涉及Quantize，Dequantize和Requantize等3种操作。",{"type":18,"tag":46,"props":1624,"children":1626},{"id":1625},"_1quantize量化",[1627],{"type":18,"tag":40,"props":1628,"children":1629},{},[1630],{"type":24,"value":1631},"1、Quantize量化",{"type":18,"tag":26,"props":1633,"children":1634},{},[1635],{"type":24,"value":1636},"将float32数据量化为int8。离线转换工具转换的过程之前，计算出数据量化需要的scale和offset，如果采用对称量化算法，则根据公式4进行数据量化，如果采用非对称量化算法，则根据本文公式6进行数据量化。",{"type":18,"tag":46,"props":1638,"children":1640},{"id":1639},"_2dequantize反量化",[1641],{"type":18,"tag":40,"props":1642,"children":1643},{},[1644],{"type":24,"value":1645},"2、Dequantize反量化",{"type":18,"tag":26,"props":1647,"children":1648},{},[1649],{"type":24,"value":1650},"INT8相乘、加之后的结果用INT32格式存储，如果下一Operation需要float32格式数据作为输入，则通过Dequantize反量化操作将INT32数据反量化为float32。",{"type":18,"tag":26,"props":1652,"children":1653},{},[1654],{"type":24,"value":1655},"Dequantize反量化推导过程如下：",{"type":18,"tag":26,"props":1657,"children":1658},{},[1659],{"type":18,"tag":30,"props":1660,"children":1662},{"alt":7,"src":1661},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/e876b1a4461e4c3db35cbeb123f93633.png",[],{"type":18,"tag":26,"props":1664,"children":1665},{},[1666,1668,1672,1674,1678,1680,1684],{"type":24,"value":1667},"因此反量化需要input和weight的scale。值得注意的是，后面实际的公式约减了",{"type":18,"tag":30,"props":1669,"children":1671},{"alt":7,"src":1670},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/8a298593e2d0407e98e4df47b57299e0.png",[],{"type":24,"value":1673},"、",{"type":18,"tag":30,"props":1675,"children":1677},{"alt":7,"src":1676},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/7618c2476a34448fbcd7fd5c4b2d0ca7.png",[],{"type":24,"value":1679},"和",{"type":18,"tag":30,"props":1681,"children":1683},{"alt":7,"src":1682},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/28e3c3aa1ce64c349a8edc9ed12a88da.png",[],{"type":24,"value":1685},"，是因为在实际网络的运行和测试过程结果表明，发现这两个参数对网络模型的精度并不影响，其作用相当于bias add，因此故作减约。",{"type":18,"tag":46,"props":1687,"children":1689},{"id":1688},"_3requantize重量化",[1690],{"type":24,"value":1691},"3、Requantize重量化",{"type":18,"tag":26,"props":1693,"children":1694},{},[1695],{"type":24,"value":1696},"INT8乘加之后的结果用INT32格式存储，如果下一层需要INT8格式数据作为输入，则通过Requantize重量化操作将INT32数据重量化为INT8。",{"type":18,"tag":26,"props":1698,"children":1699},{},[1700],{"type":24,"value":1701},"重量化推导过程如下：",{"type":18,"tag":26,"props":1703,"children":1704},{},[1705],{"type":18,"tag":30,"props":1706,"children":1708},{"alt":7,"src":1707},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/80742360504447a78526fd2d5394e8e1.png",[],{"type":18,"tag":26,"props":1710,"children":1711},{},[1712,1714],{"type":24,"value":1713},"其中y为下一个节点的输入，即",{"type":18,"tag":30,"props":1715,"children":1717},{"alt":7,"src":1716},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/c006a11b393c433ebccf77d19fdebf99.png",[],{"type":18,"tag":26,"props":1719,"children":1720},{},[1721],{"type":18,"tag":30,"props":1722,"children":1724},{"alt":7,"src":1723},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/11f20ca221e744e1b2fefd36caea93e6.png",[],{"type":18,"tag":26,"props":1726,"children":1727},{},[1728],{"type":24,"value":1729},"有：",{"type":18,"tag":26,"props":1731,"children":1732},{},[1733],{"type":18,"tag":30,"props":1734,"children":1736},{"alt":7,"src":1735},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/532fad3e51b74d70b6992bc37f53ee12.png",[],{"type":18,"tag":26,"props":1738,"children":1739},{},[1740],{"type":24,"value":1741},"因此重量化需要本Operation输入input和weight的scale，以及下一Operation的input输入数据的scale和offset。",{"type":18,"tag":46,"props":1743,"children":1745},{"id":1744},"_4conv2d的int8计算",[1746],{"type":18,"tag":40,"props":1747,"children":1748},{},[1749],{"type":24,"value":1750},"4、Conv2d的INT8计算",{"type":18,"tag":26,"props":1752,"children":1753},{},[1754],{"type":24,"value":1755},"计算公式如下：",{"type":18,"tag":26,"props":1757,"children":1758},{},[1759],{"type":18,"tag":30,"props":1760,"children":1762},{"alt":7,"src":1761},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/363c08ad0f604bd0a8e9f01431998c5a.png",[],{"type":18,"tag":26,"props":1764,"children":1765},{},[1766],{"type":24,"value":1767},"其中，P为与w矩阵相同大小的全1矩阵，Q为与x矩阵相同大小的全1矩阵",{"type":18,"tag":26,"props":1769,"children":1770},{},[1771],{"type":18,"tag":30,"props":1772,"children":1774},{"alt":7,"src":1773},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/8ef3ff35a8eb4afd962bed6e9fdeab1a.png",[],{"type":18,"tag":26,"props":1776,"children":1777},{},[1778],{"type":24,"value":1779},"其中， Wint是量化后INT8权值，Xint是量化后INT8数据， Wbias是量化后INT8数据， 是量化后INT32的bias。",{"type":18,"tag":26,"props":1781,"children":1782},{},[1783],{"type":24,"value":1784},"c3项可以在离线时计算好，作为新的bias参数传入。c1和c2项需要在线计算。Conv2d的INT8计算根据输入是float32还是int8决定是否需要插入Quantize OP，根据输出是float32还是int8决定输出是插入Dequantize OP还是Requantize OP。",{"type":18,"tag":26,"props":1786,"children":1787},{},[1788],{"type":18,"tag":40,"props":1789,"children":1790},{},[1791],{"type":24,"value":1792},"欢迎投稿",{"type":18,"tag":26,"props":1794,"children":1795},{},[1796],{"type":18,"tag":40,"props":1797,"children":1798},{},[1799,1801,1809],{"type":24,"value":1800},"欢迎大家踊跃投稿，有想投稿技术干货、项目经验等分享的同学，可以添加MindSpore官方小助手：小猫子（",{"type":18,"tag":40,"props":1802,"children":1803},{},[1804],{"type":18,"tag":40,"props":1805,"children":1806},{},[1807],{"type":24,"value":1808},"mindspore0328",{"type":24,"value":1810},"）的微信，告诉猫哥哦！",{"type":18,"tag":26,"props":1812,"children":1813},{},[1814,1819,1821,1826,1828,1836],{"type":18,"tag":40,"props":1815,"children":1816},{},[1817],{"type":24,"value":1818},"昇思MindSpore官方交流QQ群 :",{"type":24,"value":1820}," ",{"type":18,"tag":40,"props":1822,"children":1823},{},[1824],{"type":24,"value":1825},"486831414",{"type":24,"value":1827},"**（",{"type":18,"tag":40,"props":1829,"children":1830},{},[1831],{"type":18,"tag":40,"props":1832,"children":1833},{},[1834],{"type":24,"value":1835},"群里有很多技术大咖助力答疑！",{"type":24,"value":1837},"）**",{"type":18,"tag":26,"props":1839,"children":1840},{},[1841],{"type":18,"tag":30,"props":1842,"children":1844},{"alt":7,"src":1843},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/10/d91dda12c58f49e78a71645d13108aa5.jpg",[],{"type":18,"tag":26,"props":1846,"children":1847},{},[1848],{"type":18,"tag":40,"props":1849,"children":1850},{},[1851],{"type":24,"value":1852},"官方QQ群号 : 486831414",{"type":18,"tag":26,"props":1854,"children":1855},{},[1856],{"type":18,"tag":40,"props":1857,"children":1858},{},[1859],{"type":24,"value":1860},"微信小助手：mindspore0328",{"type":18,"tag":46,"props":1862,"children":1864},{"id":1863},"扫描下方二维码加入mindspore项目",[1865],{"type":24,"value":1866},"扫描下方二维码加入MindSpore项目",{"type":18,"tag":26,"props":1868,"children":1869},{},[1870],{"type":18,"tag":30,"props":1871,"children":1873},{"alt":7,"src":1872},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/09/984c702452434b10817b9084c6a2cee6.jpg",[],{"type":18,"tag":46,"props":1875,"children":1877},{"id":1876},"_1",[],{"type":18,"tag":26,"props":1879,"children":1880},{},[1881],{"type":18,"tag":1882,"props":1883,"children":1887},"a",{"href":1884,"rel":1885},"http://mp.weixin.qq.com/s?__biz=MzAxMDA1MDM0NQ==&mid=2247541446&idx=1&sn=f397c4aadf6313efe8c16d15cec8db3d&chksm=9b542c80ac23a596935839754dc4690c6c0a20319c1bb23c67bc3bd7b527c8cf6d3c1efcc9a9&scene=21#wechat_redirect",[1886],"nofollow",[1888],{"type":24,"value":1889},"MindSpore官方资料",{"type":18,"tag":26,"props":1891,"children":1892},{},[1893],{"type":18,"tag":1882,"props":1894,"children":1896},{"href":1884,"rel":1895},[1886],[1897],{"type":24,"value":1898},"GitHub : https://github.com/mindspore-ai/mindspore",{"type":18,"tag":26,"props":1900,"children":1901},{},[1902],{"type":18,"tag":1882,"props":1903,"children":1905},{"href":1884,"rel":1904},[1886],[1906],{"type":24,"value":1907},"Gitee : https : //gitee.com/mindspore/mindspore",{"type":18,"tag":26,"props":1909,"children":1910},{},[1911],{"type":18,"tag":1882,"props":1912,"children":1914},{"href":1884,"rel":1913},[1886],[1915],{"type":24,"value":1916},"官方QQ群 : 486831414",{"title":7,"searchDepth":994,"depth":994,"links":1918},[1919,1920,1921,1922,1923,1924,1925,1926,1929,1934],{"id":48,"depth":972,"text":48},{"id":59,"depth":972,"text":62},{"id":65,"depth":972,"text":68},{"id":7,"depth":972,"text":7},{"id":120,"depth":972,"text":120},{"id":132,"depth":950,"text":138},{"id":210,"depth":950,"text":216},{"id":453,"depth":950,"text":453,"children":1927},[1928],{"id":704,"depth":972,"text":710},{"id":1295,"depth":950,"text":1295,"children":1930},[1931,1932,1933],{"id":1311,"depth":972,"text":1317},{"id":1330,"depth":972,"text":1336},{"id":1466,"depth":972,"text":1472},{"id":1585,"depth":950,"text":1585,"children":1935},[1936,1937,1938,1939,1940,1941],{"id":1625,"depth":972,"text":1631},{"id":1639,"depth":972,"text":1645},{"id":1688,"depth":972,"text":1691},{"id":1744,"depth":972,"text":1750},{"id":1863,"depth":972,"text":1866},{"id":1876,"depth":972,"text":7},"markdown","content:technology-blogs:zh:937.md","content","technology-blogs/zh/937.md","technology-blogs/zh/937","md",1776506142143]