TrainingExtensions/tensorflow/src/python/aimet_tensorflow/quantsim_straight_through_grad.py
def _compute_dloss_by_dmax(x, grad, scaling, offset, bitwidth, use_symmetric_encoding):
    """
    helper function to compute derivative of loss w.r.t encoding max
    :param grad: gradient param
    :param scaling: scaling factor as tf tensor computed for given encoding min/max
    :param offset: offset computed as tf tensor
    :param bitwidth: bit-width as tf tensor
    :param use_symmetric_encoding: bool flag to indicate symmetric/asymmetric encoding
    :return: computed derivative w.r.t encoding max
    """

    steps = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), bitwidth) - 1, tf.float32)
    r_x_by_s_plus_round_o = tf.round(x / scaling) + tf.round(offset)
    # R(x/s)-(x/s)
    r_x_by_s_minus_x_by_s = tf.round(x / scaling) - (x / scaling)

    # compute dq_by_dmax
    # expr to be used if r_x_by_s_plus_round_o < n or > p
    n, p = _get_n_and_p(bitwidth, use_symmetric_encoding)
    false_expr = tf.multiply(tf.clip_by_value(r_x_by_s_plus_round_o, n, p), tf.cast(1 / steps, tf.float32))
    inner_cond = tf.where(tf.less_equal(r_x_by_s_plus_round_o, p), tf.multiply(r_x_by_s_minus_x_by_s,
                                                                               tf.cast(1 / steps, tf.float32)),
                          false_expr)

    # we need a scalar value for dq_by_dmax, so reduce 4d value computed above
    # to single value before returning gradient
    # this uses chain rule, multiply by loss and sum it to get scalar.
    dq_by_dmax = tf.where(tf.less_equal(n, r_x_by_s_plus_round_o), inner_cond, false_expr)
    dloss_by_dmax = tf.reduce_sum(dq_by_dmax * grad)

    return dloss_by_dmax

    helper function to compute derivative of loss w.r.t encoding max
    :param grad: gradient param
    :param scaling: scaling factor as tf tensor computed for given encoding min/max
    :param offset: offset computed as tf tensor
    :param bitwidth: bit-width as tf tensor
    :param use_symmetric_encoding: bool flag to indicate symmetric/asymmetric encoding
    :return: computed derivative w.r.t encoding max
    
helper function to compute derivative of loss w.r.t encoding max
helper function to compute derivative of loss w . r . t encoding max
def _compute_dloss_by_dmax(x, grad, scaling, offset, bitwidth, use_symmetric_encoding):
    steps = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), bitwidth) - 1, tf.float32)
    r_x_by_s_plus_round_o = tf.round(x / scaling) + tf.round(offset)
    r_x_by_s_minus_x_by_s = tf.round(x / scaling) - (x / scaling)
    n, p = _get_n_and_p(bitwidth, use_symmetric_encoding)
    false_expr = tf.multiply(tf.clip_by_value(r_x_by_s_plus_round_o, n, p), tf.cast(1 / steps, tf.float32))
    inner_cond = tf.where(tf.less_equal(r_x_by_s_plus_round_o, p), tf.multiply(r_x_by_s_minus_x_by_s,
                                                                               tf.cast(1 / steps, tf.float32)),
                          false_expr)
    dq_by_dmax = tf.where(tf.less_equal(n, r_x_by_s_plus_round_o), inner_cond, false_expr)
    dloss_by_dmax = tf.reduce_sum(dq_by_dmax * grad)
    return dloss_by_dmax
def _compute_dloss_by_dmax ( x , grad , scaling , offset , bitwidth , use_symmetric_encoding ) : steps = tf . cast ( tf . pow ( tf . cast ( tf . constant ( 2 ) , tf . float32 ) , bitwidth ) - 1 , tf . float32 ) r_x_by_s_plus_round_o = tf . round ( x / scaling ) + tf . round ( offset ) r_x_by_s_minus_x_by_s = tf . round ( x / scaling ) - ( x / scaling ) n , p = _get_n_and_p ( bitwidth , use_symmetric_encoding ) false_expr = tf . multiply ( tf . clip_by_value ( r_x_by_s_plus_round_o , n , p ) , tf . cast ( 1 / steps , tf . float32 ) ) inner_cond = tf . where ( tf . less_equal ( r_x_by_s_plus_round_o , p ) , tf . multiply ( r_x_by_s_minus_x_by_s , tf . cast ( 1 / steps , tf . float32 ) ) , false_expr ) dq_by_dmax = tf . where ( tf . less_equal ( n , r_x_by_s_plus_round_o ) , inner_cond , false_expr ) dloss_by_dmax = tf . reduce_sum ( dq_by_dmax * grad ) return dloss_by_dmax
helper function to compute derivative of loss w.r.t encoding max
helper function to compute derivative of loss w . r . t encoding max
"""
    helper function to compute derivative of loss w.r.t encoding max
    :param grad: gradient param
    :param scaling: scaling factor as tf tensor computed for given encoding min/max
    :param offset: offset computed as tf tensor
    :param bitwidth: bit-width as tf tensor
    :param use_symmetric_encoding: bool flag to indicate symmetric/asymmetric encoding
    :return: computed derivative w.r.t encoding max
    """ # R(x/s)-(x/s) # compute dq_by_dmax # expr to be used if r_x_by_s_plus_round_o < n or > p # we need a scalar value for dq_by_dmax, so reduce 4d value computed above # to single value before returning gradient # this uses chain rule, multiply by loss and sum it to get scalar.
computed derivative w.r.t encoding max
computed derivative w . r . t encoding max
x
grad
None
scaling
scaling factor as tf tensor computed for given encoding min/max
scaling factor as tf tensor computed for given encoding min / max
offset
offset computed as tf tensor
offset computed as tf tensor
bitwidth
bit-width as tf tensor
bit - width as tf tensor
use_symmetric_encoding
bool flag to indicate symmetric/asymmetric encoding
bool flag to indicate symmetric / asymmetric encoding