parameters: (data, scale, zero_point, quant_min, quant_max)
return_statement: return weight_int
docstring: r"""
      Calculate int8/uint8 weight from fp32. the formula is defined as:
  
      .. math::
          int8/uint8 = round(float/scale) + offset
  
      Args:
          data (numpy.ndarray): The dimension of channel or 1. Should be NCHW.
          scale (numpy.ndarray): The dimension of channel or 1.
          zero_point (numpy.ndarray): The dimension of channel or 1.
          quant_min (int): The minimum quantization integer.
          quant_max (int): The maximum quantization integer.
  
      Returns:
          weight (numpy.ndarray): The dimension of channel or 1.
docstring_summary: r"""
      Calculate int8/uint8 weight from fp32. the formula is defined as:
docstring_tokens: r Calculate int8 / uint8 weight from fp32 . the formula is defined as :
function: def weight2int(data, scale, zero_point, quant_min, quant_max):
      r"""
      Calculate int8/uint8 weight from fp32. the formula is defined as:
  
      .. math::
          int8/uint8 = round(float/scale) + offset
  
      Args:
          data (numpy.ndarray): The dimension of channel or 1. Should be NCHW.
          scale (numpy.ndarray): The dimension of channel or 1.
          zero_point (numpy.ndarray): The dimension of channel or 1.
          quant_min (int): The minimum quantization integer.
          quant_max (int): The maximum quantization integer.
  
      Returns:
          weight (numpy.ndarray): The dimension of channel or 1.
      """
      if scale.shape != zero_point.shape:
          raise ValueError("`scale` and `zero_point` should have the same shape.")
      if scale.shape[0] < 0:
          raise ValueError("`scale` and `zero_point` shape should be greater than zero.")
      if len(scale.shape) >= 1 and scale.shape[0] > 1:
          # for perchannel
          if scale.shape[0] == data.shape[0]:
              # `Conv2d` or `Dense` op weight
              shape_list = [-1] + [1] * len(data.shape[1:])
              scale = scale.reshape(shape_list)
              zero_point = zero_point.reshape(shape_list)
          elif scale.shape[0] == data.shape[1]:
              # `DepthwiseConv2d` op weight
              shape_list = [1, -1] + [1] * len(data.shape[2:])
              scale = scale.reshape(shape_list)
              zero_point = zero_point.reshape(shape_list)
          else:
              raise ValueError("Unsupported weight shape({})".format(data.shape))
  
      weight_int = np.round((data / scale) + zero_point)
      weight_int[weight_int > quant_max] = quant_max
      weight_int[weight_int < quant_min] = quant_min
      return weight_int
function_tokens: def weight2int ( data , scale , zero_point , quant_min , quant_max ) : if scale . shape != zero_point . shape : raise ValueError ( "`scale` and `zero_point` should have the same shape." ) if scale . shape [ 0 ] < 0 : raise ValueError ( "`scale` and `zero_point` shape should be greater than zero." ) if len ( scale . shape ) >= 1 and scale . shape [ 0 ] > 1 : # for perchannel if scale . shape [ 0 ] == data . shape [ 0 ] : # `Conv2d` or `Dense` op weight shape_list = [ - 1 ] + [ 1 ] * len ( data . shape [ 1 : ] ) scale = scale . reshape ( shape_list ) zero_point = zero_point . reshape ( shape_list ) elif scale . shape [ 0 ] == data . shape [ 1 ] : # `DepthwiseConv2d` op weight shape_list = [ 1 , - 1 ] + [ 1 ] * len ( data . shape [ 2 : ] ) scale = scale . reshape ( shape_list ) zero_point = zero_point . reshape ( shape_list ) else : raise ValueError ( "Unsupported weight shape({})" . format ( data . shape ) ) weight_int = np . round ( ( data / scale ) + zero_point ) weight_int [ weight_int > quant_max ] = quant_max weight_int [ weight_int < quant_min ] = quant_min return weight_int