parameters: (self, scope, place, weight_bits,
                                       threshold_rate, op, var_name, for_test)
docstring: Use abs_max method to quantize weight.
docstring_summary: Use abs_max method to quantize weight.
docstring_tokens: Use abs_max method to quantize weight .
function: def _weight_abs_max_quantization(self, scope, place, weight_bits,
                                       threshold_rate, op, var_name, for_test):
          '''
          Use abs_max method to quantize weight.
          '''
          quantize_range = (1 << (weight_bits - 1)) - 1
          save_weight_dtype = np.int8 if weight_bits == 8 else np.int16
  
          # Get quantized scale and weight data
          weight_data = _load_variable_data(scope, var_name)
          if abs(threshold_rate) < 1e-10:
              threshold_value = np.max(np.abs(weight_data))
          else:
              threshold_value = self._calculate_threshold(\
                  weight_data, threshold_rate)
              weight_data[weight_data > threshold_value] = threshold_value
              weight_data[weight_data < -threshold_value] = -threshold_value
          scale = threshold_value / quantize_range
          quantized_weight_data = \
              np.around(weight_data / scale).astype(save_weight_dtype)
  
          # Set weight data
          if not for_test:
              _set_variable_data(scope, place, var_name, quantized_weight_data)
          else:
              dequantized_weight_data = \
                  (quantized_weight_data * scale).astype(np.float32)
              _set_variable_data(scope, place, var_name, dequantized_weight_data)
  
          # Save info
          op._set_attr('quantization_type', 'post_weight_abs_max')
          op._set_attr('quantize_weight_bits', weight_bits)
          op._set_attr(var_name + "_quant_scale", [scale])  # Save as list
          op._set_attr("with_quant_attr", True)
function_tokens: def _weight_abs_max_quantization ( self , scope , place , weight_bits , threshold_rate , op , var_name , for_test ) : quantize_range = ( 1 << ( weight_bits - 1 ) ) - 1 save_weight_dtype = np . int8 if weight_bits == 8 else np . int16 # Get quantized scale and weight data weight_data = _load_variable_data ( scope , var_name ) if abs ( threshold_rate ) < 1e-10 : threshold_value = np . max ( np . abs ( weight_data ) ) else : threshold_value = self . _calculate_threshold ( weight_data , threshold_rate ) weight_data [ weight_data > threshold_value ] = threshold_value weight_data [ weight_data < - threshold_value ] = - threshold_value scale = threshold_value / quantize_range quantized_weight_data = np . around ( weight_data / scale ) . astype ( save_weight_dtype ) # Set weight data if not for_test : _set_variable_data ( scope , place , var_name , quantized_weight_data ) else : dequantized_weight_data = ( quantized_weight_data * scale ) . astype ( np . float32 ) _set_variable_data ( scope , place , var_name , dequantized_weight_data ) # Save info op . _set_attr ( 'quantization_type' , 'post_weight_abs_max' ) op . _set_attr ( 'quantize_weight_bits' , weight_bits ) op . _set_attr ( var_name + "_quant_scale" , [ scale ] ) # Save as list op . _set_attr ( "with_quant_attr" , True )