nncf/experimental/torch/nas/bootstrapNAS/elasticity/elastic_width.py
def build(self, target_model: NNCFNetwork) -> ElasticWidthHandler:
        """
        Creates modifications to the given NNCFNetwork for introducing elastic width 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 width.
        """
        filter_importance_str = self._params.filter_importance
        filter_importance = FILTER_IMPORTANCE_FUNCTIONS.get(filter_importance_str)

        graph = target_model.get_original_graph()
        device = next(target_model.parameters()).device

        if not self._grouped_node_names_to_prune:
            prunable_types = [NNCFConv2d, NNCFLinear]
            prune_operations_types = [pt.op_func_name for pt in prunable_types]
            types_of_grouping_ops = PTElementwisePruningOp.get_all_op_aliases()
            pruning_node_selector = PruningNodeSelector(PT_PRUNING_OPERATOR_METATYPES,
                                                        prune_operations_types,
                                                        types_of_grouping_ops,
                                                        ignored_scopes=self._ignored_scopes,
                                                        target_scopes=self._target_scopes,
                                                        prune_first=True,
                                                        prune_downsample_convs=True)
            groups_of_nodes_to_prune = pruning_node_selector.create_pruning_groups(graph)
            for group in groups_of_nodes_to_prune.get_all_clusters():
                grouped_node_names = [node.node_name for node in group.elements]
                self._grouped_node_names_to_prune.append(grouped_node_names)

        transformation_commands = []
        pruned_module_groups_info = Clusterization[ElasticWidthInfo](id_fn=lambda x: x.node_name)
        node_name_vs_dynamic_input_width_op_map = OrderedDict()

        metatype_vs_elastic_op_creator = {
            PTConv2dMetatype: self._create_elastic_conv_width_op,
            PTDepthwiseConv2dSubtype: self._create_elastic_conv_width_op,
            PTLinearMetatype: self._create_elastic_linear_width_op
        }

        for i, grouped_node_names in enumerate(self._grouped_node_names_to_prune):
            group_minfos = []
            list_of_node_ids = []
            for node_name in grouped_node_names:
                node = graph.get_node_by_name(node_name)
                metatype = node.metatype
                list_of_node_ids.append(node.node_id)
                layer_attrs = node.layer_attributes
                if metatype not in metatype_vs_elastic_op_creator:
                    raise RuntimeError(f'Elastic width is not supported for {metatype}')
                elastic_op_creator = metatype_vs_elastic_op_creator[metatype]
                elastic_width_operation = elastic_op_creator(layer_attrs, node_name, self._params)
                elastic_width_operation.to(device)
                update_conv_params_op = UpdateWeightAndOptionalBias(elastic_width_operation)
                transformation_commands.append(
                    PTInsertionCommand(
                        PTTargetPoint(
                            TargetType.PRE_LAYER_OPERATION,
                            target_node_name=node_name
                        ),
                        update_conv_params_op,
                        TransformationPriority.PRUNING_PRIORITY
                    )
                )
                pruned_module = target_model.get_containing_module(node_name)
                assert isinstance(pruned_module, (nn.Conv2d, nn.Linear)), \
                    'currently prune only 2D Convolutions and Linear layers'

                group_minfos.append(ElasticWidthInfo(node_name=node_name,
                                                     module=pruned_module,
                                                     elastic_op=elastic_width_operation,
                                                     node_id=node.node_id,
                                                     is_depthwise=is_prunable_depthwise_conv(node)))

            cluster = Cluster[ElasticWidthInfo](i, group_minfos, list_of_node_ids)
            pruned_module_groups_info.add_cluster(cluster)

        metatype_vs_dynamic_input_op_creator = {
            PTConv2dMetatype: self._create_dynamic_conv_input_op,
            PTDepthwiseConv2dSubtype: self._create_dynamic_dw_conv_input_op,
            PTBatchNormMetatype: self._create_dynamic_bn_input_op,
            PTLinearMetatype: self._create_dynamic_linear_input_op
        }
        for metatype, op_creator in metatype_vs_dynamic_input_op_creator.items():
            nodes = graph.get_nodes_by_metatypes([metatype])
            for node in nodes:
                node_name = node.node_name
                nncf_logger.info("Adding Dynamic Input Op for {} in scope: {}".format(metatype.name, node_name))
                layer_attrs = node.layer_attributes
                update_module_params = op_creator(layer_attrs, node_name).to(device)
                node_name_vs_dynamic_input_width_op_map[node_name] = update_module_params.op
                transformation_commands.append(
                    PTInsertionCommand(
                        PTTargetPoint(
                            TargetType.PRE_LAYER_OPERATION,
                            target_node_name=node_name
                        ),
                        update_module_params,
                        priority=TransformationPriority.DEFAULT_PRIORITY
                    )
                )

        return ElasticWidthHandler(target_model, filter_importance, self._weights_normalizer,
                                   node_name_vs_dynamic_input_width_op_map,
                                   pruned_module_groups_info, transformation_commands)

        Creates modifications to the given NNCFNetwork for introducing elastic width 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 width.
        
Creates modifications to the given NNCFNetwork for introducing elastic width and creates a handler object that
can manipulate this elasticity.
Creates modifications to the given NNCFNetwork for introducing elastic width and creates a handler object that can manipulate this elasticity .
def build(self, target_model: NNCFNetwork) -> ElasticWidthHandler:
        filter_importance_str = self._params.filter_importance
        filter_importance = FILTER_IMPORTANCE_FUNCTIONS.get(filter_importance_str)
        graph = target_model.get_original_graph()
        device = next(target_model.parameters()).device
        if not self._grouped_node_names_to_prune:
            prunable_types = [NNCFConv2d, NNCFLinear]
            prune_operations_types = [pt.op_func_name for pt in prunable_types]
            types_of_grouping_ops = PTElementwisePruningOp.get_all_op_aliases()
            pruning_node_selector = PruningNodeSelector(PT_PRUNING_OPERATOR_METATYPES,
                                                        prune_operations_types,
                                                        types_of_grouping_ops,
                                                        ignored_scopes=self._ignored_scopes,
                                                        target_scopes=self._target_scopes,
                                                        prune_first=True,
                                                        prune_downsample_convs=True)
            groups_of_nodes_to_prune = pruning_node_selector.create_pruning_groups(graph)
            for group in groups_of_nodes_to_prune.get_all_clusters():
                grouped_node_names = [node.node_name for node in group.elements]
                self._grouped_node_names_to_prune.append(grouped_node_names)
        transformation_commands = []
        pruned_module_groups_info = Clusterization[ElasticWidthInfo](id_fn=lambda x: x.node_name)
        node_name_vs_dynamic_input_width_op_map = OrderedDict()
        metatype_vs_elastic_op_creator = {
            PTConv2dMetatype: self._create_elastic_conv_width_op,
            PTDepthwiseConv2dSubtype: self._create_elastic_conv_width_op,
            PTLinearMetatype: self._create_elastic_linear_width_op
        }
        for i, grouped_node_names in enumerate(self._grouped_node_names_to_prune):
            group_minfos = []
            list_of_node_ids = []
            for node_name in grouped_node_names:
                node = graph.get_node_by_name(node_name)
                metatype = node.metatype
                list_of_node_ids.append(node.node_id)
                layer_attrs = node.layer_attributes
                if metatype not in metatype_vs_elastic_op_creator:
                    raise RuntimeError(f'Elastic width is not supported for {metatype}')
                elastic_op_creator = metatype_vs_elastic_op_creator[metatype]
                elastic_width_operation = elastic_op_creator(layer_attrs, node_name, self._params)
                elastic_width_operation.to(device)
                update_conv_params_op = UpdateWeightAndOptionalBias(elastic_width_operation)
                transformation_commands.append(
                    PTInsertionCommand(
                        PTTargetPoint(
                            TargetType.PRE_LAYER_OPERATION,
                            target_node_name=node_name
                        ),
                        update_conv_params_op,
                        TransformationPriority.PRUNING_PRIORITY
                    )
                )
                pruned_module = target_model.get_containing_module(node_name)
                assert isinstance(pruned_module, (nn.Conv2d, nn.Linear)), \
                    'currently prune only 2D Convolutions and Linear layers'
                group_minfos.append(ElasticWidthInfo(node_name=node_name,
                                                     module=pruned_module,
                                                     elastic_op=elastic_width_operation,
                                                     node_id=node.node_id,
                                                     is_depthwise=is_prunable_depthwise_conv(node)))
            cluster = Cluster[ElasticWidthInfo](i, group_minfos, list_of_node_ids)
            pruned_module_groups_info.add_cluster(cluster)
        metatype_vs_dynamic_input_op_creator = {
            PTConv2dMetatype: self._create_dynamic_conv_input_op,
            PTDepthwiseConv2dSubtype: self._create_dynamic_dw_conv_input_op,
            PTBatchNormMetatype: self._create_dynamic_bn_input_op,
            PTLinearMetatype: self._create_dynamic_linear_input_op
        }
        for metatype, op_creator in metatype_vs_dynamic_input_op_creator.items():
            nodes = graph.get_nodes_by_metatypes([metatype])
            for node in nodes:
                node_name = node.node_name
                nncf_logger.info("Adding Dynamic Input Op for {} in scope: {}".format(metatype.name, node_name))
                layer_attrs = node.layer_attributes
                update_module_params = op_creator(layer_attrs, node_name).to(device)
                node_name_vs_dynamic_input_width_op_map[node_name] = update_module_params.op
                transformation_commands.append(
                    PTInsertionCommand(
                        PTTargetPoint(
                            TargetType.PRE_LAYER_OPERATION,
                            target_node_name=node_name
                        ),
                        update_module_params,
                        priority=TransformationPriority.DEFAULT_PRIORITY
                    )
                )
        return ElasticWidthHandler(target_model, filter_importance, self._weights_normalizer,
                                   node_name_vs_dynamic_input_width_op_map,
                                   pruned_module_groups_info, transformation_commands)
def build ( self , target_model : NNCFNetwork ) -> ElasticWidthHandler : filter_importance_str = self . _params . filter_importance filter_importance = FILTER_IMPORTANCE_FUNCTIONS . get ( filter_importance_str ) graph = target_model . get_original_graph ( ) device = next ( target_model . parameters ( ) ) . device if not self . _grouped_node_names_to_prune : prunable_types = [ NNCFConv2d , NNCFLinear ] prune_operations_types = [ pt . op_func_name for pt in prunable_types ] types_of_grouping_ops = PTElementwisePruningOp . get_all_op_aliases ( ) pruning_node_selector = PruningNodeSelector ( PT_PRUNING_OPERATOR_METATYPES , prune_operations_types , types_of_grouping_ops , ignored_scopes = self . _ignored_scopes , target_scopes = self . _target_scopes , prune_first = True , prune_downsample_convs = True ) groups_of_nodes_to_prune = pruning_node_selector . create_pruning_groups ( graph ) for group in groups_of_nodes_to_prune . get_all_clusters ( ) : grouped_node_names = [ node . node_name for node in group . elements ] self . _grouped_node_names_to_prune . append ( grouped_node_names ) transformation_commands = [ ] pruned_module_groups_info = Clusterization [ ElasticWidthInfo ] ( id_fn = lambda x : x . node_name ) node_name_vs_dynamic_input_width_op_map = OrderedDict ( ) metatype_vs_elastic_op_creator = { PTConv2dMetatype : self . _create_elastic_conv_width_op , PTDepthwiseConv2dSubtype : self . _create_elastic_conv_width_op , PTLinearMetatype : self . _create_elastic_linear_width_op } for i , grouped_node_names in enumerate ( self . _grouped_node_names_to_prune ) : group_minfos = [ ] list_of_node_ids = [ ] for node_name in grouped_node_names : node = graph . get_node_by_name ( node_name ) metatype = node . metatype list_of_node_ids . append ( node . node_id ) layer_attrs = node . layer_attributes if metatype not in metatype_vs_elastic_op_creator : raise RuntimeError ( f'Elastic width is not supported for {metatype}' ) elastic_op_creator = metatype_vs_elastic_op_creator [ metatype ] elastic_width_operation = elastic_op_creator ( layer_attrs , node_name , self . _params ) elastic_width_operation . to ( device ) update_conv_params_op = UpdateWeightAndOptionalBias ( elastic_width_operation ) transformation_commands . append ( PTInsertionCommand ( PTTargetPoint ( TargetType . PRE_LAYER_OPERATION , target_node_name = node_name ) , update_conv_params_op , TransformationPriority . PRUNING_PRIORITY ) ) pruned_module = target_model . get_containing_module ( node_name ) assert isinstance ( pruned_module , ( nn . Conv2d , nn . Linear ) ) , 'currently prune only 2D Convolutions and Linear layers' group_minfos . append ( ElasticWidthInfo ( node_name = node_name , module = pruned_module , elastic_op = elastic_width_operation , node_id = node . node_id , is_depthwise = is_prunable_depthwise_conv ( node ) ) ) cluster = Cluster [ ElasticWidthInfo ] ( i , group_minfos , list_of_node_ids ) pruned_module_groups_info . add_cluster ( cluster ) metatype_vs_dynamic_input_op_creator = { PTConv2dMetatype : self . _create_dynamic_conv_input_op , PTDepthwiseConv2dSubtype : self . _create_dynamic_dw_conv_input_op , PTBatchNormMetatype : self . _create_dynamic_bn_input_op , PTLinearMetatype : self . _create_dynamic_linear_input_op } for metatype , op_creator in metatype_vs_dynamic_input_op_creator . items ( ) : nodes = graph . get_nodes_by_metatypes ( [ metatype ] ) for node in nodes : node_name = node . node_name nncf_logger . info ( "Adding Dynamic Input Op for {} in scope: {}" . format ( metatype . name , node_name ) ) layer_attrs = node . layer_attributes update_module_params = op_creator ( layer_attrs , node_name ) . to ( device ) node_name_vs_dynamic_input_width_op_map [ node_name ] = update_module_params . op transformation_commands . append ( PTInsertionCommand ( PTTargetPoint ( TargetType . PRE_LAYER_OPERATION , target_node_name = node_name ) , update_module_params , priority = TransformationPriority . DEFAULT_PRIORITY ) ) return ElasticWidthHandler ( target_model , filter_importance , self . _weights_normalizer , node_name_vs_dynamic_input_width_op_map , pruned_module_groups_info , transformation_commands )
Creates modifications to the given NNCFNetwork for introducing elastic width and creates a handler object that
can manipulate this elasticity.
Creates modifications to the given NNCFNetwork for introducing elastic width and creates a handler object that can manipulate this elasticity .
"""
        Creates modifications to the given NNCFNetwork for introducing elastic width 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 width.
        """
a handler object that can manipulate the elastic width.
a handler object that can manipulate the elastic width .
self
target_model
NNCFNetwork
a target NNCFNetwork for adding modifications
a target NNCFNetwork for adding modifications