Esempio n. 1
0
    def forward(self, x):
        res = self.conv1(x)

        res = BFPActivation.transform_activation_online(res,
                                                        self.exp_bit,
                                                        self.mantisa_bit,
                                                        -1,
                                                        is_3d=True)

        res = self.relu1(res)
        res = self.conv2(res)

        res = BFPActivation.transform_activation_online(res,
                                                        self.exp_bit,
                                                        self.mantisa_bit,
                                                        -1,
                                                        is_3d=True)

        if self.downsample:
            x = self.downsampleconv(x)
            #x = self.downsamplebn(self.downsampleconv(x))

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        return self.outrelu(x + res)
Esempio n. 2
0
    def forward(self, x):

        x = BFPActivation.transform_activation_offline(
            x,
            self.exp_bit,
            self.mantisa_bit,
            self.opt_exp_act_list[0],
            is_3d=True)

        x = self.relu1(self.conv1(x))

        x = BFPActivation.transform_activation_offline(
            x,
            self.exp_bit,
            self.mantisa_bit,
            self.opt_exp_act_list[1],
            is_3d=True)

        x = self.conv2(x)
        x = self.conv3(x)
        x = self.conv4(x)
        x = self.conv5(x)

        x = self.pool(x)

        return x.view(-1, 512)
Esempio n. 3
0
    def forward(self, x):
        residual = x

        out = self.conv1(x)
        #out = self.bn1(out)
        out = BFPActivation.transform_activation_online(
            out, self.exp_bit, self.mantisa_bit, -1)
        out = self.relu(out)

        out = self.conv2(out)
        #out = self.bn2(out)
        out = BFPActivation.transform_activation_online(
            out, self.exp_bit, self.mantisa_bit, -1)
        out = self.relu(out)

        out = self.conv3(out)
        #out = self.bn3(out)
        out = BFPActivation.transform_activation_online(
            out, self.exp_bit, self.mantisa_bit, -1)

        if self.downsample is not None:
            # Get a max of two list
            #max_exp_act_list =  np.maximum.reduce([self.opt_exp_act_list[self.start_exp_ind+2], self.opt_exp_act_list[self.start_exp_ind+3]]).tolist()
            residual = self.downsample(x)
            # bfp quantize both tensor for shortcut using the max exponent list
            # since they have the same exp list, no need for realignment
            # residual = BFPActivation.transform_activation_online(residual, self.exp_bit,
            #                                                         self.mantisa_bit, self.opt_exp_act_list[self.start_exp_ind+3])
            #out = BFPActivation.transform_activation_offline(out, self.exp_bit, self.mantisa_bit, max_exp_act_list)
        # else:
        # bfp quantize both tensor for shortcut using the third exponent list
        # residual = BFPActivation.transform_activation_online(residual, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[self.start_exp_ind+2])
        # Get the exponent from out
        out_exp = Utils.find_exponent(out, self.exp_bit)
        out_exp = Utils.find_max_exponent(out_exp,
                                          quant_dim=len(out.shape) - 1)
        out_exp = Utils.find_max_exponent(out_exp,
                                          quant_dim=len(out.shape) - 2)
        out_exp = Utils.find_max_exponent(out_exp, quant_dim=0)
        out_exp = out_exp.int().cpu().data.tolist()
        # Get the exponent from input
        in_exp = Utils.find_exponent(residual, self.exp_bit)
        in_exp = Utils.find_max_exponent(in_exp,
                                         quant_dim=len(residual.shape) - 1)
        in_exp = Utils.find_max_exponent(in_exp,
                                         quant_dim=len(residual.shape) - 2)
        in_exp = Utils.find_max_exponent(in_exp, quant_dim=0)
        in_exp = in_exp.int().cpu().data.tolist()
        # Quantize accordint to the max
        max_exp = np.maximum.reduce([out_exp, in_exp]).tolist()
        residual = BFPActivation.transform_activation_offline(
            residual, self.exp_bit, self.mantisa_bit, max_exp)
        out = BFPActivation.transform_activation_offline(
            out, self.exp_bit, self.mantisa_bit, max_exp)
        out += residual
        out = self.relu(out)

        return out
Esempio n. 4
0
 def forward(self, x):
     x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                   self.mantisa_bit, -1)
     x0 = self.conv(x)
     x1 = self.maxpool(x)
     out = torch.cat((x0, x1), 1)
     return out
Esempio n. 5
0
 def forward(self, x):
     x = F.conv2d(x, self.weight, self.bias, self.stride, self.padding,
                  self.dilation, self.groups)
     x = BFPActivation.transform_activation_offline(
         x, self.exp_bit, self.mantisa_bit,
         self.opt_exp_act_list[self.start_exp_ind])
     return x
Esempio n. 6
0
 def forward(self, x): 
     x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                      self.opt_exp_act_list[0])
     x = self.features(x)
     x = x.view(-1, self.last_channel)
     x = self.classifier(x)
     x = BFPFullyConnet.transform_fc_offline(x, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[-1])
     return x
Esempio n. 7
0
 def forward(self, x):
     x = BFPActivation.transform_activation_offline(
         x, self.exp_bit, self.mantisa_bit,
         self.opt_exp_act_list[self.start_exp_ind])
     x0 = self.conv(x)
     x1 = self.maxpool(x)
     out = torch.cat((x0, x1), 1)
     return out
Esempio n. 8
0
 def forward(self, x):
     x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                   self.mantisa_bit, -1)
     x0 = self.branch0(x)
     x1 = self.branch1(x)
     x2 = self.branch2(x)
     out = torch.cat((x0, x1, x2), 1)
     return out
Esempio n. 9
0
 def forward(self, x):
     x = BFPActivation.transform_activation_offline(
         x, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[0])
     x = self.features(x)
     x = self.avgpool(x)
     x = x.view(x.size(0), -1)
     x = self.classifier(x)
     return x
Esempio n. 10
0
 def forward(self, x):
     x = BFPActivation.transform_activation_offline(
         x, self.exp_bit, self.mantisa_bit,
         self.opt_exp_act_list[self.start_exp_ind])
     x0 = self.branch0(x)
     x1 = self.branch1(x)
     x2 = self.branch2(x)
     out = torch.cat((x0, x1, x2), 1)
     return out
Esempio n. 11
0
 def forward(self, x):
     x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                   self.mantisa_bit, -1)
     x = self.features(x)
     x = self.logits(x)
     #x = BFPFullyConnet.transform_fc_offline(x, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[-1])
     x = BFPFullyConnet.transform_fc_online(x, self.exp_bit,
                                            self.mantisa_bit, -1)
     return x
Esempio n. 12
0
 def forward(self, x):
     x = self.conv(x)
     #x = self.bn(x)
     #print ("Before:", x)
     x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                   self.mantisa_bit, -1)
     #print ("After", x)
     x = self.relu(x)
     return x
Esempio n. 13
0
    def forward(self, x):
        residual = x

        out = self.conv1(x)
        #out = self.bn1(out)
        out = BFPActivation.transform_activation_offline(
            out, self.exp_bit, self.mantisa_bit,
            self.opt_exp_act_list[self.start_exp_ind])
        out = self.relu(out)

        out = self.conv2(out)
        #out = self.bn2(out)
        out = BFPActivation.transform_activation_offline(
            out, self.exp_bit, self.mantisa_bit,
            self.opt_exp_act_list[self.start_exp_ind + 1])
        out = self.relu(out)

        out = self.conv3(out)
        #out = self.bn3(out)
        out = BFPActivation.transform_activation_offline(
            out, self.exp_bit, self.mantisa_bit,
            self.opt_exp_act_list[self.start_exp_ind + 2])

        if self.downsample is not None:
            # Get a max of two list
            #max_exp_act_list =  np.maximum.reduce([self.opt_exp_act_list[self.start_exp_ind+2], self.opt_exp_act_list[self.start_exp_ind+3]]).tolist()
            residual = self.downsample(x)
            # bfp quantize both tensor for shortcut using the max exponent list
            # since they have the same exp list, no need for realignment
            residual = BFPActivation.transform_activation_offline(
                residual, self.exp_bit, self.mantisa_bit,
                self.opt_exp_act_list[self.start_exp_ind + 3])
            #out = BFPActivation.transform_activation_offline(out, self.exp_bit, self.mantisa_bit, max_exp_act_list)
        else:
            # bfp quantize both tensor for shortcut using the third exponent list
            residual = BFPActivation.transform_activation_offline(
                residual, self.exp_bit, self.mantisa_bit,
                self.opt_exp_act_list[self.start_exp_ind + 2])

        out += residual
        out = self.relu(out)

        return out
Esempio n. 14
0
 def forward(self, x):
     x = self.conv(x)
     #x = self.bn(x)
     #print ("Before:", x)
     x = BFPActivation.transform_activation_offline(
         x, self.exp_bit, self.mantisa_bit,
         self.opt_exp_act_list[self.start_exp_ind])
     #print ("After", x)
     x = self.relu(x)
     return x
Esempio n. 15
0
    def forward(self, x):
        residual = x

        out = self.conv1(x)
        #out = self.bn1(out)
        # disble bn for fused BN
        out = BFPActivation.transform_activation_online(
            out, self.exp_bit, self.mantisa_bit, -1)
        out = self.relu(out)

        out = self.conv2(out)
        #out = self.bn2(out)
        out = BFPActivation.transform_activation_online(
            out, self.exp_bit, self.mantisa_bit, -1)
        if self.downsample is not None:
            residual = self.downsample(x)
            residual = BFPActivation.transform_activation_online(
                residual, self.exp_bit, self.mantisa_bit, -1)
        out += residual
        out = self.relu(out)
        return out
Esempio n. 16
0
    def forward(self, x):
        x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                      self.mantisa_bit, -1)
        x = self.conv1(x)
        #x = self.bn1(x) #Fused BN
        x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                      self.mantisa_bit, -1)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        #x = BFPFullyConnet.transform_fc_offline(x, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[-1])
        x = BFPFullyConnet.transform_fc_online(x, self.exp_bit,
                                               self.mantisa_bit, -1)
        return x
Esempio n. 17
0
    def forward(self, x):
        if self.use_res_connect:
            '''
            max_exp_act_list =  np.maximum.reduce([self.opt_exp_act_list[self.start_exp_ind+2], self.opt_exp_act_list[self.start_exp_ind-1]]).tolist()
            out = self.conv(x)
            x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit, max_exp_act_list)
            out = BFPActivation.transform_activation_offline(out, self.exp_bit, self.mantisa_bit, max_exp_act_list)
            return x + out
            '''
            out = self.conv(x)
            # Get the exponent from out
            out_exp = Utils.find_exponent(out, self.exp_bit)
            out_exp = Utils.find_max_exponent(out_exp,
                                              quant_dim=len(out.shape) - 1)
            out_exp = Utils.find_max_exponent(out_exp,
                                              quant_dim=len(out.shape) - 2)
            out_exp = Utils.find_max_exponent(out_exp, quant_dim=0)
            out_exp = out_exp.int().cpu().data.tolist()
            # Get the exponent from input
            in_exp = Utils.find_exponent(x, self.exp_bit)
            in_exp = Utils.find_max_exponent(in_exp,
                                             quant_dim=len(x.shape) - 1)
            in_exp = Utils.find_max_exponent(in_exp,
                                             quant_dim=len(x.shape) - 2)
            in_exp = Utils.find_max_exponent(in_exp, quant_dim=0)
            in_exp = in_exp.int().cpu().data.tolist()
            # Quantize accordint to the max
            max_exp = np.maximum.reduce([out_exp, in_exp]).tolist()
            x = BFPActivation.transform_activation_offline(
                x, self.exp_bit, self.mantisa_bit, max_exp)
            out = BFPActivation.transform_activation_offline(
                out, self.exp_bit, self.mantisa_bit, max_exp)
            return x + out

        else:
            return self.conv(x)
Esempio n. 18
0
 def test_channel_split(self):
     tensor_shape = (1, 8, 4, 4)
     channel_group = 4
     act = 1.1111 * torch.ones((tensor_shape))
     act[0, 0, 0, 0] = 2**7
     act[0, 6, 0, 1] = 2**5
     #act = torch.arange(1 * 16 * 32 * 32)
     #act = torch.reshape(act, (1, 16, 32, 32))
     #act = act.float()
     bfp_act = BFPActivation.transform_activation(act, 8, 8, 4)
     #print(bfp_act)
     golden = torch.zeros(tensor_shape)
     golden[0, 0, 0, 0] = 2**6 - 1
     result = torch.equal(bfp_act, golden)
     self.assertEqual(result, True)
Esempio n. 19
0
    def forward(self, x):
        residual = x

        out = self.conv1(x)
        #out = self.bn1(out)
        # disble bn for fused BN
        out = BFPActivation.transform_activation_offline(
            out, self.exp_bit, self.mantisa_bit,
            self.opt_exp_act_list[self.start_exp_ind])
        out = self.relu(out)

        out = self.conv2(out)
        #out = self.bn2(out)
        out = BFPActivation.transform_activation_offline(
            out, self.exp_bit, self.mantisa_bit,
            self.opt_exp_act_list[self.start_exp_ind + 1])
        if self.downsample is not None:
            residual = self.downsample(x)
            residual = BFPActivation.transform_activation_offline(
                residual, self.exp_bit, self.mantisa_bit,
                self.opt_exp_act_list[self.start_exp_ind + 2])
        out += residual
        out = self.relu(out)
        return out
Esempio n. 20
0
 def forward(self, x):
     if self.use_res_connect:
         '''
         max_exp_act_list =  np.maximum.reduce([self.opt_exp_act_list[self.start_exp_ind+2], self.opt_exp_act_list[self.start_exp_ind-1]]).tolist()
         out = self.conv(x)
         x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit, max_exp_act_list)
         out = BFPActivation.transform_activation_offline(out, self.exp_bit, self.mantisa_bit, max_exp_act_list)
         return x + out
         '''
         out = self.conv(x)
         x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[self.start_exp_ind+2])
         return x + out
         
     else:
         return self.conv(x)
Esempio n. 21
0
    def forward(self, x):
        x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                      self.mantisa_bit, -1)
        x0 = self.branch0(x)

        x1_0 = self.branch1_0(x)
        x1_1a = self.branch1_1a(x1_0)
        x1_1b = self.branch1_1b(x1_0)
        x1 = torch.cat((x1_1a, x1_1b), 1)

        x2_0 = self.branch2_0(x)
        x2_1 = self.branch2_1(x2_0)
        x2_2 = self.branch2_2(x2_1)
        x2_3a = self.branch2_3a(x2_2)
        x2_3b = self.branch2_3b(x2_2)
        x2 = torch.cat((x2_3a, x2_3b), 1)

        x3 = self.branch3(x)

        out = torch.cat((x0, x1, x2, x3), 1)

        return out
def bfp_quant(model_name,
              dataset_dir,
              num_classes,
              gpus,
              mantisa_bit,
              exp_bit,
              batch_size=1,
              num_bins=8001,
              eps=0.0001,
              num_workers=2,
              num_examples=10,
              std=None,
              mean=None,
              resize=256,
              crop=224,
              exp_act=None,
              bfp_act_chnl=1,
              bfp_weight_chnl=1,
              bfp_quant=1,
              target_module_list=None,
              act_bins_factor=3,
              fc_bins_factor=4,
              is_online=0):
    # Setting up gpu environment
    os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(gpus)

    # Setting up dataload for evaluation
    valdir = os.path.join(dataset_dir, 'val')
    normalize = transforms.Normalize(mean=mean, std=std)

    # for collect intermediate data use
    collect_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        valdir,
        transforms.Compose([
            transforms.Resize(resize),
            transforms.CenterCrop(crop),
            transforms.ToTensor(),
            normalize,
        ])),
                                                 batch_size=num_examples,
                                                 shuffle=False,
                                                 num_workers=num_workers,
                                                 pin_memory=True)
    # for validate the bfp model use
    val_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
        valdir,
        transforms.Compose([
            transforms.Resize(resize),
            transforms.CenterCrop(crop),
            transforms.ToTensor(),
            normalize,
        ])),
                                             batch_size=batch_size,
                                             shuffle=False,
                                             num_workers=num_workers,
                                             pin_memory=True)

    # Loading the model
    model, _ = model_factory.get_network(model_name, pretrained=True)
    # Insert the hook to record the intermediate result
    #target_module_list = [nn.BatchNorm2d,nn.Linear] # Insert hook after BN and FC
    model, intern_outputs = Stat_Collector.insert_hook(model,
                                                       target_module_list)
    #model = nn.DataParallel(model)
    model.cuda()
    model.eval()

    # Collect the intermediate result while running number of examples
    logging.info("Collecting the statistics while running image examples....")
    images_statistc = torch.empty((1))
    with torch.no_grad():
        for i_batch, (images, lables) in enumerate(collect_loader):
            images = images.cuda()
            outputs = model(images)
            #print(lables)
            _, predicted = torch.max(outputs.data, 1)
            predicted = predicted.cpu(
            )  # needs to verify if this line can be deleted
            # Collect the input data
            image_shape = images.shape
            images_statistc = torch.reshape(images,
                                            (image_shape[0], image_shape[1],
                                             image_shape[2] * image_shape[3]))
            break

    # Deternmining the optimal exponent of activation and
    # Constructing the distribution for tensorboardX visualization
    logging.info(
        "Determining the optimal exponent by minimizing the KL divergence....")
    start = time.time()
    opt_exp_act_list = []
    max_exp_act_list = []
    # For original input
    opt_exp, max_exp = Utils.find_exp_act(images_statistc,
                                          mantisa_bit,
                                          exp_bit,
                                          group=3,
                                          eps=eps,
                                          bins_factor=act_bins_factor)
    opt_exp_act_list.append(opt_exp)
    max_exp_act_list.append(max_exp)
    sc_layer_num = [7, 10, 17, 20, 23, 30, 33, 36, 39, 42, 49, 52]
    ds_sc_layer_num = [14, 27, 46]
    mobilev2_sc_layer_num = [9, 15, 18, 24, 27, 30, 36, 39, 45, 48]
    for i, intern_output in enumerate(intern_outputs):
        #print ("No.", i, " ", intern_output.out_features.shape)
        #Deternmining the optimal exponent by minimizing the KL_Divergence in channel-wise manner
        if (isinstance(intern_output.m, nn.Conv2d)
                or isinstance(intern_output.m, nn.BatchNorm2d)):
            intern_shape = intern_output.out_features.shape
            #print (intern_shape, "No.", i)
            # assmue internal activation has shape: (batch, channel, height, width)

            if ((model_name == "resnet50") and (i in sc_layer_num)):
                #print ("Before:", intern_shape[1])
                intern_features1 = intern_output.out_features
                intern_features2 = intern_outputs[i - 3].out_features
                intern_features = torch.cat(
                    (intern_features1, intern_features2), 0)
                intern_features = torch.reshape(
                    intern_features, (2 * intern_shape[0], intern_shape[1],
                                      intern_shape[2] * intern_shape[3]))
                #print (intern_features.shape)
                opt_exp, max_exp = Utils.find_exp_act(
                    intern_features,
                    mantisa_bit,
                    exp_bit,
                    group=bfp_act_chnl,
                    eps=eps,
                    bins_factor=act_bins_factor)
                opt_exp_act_list.append(opt_exp)
                max_exp_act_list.append(max_exp)
                #print ("After:", len(opt_exp))
            elif ((model_name == "resnet50") and (i in ds_sc_layer_num)):
                intern_features1 = intern_output.out_features
                intern_features2 = intern_outputs[i - 1].out_features
                intern_features = torch.cat(
                    (intern_features1, intern_features2), 0)
                intern_features = torch.reshape(
                    intern_features, (2 * intern_shape[0], intern_shape[1],
                                      intern_shape[2] * intern_shape[3]))
                #print (intern_features.shape)
                opt_exp, max_exp = Utils.find_exp_act(
                    intern_features,
                    mantisa_bit,
                    exp_bit,
                    group=bfp_act_chnl,
                    eps=eps,
                    bins_factor=act_bins_factor)
                #print ("Current shape", np.shape(opt_exp), " No.", i)
                #print ("Previous shape", np.shape(opt_exp_act_list[i]), " No.", i-1)
                opt_exp_act_list.append(opt_exp)
                max_exp_act_list.append(max_exp)
                opt_exp_act_list[i] = (opt_exp)  #Starting from 1
                max_exp_act_list[i] = (max_exp)
            elif ((model_name == "mobilenetv2")
                  and (i in mobilev2_sc_layer_num)):
                intern_features1 = intern_output.out_features
                intern_features2 = intern_outputs[i - 3].out_features
                intern_features = torch.cat(
                    (intern_features1, intern_features2), 0)
                intern_features = torch.reshape(
                    intern_features, (2 * intern_shape[0], intern_shape[1],
                                      intern_shape[2] * intern_shape[3]))
                #print (intern_features.shape)
                opt_exp, max_exp = Utils.find_exp_act(
                    intern_features,
                    mantisa_bit,
                    exp_bit,
                    group=bfp_act_chnl,
                    eps=eps,
                    bins_factor=act_bins_factor)
                opt_exp_act_list.append(opt_exp)  ##changed
                max_exp_act_list.append(max_exp)
            else:
                intern_features = torch.reshape(
                    intern_output.out_features,
                    (intern_shape[0], intern_shape[1],
                     intern_shape[2] * intern_shape[3]))
                opt_exp, max_exp = Utils.find_exp_act(
                    intern_features,
                    mantisa_bit,
                    exp_bit,
                    group=bfp_act_chnl,
                    eps=eps,
                    bins_factor=act_bins_factor)
                opt_exp_act_list.append(opt_exp)  ##changed
                max_exp_act_list.append(max_exp)
                # ploting the distribution
                #writer.add_histogram("layer%d" % (i), intern_output.out_features.cpu().data.numpy(), bins='auto')
                quant_tensor = BFPActivation.transform_activation_offline(
                    intern_output.out_features, exp_bit, mantisa_bit, max_exp)
                #writer.add_histogram("layer%d" % (i), quant_tensor.cpu().data.numpy(), bins='auto')
                quant_tensor = BFPActivation.transform_activation_offline(
                    intern_output.out_features, exp_bit, mantisa_bit, opt_exp)
                writer.add_histogram("layer%d" % (i),
                                     quant_tensor.cpu().data.numpy(),
                                     bins='auto')

            #print (np.shape(opt_exp), " No.", i)
        elif (isinstance(intern_output.m, nn.Linear)):
            intern_shape = intern_output.out_features.shape
            opt_exp, max_exp = Utils.find_exp_fc(intern_output.out_features,
                                                 mantisa_bit,
                                                 exp_bit,
                                                 block_size=intern_shape[1],
                                                 eps=eps,
                                                 bins_factor=fc_bins_factor)
            #print ("shape of fc exponent:", np.shape(opt_exp))
            opt_exp_act_list.append(max_exp)
            max_exp_act_list.append(max_exp)
        else:
            intern_shape = intern_output.in_features[0].shape
            intern_features = torch.reshape(
                intern_output.in_features[0],
                (intern_shape[0], intern_shape[1],
                 intern_shape[2] * intern_shape[3]))
            opt_exp, max_exp = Utils.find_exp_act(intern_features,
                                                  mantisa_bit,
                                                  exp_bit,
                                                  group=bfp_act_chnl,
                                                  eps=eps,
                                                  bins_factor=act_bins_factor)
            opt_exp_act_list.append(opt_exp)
            max_exp_act_list.append(max_exp)

        #logging.info("The internal shape: %s" % ((str)(intern_output.out_features.shape)))
    end = time.time()
    logging.info(
        "It took %f second to determine the optimal shared exponent for each block."
        % ((float)(end - start)))
    logging.info("The shape of collect exponents: %s" %
                 ((str)(np.shape(opt_exp_act_list))))

    # Building a BFP model by insert BFPAct and BFPWeiht based on opt_exp_act_list
    torch.cuda.empty_cache()
    if (exp_act == 'kl'):
        exp_act_list = opt_exp_act_list
    else:
        exp_act_list = max_exp_act_list
    if (is_online == 1):
        model_name = "br_" + model_name
    bfp_model, weight_exp_list = model_factory.get_network(
        model_name,
        pretrained=True,
        bfp=(bfp_quant == 1),
        group=bfp_weight_chnl,
        mantisa_bit=mantisa_bit,
        exp_bit=exp_bit,
        opt_exp_act_list=exp_act_list)

    writer.close()
Esempio n. 23
0
 def forward(self, x):
     #x = F.batch_norm(x, self.running_mean, self.running_var, self.weight, self.bias)
     x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                      self.opt_exp_act_list[self.start_exp_ind])
     return x
Esempio n. 24
0
    def forward(self, x):
        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        #x = self.bn1(self.conv1(x))
        x = self.conv1(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        x = self.pool1(x)

        #x = self.bn2(self.conv2(x))
        x = self.conv2(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        x = self.pool2(x)

        #x = self.bn3a(self.conv3a(x))
        x = self.conv3a(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        #x = self.bn3b(self.conv3b(x))
        x = self.conv3b(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        x = self.pool3(x)

        #x = self.bn4a(self.conv4a(x))
        x = self.conv4a(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        #x = self.bn4b(self.conv4b(x))
        x = self.conv4b(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        x = self.pool4(x)

        #x = self.bn5a(self.conv5a(x))
        x = self.conv5a(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        #x = self.bn5b(self.conv5b(x))
        x = self.conv5b(x)

        x = BFPActivation.transform_activation_online(x,
                                                      self.exp_bit,
                                                      self.mantisa_bit,
                                                      -1,
                                                      is_3d=True)

        x = self.relu(x)
        x = self.pool5(x)

        x = x.view(-1, 8192)
        x = self.fc6(x)
        x = BFPFullyConnet.transform_fc_online(x, self.exp_bit,
                                               self.mantisa_bit, -1)
        x = self.relu(x)
        x = self.dropout(x)
        x = self.fc7(x)
        x = BFPFullyConnet.transform_fc_online(x, self.exp_bit,
                                               self.mantisa_bit, -1)

        x = self.relu(x)
        x = self.dropout(x)

        logits = self.fc8(x)
        #print (logits.shape)
        x = BFPFullyConnet.transform_fc_online(x, self.exp_bit,
                                               self.mantisa_bit, -1)

        return logits
Esempio n. 25
0
 def forward(self, x):
     #x = F.batch_norm(x, self.running_mean, self.running_var, self.weight, self.bias)
     x = BFPActivation.transform_activation_online(x, self.exp_bit,
                                                   self.mantisa_bit, -1)
     return x
Esempio n. 26
0
 def forward(self, x):
     x = F.conv2d(x, self.weight, self.bias, self.stride, self.padding,
                     self.dilation, self.groups)
     x = BFPActivation.transform_activation_online(x, self.exp_bit, self.mantisa_bit,
                                                      -1)
     return x
Esempio n. 27
0
    def forward(self, x):

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[0], is_3d=True)
        
        #x = self.bn1(self.conv1(x))
        x = self.conv1(x)

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[1], is_3d=True)

        x = self.relu(x)
        x = self.pool1(x)

        #x = self.bn2(self.conv2(x))
        x = self.conv2(x)


        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[2], is_3d=True)

        x = self.relu(x)
        x = self.pool2(x)

        #x = self.bn3a(self.conv3a(x))
        x = self.conv3a(x)

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[3], is_3d=True)

        x = self.relu(x)
        #x = self.bn3b(self.conv3b(x))
        x = self.conv3b(x)

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[4], is_3d=True)

        x = self.relu(x)
        x = self.pool3(x)

        #x = self.bn4a(self.conv4a(x))
        x = self.conv4a(x)

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[5], is_3d=True)

        x = self.relu(x)
        #x = self.bn4b(self.conv4b(x))
        x = self.conv4b(x)

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[6], is_3d=True)

        x = self.relu(x)
        x = self.pool4(x)

        #x = self.bn5a(self.conv5a(x))
        x = self.conv5a(x)

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[7], is_3d=True)

        x = self.relu(x)
        #x = self.bn5b(self.conv5b(x))
        x = self.conv5b(x)

        x = BFPActivation.transform_activation_offline(x, self.exp_bit, self.mantisa_bit,
                                                         self.opt_exp_act_list[8], is_3d=True)

        x = self.relu(x)
        x = self.pool5(x)

        x = x.view(-1, 8192)
        x = self.fc6(x)
        x = BFPFullyConnet.transform_fc_offline(x, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[9])
        x = self.relu(x)
        x = self.dropout(x)
        x = self.fc7(x)
        x = BFPFullyConnet.transform_fc_offline(x, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[10])

        x = self.relu(x)
        x = self.dropout(x)

        logits = self.fc8(x)
        #print (logits.shape)
        x = BFPFullyConnet.transform_fc_offline(logits, self.exp_bit, self.mantisa_bit, self.opt_exp_act_list[11])

        return logits