TrainingExtensions/tensorflow/src/python/aimet_tensorflow/quantsim_straight_through_grad.py
def _get_n_and_p(bitwidth, use_symmetric_encoding):
    """
    compute bounds n and p params given bitwidth and use_symmetric_encoding flag
    :param bitwidth: tf tensor with bitwidth configured
    :param use_symmetric_encoding: boolean flag tf tensor that indicates symmetric/ asymmetric encoding
    :return: n and p params computed
    """

    bitwidth = tf.cast(bitwidth, tf.float32)
    two_pow_bw = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), bitwidth), tf.float32)
    two_pow_bw_minus_1 = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), (bitwidth - 1)), tf.float32)
    minus_one_as_float32 = tf.cast(tf.constant(-1), tf.float32)
    one_as_float32 = tf.cast(tf.constant(1), tf.float32)

    # symmetric case : n = (- 2 ** (bw -1 )) + 1 , p = (2 ** (bw -1) - 1)
    def n_symmetric_encoding():
        return tf.add(tf.multiply(minus_one_as_float32, two_pow_bw_minus_1), one_as_float32)

    def p_symmetric_encoding():
        return tf.subtract(two_pow_bw_minus_1, one_as_float32)

    # asymmetric case : n = 0 , p = 2 ** (bw) - 1
    def n_asymmetric_encoding():
        return tf.cast(0, tf.float32)

    def p_asymmetric_encoding():
        return tf.cast(two_pow_bw - 1, tf.float32)

    n = tf.cond(use_symmetric_encoding, n_symmetric_encoding, n_asymmetric_encoding)

    p = tf.cond(use_symmetric_encoding, p_symmetric_encoding, p_asymmetric_encoding)

    return n, p

    compute bounds n and p params given bitwidth and use_symmetric_encoding flag
    :param bitwidth: tf tensor with bitwidth configured
    :param use_symmetric_encoding: boolean flag tf tensor that indicates symmetric/ asymmetric encoding
    :return: n and p params computed
    
compute bounds n and p params given bitwidth and use_symmetric_encoding flag
compute bounds n and p params given bitwidth and use_symmetric_encoding flag
def _get_n_and_p(bitwidth, use_symmetric_encoding):
    bitwidth = tf.cast(bitwidth, tf.float32)
    two_pow_bw = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), bitwidth), tf.float32)
    two_pow_bw_minus_1 = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), (bitwidth - 1)), tf.float32)
    minus_one_as_float32 = tf.cast(tf.constant(-1), tf.float32)
    one_as_float32 = tf.cast(tf.constant(1), tf.float32)
    def n_symmetric_encoding():
        return tf.add(tf.multiply(minus_one_as_float32, two_pow_bw_minus_1), one_as_float32)
    def p_symmetric_encoding():
        return tf.subtract(two_pow_bw_minus_1, one_as_float32)
    def n_asymmetric_encoding():
        return tf.cast(0, tf.float32)
    def p_asymmetric_encoding():
        return tf.cast(two_pow_bw - 1, tf.float32)
    n = tf.cond(use_symmetric_encoding, n_symmetric_encoding, n_asymmetric_encoding)
    p = tf.cond(use_symmetric_encoding, p_symmetric_encoding, p_asymmetric_encoding)
    return n, p
def _get_n_and_p ( bitwidth , use_symmetric_encoding ) : bitwidth = tf . cast ( bitwidth , tf . float32 ) two_pow_bw = tf . cast ( tf . pow ( tf . cast ( tf . constant ( 2 ) , tf . float32 ) , bitwidth ) , tf . float32 ) two_pow_bw_minus_1 = tf . cast ( tf . pow ( tf . cast ( tf . constant ( 2 ) , tf . float32 ) , ( bitwidth - 1 ) ) , tf . float32 ) minus_one_as_float32 = tf . cast ( tf . constant ( - 1 ) , tf . float32 ) one_as_float32 = tf . cast ( tf . constant ( 1 ) , tf . float32 ) def n_symmetric_encoding ( ) : return tf . add ( tf . multiply ( minus_one_as_float32 , two_pow_bw_minus_1 ) , one_as_float32 ) def p_symmetric_encoding ( ) : return tf . subtract ( two_pow_bw_minus_1 , one_as_float32 ) def n_asymmetric_encoding ( ) : return tf . cast ( 0 , tf . float32 ) def p_asymmetric_encoding ( ) : return tf . cast ( two_pow_bw - 1 , tf . float32 ) n = tf . cond ( use_symmetric_encoding , n_symmetric_encoding , n_asymmetric_encoding ) p = tf . cond ( use_symmetric_encoding , p_symmetric_encoding , p_asymmetric_encoding ) return n , p
compute bounds n and p params given bitwidth and use_symmetric_encoding flag
compute bounds n and p params given bitwidth and use_symmetric_encoding flag
"""
    compute bounds n and p params given bitwidth and use_symmetric_encoding flag
    :param bitwidth: tf tensor with bitwidth configured
    :param use_symmetric_encoding: boolean flag tf tensor that indicates symmetric/ asymmetric encoding
    :return: n and p params computed
    """ # symmetric case : n = (- 2 ** (bw -1 )) + 1 , p = (2 ** (bw -1) - 1) # asymmetric case : n = 0 , p = 2 ** (bw) - 1
n and p params computed
n and p params computed
bitwidth
tf tensor with bitwidth configured
tf tensor with bitwidth configured
use_symmetric_encoding
boolean flag tf tensor that indicates symmetric/ asymmetric encoding
boolean flag tf tensor that indicates symmetric / asymmetric encoding