当前位置: 首页 > wzjs >正文

资讯网站模板带会员投稿功能百度搜索引擎提交入口

资讯网站模板带会员投稿功能,百度搜索引擎提交入口,国内十大电商平台排名,外贸找客户平台本节我们将基于pytorch来实测量化的实现,pytorch基于quantize_per_tensor_dynamic函数可以实现动态量化,我们自己写个量化函数然后和pytorch对比来看其是如何实现的: import torch import numpy as np import math #自己写的动态量化函数&am…

本节我们将基于pytorch来实测量化的实现,pytorch基于quantize_per_tensor_dynamic函数可以实现动态量化,我们自己写个量化函数然后和pytorch对比来看其是如何实现的:

import torch
import numpy as np
import math
#自己写的动态量化函数,与pytorch自带quantize_tensor函数做对比def quantize_tensor(array, num_bits=8):highB = array.max()lowB = array.min()rangeB = highB - lowBshiftDist = -(highB + lowB) / 2qmax = 2.**num_bits - 1.zero_point = shiftDist / rangeB * qmax;#if zero_point < 0:#    zero_point = zero_point - 1.0print(zero_point)zero_point = zero_point.floor().int()scale = rangeB / qmaxq_x = array/scale + zero_pointq_x = q_x.round().int()return q_x, zero_point, scalex1 = torch.randn(1, 10, dtype=torch.float32)
x2 = torch.randn(10, 10, dtype=torch.float32)
xq1 = torch.quantize_per_tensor_dynamic(x1, dtype=torch.qint8, reduce_range = False)
xq2 = torch.quantize_per_tensor_dynamic(x2, dtype=torch.qint8, reduce_range = False)
print('************martrix value**************')
print(x1)
#calcScaleAndZero(x2.numpy())
print(x2)
print('************scale**********************')
scale1 = xq1.q_scale()
scale2 = xq2.q_scale()
print(scale1)
print(scale2)
print('************zero point**********************')
zpoint1 = xq1.q_zero_point()
zpoint2 = xq2.q_zero_point()
print(zpoint1)
print(zpoint2)
print('*************calc quant and zero point*********************')q1, z1, s1 = quantize_tensor(x1)
print(q1, z1, s1)q2, z2, s2 = quantize_tensor(x2)
print(q2, z2, s2)xquant1 = xq1.int_repr().int()
xquant2 = xq2.int_repr().int()
print('************quant**********************')
print(xquant1)
print(xquant2)print('************mult result****************')
multZ = torch.matmul(xquant1 - zpoint1, xquant2 - zpoint2)
print(multZ)
print(scale1)
print(scale2)
print('quant result:')
print(multZ*scale1*scale2)
realResult = torch.matmul(x1,x2)
print('real result')
print(realResult)

结果如下:

************martrix value**************
tensor([[ 1.2477,  0.0531,  0.7887, -1.9008,  0.0422,  0.0558,  2.1269, -0.5745,-1.1107, -0.9602]])
tensor([[-0.0498, -1.9346,  1.1775, -0.2848,  1.9393,  0.1473, -0.6528,  1.4783,-1.0426, -0.1134],[-1.4242, -1.1538, -1.0923,  0.7910, -0.8136,  0.2567,  0.7243,  2.5828,-0.5604,  0.1569],[ 0.4030,  0.2074,  1.6686, -0.0956,  1.3616, -0.1492,  1.0531, -0.6623,-1.1229, -1.9445],[-0.3417,  0.4932,  1.1417,  0.0104,  0.2803, -0.1214, -0.2549, -1.2193,0.8666, -0.9464],[-0.6474, -0.9055, -1.0907, -0.8223, -1.3726,  0.2854, -0.3068, -0.7960,0.3766,  0.9145],[ 0.4355,  0.3613, -1.0598,  0.8375, -0.6023,  0.6905, -0.4290,  0.7039,0.5284,  1.2257],[ 1.5872,  0.2304,  0.8338, -1.7823,  2.5621,  0.4503, -0.2524, -0.5032,1.1579, -0.4619],[-0.3367,  0.9936, -0.9854, -0.9287, -0.2374,  2.7017,  0.3184, -0.1240,0.8407, -1.0258],[-0.3044,  0.3404, -3.9793, -0.0676,  1.1238, -0.1845, -1.1807, -1.3403,0.3283,  1.3031],[ 0.5594, -0.8091,  0.5098,  0.7334, -0.1245,  0.5204, -0.0674, -0.6535,0.3256, -0.3021]])
************scale**********************
0.015794984967100852
0.026200167338053384
************zero point**********************
-8
24
*************calc quant and zero point*********************
tensor(-7.1556)
tensor([[  71,   -5,   42, -128,   -5,   -4,  127,  -44,  -78,  -69]],dtype=torch.int32) tensor(-8, dtype=torch.int32) tensor(0.0158)
tensor(24.3810)
tensor([[  22,  -50,   69,   13,   98,   30,   -1,   80,  -16,   20],[ -30,  -20,  -18,   54,   -7,   34,   52,  123,    3,   30],[  39,   32,   88,   20,   76,   18,   64,   -1,  -19,  -50],[  11,   43,   68,   24,   35,   19,   14,  -23,   57,  -12],[  -1,  -11,  -18,   -7,  -28,   35,   12,   -6,   38,   59],[  41,   38,  -16,   56,    1,   50,    8,   51,   44,   71],[  85,   33,   56,  -44,  122,   41,   14,    5,   68,    6],[  11,   62,  -14,  -11,   15,  127,   36,   19,   56,  -15],[  12,   37, -128,   21,   67,   17,  -21,  -27,   37,   74],[  45,   -7,   43,   52,   19,   44,   21,   -1,   36,   12]],dtype=torch.int32) tensor(24, dtype=torch.int32) tensor(0.0262)
************quant**********************
tensor([[  71,   -5,   42, -128,   -5,   -4,  127,  -44,  -78,  -69]],dtype=torch.int32)
tensor([[  22,  -50,   69,   13,   98,   30,   -1,   80,  -16,   20],[ -30,  -20,  -18,   54,   -7,   34,   52,  123,    3,   30],[  39,   32,   88,   20,   76,   18,   64,   -1,  -19,  -50],[  11,   43,   68,   24,   35,   19,   14,  -23,   57,  -12],[  -1,  -11,  -18,   -7,  -28,   35,   12,   -6,   38,   59],[  41,   38,  -16,   56,    1,   50,    8,   51,   44,   71],[  85,   33,   56,  -44,  122,   41,   14,    5,   68,    6],[  11,   62,  -14,  -11,   15,  127,   36,   19,   56,  -15],[  12,   37, -128,   21,   67,   17,  -21,  -27,   37,   74],[  45,   -7,   43,   52,   19,   44,   21,   -1,   36,   12]],dtype=torch.int32)
************mult result****************
tensor([[ 10245,  -7079,  16232, -10362,  17634,  -1202,   2760,  11839,  -6065,-3179]], dtype=torch.int32)
0.015794984967100852
0.026200167338053384
quant result:
tensor([[ 4.2397, -2.9295,  6.7173, -4.2881,  7.2975, -0.4974,  1.1422,  4.8993,-2.5099, -1.3156]])
real result
tensor([[ 4.1968, -2.9492,  6.7218, -4.2829,  7.2829, -0.5282,  1.1583,  4.8998,-2.5157, -1.3109]])

可以通过我们给的自定义函数quantize_tensor看出动态量化的原理,最后我们比较了量化矩阵相乘后的结果,可以看到我们可以保证小数点后一位的精度,当然精度取决于我们的scale的大小,越小精度越高,当然随之而来的要求数据的取值范围要小,否则会出现溢出的情况。


文章转载自:

http://PZN7YEZ0.ngpLy.cn
http://yLNcWsQE.ngpLy.cn
http://SOGKTdfD.ngpLy.cn
http://jjTxCKPW.ngpLy.cn
http://OSjgbcBd.ngpLy.cn
http://gXt21aPV.ngpLy.cn
http://v4AJi1d7.ngpLy.cn
http://fW1yK9YN.ngpLy.cn
http://s3pEFvev.ngpLy.cn
http://Ss4rdyHg.ngpLy.cn
http://yy2KkpoH.ngpLy.cn
http://un7KI9ms.ngpLy.cn
http://ltokCboc.ngpLy.cn
http://34s1QtZj.ngpLy.cn
http://8Ly9iRsl.ngpLy.cn
http://pZaT2Li5.ngpLy.cn
http://LZpagRT4.ngpLy.cn
http://SeAdoo19.ngpLy.cn
http://2jVh3BUy.ngpLy.cn
http://b7sdy5Z7.ngpLy.cn
http://LlVwDh9X.ngpLy.cn
http://MrhThcr6.ngpLy.cn
http://QLKwkdJI.ngpLy.cn
http://CGYHn9XA.ngpLy.cn
http://YKCaWHkg.ngpLy.cn
http://mYYrqXeb.ngpLy.cn
http://FbetGt6B.ngpLy.cn
http://rIIdP6ZF.ngpLy.cn
http://b0KgyBYu.ngpLy.cn
http://NJXcRKKo.ngpLy.cn
http://www.dtcms.com/wzjs/617439.html

相关文章:

  • 网站维护机构自己做视频网站用cdn那个便宜
  • 易语言做网站源码迎访问备案网站管理系统
  • 做淘口令网站wordpress改变访问目录
  • 如何对自己建设的网站进行推广试玩无限刷一天赚500
  • 网站建设图片教程线上直播营销策划方案
  • 建立网站目录结构时应该注意哪几个方面个人网页设计模板教程
  • wordpress免费建站吗徐州市制作网站
  • 垫江网站建设网站建设能挣钱
  • php怎么做多个网站六安网站优化
  • 网站设计方案怎么做计算机网站开发参考文献
  • 中山建设网站的公司四川省建设网站建筑电工
  • 大型网站制作小程序网站优化外包费用
  • 长沙企业网站seo石家庄网站制作
  • 松江区网站建设公司电商培训在线课程
  • 做网站做推广3d溜溜网室内设计图库
  • 校友会网站建设方案网站运营这么做
  • 网站建设时应该做的优化百度推广代理公司
  • 濮阳seo网站建设wordpress 建站 pdf
  • 江苏景禾瑜博建设工程有限公司网站深圳 网站设计公司价格
  • 建站有哪些需求公司企业邮箱优势
  • 建设银行网站怎么登陆网站悬浮窗广告
  • zencart 网站从哪个网站设置宽带主机
  • 前端开发培训多少钱百度seo关键词排名技术
  • wap网站制作方案怎么做网页链接跳转
  • 重庆专业网站设计服务一个月捞偏门可挣20万
  • 企业网站开发项目策划书绵阳欣城建设
  • 手机网站建设可行性分析福州在线
  • 做公司网站需要服务器吗财务公司网站建设
  • 有经验的武进网站建设中山建设公司网站
  • 戴尔网站建设成功的关键网站建设后预期推广方式