nncf/experimental/torch/nas/bootstrapNAS/elasticity/elastic_kernel.py
def build(self, target_model: NNCFNetwork) -> ElasticKernelHandler:
        """
        Creates modifications to the given NNCFNetwork for introducing elastic kernel and creates a handler object that
        can manipulate this elasticity.

        :param target_model: a target NNCFNetwork for adding modifications
        :return: a handler object that can manipulate the elastic kernel.
        """
        elastic_kernel_ops = []  # type: List[ElasticKernelOp]
        transformation_commands = []

        graph = target_model.get_original_graph()
        device = next(target_model.parameters()).device
        pad_commands = []

        if not self._node_names_to_make_elastic:
            elastic_kernel_types = [NNCFConv2d.op_func_name]
            all_elastic_kernel_nodes = graph.get_nodes_by_types(elastic_kernel_types)  # type: List[NNCFNode]
            self._node_names_to_make_elastic = [node.node_name for node in all_elastic_kernel_nodes]

        for node_name in self._node_names_to_make_elastic:
            nncf_logger.info("Adding Elastic Kernel op for Conv2D in scope: {}".format(node_name))
            node = graph.get_node_by_name(node_name)
            layer_attrs = node.layer_attributes
            assert isinstance(layer_attrs, ConvolutionLayerAttributes), 'Conv2D can have elastic kernel only'
            max_kernel_size = layer_attrs.kernel_size[0]
            elastic_kernel_op = ElasticKernelConv2DOp(max_kernel_size, node_name, self._params)
            elastic_kernel_op.to(device)
            update_conv_params_op = UpdateWeight(elastic_kernel_op)
            transformation_commands.append(
                PTInsertionCommand(
                    PTTargetPoint(
                        TargetType.PRE_LAYER_OPERATION,
                        target_node_name=node_name
                    ),
                    update_conv_params_op,
                    TransformationPriority.PRUNING_PRIORITY
                )
            )
            # TODO(nlyalyus): ticket 71613: remove hardcode for EfficientNet
            #   should be pairing of pad and conv operations by graph analysis
            #   get
            #       1) input size before pad
            #       2) input size after pad
            #       3) active kernel size
            #       4) max kernel size
            #       5) built-in conv padding
            #   output
            #       how to change padded input (e.g. center crop)
            if 'NNCFUserConv2dStaticSamePadding' in node.node_name:
                if max_kernel_size >= 3:
                    crop_op = ElasticKernelInputForExternalPadding(elastic_kernel_op, max_kernel_size)
                    op = UpdateInputs(crop_op).to(device)
                    nncf_logger.warning('Padded input will be cropped for {}'.format(node_name))
                    pad_commands.append(
                        PTInsertionCommand(
                            PTTargetPoint(
                                target_type=TargetType.PRE_LAYER_OPERATION,
                                target_node_name=node_name
                            ),
                            op,
                            TransformationPriority.DEFAULT_PRIORITY
                        )
                    )
            else:
                # Padding
                ap = ElasticKernelPaddingAdjustment(elastic_kernel_op)
                pad_op = UpdatePadding(ap).to(device)
                nncf_logger.warning('Padding will be adjusted for {}'.format(node_name))
                pad_commands.append(
                    PTInsertionCommand(
                        PTTargetPoint(
                            target_type=TargetType.PRE_LAYER_OPERATION,
                            target_node_name=node_name
                        ),
                        pad_op,
                        TransformationPriority.DEFAULT_PRIORITY
                    )
                )
            elastic_kernel_ops.append(elastic_kernel_op)
        if pad_commands:
            transformation_commands += pad_commands

        return ElasticKernelHandler(elastic_kernel_ops, transformation_commands)

        Creates modifications to the given NNCFNetwork for introducing elastic kernel and creates a handler object that
        can manipulate this elasticity.

        :param target_model: a target NNCFNetwork for adding modifications
        :return: a handler object that can manipulate the elastic kernel.
        
Creates modifications to the given NNCFNetwork for introducing elastic kernel and creates a handler object that
can manipulate this elasticity.
Creates modifications to the given NNCFNetwork for introducing elastic kernel and creates a handler object that can manipulate this elasticity .
def build(self, target_model: NNCFNetwork) -> ElasticKernelHandler:
        elastic_kernel_ops = []  
        transformation_commands = []
        graph = target_model.get_original_graph()
        device = next(target_model.parameters()).device
        pad_commands = []
        if not self._node_names_to_make_elastic:
            elastic_kernel_types = [NNCFConv2d.op_func_name]
            all_elastic_kernel_nodes = graph.get_nodes_by_types(elastic_kernel_types)  
            self._node_names_to_make_elastic = [node.node_name for node in all_elastic_kernel_nodes]
        for node_name in self._node_names_to_make_elastic:
            nncf_logger.info("Adding Elastic Kernel op for Conv2D in scope: {}".format(node_name))
            node = graph.get_node_by_name(node_name)
            layer_attrs = node.layer_attributes
            assert isinstance(layer_attrs, ConvolutionLayerAttributes), 'Conv2D can have elastic kernel only'
            max_kernel_size = layer_attrs.kernel_size[0]
            elastic_kernel_op = ElasticKernelConv2DOp(max_kernel_size, node_name, self._params)
            elastic_kernel_op.to(device)
            update_conv_params_op = UpdateWeight(elastic_kernel_op)
            transformation_commands.append(
                PTInsertionCommand(
                    PTTargetPoint(
                        TargetType.PRE_LAYER_OPERATION,
                        target_node_name=node_name
                    ),
                    update_conv_params_op,
                    TransformationPriority.PRUNING_PRIORITY
                )
            )
            if 'NNCFUserConv2dStaticSamePadding' in node.node_name:
                if max_kernel_size >= 3:
                    crop_op = ElasticKernelInputForExternalPadding(elastic_kernel_op, max_kernel_size)
                    op = UpdateInputs(crop_op).to(device)
                    nncf_logger.warning('Padded input will be cropped for {}'.format(node_name))
                    pad_commands.append(
                        PTInsertionCommand(
                            PTTargetPoint(
                                target_type=TargetType.PRE_LAYER_OPERATION,
                                target_node_name=node_name
                            ),
                            op,
                            TransformationPriority.DEFAULT_PRIORITY
                        )
                    )
            else:
                ap = ElasticKernelPaddingAdjustment(elastic_kernel_op)
                pad_op = UpdatePadding(ap).to(device)
                nncf_logger.warning('Padding will be adjusted for {}'.format(node_name))
                pad_commands.append(
                    PTInsertionCommand(
                        PTTargetPoint(
                            target_type=TargetType.PRE_LAYER_OPERATION,
                            target_node_name=node_name
                        ),
                        pad_op,
                        TransformationPriority.DEFAULT_PRIORITY
                    )
                )
            elastic_kernel_ops.append(elastic_kernel_op)
        if pad_commands:
            transformation_commands += pad_commands
        return ElasticKernelHandler(elastic_kernel_ops, transformation_commands)
def build ( self , target_model : NNCFNetwork ) -> ElasticKernelHandler : elastic_kernel_ops = [ ] transformation_commands = [ ] graph = target_model . get_original_graph ( ) device = next ( target_model . parameters ( ) ) . device pad_commands = [ ] if not self . _node_names_to_make_elastic : elastic_kernel_types = [ NNCFConv2d . op_func_name ] all_elastic_kernel_nodes = graph . get_nodes_by_types ( elastic_kernel_types ) self . _node_names_to_make_elastic = [ node . node_name for node in all_elastic_kernel_nodes ] for node_name in self . _node_names_to_make_elastic : nncf_logger . info ( "Adding Elastic Kernel op for Conv2D in scope: {}" . format ( node_name ) ) node = graph . get_node_by_name ( node_name ) layer_attrs = node . layer_attributes assert isinstance ( layer_attrs , ConvolutionLayerAttributes ) , 'Conv2D can have elastic kernel only' max_kernel_size = layer_attrs . kernel_size [ 0 ] elastic_kernel_op = ElasticKernelConv2DOp ( max_kernel_size , node_name , self . _params ) elastic_kernel_op . to ( device ) update_conv_params_op = UpdateWeight ( elastic_kernel_op ) transformation_commands . append ( PTInsertionCommand ( PTTargetPoint ( TargetType . PRE_LAYER_OPERATION , target_node_name = node_name ) , update_conv_params_op , TransformationPriority . PRUNING_PRIORITY ) ) if 'NNCFUserConv2dStaticSamePadding' in node . node_name : if max_kernel_size >= 3 : crop_op = ElasticKernelInputForExternalPadding ( elastic_kernel_op , max_kernel_size ) op = UpdateInputs ( crop_op ) . to ( device ) nncf_logger . warning ( 'Padded input will be cropped for {}' . format ( node_name ) ) pad_commands . append ( PTInsertionCommand ( PTTargetPoint ( target_type = TargetType . PRE_LAYER_OPERATION , target_node_name = node_name ) , op , TransformationPriority . DEFAULT_PRIORITY ) ) else : ap = ElasticKernelPaddingAdjustment ( elastic_kernel_op ) pad_op = UpdatePadding ( ap ) . to ( device ) nncf_logger . warning ( 'Padding will be adjusted for {}' . format ( node_name ) ) pad_commands . append ( PTInsertionCommand ( PTTargetPoint ( target_type = TargetType . PRE_LAYER_OPERATION , target_node_name = node_name ) , pad_op , TransformationPriority . DEFAULT_PRIORITY ) ) elastic_kernel_ops . append ( elastic_kernel_op ) if pad_commands : transformation_commands += pad_commands return ElasticKernelHandler ( elastic_kernel_ops , transformation_commands )
Creates modifications to the given NNCFNetwork for introducing elastic kernel and creates a handler object that
can manipulate this elasticity.
Creates modifications to the given NNCFNetwork for introducing elastic kernel and creates a handler object that can manipulate this elasticity .
"""
        Creates modifications to the given NNCFNetwork for introducing elastic kernel and creates a handler object that
        can manipulate this elasticity.

        :param target_model: a target NNCFNetwork for adding modifications
        :return: a handler object that can manipulate the elastic kernel.
        """ # type: List[ElasticKernelOp] # type: List[NNCFNode] # TODO(nlyalyus): ticket 71613: remove hardcode for EfficientNet #   should be pairing of pad and conv operations by graph analysis #   get #       1) input size before pad #       2) input size after pad #       3) active kernel size #       4) max kernel size #       5) built-in conv padding #   output #       how to change padded input (e.g. center crop) # Padding
a handler object that can manipulate the elastic kernel.
a handler object that can manipulate the elastic kernel .
self
target_model
NNCFNetwork
a target NNCFNetwork for adding modifications
a target NNCFNetwork for adding modifications