TrainingExtensions/torch/src/python/aimet_torch/cross_layer_equalization.py
def call_mo_scale(cls_set: Union[Tuple[torch.nn.Conv2d, torch.nn.Conv2d],
                                     Tuple[torch.nn.ConvTranspose2d, torch.nn.ConvTranspose2d]]) \
            -> Tuple[np.ndarray, libpymo.EqualizationParams, libpymo.EqualizationParams]:
        """
        Invokes scale API in model optimization library
        :param cls_set: Consecutive Conv layers Tuple whose weights and biases need to be equalized
        :return: Scaling factor, prev and current layer updated parameters
        """
        # Create structs for holding layer weights and bias parameters
        prev_layer_params = libpymo.EqualizationParams()
        curr_layer_params = libpymo.EqualizationParams()

        weight_set_0 = cls_set[0].weight
        # Transpose weights to C, N, H, W from N, C, H, W since axis are flipped for transposed conv
        if isinstance(cls_set[0], torch.nn.ConvTranspose2d):
            weight_set_0 = weight_set_0.permute(1, 0, 2, 3)
        prev_layer_params.weight = weight_set_0.detach().numpy().reshape(-1)
        prev_layer_params.weightShape = np.array(weight_set_0.shape)

        weight_set_1 = cls_set[1].weight
        # Transpose weights to C, N, H, W from N, C, H, W since axis are flipped for transposed conv
        if isinstance(cls_set[1], torch.nn.ConvTranspose2d):
            weight_set_1 = weight_set_1.permute(1, 0, 2, 3)
        curr_layer_params.weight = weight_set_1.detach().numpy().reshape(-1)
        curr_layer_params.weightShape = np.array(weight_set_1.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

        scaling_factor = libpymo.scaleLayerParams(prev_layer_params, curr_layer_params)
        return scaling_factor, prev_layer_params, curr_layer_params

        Invokes scale API in model optimization library
        :param cls_set: Consecutive Conv layers Tuple whose weights and biases need to be equalized
        :return: Scaling factor, prev and current layer updated parameters
        
Invokes scale API in model optimization library
Invokes scale API in model optimization library
def call_mo_scale(cls_set: Union[Tuple[torch.nn.Conv2d, torch.nn.Conv2d],
                                     Tuple[torch.nn.ConvTranspose2d, torch.nn.ConvTranspose2d]]) \
            -> Tuple[np.ndarray, libpymo.EqualizationParams, libpymo.EqualizationParams]:
        prev_layer_params = libpymo.EqualizationParams()
        curr_layer_params = libpymo.EqualizationParams()
        weight_set_0 = cls_set[0].weight
        if isinstance(cls_set[0], torch.nn.ConvTranspose2d):
            weight_set_0 = weight_set_0.permute(1, 0, 2, 3)
        prev_layer_params.weight = weight_set_0.detach().numpy().reshape(-1)
        prev_layer_params.weightShape = np.array(weight_set_0.shape)
        weight_set_1 = cls_set[1].weight
        if isinstance(cls_set[1], torch.nn.ConvTranspose2d):
            weight_set_1 = weight_set_1.permute(1, 0, 2, 3)
        curr_layer_params.weight = weight_set_1.detach().numpy().reshape(-1)
        curr_layer_params.weightShape = np.array(weight_set_1.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
        scaling_factor = libpymo.scaleLayerParams(prev_layer_params, curr_layer_params)
        return scaling_factor, prev_layer_params, curr_layer_params
def call_mo_scale ( cls_set : Union [ Tuple [ torch . nn . Conv2d , torch . nn . Conv2d ] , Tuple [ torch . nn . ConvTranspose2d , torch . nn . ConvTranspose2d ] ] ) -> Tuple [ np . ndarray , libpymo . EqualizationParams , libpymo . EqualizationParams ] : prev_layer_params = libpymo . EqualizationParams ( ) curr_layer_params = libpymo . EqualizationParams ( ) weight_set_0 = cls_set [ 0 ] . weight if isinstance ( cls_set [ 0 ] , torch . nn . ConvTranspose2d ) : weight_set_0 = weight_set_0 . permute ( 1 , 0 , 2 , 3 ) prev_layer_params . weight = weight_set_0 . detach ( ) . numpy ( ) . reshape ( - 1 ) prev_layer_params . weightShape = np . array ( weight_set_0 . shape ) weight_set_1 = cls_set [ 1 ] . weight if isinstance ( cls_set [ 1 ] , torch . nn . ConvTranspose2d ) : weight_set_1 = weight_set_1 . permute ( 1 , 0 , 2 , 3 ) curr_layer_params . weight = weight_set_1 . detach ( ) . numpy ( ) . reshape ( - 1 ) curr_layer_params . weightShape = np . array ( weight_set_1 . 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 scaling_factor = libpymo . scaleLayerParams ( prev_layer_params , curr_layer_params ) return scaling_factor , prev_layer_params , curr_layer_params
Invokes scale API in model optimization library
Invokes scale API in model optimization library
"""
        Invokes scale API in model optimization library
        :param cls_set: Consecutive Conv layers Tuple whose weights and biases need to be equalized
        :return: Scaling factor, prev and current layer updated parameters
        """ # Create structs for holding layer weights and bias parameters # Transpose weights to C, N, H, W from N, C, H, W since axis are flipped for transposed conv # Transpose weights to C, N, H, W from N, C, H, W since axis are flipped for transposed conv
Scaling factor, prev and current layer updated parameters
Scaling factor prev and current layer updated parameters
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