中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

b2b電子商務(wù)的模式有哪些seo是指什么崗位

b2b電子商務(wù)的模式有哪些,seo是指什么崗位,互聯(lián)網(wǎng)軟件開發(fā)是什么工作,佛山網(wǎng)站快照優(yōu)化公司使用Pytorch訓(xùn)練出的模型權(quán)重為fp32,部署時(shí),為了加快速度,一般會(huì)將模型量化至int8。與fp32相比,int8模型的大小為原來的1/4, 速度為2~4倍。 Pytorch支持三種量化方式: 動(dòng)態(tài)量化(Dynamic Quantization&…

使用Pytorch訓(xùn)練出的模型權(quán)重為fp32,部署時(shí),為了加快速度,一般會(huì)將模型量化至int8。與fp32相比,int8模型的大小為原來的1/4, 速度為2~4倍。
Pytorch支持三種量化方式:

  • 動(dòng)態(tài)量化(Dynamic Quantization): 只量化權(quán)重,激活在推理過程中進(jìn)行量化
  • 靜態(tài)量化(Static Quantization): 量化權(quán)重和激活
  • 量化感知訓(xùn)練(Quantization Aware Training,QAT): 插入量化算子后進(jìn)行訓(xùn)練,主要在靜態(tài)量化精度不滿足需求時(shí)進(jìn)行。
    大多數(shù)情況下,我們只需要進(jìn)行靜態(tài)量化,少數(shù)情況下在量化感知訓(xùn)練不滿足時(shí)使用QAT進(jìn)行微調(diào)。所以本篇只重點(diǎn)講靜態(tài)量化,并且理論部分先略過(后面再專門總結(jié)),只關(guān)注實(shí)操。
    注:下面的代碼是在pytorch1.10下,后面Pytorch對(duì)量化的接口有調(diào)整
    官方文檔:Quantization — PyTorch 1.10 documentation

動(dòng)態(tài)模式(Eager Mode)與靜態(tài)模式(fx graph)

Pytorch支持用2種方式量化,一種是動(dòng)態(tài)圖模式,也是我們?nèi)粘J褂肞ytorch訓(xùn)練所使用的方式,使用這種方式量化需要自己手動(dòng)修改網(wǎng)絡(luò)結(jié)構(gòu),在支持量化的算子前、后插入量化節(jié)點(diǎn),優(yōu)點(diǎn)是方便調(diào)試。靜態(tài)模式則是由pytorch自動(dòng)在計(jì)算圖中插入量化節(jié)點(diǎn),不需要手動(dòng)修改網(wǎng)絡(luò)。
網(wǎng)絡(luò)上大部分的教程都是基于靜態(tài)模式,這種方式比較大的問題就是需要手動(dòng)修改網(wǎng)絡(luò)結(jié)構(gòu),官方教程里的網(wǎng)絡(luò)是屬于demo型, 其中的QuantStub和DeQuantStub就分別是量化和反量化的節(jié)點(diǎn):

# define a floating point model where some layers could be statically quantized
class M(torch.nn.Module):def __init__(self):super(M, self).__init__()# QuantStub converts tensors from floating point to quantizedself.quant = torch.quantization.QuantStub()self.conv = torch.nn.Conv2d(1, 1, 1)self.relu = torch.nn.ReLU()# DeQuantStub converts tensors from quantized to floating pointself.dequant = torch.quantization.DeQuantStub()def forward(self, x):# manually specify where tensors will be converted from floating# point to quantized in the quantized modelx = self.quant(x)x = self.conv(x)x = self.relu(x)# manually specify where tensors will be converted from quantized# to floating point in the quantized modelx = self.dequant(x)return x

Pytorch對(duì)于很多網(wǎng)絡(luò)層是不支持量化的(比如很常用的Prelu),如果我們用這種方式,我們就必須在這些不支持的層前面插入DeQuantStub,然后在支持的層前面插入QuantStub。筆者體驗(yàn)下來,體驗(yàn)很差,個(gè)人覺得不太實(shí)用,會(huì)破壞原來的網(wǎng)絡(luò)結(jié)構(gòu)。
而靜態(tài)圖模式,我們只需要調(diào)用Pytorch提供的接口將原模型轉(zhuǎn)換一下即可,不需要修改原來的網(wǎng)絡(luò)結(jié)構(gòu)文件,個(gè)人認(rèn)為實(shí)用性更強(qiáng)。
image.png

靜態(tài)模式量化

1. 載入fp32模型,并轉(zhuǎn)成fx graph

其中量化參數(shù)有‘fbgemm’和‘qnnpack’兩種,前者在x86運(yùn)行,后者在arm運(yùn)行。

model_fp32 = torch.load(xxx)
model_fp32_quantize = copy.deepcopy(model_fp32)
qconfig_dict = {"": torch.quantization.get_default_qconfig('fbgemm')}
model_fp32_quantize.eval()
# preparemodel_prepared = quantize_fx.prepare_fx(model_fp32_quantize, qconfig_dict)
model_prepared.eval()

2.讀取量化數(shù)據(jù),標(biāo)定(Calibration)量化參數(shù)

標(biāo)定的過程就是使用模型推理量化圖片,然后統(tǒng)計(jì)權(quán)重和激活分布,從而得到量化參數(shù)。量化圖片一般來源于訓(xùn)練集(幾百張左右,根據(jù)測(cè)試情況調(diào)整)。量化圖片可以通過Pytorch的Dataloader讀取,也可以直接自行實(shí)現(xiàn)讀圖片然后送入網(wǎng)絡(luò)。

### 使用dataloader讀取
for i, (data, label) in enumerate(train_loader):data = data.to(torch.device("cpu:0"))outputs = model_prepared(data)print("calibrating {}".format(i))if i > 1000:break

3. 轉(zhuǎn)換為量化模型并保存

quantized_model = quantize_fx.convert_fx(model_prepared)
torch.jit.save(torch.jit.script(quantized_model), "quantized_model.pt")

速度測(cè)試

量化后的模型使用方法與fp32模型一樣:

import torch
import cv2
import numpy as np
torch.set_num_threads(1)fused_model = torch.jit.load("jit_model.pt")
fused_model.eval()
fused_model.to(torch.device("cpu:0"))img = cv2.imread("./1.png")
img_fp32 = img.astype(np.float32)
img_fp32 = (img_fp32-127.5) / 127.5
input = torch.from_numpy(img).permute(2, 0, 1).unsqueeze(0).float()def speed_test(model, input):# warm upfor i in range(10):model(input)import timestart = time.time()for i in range(100):model(input)end = time.time()print("model time: ", (end-start)/100)time.sleep(10)# quantized model
quantized_model= torch.jit.load("quantized_model.pt")
quantized_model.eval()
quantized_model.to(torch.device("cpu:0"))speed_test(fused_model, input)
speed_test(quantized_model, input)

實(shí)測(cè)fp32模型單核運(yùn)行120ms, 量化后47ms

結(jié)語

本文介紹了fx graph模式下的Pytorch的PTSQ方法,并實(shí)測(cè)了一個(gè)模型,效果還比較不錯(cuò)。
1_995567224_161_79_3_732056265_62005da0d7c1b531a6cf91ea587d312e.jpg

http://www.risenshineclean.com/news/62761.html

相關(guān)文章:

  • 嘉興快速建站合作阿里云域名注冊(cè)官網(wǎng)網(wǎng)址
  • 做的網(wǎng)站百度沒收錄關(guān)鍵詞舉例
  • 怎么看網(wǎng)站是用什么程序做的百度網(wǎng)頁電腦版入口
  • 簡單網(wǎng)頁html模板西安的網(wǎng)絡(luò)優(yōu)化公司
  • 網(wǎng)站建設(shè) 網(wǎng)址導(dǎo)航網(wǎng)站seo啥意思
  • 常州免費(fèi)網(wǎng)站制作百度推廣登錄后臺(tái)登錄入口
  • 做外包胡it網(wǎng)站有哪些網(wǎng)頁設(shè)計(jì)公司
  • 建個(gè)可以注冊(cè)會(huì)員網(wǎng)站多少錢陽泉seo
  • 做網(wǎng)站運(yùn)維應(yīng)該看的書朋友圈產(chǎn)品推廣文案
  • 玉林市城鄉(xiāng)建設(shè)委員會(huì)網(wǎng)站什么軟件可以免費(fèi)引流
  • 百度站長工具鏈接提交長沙優(yōu)化網(wǎng)站
  • 網(wǎng)站建設(shè)銷售好做合肥seo網(wǎng)站排名
  • 網(wǎng)站運(yùn)營團(tuán)隊(duì)成人大專
  • 重慶政府是指什么廣州網(wǎng)站運(yùn)營專業(yè)樂云seo
  • 大連模版網(wǎng)站seo服務(wù)優(yōu)化
  • wordpress插件小蜜蜂seo的形式有哪些
  • 給網(wǎng)站做排名優(yōu)化學(xué)什么好處百度數(shù)據(jù)查詢
  • 免費(fèi)建網(wǎng)站域名新聞?lì)^條新聞
  • wordpress 后臺(tái)列表惠東seo公司
  • 有哪些網(wǎng)站結(jié)構(gòu)是不合理的企業(yè)網(wǎng)站注冊(cè)
  • 寧波網(wǎng)站建設(shè)使用技巧分享陜西seo優(yōu)化
  • 做分析圖用的地圖網(wǎng)站免費(fèi)創(chuàng)建網(wǎng)站的平臺(tái)
  • 網(wǎng)站開發(fā)公司加盟seo資料網(wǎng)
  • 大淘客網(wǎng)站如何做制作常用的網(wǎng)絡(luò)推廣手段有哪些
  • 博星卓越電子商務(wù)網(wǎng)站建設(shè)實(shí)訓(xùn)平臺(tái)服裝品牌策劃及營銷推廣方案
  • 專業(yè)做二手房的網(wǎng)站有哪些安徽網(wǎng)站seo
  • 深圳多語言網(wǎng)站建設(shè)長沙弧度seo
  • js怎么做打開網(wǎng)站就復(fù)制內(nèi)容網(wǎng)絡(luò)營銷推廣方式案例
  • 建網(wǎng)站業(yè)務(wù)員百度網(wǎng)絡(luò)科技有限公司
  • 打開這個(gè)網(wǎng)站你會(huì)回來感謝我的汕頭網(wǎng)站排名優(yōu)化