Exemplo n.º 1
0
    def _sparsify_weights(
            self, target_model: NNCFNetwork) -> List[PTInsertionCommand]:
        device = next(target_model.parameters()).device
        sparsified_module_nodes = target_model.get_weighted_original_graph_nodes(
            nncf_module_names=self.compressed_nncf_module_names)
        insertion_commands = []
        for module_node in sparsified_module_nodes:
            node_name = module_node.node_name

            if not self._should_consider_scope(node_name):
                nncf_logger.info(
                    "Ignored adding Weight Sparsifier in scope: {}".format(
                        node_name))
                continue

            nncf_logger.info(
                "Adding Weight Sparsifier in scope: {}".format(node_name))
            compression_lr_multiplier = \
                self.config.get_redefinable_global_param_value_for_algo('compression_lr_multiplier',
                                                                        self.name)
            operation = self.create_weight_sparsifying_operation(
                module_node, compression_lr_multiplier)
            hook = operation.to(device)
            insertion_commands.append(
                PTInsertionCommand(
                    PTTargetPoint(TargetType.OPERATION_WITH_WEIGHTS,
                                  target_node_name=node_name), hook,
                    TransformationPriority.SPARSIFICATION_PRIORITY))
            sparsified_module = target_model.get_containing_module(node_name)
            self._sparsified_module_info.append(
                SparseModuleInfo(node_name, sparsified_module, hook))

        return insertion_commands
Exemplo n.º 2
0
    def _binarize_weights_and_module_inputs(
            self, target_model: NNCFNetwork) -> List[PTInsertionCommand]:
        device = next(target_model.parameters()).device

        module_nodes = target_model.get_weighted_original_graph_nodes(
            nncf_module_names=self.compressed_nncf_module_names)

        insertion_commands = []
        for module_node in module_nodes:
            if not self._should_consider_scope(module_node.node_name):
                nncf_logger.info(
                    "Ignored adding binarizers in scope: {}".format(
                        module_node.node_name))
                continue

            nncf_logger.info("Adding Weight binarizer in scope: {}".format(
                module_node.node_name))
            op_weights = self.__create_binarize_module().to(device)

            nncf_logger.info("Adding Activation binarizer in scope: {}".format(
                module_node.node_name))

            compression_lr_multiplier = self.config.get_redefinable_global_param_value_for_algo(
                'compression_lr_multiplier', self.name)

            op_inputs = UpdateInputs(
                ActivationBinarizationScaleThreshold(
                    module_node.layer_attributes.get_weight_shape(),
                    compression_lr_multiplier=compression_lr_multiplier)).to(
                        device)

            ip_w = PTTargetPoint(TargetType.OPERATION_WITH_WEIGHTS,
                                 target_node_name=module_node.node_name)
            insertion_commands.append(
                PTInsertionCommand(
                    ip_w, op_weights,
                    TransformationPriority.QUANTIZATION_PRIORITY))

            ip_i = PTTargetPoint(TargetType.PRE_LAYER_OPERATION,
                                 target_node_name=module_node.node_name,
                                 input_port_id=0)
            insertion_commands.append(
                PTInsertionCommand(
                    ip_i, op_inputs,
                    TransformationPriority.QUANTIZATION_PRIORITY))
        return insertion_commands
 def _handle_frozen_layers(self, target_model: NNCFNetwork):
     scopes_of_frozen_layers = []
     for weighted_node in target_model.get_weighted_original_graph_nodes():
         if not weighted_node.layer_attributes.weight_requires_grad:
             if self._should_consider_scope(weighted_node.node_name):
                 scopes_of_frozen_layers.append(weighted_node.node_name)
     scopes_to_print = '\n'.join(scopes_of_frozen_layers)
     if len(scopes_of_frozen_layers) > 0:
         is_allowed, reason = self._are_frozen_layers_allowed()
         if is_allowed:
             nncf_logger.warning(
                 '{}, compressing them without tuning weights.\n'
                 'Frozen layers:\n'
                 '{}'.format(reason, scopes_to_print))
         else:
             raise RuntimeError(
                 f'{reason}.\n'
                 f'Please unfreeze them or put into the Ignored Scope.\n'
                 f'Frozen Layers:\n'
                 f'{scopes_to_print}')