Dynamic Quantization PyTorch官方教程学习笔记

诸神缄默不语-个人CSDN博文目录

本文是PyTorch的教程Dynamic Quantization — PyTorch Tutorials 1.11.0+cu102 documentation的学习笔记。事实上由于我对该领域的不了解,本篇笔记基本上就是翻译+一点点我对其的理解。
本文介绍如何用Dynamic Quantization加速一个LSTM1模型的推理过程。Dynamic Quantization减少了模型权重的尺寸,加速了模型处理过程。
本文代码及全部输出已以jupyter notebook格式展示在GitHub上,可以直接下载运行:all-notes-in-one/dynamicquantization.ipynb at main · PolarisRisingWar/all-notes-in-one(我所使用的环境是Python3.8+PyTorch1.8.2+cuda11.1(cudatoolkit),源是pytorch-lts,但是据我所知,别的大多数PyTorch版本也都支持这套代码的运行)

我后期准备出模型量化相关的博文,到时候应该也会补到这里作为参考资料。

1. 介绍

设计模型时需要权衡一些特征,如调整模型层数、RNN参数量,在准确率和performance(如模型尺寸 和/或 model latency或throughput2)之间进行权衡。这样的改变可能会非常浪费时间和计算资源,因为你需要重复迭代训练过程以得到实验结果。Quantization可以让你一个训练好的模型上做推理任务时,在performance和模型准确率之间实现类似的权衡。
它可以使你的模型尺寸大幅下降,latency显著减少,但是模型准确率下降很少。

2. 什么是Dynamic Quantization?

Quantizing一个网络,是对网络本身进行转换,使其权重和/或激活函数使用更低精度的整数表示。这使model size减小,让CPU/GPU上可以用更高的throughput math operations(这句话我没看懂什么意思,总之应该是表示模型运行更快了)。
在从浮点数转换成整型时,你需要将浮点数乘以某一scale factor,将结果round到一个整数上。不同quantization方法的区别就在于选择scale factor的方法。
正如将在本文中描述的,dynamic quantization的核心思想,就是根据运行时观察到的数据范围来动态地(dynamically)决定激活函数的scale factor。这保证了scale factor是经过调整的,可以保留被观察到数据集尽可能多的信息。
模型参数在转换时已知,它们会被提前转换为INT8格式。
quantized model中的计算使用vectorized INT8 instructions执行。数据累积时一般使用INT16或INT32以防溢出。在下一层被量化、或为输出转化为FP32后,这个高精度值就会被scale回INT8。
Dynamic quantization调整参数的过程是相对自由的,这使它很适宜于被添加到production pipelines,作为部署LSTM模型的一个标准环节。
(注意:本文的介绍有所简化)

  1. 你需要一个小LSTM模型。
  2. 你用随机的hidden state初始化网络。
  3. 你在随机输入上测试网络。
  4. 你要在这个教程中训练网络。
  5. 你将会看到这个网络的quantized版本比我们一开始的floating point版本更小、运算更快。
  6. 你会看到这个网络的输出值几乎和FP32网络的输出值相当,但是我们并不保证在真实的训练好的模型上期望的accuracy loss。(我觉得这个应该是说本教程没有证明在真实网络上,准确率、损失函数这些量化评估指标在转变后依然可以保持原水平
    (对这一点的证明,教程中给出了另一个参考资料,我还没看,以后也准备写相应教程的笔记:(beta) Dynamic Quantization on an LSTM Word Language Model — PyTorch Tutorials 1.11.0+cu102 documentation

3. 代码步骤

  1. Set Up:定义一个简单的LSTM,导入模块,创建一些随机输入张量。
  2. Do the Quantization:初始化一个浮点数的模型,并创建一个它的quantized版。
  3. Look at Model Size:在这一步可以看到模型尺寸有所变小。
  4. Look at Latency:在这一步运行2个模型,比较模型运行时间(latency)。
  5. Look at Accuracy:在这一步运行2个模型,比较模型输出。

3.1 Set Up

# import the modules used here in this recipe
import torch
import torch.quantization
import torch.nn as nn
import copy
import os
import time

# define a very, very simple LSTM for demonstration purposes
# in this case, we are wrapping nn.LSTM, one layer, no pre or post processing
# inspired by
# https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html, by Robert Guthrie
# and https://pytorch.org/tutorials/advanced/dynamic_quantization_tutorial.html
class lstm_for_demonstration(nn.Module):
  """Elementary Long Short Term Memory style model which simply wraps nn.LSTM
     Not to be used for anything other than demonstration.
  """
  def __init__(self,in_dim,out_dim,depth):
     super(lstm_for_demonstration,self).__init__()
     self.lstm = nn.LSTM(in_dim,out_dim,depth)

  def forward(self,inputs,hidden):
     out,hidden = self.lstm(inputs,hidden)
     return out, hidden


torch.manual_seed(29592)  # set the seed for reproducibility

#shape parameters
model_dimension=8
sequence_length=20
batch_size=1
lstm_depth=1

# random data for input
inputs = torch.randn(sequence_length,batch_size,model_dimension)
# hidden is actually is a tuple of the initial hidden state and the initial cell state
hidden = (torch.randn(lstm_depth,batch_size,model_dimension), torch.randn(lstm_depth,batch_size,model_dimension))

3.2 Do the Quantization

在这一部分,我们将运用torch.quantization.quantize_dynamic()函数。
其入参为模型,想要quantize的submodules(如果出现的话),目标datatype,返回一个原模型的quantized版本(nn.Module类)。

 # here is our floating point instance
float_lstm = lstm_for_demonstration(model_dimension, model_dimension,lstm_depth)

# this is the call that does the work
quantized_lstm = torch.quantization.quantize_dynamic(
    float_lstm, {nn.LSTM, nn.Linear}, dtype=torch.qint8
)

# show the changes that were made
print('Here is the floating point version of this module:')
print(float_lstm)
print('')
print('and now the quantized version:')
print(quantized_lstm)

输出:

Here is the floating point version of this module:
lstm_for_demonstration(
  (lstm): LSTM(8, 8)
)

and now the quantized version:
lstm_for_demonstration(
  (lstm): DynamicQuantizedLSTM(8, 8)
)

3.3 Look at Model Size

现在我们已经quantize了模型,将FP32的模型参数替换为INT8(和一些被记录的scale factors),这意味着我们减少了75%左右的模型储存空间。在本文所使用的默认值上的减少会小于75%,但如果你增加模型尺寸(如设置model dimension到80),这个压缩程度就会趋近于25%,因为此时模型尺寸受参数值的影响更大。

#临时储存模型,计算储存空间,然后删除
def print_size_of_model(model, label=""):
    torch.save(model.state_dict(), "temp.p")
    size=os.path.getsize("temp.p")
    print("model: ",label,' \t','Size (KB):', size/1e3)
    os.remove('temp.p')
    return size

# compare the sizes
f=print_size_of_model(float_lstm,"fp32")
q=print_size_of_model(quantized_lstm,"int8")
print("{0:.2f} times smaller".format(f/q))

输出:

model:  fp32  	 Size (KB): 4.051
model:  int8  	 Size (KB): 2.963
1.37 times smaller

可以看到正如本节前文所说,这个压缩程度是大于25%的。

3.4 Look at Latency

quantized模型也会运行更快。这是因为:

  1. 转移参数花费的时间更少。
  2. INT8的运算操作更快。

在这个简易版模型上你就能看到速度的提升(这是文档原话,但其实实验结果是原模型运行更快……),在复杂模型上一般会提升更多。但影响latency的原因很多。

原模型:

print("Floating point FP32")
%timeit float_lstm.forward(inputs, hidden)

输出:

Floating point FP32
830 µs ± 9.39 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

quantized模型:

print("Quantized INT8")
%timeit quantized_lstm.forward(inputs,hidden)

输出:

Quantized INT8
913 µs ± 13.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

3.5 Look at Accuracy

因为模型是随机初始化而非经过训练的,所以我们就不严格计算准确率的改变程度了(因为没有意义)。但是我们可以迅速、简单看一下quantized网络可以输出跟原网络差不多的结果。
更多分析请参考本文末提到的进阶教程。

计算输出的平均值,经比较后发现差异很小:

# run the float model
out1, hidden1 = float_lstm(inputs, hidden)
mag1 = torch.mean(abs(out1)).item()
print('mean absolute value of output tensor values in the FP32 model is {0:.5f} '.format(mag1))

# run the quantized model
out2, hidden2 = quantized_lstm(inputs, hidden)
mag2 = torch.mean(abs(out2)).item()
print('mean absolute value of output tensor values in the INT8 model is {0:.5f}'.format(mag2))

# compare them
mag3 = torch.mean(abs(out1-out2)).item()
print('mean absolute value of the difference between the output tensors is {0:.5f} or {1:.2f} percent'.format(mag3,mag3/mag1*100))

输出:

mean absolute value of output tensor values in the FP32 model is 0.12887 
mean absolute value of output tensor values in the INT8 model is 0.12912
mean absolute value of the difference between the output tensors is 0.00156 or 1.21 percent

4. 教程中提供的其他参考资料

  1. (beta) Dynamic Quantization on an LSTM Word Language Model Tutorial:这一篇我已经计划要撰写学习笔记博文
  2. Quantization API Documentaion
  3. (beta) Dynamic Quantization on BERT:这一篇我已经计划要撰写学习笔记博文
  4. Introduction to Quantization on PyTorch | PyTorch

  1. 我后期计划撰写LSTM模型相关、尤其是在PyTorch上应用的博文,包括后文代码注释中的PyTorch官方教程。此处先留下位置,以后等我写了来补一下作为扩充阅读资料。 ↩︎

  2. model latency是模型每次处理一个单位的数据所需的时间,单位为秒(时间单位)。
    举例来说,在图像分类任务上,GoogleNet模型在Intel CascadeLake上分类一张图像需要0.057秒。
    throughput是模型在单位时间内能够处理的数据量。如每秒图像数。
    此处仅对这两个概念做简单了解,对其更多理解请参考其他参考资料,本文撰写过程中所参考的资料为:
    What is Latency in Machine Learning (ML)?
    PLASTER: A Framework for Deep Learning Performance ↩︎

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸神缄默不语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值