TrainingExtensions/tensorflow/src/python/aimet_tensorflow/quantsim_straight_through_grad.py
def quantsim_custom_grad_learned_grid(op, grad):
    """
    Performs custom gradient calculations for trained Quantize op
    :param op: Tf operation for which gradients are to be computed
    :param grad: Gradient flowing through
    """
    # pylint: disable=R0914

    # read bitwidth, use_symmetric_encoding_flag,
    # encoding_min and encoding_max from the op inputs
    x = tf.cast(op.inputs[0], tf.float32)
    bitwidth = tf.cast(op.inputs[int(QuantizeOpIndices.bit_width)], tf.float32)
    op_mode = tf.cast(op.inputs[int(QuantizeOpIndices.op_mode)], tf.int8)

    encoding_min = tf.cast(op.inputs[int(QuantizeOpIndices.encoding_min)], tf.float32)
    encoding_max_read = tf.cast(op.inputs[int(QuantizeOpIndices.encoding_max)], tf.float32)

    # handle min == max to avoid divide by zero
    epsilon = tf.constant(1e-5, dtype=tf.float32)
    encoding_max = tf.math.maximum(encoding_max_read, tf.add(encoding_min, epsilon))

    # compute n, p, scaling and offset params
    # choose n based on symmetric or asymmetric flag
    # symmetric : -two_pow_bw + 1
    # asymmetric : 0
    n, p = _get_n_and_p(bitwidth, op.inputs[int(QuantizeOpIndices.use_symmetric_encoding)])
    steps = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), bitwidth) - 1, tf.float32)
    scaling = tf.cast(((encoding_max - encoding_min) / steps), tf.float32)
    rounded_offset = tf.round(-encoding_min / scaling)
    # R(x/s) + R(o)
    r_x_by_s_plus_round_o = tf.round(x / scaling) + rounded_offset

    # compute dQ(x,s)/dx , dQ(x,s)/dmin and dQ(x,s)/dmax
    # compute dq_by_dx, dq_by_dmax, dq_by_dmin
    inner_cond = tf.where(tf.less_equal(r_x_by_s_plus_round_o, p),  # condition to check per value
                          tf.ones_like(r_x_by_s_plus_round_o),  # execute if true
                          tf.zeros_like(r_x_by_s_plus_round_o))  # execute if false
    dloss_by_dx = (tf.where(tf.less_equal(n, r_x_by_s_plus_round_o),  # condition to check per value
                            inner_cond,  # execute if true
                            tf.zeros_like(r_x_by_s_plus_round_o))) * grad

    dloss_by_dmax = tf.cast(_compute_dloss_by_dmax(x, grad, scaling, rounded_offset, bitwidth,
                                                   op.inputs[int(QuantizeOpIndices.use_symmetric_encoding)]),
                            tf.float64)
    dloss_by_dmin = tf.cast(_compute_dloss_by_dmin_using_dmax(dloss_by_dmax), tf.float64)

    # Pass through gradient for skipped ops
    dloss_by_dx = tf.cond(tf.equal(op_mode, 3), lambda: grad, lambda: dloss_by_dx)

    return dloss_by_dx, None, None, dloss_by_dmin, dloss_by_dmax, None, None

    Performs custom gradient calculations for trained Quantize op
    :param op: Tf operation for which gradients are to be computed
    :param grad: Gradient flowing through
    
Performs custom gradient calculations for trained Quantize op
Performs custom gradient calculations for trained Quantize op
def quantsim_custom_grad_learned_grid(op, grad):
    x = tf.cast(op.inputs[0], tf.float32)
    bitwidth = tf.cast(op.inputs[int(QuantizeOpIndices.bit_width)], tf.float32)
    op_mode = tf.cast(op.inputs[int(QuantizeOpIndices.op_mode)], tf.int8)
    encoding_min = tf.cast(op.inputs[int(QuantizeOpIndices.encoding_min)], tf.float32)
    encoding_max_read = tf.cast(op.inputs[int(QuantizeOpIndices.encoding_max)], tf.float32)
    epsilon = tf.constant(1e-5, dtype=tf.float32)
    encoding_max = tf.math.maximum(encoding_max_read, tf.add(encoding_min, epsilon))
    n, p = _get_n_and_p(bitwidth, op.inputs[int(QuantizeOpIndices.use_symmetric_encoding)])
    steps = tf.cast(tf.pow(tf.cast(tf.constant(2), tf.float32), bitwidth) - 1, tf.float32)
    scaling = tf.cast(((encoding_max - encoding_min) / steps), tf.float32)
    rounded_offset = tf.round(-encoding_min / scaling)
    r_x_by_s_plus_round_o = tf.round(x / scaling) + rounded_offset
    inner_cond = tf.where(tf.less_equal(r_x_by_s_plus_round_o, p),  
                          tf.ones_like(r_x_by_s_plus_round_o),  
                          tf.zeros_like(r_x_by_s_plus_round_o))  
    dloss_by_dx = (tf.where(tf.less_equal(n, r_x_by_s_plus_round_o),  
                            inner_cond,  
                            tf.zeros_like(r_x_by_s_plus_round_o))) * grad
    dloss_by_dmax = tf.cast(_compute_dloss_by_dmax(x, grad, scaling, rounded_offset, bitwidth,
                                                   op.inputs[int(QuantizeOpIndices.use_symmetric_encoding)]),
                            tf.float64)
    dloss_by_dmin = tf.cast(_compute_dloss_by_dmin_using_dmax(dloss_by_dmax), tf.float64)
    dloss_by_dx = tf.cond(tf.equal(op_mode, 3), lambda: grad, lambda: dloss_by_dx)
    return dloss_by_dx, None, None, dloss_by_dmin, dloss_by_dmax, None, None
def quantsim_custom_grad_learned_grid ( op , grad ) : x = tf . cast ( op . inputs [ 0 ] , tf . float32 ) bitwidth = tf . cast ( op . inputs [ int ( QuantizeOpIndices . bit_width ) ] , tf . float32 ) op_mode = tf . cast ( op . inputs [ int ( QuantizeOpIndices . op_mode ) ] , tf . int8 ) encoding_min = tf . cast ( op . inputs [ int ( QuantizeOpIndices . encoding_min ) ] , tf . float32 ) encoding_max_read = tf . cast ( op . inputs [ int ( QuantizeOpIndices . encoding_max ) ] , tf . float32 ) epsilon = tf . constant ( 1e-5 , dtype = tf . float32 ) encoding_max = tf . math . maximum ( encoding_max_read , tf . add ( encoding_min , epsilon ) ) n , p = _get_n_and_p ( bitwidth , op . inputs [ int ( QuantizeOpIndices . use_symmetric_encoding ) ] ) steps = tf . cast ( tf . pow ( tf . cast ( tf . constant ( 2 ) , tf . float32 ) , bitwidth ) - 1 , tf . float32 ) scaling = tf . cast ( ( ( encoding_max - encoding_min ) / steps ) , tf . float32 ) rounded_offset = tf . round ( - encoding_min / scaling ) r_x_by_s_plus_round_o = tf . round ( x / scaling ) + rounded_offset inner_cond = tf . where ( tf . less_equal ( r_x_by_s_plus_round_o , p ) , tf . ones_like ( r_x_by_s_plus_round_o ) , tf . zeros_like ( r_x_by_s_plus_round_o ) ) dloss_by_dx = ( tf . where ( tf . less_equal ( n , r_x_by_s_plus_round_o ) , inner_cond , tf . zeros_like ( r_x_by_s_plus_round_o ) ) ) * grad dloss_by_dmax = tf . cast ( _compute_dloss_by_dmax ( x , grad , scaling , rounded_offset , bitwidth , op . inputs [ int ( QuantizeOpIndices . use_symmetric_encoding ) ] ) , tf . float64 ) dloss_by_dmin = tf . cast ( _compute_dloss_by_dmin_using_dmax ( dloss_by_dmax ) , tf . float64 ) dloss_by_dx = tf . cond ( tf . equal ( op_mode , 3 ) , lambda : grad , lambda : dloss_by_dx ) return dloss_by_dx , None , None , dloss_by_dmin , dloss_by_dmax , None , None
Performs custom gradient calculations for trained Quantize op
Performs custom gradient calculations for trained Quantize op
"""
    Performs custom gradient calculations for trained Quantize op
    :param op: Tf operation for which gradients are to be computed
    :param grad: Gradient flowing through
    """ # pylint: disable=R0914 # read bitwidth, use_symmetric_encoding_flag, # encoding_min and encoding_max from the op inputs # handle min == max to avoid divide by zero # compute n, p, scaling and offset params # choose n based on symmetric or asymmetric flag # symmetric : -two_pow_bw + 1 # asymmetric : 0 # R(x/s) + R(o) # compute dQ(x,s)/dx , dQ(x,s)/dmin and dQ(x,s)/dmax # compute dq_by_dx, dq_by_dmax, dq_by_dmin # condition to check per value # execute if true # execute if false # condition to check per value # execute if true # Pass through gradient for skipped ops
op
Tf operation for which gradients are to be computed
Tf operation for which gradients are to be computed
grad
Gradient flowing through
Gradient flowing through