def __init__( self, inplanes, planes, stride=1, downsample=None, act_bitwidth=32, act_quant=False, act_noise=False, ): super(BasicBlock, self).__init__() if isinstance(act_bitwidth, list): assert (len(act_bitwidth) == 2) self.act_bitwidth = act_bitwidth else: self.act_bitwidth = [act_bitwidth] * 2 self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.relu1 = actquant.ActQuantBuffers(quant=act_quant, bitwidth=act_bitwidth[0]) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.relu2 = actquant.ActQuantBuffers(quant=act_quant, bitwidth=act_bitwidth[1]) self.stride = stride
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, act_bitwidth=32, act_quant=False, act_noise=False, uniq=True): super(DepthwiseSeparableFusedConv2d, self).__init__() if act_bitwidth == 32: self.components = nn.Sequential( Conv2d(in_channels, in_channels, kernel_size, stride=stride, padding=padding, groups=in_channels), nn.BatchNorm2d(in_channels), # nn.ReLU(inplace=True), actquant.ActQuantBuffers(quant=act_quant, noise=act_noise, bitwidth=act_bitwidth, uniq=uniq), Conv2d(in_channels, out_channels, 1, bias=False), nn.BatchNorm2d(out_channels), # nn.ReLU(inplace=True) actquant.ActQuantBuffers(quant=act_quant, noise=act_noise, bitwidth=act_bitwidth, uniq=uniq)) else: self.components = nn.Sequential( Conv2d(in_channels, in_channels, kernel_size, stride=stride, padding=padding, groups=in_channels), nn.BatchNorm2d(in_channels), actquant.ActQuantBuffers(quant=act_quant, noise=act_noise, bitwidth=act_bitwidth, uniq=uniq), Conv2d(in_channels, out_channels, 1, bias=False), nn.BatchNorm2d(out_channels), actquant.ActQuantBuffers(quant=act_quant, noise=act_noise, bitwidth=act_bitwidth, uniq=uniq))
def get_feature_activation(self): if self.wrpn: return actquant.ActQuantWRPN(act_quant=self.act_quant, act_bitwidth=self.act_bitwidth) else: return actquant.ActQuantBuffers(quant=self.act_quant, bitwidth=self.act_bitwidth)
def __init__(self, inplanes, planes, stride=1, downsample=None, act_bitwidth=32, act_quant=False): super(Bottleneck, self).__init__() if isinstance(act_bitwidth, list): assert (len(act_bitwidth) == 3) self.act_bitwidth = act_bitwidth else: self.act_bitwidth = [act_bitwidth] * 3 self.conv1 = Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.relu1 = actquant.ActQuantBuffers(quant=act_quant, bitwidth=act_bitwidth[0]) self.conv2 = Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False, groups=32) self.bn2 = nn.BatchNorm2d(planes) self.relu2 = actquant.ActQuantBuffers(quant=act_quant, bitwidth=act_bitwidth[1]) self.conv3 = Conv2d(planes, planes * 4, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * 4) self.relu3 = actquant.ActQuantBuffers(quant=act_quant, bitwidth=act_bitwidth[2]) self.downsample = downsample self.stride = stride
def __init__(self, quant_epoch_step, quant_start_stage, alpha=1.0, shallow=False, num_classes=1000, quant=False, noise=False, bitwidth=32, step=2, quant_edges=True, act_bitwidth=32, act_noise=True, step_setup=[15, 9], act_quant=False, uniq=True, normalize=False): super(MobileNet, self).__init__(quant_epoch_step=quant_epoch_step, quant_start_stage=quant_start_stage, quant=quant, noise=noise, bitwidth=bitwidth, step=step, quant_edges=quant_edges, act_noise=act_noise, step_setup=step_setup, act_bitwidth=act_bitwidth, act_quant=act_quant, uniq=uniq) # if act_bitwidth == 32: # act = nn.ReLU(inplace=True) # else: self.preprocess = lambda x: x act = actquant.ActQuantBuffers(quant=act_quant, noise=act_noise, bitwidth=act_bitwidth, uniq=self.uniq) layers = [ Conv2d(3, nearby_int(alpha * 32), kernel_size=3, stride=2, padding=1, bias=False), nn.BatchNorm2d(nearby_int(alpha * 32)), act, DepthwiseSeparableFusedConv2d(nearby_int(alpha * 32), nearby_int(alpha * 64), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 64), nearby_int(alpha * 128), kernel_size=3, stride=2, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 128), nearby_int(alpha * 128), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 128), nearby_int(alpha * 256), kernel_size=3, stride=2, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 256), nearby_int(alpha * 256), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 256), nearby_int(alpha * 512), kernel_size=3, stride=2, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq) ] if not shallow: # 5x 512->512 DW-separable convolutions layers += [ DepthwiseSeparableFusedConv2d(nearby_int(alpha * 512), nearby_int(alpha * 512), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 512), nearby_int(alpha * 512), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 512), nearby_int(alpha * 512), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 512), nearby_int(alpha * 512), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), DepthwiseSeparableFusedConv2d(nearby_int(alpha * 512), nearby_int(alpha * 512), kernel_size=3, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), ] layers += [ DepthwiseSeparableFusedConv2d(nearby_int(alpha * 512), nearby_int(alpha * 1024), kernel_size=3, stride=2, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq), # Paper specifies stride-2, but unchanged size. # Assume its a typo and use stride-1 convolution DepthwiseSeparableFusedConv2d(nearby_int(alpha * 1024), nearby_int(alpha * 1024), kernel_size=3, stride=1, padding=1, act_bitwidth=act_bitwidth, act_quant=act_quant, act_noise=act_noise, uniq=uniq) ] self.features = nn.Sequential(*layers) self.avg_pool = nn.AvgPool2d(7) self.classifier = Linear(nearby_int(alpha * 1024), num_classes) self.regime = [ { 'epoch': 0, 'optimizer': 'SGD', 'lr': 1e-3, # 'weight_decay': 5e-4, 'momentum': 0.9}, 'weight_decay': 1e-4, 'momentum': 0.9 }, { 'epoch': 30, 'lr': 5e-3 }, { 'epoch': 60, 'lr': 5e-4, 'weight_decay': 0 }, { 'epoch': 90, 'lr': 5e-5 } ]
def __init__(self, quant_epoch_step, quant_start_stage, num_classes=10, block=BasicBlock, depth=18, quant=False, noise=False, bitwidth=32, step=2, quant_edges=True, act_noise=True, step_setup=[15, 9], act_bitwidth=32, act_quant=False, layers=[2, 2, 2, 2], normalize=False, noise_mask=0.05): super(ResNet_cifar10, self).__init__(quant_epoch_step=quant_epoch_step, quant_start_stage=quant_start_stage, quant=quant, noise=noise, bitwidth=bitwidth, step=step, quant_edges=quant_edges, step_setup=step_setup, act_bitwidth=act_bitwidth, act_quant=act_quant, noise_mask=noise_mask) self.preprocess = lambda x: x self.inplanes = 64 n = int((depth - 2) / 6) self.conv1 = Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(64) if isinstance(act_bitwidth, list): if self.quant_edges: assert (len(act_bitwidth) == depth - 1) else: assert (len(act_bitwidth) == depth - 2) self.act_bitwidth = act_bitwidth else: if self.quant_edges: self.act_bitwidth = [act_bitwidth] * (depth - 1) else: self.act_bitwidth = [act_bitwidth] * (depth - 2) self.relu = actquant.ActQuantBuffers(quant=act_quant, bitwidth=act_bitwidth) self.maxpool = lambda x: x self.layer1 = self._make_layer(block, 64, layers[0], act_bitwidth=act_bitwidth, act_quant=act_quant) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, act_bitwidth=act_bitwidth, act_quant=act_quant) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, act_bitwidth=act_bitwidth, act_quant=act_quant) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, act_bitwidth=act_bitwidth, act_quant=act_quant) # self.layer4 = lambda x: x self.avgpool = nn.AvgPool2d(4) self.fc = Linear(512, num_classes) init_model(self) self.regime = [{ 'epoch': 0, 'optimizer': 'SGD', 'lr': 1e-1, 'weight_decay': 1e-4, 'momentum': 0.9 }, { 'epoch': 81, 'lr': 1e-2 }, { 'epoch': 122, 'lr': 1e-3, 'weight_decay': 0 }, { 'epoch': 164, 'lr': 1e-4 }]
def __init__(self, quant_epoch_step, quant_start_stage, num_classes=1000, block=Bottleneck, layers=[3, 4, 23, 3], quant=False, noise=False, bitwidth=32, step=2, quant_edges=True, step_setup=[15, 9], act_bitwidth=32, act_quant=False, uniq=True, normalize=False, noise_mask=0.05): super(ResNet_imagenet, self).__init__(quant_epoch_step=quant_epoch_step, quant_start_stage=quant_start_stage, quant=quant, noise=noise, bitwidth=bitwidth, step=step, quant_edges=quant_edges, step_setup=step_setup, act_bitwidth=act_bitwidth, act_quant=act_quant, noise_mask=noise_mask) self.inplanes = 64 self.preprocess = lambda x: x self.conv1 = Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64) block_size = 2 if block is BasicBlock else 3 self.relu = actquant.ActQuantBuffers(quant=act_quant, bitwidth=self.act_bitwidth) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) act_nums = [1] for i in range(4): act_nums.append(act_nums[i] + layers[i] * block_size) self.layer1 = self._make_layer(block, 64, layers[0], act_bitwidth=self.act_bitwidth, act_quant=act_quant) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, act_bitwidth=self.act_bitwidth, act_quant=act_quant) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, act_bitwidth=self.act_bitwidth, act_quant=act_quant) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, act_bitwidth=self.act_bitwidth, act_quant=act_quant) self.avgpool = nn.AvgPool2d(7) self.fc = Linear(512 * block.expansion, num_classes) init_model(self) self.regime = [{ 'epoch': 0, 'optimizer': 'SGD', 'lr': 5e-2, 'weight_decay': 1e-4, 'momentum': 0.9 }, { 'epoch': 30, 'lr': 5e-3 }, { 'epoch': 60, 'lr': 5e-4, 'weight_decay': 0 }, { 'epoch': 90, 'lr': 5e-5 }]