tensorflow_model_optimization/python/core/internal/tensor_encoding/stages/research/quantization.py [105:134]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    max_value = tf.cast(encode_params[self.MAX_INT_VALUE_PARAMS_KEY], x.dtype)
    # Shift the values to range [0, max_value].
    # In the case of min_x == max_x, this will return all zeros.
    x = tf.compat.v1.div_no_nan(x - min_x, max_x - min_x) * max_value

    # Randomized rounding.
    floored_x = tf.floor(x)
    random_seed = tf.random.uniform((2,), maxval=tf.int64.max, dtype=tf.int64)
    num_elements = tf.reduce_prod(tf.shape(x))
    rounding_floats = tf.reshape(
        self._random_floats(num_elements, random_seed, x.dtype), tf.shape(x))

    bernoulli = rounding_floats < (x - floored_x)
    quantized_x = floored_x + tf.cast(bernoulli, x.dtype)

    # Include the random seed in the encoded tensors so that it can be used to
    # generate the same random sequence in the decode method.
    encoded_tensors = collections.OrderedDict([
        (self.ENCODED_VALUES_KEY, quantized_x),
        (self.SEED_PARAMS_KEY, random_seed),
        (self.MIN_MAX_VALUES_KEY, tf.stack([min_x, max_x]))
    ])

    return encoded_tensors

  def decode(self,
             encoded_tensors,
             decode_params,
             num_summands=None,
             shape=None):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



tensorflow_model_optimization/python/core/internal/tensor_encoding/stages/research/quantization.py [378:407]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    max_value = tf.cast(encode_params[self.MAX_INT_VALUE_PARAMS_KEY], x.dtype)
    # Shift the values to range [0, max_value].
    # In the case of min_x == max_x, this will return all zeros.
    x = tf.compat.v1.div_no_nan(x - min_x, max_x - min_x) * max_value

    # Randomized rounding.
    floored_x = tf.floor(x)
    random_seed = tf.random.uniform((2,), maxval=tf.int64.max, dtype=tf.int64)
    num_elements = tf.reduce_prod(tf.shape(x))
    rounding_floats = tf.reshape(
        self._random_floats(num_elements, random_seed, x.dtype), tf.shape(x))

    bernoulli = rounding_floats < (x - floored_x)
    quantized_x = floored_x + tf.cast(bernoulli, x.dtype)

    # Include the random seed in the encoded tensors so that it can be used to
    # generate the same random sequence in the decode method.
    encoded_tensors = collections.OrderedDict([
        (self.ENCODED_VALUES_KEY, quantized_x),
        (self.SEED_PARAMS_KEY, random_seed),
        (self.MIN_MAX_VALUES_KEY, tf.stack([min_x, max_x]))
    ])

    return encoded_tensors

  def decode(self,
             encoded_tensors,
             decode_params,
             num_summands=None,
             shape=None):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



