TrainingExtensions/torch/src/python/aimet_torch/cross_layer_equalization.py
def scale_cls_set_with_conv_layers(cls_set: Union[Tuple[torch.nn.Conv2d, torch.nn.Conv2d],
                                                      Tuple[torch.nn.ConvTranspose2d, torch.nn.ConvTranspose2d]]) \
            -> np.ndarray:
        """
        API to invoke equalize layer params (update for weights and bias is in place)
        :param cls_set: Consecutive Conv layers Tuple whose weights and biases need to be equalized
        :return: Scaling factor S_12 for each conv layer pair: numpy array
        """

        on_gpu = False
        for module in cls_set:
            if not isinstance(module, (torch.nn.Conv2d, torch.nn.ConvTranspose2d)):
                raise ValueError("Only Conv or Transposed Conv layers are supported for cross layer equalization")
            if module.weight.is_cuda:
                on_gpu = True
                module.cpu()

        scaling_factor, prev_layer_params, curr_layer_params = CrossLayerScaling.call_mo_scale(cls_set)

        cls_set[0].weight.data = torch.from_numpy(np.reshape(prev_layer_params.weight,
                                                             prev_layer_params.weightShape))
        cls_set[0].weight.data = cls_set[0].weight.data.type(torch.FloatTensor)

        # Transpose weight back to N, C, H, W for transposed Conv2D
        if isinstance(cls_set[0], torch.nn.ConvTranspose2d):
            cls_set[0].weight.data = cls_set[0].weight.data.permute(1, 0, 2, 3)

        cls_set[1].weight.data = torch.from_numpy(np.reshape(curr_layer_params.weight,
                                                             curr_layer_params.weightShape))
        cls_set[1].weight.data = cls_set[1].weight.data.type(torch.FloatTensor)

        # Transpose weight back to N, C, H, W for transposed Conv2D
        if isinstance(cls_set[1], torch.nn.ConvTranspose2d):
            cls_set[1].weight.data = cls_set[1].weight.data.permute(1, 0, 2, 3)

        if cls_set[0].bias is not None:
            cls_set[0].bias.data = torch.from_numpy(np.reshape(prev_layer_params.bias,
                                                               prev_layer_params.weightShape[0]))
            cls_set[0].bias.data = cls_set[0].bias.data.type(torch.FloatTensor)

        if on_gpu:
            for module in cls_set:
                module.to(device="cuda")
        return scaling_factor

        API to invoke equalize layer params (update for weights and bias is in place)
        :param cls_set: Consecutive Conv layers Tuple whose weights and biases need to be equalized
        :return: Scaling factor S_12 for each conv layer pair: numpy array
        
API to invoke equalize layer params (update for weights and bias is in place)
API to invoke equalize layer params ( update for weights and bias is in place )
def scale_cls_set_with_conv_layers(cls_set: Union[Tuple[torch.nn.Conv2d, torch.nn.Conv2d],
                                                      Tuple[torch.nn.ConvTranspose2d, torch.nn.ConvTranspose2d]]) \
            -> np.ndarray:
        on_gpu = False
        for module in cls_set:
            if not isinstance(module, (torch.nn.Conv2d, torch.nn.ConvTranspose2d)):
                raise ValueError("Only Conv or Transposed Conv layers are supported for cross layer equalization")
            if module.weight.is_cuda:
                on_gpu = True
                module.cpu()
        scaling_factor, prev_layer_params, curr_layer_params = CrossLayerScaling.call_mo_scale(cls_set)
        cls_set[0].weight.data = torch.from_numpy(np.reshape(prev_layer_params.weight,
                                                             prev_layer_params.weightShape))
        cls_set[0].weight.data = cls_set[0].weight.data.type(torch.FloatTensor)
        if isinstance(cls_set[0], torch.nn.ConvTranspose2d):
            cls_set[0].weight.data = cls_set[0].weight.data.permute(1, 0, 2, 3)
        cls_set[1].weight.data = torch.from_numpy(np.reshape(curr_layer_params.weight,
                                                             curr_layer_params.weightShape))
        cls_set[1].weight.data = cls_set[1].weight.data.type(torch.FloatTensor)
        if isinstance(cls_set[1], torch.nn.ConvTranspose2d):
            cls_set[1].weight.data = cls_set[1].weight.data.permute(1, 0, 2, 3)
        if cls_set[0].bias is not None:
            cls_set[0].bias.data = torch.from_numpy(np.reshape(prev_layer_params.bias,
                                                               prev_layer_params.weightShape[0]))
            cls_set[0].bias.data = cls_set[0].bias.data.type(torch.FloatTensor)
        if on_gpu:
            for module in cls_set:
                module.to(device="cuda")
        return scaling_factor
def scale_cls_set_with_conv_layers ( cls_set : Union [ Tuple [ torch . nn . Conv2d , torch . nn . Conv2d ] , Tuple [ torch . nn . ConvTranspose2d , torch . nn . ConvTranspose2d ] ] ) -> np . ndarray : on_gpu = False for module in cls_set : if not isinstance ( module , ( torch . nn . Conv2d , torch . nn . ConvTranspose2d ) ) : raise ValueError ( "Only Conv or Transposed Conv layers are supported for cross layer equalization" ) if module . weight . is_cuda : on_gpu = True module . cpu ( ) scaling_factor , prev_layer_params , curr_layer_params = CrossLayerScaling . call_mo_scale ( cls_set ) cls_set [ 0 ] . weight . data = torch . from_numpy ( np . reshape ( prev_layer_params . weight , prev_layer_params . weightShape ) ) cls_set [ 0 ] . weight . data = cls_set [ 0 ] . weight . data . type ( torch . FloatTensor ) if isinstance ( cls_set [ 0 ] , torch . nn . ConvTranspose2d ) : cls_set [ 0 ] . weight . data = cls_set [ 0 ] . weight . data . permute ( 1 , 0 , 2 , 3 ) cls_set [ 1 ] . weight . data = torch . from_numpy ( np . reshape ( curr_layer_params . weight , curr_layer_params . weightShape ) ) cls_set [ 1 ] . weight . data = cls_set [ 1 ] . weight . data . type ( torch . FloatTensor ) if isinstance ( cls_set [ 1 ] , torch . nn . ConvTranspose2d ) : cls_set [ 1 ] . weight . data = cls_set [ 1 ] . weight . data . permute ( 1 , 0 , 2 , 3 ) if cls_set [ 0 ] . bias is not None : cls_set [ 0 ] . bias . data = torch . from_numpy ( np . reshape ( prev_layer_params . bias , prev_layer_params . weightShape [ 0 ] ) ) cls_set [ 0 ] . bias . data = cls_set [ 0 ] . bias . data . type ( torch . FloatTensor ) if on_gpu : for module in cls_set : module . to ( device = "cuda" ) return scaling_factor
API to invoke equalize layer params (update for weights and bias is in place)
API to invoke equalize layer params ( update for weights and bias is in place )
"""
        API to invoke equalize layer params (update for weights and bias is in place)
        :param cls_set: Consecutive Conv layers Tuple whose weights and biases need to be equalized
        :return: Scaling factor S_12 for each conv layer pair: numpy array
        """ # Transpose weight back to N, C, H, W for transposed Conv2D # Transpose weight back to N, C, H, W for transposed Conv2D
Scaling factor S_12 for each conv layer pair: numpy array
Scaling factor S_12 for each conv layer pair : numpy array
cls_set
Union[Tuple[torch.nn.Conv2d, torch.nn.Conv2d],
                                                      Tuple[torch.nn.ConvTranspose2d, torch.nn.ConvTranspose2d]]
Consecutive Conv layers Tuple whose weights and biases need to be equalized
Consecutive Conv layers Tuple whose weights and biases need to be equalized