def call(self, inputs, training=None):
        # TODO(pulkitb): Construct graph for both training/eval modes.
        if training is None:
            training = K.learning_phase()

        x = inputs
        if self._requires_pre_quant():
            x = quant_ops.MovingAvgQuantize(inputs,
                                            self._min_pre_activation,
                                            self._max_pre_activation,
                                            ema_decay=0.999,
                                            is_training=training,
                                            num_bits=self.num_bits,
                                            symmetric=self.symmetric,
                                            name_prefix=self.name)

        x = self.activation(x)
        x = quant_ops.MovingAvgQuantize(x,
                                        self._min_post_activation,
                                        self._max_post_activation,
                                        ema_decay=0.999,
                                        is_training=training,
                                        num_bits=self.num_bits,
                                        symmetric=self.symmetric,
                                        name_prefix=self.name)

        return x
 def fn():
     return quant_ops.MovingAvgQuantize(outputs,
                                        self._min_activation,
                                        self._max_activation,
                                        ema_decay=0.999,
                                        is_training=is_training,
                                        num_bits=self._num_bits,
                                        symmetric=self._symmetric,
                                        narrow_range=False,
                                        name_prefix=self.layer.name)
Exemplo n.º 3
0
 def fn():
   return quant_ops.MovingAvgQuantize(
       outputs,
       init_min=-6.0,
       init_max=6.0,
       ema_decay=0.999,
       is_training=is_training,
       num_bits=self._num_bits,
       symmetric=self._symmetric,
       narrow_range=False,
       vars_collection=ops.GraphKeys.GLOBAL_VARIABLES,
       name_prefix=self.layer.name)
Exemplo n.º 4
0
 def testVariablesNotPartitioned_MovingAvg(self):
     # Variables added should not use a default partiioner since they are
     # scalar. There would be a tensorflow error thrown if the partitioner was
     # respected by the rewrite.
     with ops.Graph().as_default():
         with variable_scope.variable_scope(
                 'part',
                 partitioner=partitioned_variables.fixed_size_partitioner(
                     2)):
             x = array_ops.placeholder(dtypes.float32, shape=[2])
             _ = quant_ops.MovingAvgQuantize(x,
                                             init_min=0.0,
                                             init_max=0.0,
                                             is_training=True,
                                             vars_collection=_MIN_MAX_VARS)
Exemplo n.º 5
0
    def call(self, inputs, **kwargs):
        for unquantized_kernel in self.layer.get_quantizable_weights():
            # unquantized_kernel is the weight variable constructed by the wrapped
            # layer which needs to be quantized. quantized_kernel is the resultant
            # tensor when FakeQuant is applied to it.
            quantized_kernel = quant_ops.LastValueQuantize(
                unquantized_kernel,
                init_min=-6.0,
                init_max=6.0,
                is_training=True,
                num_bits=self._num_bits,
                symmetric=self._symmetric,
                narrow_range=self._narrow_range,
                vars_collection=ops.GraphKeys.GLOBAL_VARIABLES,
                name_prefix=self.layer.name)

            # set_quantizable_weights on the wrapped layer removes unquantized_kernel
            # from _trainable_weights. We add it to the wrappers _trainable_weights
            # to ensure it gets gradient updates.
            self._trainable_weights.append(unquantized_kernel)

            self._unquantized_kernels.append(unquantized_kernel)
            self._quantized_kernels.append(quantized_kernel)

        self.layer.set_quantizable_weights(self._quantized_kernels)

        outputs = self.layer.call(inputs, **kwargs)

        if self.layer.activation is None:
            return outputs

        outputs = quant_ops.MovingAvgQuantize(
            outputs,
            init_min=-6.0,
            init_max=6.0,
            ema_decay=0.999,
            is_training=True,
            num_bits=self._num_bits,
            symmetric=self._symmetric,
            narrow_range=self._narrow_range,
            vars_collection=ops.GraphKeys.GLOBAL_VARIABLES,
            name_prefix=self.layer.name)

        return outputs
  def call(self, inputs, **kwargs):
    for unquantized_kernel in self.layer.get_quantizable_weights():
      # Quantize the layer's weights and assign the resulting tensor to the
      # layer. This ensures the results of the forward pass use the quantized
      # tensor value. However, the internal _trainable_weights is not modified
      # since the unquantized weights need to be updated.
      quantized_kernel = quant_ops.LastValueQuantize(
          unquantized_kernel,
          init_min=-6.0,
          init_max=6.0,
          is_training=True,
          num_bits=self.quant_params.num_bits,
          symmetric=self.quant_params.symmetric,
          narrow_range=self.quant_params.narrow_range,
          vars_collection=ops.GraphKeys.GLOBAL_VARIABLES,
          name_prefix=self.layer.name)

      self.unquantized_kernels.append(unquantized_kernel)
      self.quantized_kernels.append(quantized_kernel)

    self.layer.set_quantizable_weights(self.quantized_kernels)

    outputs = self.layer.call(inputs, **kwargs)

    if self.layer.activation is None:
      return outputs

    outputs = quant_ops.MovingAvgQuantize(
        outputs,
        init_min=-6.0,
        init_max=6.0,
        ema_decay=0.999,
        is_training=True,
        num_bits=self.quant_params.num_bits,
        symmetric=self.quant_params.symmetric,
        narrow_range=self.quant_params.narrow_range,
        vars_collection=ops.GraphKeys.GLOBAL_VARIABLES,
        name_prefix=self.layer.name)

    return outputs
Exemplo n.º 7
0
    def __call__(self, inputs, step, training, **kwargs):
        """Quantize tensor.

    Args:
      inputs: Input tensor to be quantized.
      step: Current step in graph execution.
      training: Whether the graph is currently training.
      **kwargs: Contains `min_var` and `max_var` tf variables.

    Returns:
      Quantized tensor.
    """
        return quant_ops.MovingAvgQuantize(
            inputs,
            kwargs['min_var'],
            kwargs['max_var'],
            ema_decay=0.999,
            is_training=training,
            num_bits=self.num_bits,
            per_channel=self.per_axis,
            symmetric=self.symmetric,
            narrow_range=self.narrow_range,
        )
Exemplo n.º 8
0
    def __call__(self, inputs, training, weights, **kwargs):
        """Quantize tensor.

    Args:
      inputs: Input tensor to be quantized.
      training: Whether the graph is currently training.
      weights: Dictionary of weights the quantizer can use to quantize the
        tensor. This contains the weights created in the `build` function.
      **kwargs: Additional variables which may be passed to the quantizer.

    Returns:
      Quantized tensor.
    """
        return quant_ops.MovingAvgQuantize(
            inputs,
            weights['min_var'],
            weights['max_var'],
            ema_decay=0.999,
            is_training=training,
            num_bits=self.num_bits,
            per_channel=self.per_axis,
            symmetric=self.symmetric,
            narrow_range=self.narrow_range,
        )
Exemplo n.º 9
0
 def testVariablesNotPartitioned_MovingAvg(self):
     x = tf.constant([1.0, 2.0])
     min_var = tf.Variable(0.0)
     max_var = tf.Variable(0.0)
     _ = quant_ops.MovingAvgQuantize(x, min_var, max_var, is_training=True)