pytorch 模型量化quantization

2023-12-05

pytorch框架提供了三种量化方法,包括:

  • Dynamic Quantization
  • Post-Training Static Quantization(PTQ)
  • Quantization Aware Training(QAT)

此博客结合CIFAR100数据集分类任务,分别采用 Post-Training Static Quantization Quantization Aware Training 对resnet101模型进行量化。

1.workflow

1.1 PTQ

在这里插入图片描述

图片来自 Practical Quantization in PyTorch

1.2 QAT

在这里插入图片描述

图片来自 Practical Quantization in PyTorch

从两张图片来看,PTQ和QAT的差别在于:PTQ量化前使用了calibration data,而QAT则有一个和训练阶段类似的训练过程。

2. demo

2.1 构建resnet101_quantization模型

import torch
import torch.nn as nn
from torch.ao.quantization import QuantStub, DeQuantStub

class ConvBNReLU(nn.Sequential):
    def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
        padding = (kernel_size - 1) // 2
        super(ConvBNReLU, self).__init__(
            nn.Conv2d(in_planes, out_planes, kernel_size, stride,
                      padding, groups=groups, bias=False),
            nn.BatchNorm2d(out_planes, momentum=0.1),
            nn.ReLU(inplace=False)
        )


class ConvBN(nn.Sequential):
    def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
        padding = (kernel_size - 1) // 2
        super(ConvBN, self).__init__(
            nn.Conv2d(in_planes, out_planes, kernel_size, stride,
                      padding, groups=groups, bias=False),
            nn.BatchNorm2d(out_planes, momentum=0.1),
        )


class BottleNeck(nn.Module):
    expansion = 4

    def __init__(self, in_channels, out_channels, stride=1):
        super().__init__()
        self.residual_function = nn.Sequential(
            ConvBNReLU(in_channels, out_channels, kernel_size=1),
            ConvBNReLU(out_channels, out_channels, kernel_size=3),
            ConvBN(out_channels, out_channels *
                   BottleNeck.expansion, kernel_size=1)
        )

        self.shortcut = nn.Sequential()

        if stride != 1 or in_channels != out_channels * BottleNeck.expansion:
            self.shortcut = ConvBN(in_channels, out_channels *
                                   BottleNeck.expansion, kernel_size=1)

        self.skip_add = nn.quantized.FloatFunctional()

    def forward(self, x):
        return nn.ReLU(inplace=True)(self.skip_add.add(self.residual_function(x), self.shortcut(x)))


class ResNet(nn.Module):

    def __init__(self, block, num_block, num_classes=100):
        super().__init__()

        self.in_channels = 64

        self.conv1 = nn.Sequential(
            ConvBNReLU(3, 64, kernel_size=3))

        self.conv2_x = self._make_layer(block, 64, num_block[0], 1)
        self.conv3_x = self._make_layer(block, 128, num_block[1], 2)
        self.conv4_x = self._make_layer(block, 256, num_block[2], 2)
        self.conv5_x = self._make_layer(block, 512, num_block[3], 2)
        self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))

        self.fc = nn.Linear(512 * block.expansion, num_classes)

        self.quant = QuantStub()
        self.dequant = DeQuantStub()

    def _make_layer(self, block, out_channels, num_blocks, stride):

        strides = [stride] + [1] * (num_blocks - 1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_channels, out_channels, stride))
            self.in_channels = out_channels * block.expansion

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.quant(x)
        output = self.conv1(x)
        output = self.conv2_x(output)
        output = self.conv3_x(output)
        output = self.conv4_x(output)
        output = self.conv5_x(output)
        output = self.avg_pool(output)
        output = output.view(output.size(0), -1)
        output = self.fc(output)
        x = self.dequant(x)
        return output
    # Fuse Conv+BN and Conv+BN+Relu modules prior to quantization
    # This operation does not change the numerics

    def fuse_model(self, is_qat=False):
        fuse_modules = torch.ao.quantization.fuse_modules_qat if is_qat else torch.ao.quantization.fuse_modules
        for m in self.modules():
            if type(m) == ConvBNReLU:
                fuse_modules(m, ['0', '1', '2'], inplace=True)

            if type(m) == ConvBN:
                fuse_modules(m, ['0', '1'], inplace=True)
def resnet101():
    """ return a ResNet 101 object
    """
    return ResNet(BottleNeck, [3, 4, 23, 3])

代码改编自 https://github.com/weiaicunzai/pytorch-cifar100

如果要使用quantization,构建的模型和常规模型差别主要在以下内容:


class BottleNeck(nn.Module):
    expansion = 4

    def __init__(self, in_channels, out_channels, stride=1):
        super().__init__()
        ...
        ...
        self.skip_add = nn.quantized.FloatFunctional()

    def forward(self, x):
        return nn.ReLU(inplace=True)(self.skip_add.add(self.residual_function(x), self.shortcut(x)))

这是因为没有直接用于相加的算子。

如果没有这一操作,可能会报如下错误:

NotImplementedError: Could not run 'aten::add.out' with arguments from the 'QuantizedCPU' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). 

另外就是:


class ResNet(nn.Module):
    def __init__(self, block, num_block, num_classes=100):
        super().__init__()
        ...
        ...
        self.quant = QuantStub() #observer
        self.dequant = DeQuantStub()

    def _make_layer(self, block, out_channels, num_blocks, stride):
        ...
        ...

    def forward(self, x):
        x = self.quant(x)
        ...
        ...
        x = self.dequant(x)
        return output
    # Fuse Conv+BN and Conv+BN+Relu modules prior to quantization
    # This operation does not change the numerics

    def fuse_model(self, is_qat=False):
        ...
        ...

即添加observer,以及将Conv+BN 和Conv+BN+Relu 模块融合到一起。

2.2 PTQ

def print_size_of_model(model):
    torch.save(model.state_dict(), "temp.p")
    print('Size (MB):', os.path.getsize("temp.p")/1e6)
    os.remove('temp.p')

qmodel = resnet101()
# qmodel.load_state_dict(torch.load(args.weights))

qmodel = qmodel.to(device)
# # print(qmodel)
qmodel.eval()

print("Size of model befor quantization")
print_size_of_model(qmodel)

num_calibration_batches = 32
qmodel.eval()
# Fuse Conv, bn and relu
qmodel.fuse_model()

# Specify quantization configuration
# Start with simple min/max range estimation and per-tensor quantization of weights
qmodel.qconfig = torch.ao.quantization.default_qconfig
print(qmodel.qconfig)
torch.ao.quantization.prepare(qmodel, inplace=True)

# Calibrate first
print('Post Training Quantization Prepare: Inserting Observers')


# Calibrate with the training set
criterion = nn.CrossEntropyLoss()
evaluate(qmodel, criterion, cifar100_test_loader,
             neval_batches=10)
print('Post Training Quantization: Calibration done')

# Convert to quantized model
torch.ao.quantization.convert(qmodel, inplace=True)
print('Post Training Quantization: Convert done')
# print('\n Inverted Residual Block: After fusion and quantization, note fused modules: \n\n',
#       qmodel.features[1].conv)

print("Size of model after quantization")
print_size_of_model(qmodel)
Size of model befor quantization
Size (MB): 171.40158

Size of model after quantization
Size (MB): 42.970334

size大致缩小了四倍。

经过测试,在本地cpu上推断时间也缩小了3~4倍。

2.3 QAT

#%% QAT
float_model_file="resnet101.pt"
qat_model=resnet101()
qat_model.load_state_dict(torch.load(float_model_file))
qat_model.fuse_model(is_qat=True)

optimizer = torch.optim.SGD(qat_model.parameters(), lr = 0.0001)
# The old 'fbgemm' is still available but 'x86' is the recommended default.
qat_model.qconfig = torch.ao.quantization.get_default_qat_qconfig('x86')
torch.ao.quantization.prepare_qat(qat_model, inplace=True)
# print('Inverted Residual Block: After preparation for QAT, note fake-quantization modules \n',qat_model.features[1].conv)

num_train_batches,num_eval_batches = 2,2
eval_batch_size=32
criterion = nn.CrossEntropyLoss()
nepochs=10

# QAT takes time and one needs to train over a few epochs.
# Train and check accuracy after each epoch
for nepoch in range(nepochs):
    train_one_epoch(qat_model, criterion, optimizer, cifar100_test_loader, torch.device('cpu'),  num_train_batches)
    if nepoch > 3:
        # Freeze quantizer parameters
        qat_model.apply(torch.ao.quantization.disable_observer)
    if nepoch > 2:
        # Freeze batch norm mean and variance estimates
        qat_model.apply(torch.nn.intrinsic.qat.freeze_bn_stats)

    # Check the accuracy after each epoch
    quantized_model = torch.ao.quantization.convert(qat_model.eval(), inplace=False)
    quantized_model.eval()
    top1, top5 = evaluate(quantized_model,criterion, cifar100_test_loader, neval_batches=2)
    print('Epoch %d :Evaluation accuracy on %d images, %2.2f'%(nepoch, num_eval_batches * eval_batch_size, top1.avg))

完整代码后续将分享在github或csdn资源中。

参考文献

[1] Introduction to Quantization on PyTorch
[2] https://github.com/pytorch/pytorch/wiki/Introducing-Quantized-Tensor
[3] tensorflow 训练后量化
[4] pytorch dynamic and static quantization 适用的算子
[5] ★★★pytorch_static quantization tutorial
[6] PyTorch Static Quantization
[7] Practical Quantization in PyTorch
[8] ★★★https://github.com/weiaicunzai/pytorch-cifar100

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pytorch 模型量化quantization 的相关文章

随机推荐

  • linux下gdb的使用以及dump转存文件的生成使用

    1 gdb Linux 调试器 gdb的使用 Linux之gdb的使用 gdb调试工具 如何在多线程 多进程以及正在运行的程序下调试 2 dump文件 c linux dump定位错误 Linux下更改 coredump文件生成路径 lin
  • Node.js爬虫实战:百度图片爬取

    说在前面 网络爬虫是一种自动化工具 能够模拟人类在互联网上浏览和提取信息的行为 它的应用范围广泛 包括数据采集 信息监控 搜索引擎优化等方面 而在数据抓取和处理中 获取图片资源往往是一个常见的需求 本文将介绍如何使用Node js和相关库构
  • 抖音商品详情接口在电商行业中的重要性及实时数据获取实现

    一 引言 抖音作为当下最热门的短视频平台之一 拥有庞大的用户群体和活跃度 为电商行业带来了巨大的商业机会 抖音商品详情接口作为连接抖音平台和电商系统的关键纽带 具有重要的作用 本文将深入探讨抖音商品详情接口在电商行业中的重要性 并介绍如何通
  • 提取音频哪个软件好?揭秘市面上的热门选择

    就像许多人都喜欢在视频中加入动听的音乐来增强情感氛围一样 你有没有想过如果能够将那些打动你的音乐片段单独提取出来 作为自己的手机铃声 那该有多好呢 是的 某个视频中听到了一段难以忘怀的音乐 你可能会迫不及待地想将其设置为自己的专属手机铃声
  • 阿里云服务器有WordPress还可以再安装宝塔面板吗?

    不推荐 宝塔面板要求必须是纯净的操作系统环境安装宝塔 否则可能会有问题的 所以最好是先安装宝塔面板 再去安装wordpress 原文地址 阿里云服务器 WordPress 还可以再安装宝塔面板吗 轻量云Cloud WordPress一款广泛
  • C++简易计数器

    Created by Carlgood Note This program is written in version DEV C 5 11 include
  • 腾讯云用centos还是ubuntu系统好?

    腾讯云服务器提供了多种操作系统选择 包括 CentOS Ubuntu Windows Server 等 用户可以根据自己的需求和习惯选择适合的操作系统 通常比较推荐安装centos 7 x版本的系统 但在 CentOS 和 Ubuntu 之
  • HAL库STM32常用外设教程(二)—— GPIO输入\输出

    HAL库STM32常用外设教程 二 GPIO输入 输出 文章目录 HAL库STM32常用外设教程 二 GPIO输入 输出 前言 一 GPIO功能概述 二 GPIO的HAl库驱动 三 GPIO使用示例 1 示例功能 四 代码讲解 五 总结
  • VBA技术资料MF91:计算机自动执行VBA脚本代码

    我给VBA的定义 VBA是个人小型自动化处理的有效工具 利用好了 可以大大提高自己的工作效率 而且可以提高数据的准确度 我的教程一共九套 分为初级 中级 高级三大部分 是对VBA的系统讲解 从简单的入门 到数据库 到字典 到高级的网抓及类的
  • 计算机毕设项目 - HTML的健身房信息管理系统

    项目背景 随着科学技术的飞速发展 各行各业都在努力与现代先进技术接轨 通过科技手段提高自身的优势 对于健身房信息管理系统当然也不能排除在外 随着网络技术的不断成熟 带动了健身房信息管理系统 它彻底改变了过去传统的管理方式 不仅使服务管理难度
  • python爬虫概述及简单实践

    文章目录 一 先了解用户获取网络数据的方式 二 简单了解网页源代码的组成 1 web基本的编程语言 2 使用浏览器查看网页源代码 三 爬虫概述 1 认识爬虫 2 python爬虫 3 爬虫分类 4 爬虫应用 5 爬虫是一把双刃剑 6 pyt
  • AIGC: 关于ChatGPT中基于API实现一个StreamClient流式客户端

    Java版GPT的StreamClient 可作为其他编程语言的参考 注意 下面包名中的 xxx 可以换成自己的 代码基于java 来源于网络 可修改成其他编程语言实现 参考前文 https blog csdn net Tyro java
  • windows彻底卸载VMware虚拟机

    右键停止这些服务 在任务管理器中也结束vmware 在控制面板中卸载 找到注册表 然后在 software 目录下找到 VMware Inc 然后选中右键删除 确保C盘中也没了
  • VS数据断点的使用

    https blog csdn net chenlycly article details 125626617
  • Ubuntu20.24 安装ecCodes,包括 tar.gz 和 python(笔记)

    这里写目录标题 动机 为此找了解决方案 废话不多说 如下 1 下载 ecCodes 的源文件 网址如下 https confluence ecmwf int display ECC Releases 2 解压包 3 创建 ecCodes 的
  • 默克尔树(Merkle Tree)

    默克尔树 Merkle Tree 是一种哈希树的变体 它是一种有向无环图 DAG 通常用于数据完整性验证 它以密码学家拉尔夫 默克尔的名字命名 是由一系列哈希值构成的树状结构 默克尔树的特点是 它的每个非叶子节点都是其子节点的哈希值的哈希
  • B2B公司如何寻找意向客户的联系方式?

    在B2B公司的营销过程中 少不了寻找意向客户的阶段 这也是销售过程中非常重要的一步 很多新人都是拿到客户联系方式 就直接打电话拜访 俗话说不打没有准备的仗 因此在拜访客户之前就应该做好功课 充分了解客户 这也是B2B业务场景下必做的一环 通
  • SpringCloud | Dubbo 微服务实战——注册中心详解

    前言 作者主页 雪碧有白泡泡 个人网站 雪碧的个人网站 Eureka Nacos Consul Zookeeper在Spring Cloud和Dubbo中实战 引言 在项目开发过程中 随着项目不断扩大 也就是业务的不断增多 我们将采用集群
  • 提升 tomcat 性能的三个方式

    Tomcat是一个流行的Java Web服务器 它可以运行Java Servlet和JavaServer Pages技术 为Web应用程序提供动态内容 Tomcat的性能优化是一个重要的话题 因为它直接影响了Web应用程序的响应速度和并发能
  • pytorch 模型量化quantization

    pytorch 模型量化quantization 1 workflow 1 1 PTQ 1 2 QAT 2 demo 2 1 构建resnet101 quantization模型 2 2 PTQ 2 3 QAT