Esempio n. 1
0
    def call(self, inputs, weights, training: tf.constant):
        """
        Apply rb sparsity mask to given weights.

        :param inputs: Target weights to sparsify.
        :param weights: Operation weights contains
            `mask` and param `trainable`.
        :param training: True if operation called in training mode
            else False
        """
        true_fn = lambda: apply_mask(inputs, self._calc_rb_binary_mask(weights))
        false_fn = lambda: apply_mask(inputs, binary_mask(weights['mask']))
        return smart_cond(training,
                          true_fn=lambda: smart_cond(weights['trainable'],
                                                     true_fn=true_fn, false_fn=false_fn),
                          false_fn=false_fn)
Esempio n. 2
0
 def test_loss_value(self, mask_value, ref_loss, frozen):
     sw, layer, op_name = get_RBSParsityWeigth_and_layer(frozen=frozen)
     op_weights = layer.ops_weights[op_name]
     mask = op_weights['mask']
     if mask_value is not None:
         mask.assign(tf.fill(mask.shape, mask_value))
     assert sw.loss(op_weights) == ref_loss
     w = tf.ones(1)
     assert apply_mask(w, st_binary_mask(mask)) == ref_loss
     sw.freeze(op_weights)
     assert sw(w, op_weights, tf.constant(True, tf.bool)) == ref_loss
Esempio n. 3
0
    def _collect_weights_descriptions(self) -> List[WeightDescription]:
        weights_descriptions = []
        excluded_names = []

        # Collect description for sparse weights i.e. weights for which
        # sparsity algorithm was applied.
        for wrapped_layer, weight_attr, op in get_nncf_operations(
                self._model, self._operation_names):
            weight = wrapped_layer.layer_weights[weight_attr]
            operation_weights = wrapped_layer.get_operation_weights(op.name)
            binary_mask = op.get_binary_mask(operation_weights)
            sparse_weight = apply_mask(weight, binary_mask)

            weights_descriptions.append(
                WeightDescription(
                    weight.name,
                    _get_standardized_weight_shape(weight.shape.as_list()),
                    tf.math.count_nonzero(sparse_weight).numpy().item(),
                    is_sparse=True))

            # Exclude this name because it has been processed.
            excluded_names.append(weight.name)

            # Exclude these names because they were added to the model
            # by the sparsity algorithm.
            excluded_names.extend([w.name for w in operation_weights.values()])
            if isinstance(op, BinaryMaskWithWeightsBackup):
                excluded_names.append(op.bkup_var.name)

        # Collect descriptions for rest weights.
        unique_weights = {
            id(w): w
            for w in self._model.weights if w.name not in excluded_names
        }.values()
        for weight in unique_weights:
            weights_descriptions.append(
                WeightDescription(weight.name,
                                  _get_standardized_weight_shape(
                                      weight.shape.as_list()),
                                  tf.math.count_nonzero(weight).numpy().item(),
                                  is_sparse=False))

            return weights_descriptions
Esempio n. 4
0
 def call(self, inputs, weights, _):
     self.bkup_var.assign(
         tf.where(weights['mask'] > 0.5, inputs, self.bkup_var))
     return apply_mask(self.bkup_var, weights['mask'])
Esempio n. 5
0
 def call(self, inputs, weights, _):
     return apply_mask(inputs, weights['mask'])