# -*- coding: utf-8 -*- """ Created on Fri Mar 5 19:13:21 2021 @author: LXM """ import torch import torch.nn as nn from torch.autograd import Function class UpdateRange(nn.Module): def __init__(self, device): super(UpdateRange, self).__init__() self.device = device self.flag = 0 self.fmin = torch.zeros((1), dtype = torch.float32, device = self.device) self.fmax = torch.zeros((1), dtype = torch.float32, device = self.device) def Update(self, fmin, fmax): if self.flag == 0: self.flag = 1 new_fmin = fmin new_fmax = fmax else: new_fmin = torch.min(fmin, self.fmin) new_fmax = torch.max(fmax, self.fmax) self.fmin.copy_(new_fmin) self.fmax.copy_(new_fmax) @torch.no_grad() def forward(self, input): fmin = torch.min(input) fmax = torch.max(input) self.Update(fmin, fmax) class Round(Function): @staticmethod def forward(self, input): # output = torch.round(input) # output = torch.floor(input) output = input.int().float() return output @staticmethod def backward(self, output): input = output.clone() return input class Quantizer(nn.Module): def __init__(self, bits, device): super(Quantizer, self).__init__() self.bits = bits self.scale = 1 self.UpdateRange = UpdateRange(device) self.qmin = torch.tensor((-((1 << (bits - 1)) - 1)), device = device) self.qmax = torch.tensor((+((1 << (bits - 1)) - 1)), device = device) def round(self, input): output = Round.apply(input) return output def Quantization(self): quant_range = float(1 << (self.bits - 1)) float_range = torch.max(torch.abs(self.UpdateRange.fmin), torch.abs(self.UpdateRange.fmax)) scale = 1 for i in range(32): if torch.round(float_range * (1 << i)) < quant_range: scale = 1 << i else: break self.scale = scale def forward(self, input): if self.training: self.UpdateRange(input) self.Quantization() output = (torch.clamp(self.round(input * self.scale), self.qmin, self.qmax)) / self.scale return output
时间: 2024-02-14 13:25:43 浏览: 154
这段代码是一个用于量化神经网络参数的模块。它定义了三个类:UpdateRange、Round和Quantizer。
UpdateRange类用于更新输入数据的范围,它记录了输入数据的最小值和最大值,并在每个前向传播过程中更新这些值。
Round类是一个自定义的torch.autograd.Function,用于将输入数据四舍五入为整数。
Quantizer类是一个量化器模块,它接收一个bits参数,用于指定量化的位数。在前向传播过程中,它首先使用UpdateRange类更新输入数据的范围,然后根据量化范围和位数计算量化的比例因子scale。最后,它将输入数据乘以scale进行量化,并使用clamp函数将量化结果限制在指定的最小值和最大值范围内。
这段代码看起来是一个基于PyTorch框架实现的量化模块,用于将浮点数参数量化为固定位数的整数,以减少存储空间和计算资源的使用。
阅读全文