TrainingExtensions/torch/src/python/aimet_torch/cross_layer_equalization.py
def scale_cls_set_with_depthwise_layers(cls_set: Tuple[torch.nn.Conv2d,
                                                           torch.nn.Conv2d,
                                                           torch.nn.Conv2d]) -> [np.ndarray, np.ndarray]:
        """
        API to invoke equalize layer params for depth wise separable layers(update for weights and bias is in place)
        :param cls_set: Consecutive Conv layers whose weights and biases need to be equalized.
                        Second Conv layer is a depth-wise conv and third conv layer is point-wise conv
        :return: Scaling factors S_12 and S_23 : numpy arrays
        """
        # pylint:disable=too-many-branches
        on_gpu = False
        for module in cls_set:
            if not isinstance(module, (torch.nn.Conv2d, torch.nn.ConvTranspose2d)):
                raise ValueError("Only conv layers are supported for cross layer equalization")
            if module.weight.is_cuda:
                on_gpu = True
                module.cpu()

        # Create structs for holding layer weights and bias parameters
        prev_layer_params = libpymo.EqualizationParams()
        curr_layer_params = libpymo.EqualizationParams()
        next_layer_params = libpymo.EqualizationParams()

        if isinstance(cls_set[0], torch.nn.ConvTranspose2d):
            cls_set[0].weight.data = cls_set[0].weight.data.permute(1, 0, 2, 3)
        if isinstance(cls_set[2], torch.nn.ConvTranspose2d):
            cls_set[2].weight.data = cls_set[2].weight.data.permute(1, 0, 2, 3)
        assert cls_set[1].groups > 1

        prev_layer_params.weight = cls_set[0].weight.detach().numpy().flatten()
        prev_layer_params.weightShape = np.array(cls_set[0].weight.shape)

        curr_layer_params.weight = cls_set[1].weight.detach().numpy().flatten()
        curr_layer_params.weightShape = np.array(cls_set[1].weight.shape)

        next_layer_params.weight = cls_set[2].weight.detach().numpy().flatten()
        next_layer_params.weightShape = np.array(cls_set[2].weight.shape)

        if cls_set[0].bias is not None:
            prev_layer_params.bias = cls_set[0].bias.detach().numpy()
        else:
            prev_layer_params.isBiasNone = True

        if cls_set[1].bias is not None:
            curr_layer_params.bias = cls_set[1].bias.detach().numpy()
        else:
            curr_layer_params.isBiasNone = True

        scaling_params = libpymo.scaleDepthWiseSeparableLayer(prev_layer_params, curr_layer_params, next_layer_params)

        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)

        cls_set[2].weight.data = torch.from_numpy(np.reshape(next_layer_params.weight,
                                                             next_layer_params.weightShape))
        cls_set[2].weight.data = cls_set[2].weight.data.type(torch.FloatTensor)
        if isinstance(cls_set[2], torch.nn.ConvTranspose2d):
            cls_set[2].weight.data = cls_set[2].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 cls_set[1].bias is not None:
            cls_set[1].bias.data = torch.from_numpy(np.reshape(curr_layer_params.bias,
                                                               curr_layer_params.weightShape[0]))
            cls_set[1].bias.data = cls_set[1].bias.data.type(torch.FloatTensor)

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

        return scaling_params.scalingMatrix12, scaling_params.scalingMatrix23

        API to invoke equalize layer params for depth wise separable layers(update for weights and bias is in place)
        :param cls_set: Consecutive Conv layers whose weights and biases need to be equalized.
                        Second Conv layer is a depth-wise conv and third conv layer is point-wise conv
        :return: Scaling factors S_12 and S_23 : numpy arrays
        
API to invoke equalize layer params for depth wise separable layers(update for weights and bias is in place)
API to invoke equalize layer params for depth wise separable layers ( update for weights and bias is in place )
def scale_cls_set_with_depthwise_layers(cls_set: Tuple[torch.nn.Conv2d,
                                                           torch.nn.Conv2d,
                                                           torch.nn.Conv2d]) -> [np.ndarray, np.ndarray]:
        on_gpu = False
        for module in cls_set:
            if not isinstance(module, (torch.nn.Conv2d, torch.nn.ConvTranspose2d)):
                raise ValueError("Only conv layers are supported for cross layer equalization")
            if module.weight.is_cuda:
                on_gpu = True
                module.cpu()
        prev_layer_params = libpymo.EqualizationParams()
        curr_layer_params = libpymo.EqualizationParams()
        next_layer_params = libpymo.EqualizationParams()
        if isinstance(cls_set[0], torch.nn.ConvTranspose2d):
            cls_set[0].weight.data = cls_set[0].weight.data.permute(1, 0, 2, 3)
        if isinstance(cls_set[2], torch.nn.ConvTranspose2d):
            cls_set[2].weight.data = cls_set[2].weight.data.permute(1, 0, 2, 3)
        assert cls_set[1].groups > 1
        prev_layer_params.weight = cls_set[0].weight.detach().numpy().flatten()
        prev_layer_params.weightShape = np.array(cls_set[0].weight.shape)
        curr_layer_params.weight = cls_set[1].weight.detach().numpy().flatten()
        curr_layer_params.weightShape = np.array(cls_set[1].weight.shape)
        next_layer_params.weight = cls_set[2].weight.detach().numpy().flatten()
        next_layer_params.weightShape = np.array(cls_set[2].weight.shape)
        if cls_set[0].bias is not None:
            prev_layer_params.bias = cls_set[0].bias.detach().numpy()
        else:
            prev_layer_params.isBiasNone = True
        if cls_set[1].bias is not None:
            curr_layer_params.bias = cls_set[1].bias.detach().numpy()
        else:
            curr_layer_params.isBiasNone = True
        scaling_params = libpymo.scaleDepthWiseSeparableLayer(prev_layer_params, curr_layer_params, next_layer_params)
        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)
        cls_set[2].weight.data = torch.from_numpy(np.reshape(next_layer_params.weight,
                                                             next_layer_params.weightShape))
        cls_set[2].weight.data = cls_set[2].weight.data.type(torch.FloatTensor)
        if isinstance(cls_set[2], torch.nn.ConvTranspose2d):
            cls_set[2].weight.data = cls_set[2].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 cls_set[1].bias is not None:
            cls_set[1].bias.data = torch.from_numpy(np.reshape(curr_layer_params.bias,
                                                               curr_layer_params.weightShape[0]))
            cls_set[1].bias.data = cls_set[1].bias.data.type(torch.FloatTensor)
        if on_gpu:
            for module in cls_set:
                module.to(device="cuda")
        return scaling_params.scalingMatrix12, scaling_params.scalingMatrix23
def scale_cls_set_with_depthwise_layers ( cls_set : Tuple [ torch . nn . Conv2d , torch . nn . Conv2d , torch . nn . Conv2d ] ) -> [ np . ndarray , np . ndarray ] : on_gpu = False for module in cls_set : if not isinstance ( module , ( torch . nn . Conv2d , torch . nn . ConvTranspose2d ) ) : raise ValueError ( "Only conv layers are supported for cross layer equalization" ) if module . weight . is_cuda : on_gpu = True module . cpu ( ) prev_layer_params = libpymo . EqualizationParams ( ) curr_layer_params = libpymo . EqualizationParams ( ) next_layer_params = libpymo . EqualizationParams ( ) if isinstance ( cls_set [ 0 ] , torch . nn . ConvTranspose2d ) : cls_set [ 0 ] . weight . data = cls_set [ 0 ] . weight . data . permute ( 1 , 0 , 2 , 3 ) if isinstance ( cls_set [ 2 ] , torch . nn . ConvTranspose2d ) : cls_set [ 2 ] . weight . data = cls_set [ 2 ] . weight . data . permute ( 1 , 0 , 2 , 3 ) assert cls_set [ 1 ] . groups > 1 prev_layer_params . weight = cls_set [ 0 ] . weight . detach ( ) . numpy ( ) . flatten ( ) prev_layer_params . weightShape = np . array ( cls_set [ 0 ] . weight . shape ) curr_layer_params . weight = cls_set [ 1 ] . weight . detach ( ) . numpy ( ) . flatten ( ) curr_layer_params . weightShape = np . array ( cls_set [ 1 ] . weight . shape ) next_layer_params . weight = cls_set [ 2 ] . weight . detach ( ) . numpy ( ) . flatten ( ) next_layer_params . weightShape = np . array ( cls_set [ 2 ] . weight . shape ) if cls_set [ 0 ] . bias is not None : prev_layer_params . bias = cls_set [ 0 ] . bias . detach ( ) . numpy ( ) else : prev_layer_params . isBiasNone = True if cls_set [ 1 ] . bias is not None : curr_layer_params . bias = cls_set [ 1 ] . bias . detach ( ) . numpy ( ) else : curr_layer_params . isBiasNone = True scaling_params = libpymo . scaleDepthWiseSeparableLayer ( prev_layer_params , curr_layer_params , next_layer_params ) 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 ) cls_set [ 2 ] . weight . data = torch . from_numpy ( np . reshape ( next_layer_params . weight , next_layer_params . weightShape ) ) cls_set [ 2 ] . weight . data = cls_set [ 2 ] . weight . data . type ( torch . FloatTensor ) if isinstance ( cls_set [ 2 ] , torch . nn . ConvTranspose2d ) : cls_set [ 2 ] . weight . data = cls_set [ 2 ] . 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 cls_set [ 1 ] . bias is not None : cls_set [ 1 ] . bias . data = torch . from_numpy ( np . reshape ( curr_layer_params . bias , curr_layer_params . weightShape [ 0 ] ) ) cls_set [ 1 ] . bias . data = cls_set [ 1 ] . bias . data . type ( torch . FloatTensor ) if on_gpu : for module in cls_set : module . to ( device = "cuda" ) return scaling_params . scalingMatrix12 , scaling_params . scalingMatrix23
API to invoke equalize layer params for depth wise separable layers(update for weights and bias is in place)
API to invoke equalize layer params for depth wise separable layers ( update for weights and bias is in place )
"""
        API to invoke equalize layer params for depth wise separable layers(update for weights and bias is in place)
        :param cls_set: Consecutive Conv layers whose weights and biases need to be equalized.
                        Second Conv layer is a depth-wise conv and third conv layer is point-wise conv
        :return: Scaling factors S_12 and S_23 : numpy arrays
        """ # pylint:disable=too-many-branches # Create structs for holding layer weights and bias parameters
Scaling factors S_12 and S_23 : numpy arrays
Scaling factors S_12 and S_23 : numpy arrays
cls_set
Tuple[torch.nn.Conv2d,
                                                           torch.nn.Conv2d,
                                                           torch.nn.Conv2d]
Consecutive Conv layers whose weights and biases need to be equalized.
Second Conv layer is a depth-wise conv and third conv layer is point-wise conv
Consecutive Conv layers whose weights and biases need to be equalized . Second Conv layer is a depth - wise conv and third conv layer is point - wise conv