TrainingExtensions/tensorflow/src/python/aimet_tensorflow/quantsim.py
def _create_and_init_quant_op_input_vars(self, quant_op_name: str, quantizer_dict: Dict[str, QuantizerInfo],
                                             quantizer_type, op_mode: libpymo.QuantizationMode, bit_width: int = 8):
        """
        creates input variables to Quantize op and initializes them
        :param quant_op_name: quantize op name
        :param quantizer_dict: dictionary of op and QuantizerInfo
        :param quantizer_type: indicate param or activation quantizer
        :param op_mode: Starting mode to configure for the new quant op
        :param bit_width: bit-width to be used (output or param quantization bit-width), default set to 8.
        :return: quant op input variables created
        """
        with self.session.graph.as_default():
            op_mode_var = tf.Variable(int(op_mode),
                                      name=quant_op_name + '_op_mode', trainable=False,
                                      dtype=tf.int32)

            # Note: Last param of TensorQuantizer is a flag to indicate is_symmetric_encoding,
            # this value is to be read from config file
            tensor_quantizer = libpymo.TensorQuantizer(quant_scheme_to_libpymo[self._quant_scheme],
                                                       libpymo.RoundingMode.ROUND_NEAREST)
            tensor_quantizer_int64 = libpymo.PtrToInt64(tensor_quantizer)
            tensor_quant_ref = tf.Variable(tensor_quantizer_int64, name=quant_op_name + '_quant_ref',
                                           trainable=False, dtype=tf.int64)

            # Add to quantizer dict
            quantizer_info = QuantizerInfo(self.session, tensor_quantizer, quant_op_name, quantizer_type)
            quantizer_dict[quant_op_name] = quantizer_info

            bit_width = tf.Variable(initial_value=bit_width,
                                    name=quant_op_name + '_bit_width',
                                    trainable=False, dtype=tf.int8)

            encoding_min, encoding_max = self._create_encoding_min_max_vars(quant_op_name)

            # Note: Later, is_symmetric_encoding value is to be read from config file
            use_symmetric_encoding = tf.Variable(initial_value=False,
                                                 name=quant_op_name + '_use_symmetric_encoding',
                                                 trainable=False, dtype=tf.bool)

            self.session.run([op_mode_var.initializer, tensor_quant_ref.initializer, encoding_min.initializer,
                              encoding_max.initializer, bit_width.initializer, use_symmetric_encoding.initializer])

        return op_mode_var, tensor_quant_ref, encoding_min, encoding_max, bit_width, use_symmetric_encoding

        creates input variables to Quantize op and initializes them
        :param quant_op_name: quantize op name
        :param quantizer_dict: dictionary of op and QuantizerInfo
        :param quantizer_type: indicate param or activation quantizer
        :param op_mode: Starting mode to configure for the new quant op
        :param bit_width: bit-width to be used (output or param quantization bit-width), default set to 8.
        :return: quant op input variables created
        
creates input variables to Quantize op and initializes them
creates input variables to Quantize op and initializes them
def _create_and_init_quant_op_input_vars(self, quant_op_name: str, quantizer_dict: Dict[str, QuantizerInfo],
                                             quantizer_type, op_mode: libpymo.QuantizationMode, bit_width: int = 8):
        with self.session.graph.as_default():
            op_mode_var = tf.Variable(int(op_mode),
                                      name=quant_op_name + '_op_mode', trainable=False,
                                      dtype=tf.int32)
            tensor_quantizer = libpymo.TensorQuantizer(quant_scheme_to_libpymo[self._quant_scheme],
                                                       libpymo.RoundingMode.ROUND_NEAREST)
            tensor_quantizer_int64 = libpymo.PtrToInt64(tensor_quantizer)
            tensor_quant_ref = tf.Variable(tensor_quantizer_int64, name=quant_op_name + '_quant_ref',
                                           trainable=False, dtype=tf.int64)
            quantizer_info = QuantizerInfo(self.session, tensor_quantizer, quant_op_name, quantizer_type)
            quantizer_dict[quant_op_name] = quantizer_info
            bit_width = tf.Variable(initial_value=bit_width,
                                    name=quant_op_name + '_bit_width',
                                    trainable=False, dtype=tf.int8)
            encoding_min, encoding_max = self._create_encoding_min_max_vars(quant_op_name)
            use_symmetric_encoding = tf.Variable(initial_value=False,
                                                 name=quant_op_name + '_use_symmetric_encoding',
                                                 trainable=False, dtype=tf.bool)
            self.session.run([op_mode_var.initializer, tensor_quant_ref.initializer, encoding_min.initializer,
                              encoding_max.initializer, bit_width.initializer, use_symmetric_encoding.initializer])
        return op_mode_var, tensor_quant_ref, encoding_min, encoding_max, bit_width, use_symmetric_encoding
def _create_and_init_quant_op_input_vars ( self , quant_op_name : str , quantizer_dict : Dict [ str , QuantizerInfo ] , quantizer_type , op_mode : libpymo . QuantizationMode , bit_width : int = 8 ) : with self . session . graph . as_default ( ) : op_mode_var = tf . Variable ( int ( op_mode ) , name = quant_op_name + '_op_mode' , trainable = False , dtype = tf . int32 ) tensor_quantizer = libpymo . TensorQuantizer ( quant_scheme_to_libpymo [ self . _quant_scheme ] , libpymo . RoundingMode . ROUND_NEAREST ) tensor_quantizer_int64 = libpymo . PtrToInt64 ( tensor_quantizer ) tensor_quant_ref = tf . Variable ( tensor_quantizer_int64 , name = quant_op_name + '_quant_ref' , trainable = False , dtype = tf . int64 ) quantizer_info = QuantizerInfo ( self . session , tensor_quantizer , quant_op_name , quantizer_type ) quantizer_dict [ quant_op_name ] = quantizer_info bit_width = tf . Variable ( initial_value = bit_width , name = quant_op_name + '_bit_width' , trainable = False , dtype = tf . int8 ) encoding_min , encoding_max = self . _create_encoding_min_max_vars ( quant_op_name ) use_symmetric_encoding = tf . Variable ( initial_value = False , name = quant_op_name + '_use_symmetric_encoding' , trainable = False , dtype = tf . bool ) self . session . run ( [ op_mode_var . initializer , tensor_quant_ref . initializer , encoding_min . initializer , encoding_max . initializer , bit_width . initializer , use_symmetric_encoding . initializer ] ) return op_mode_var , tensor_quant_ref , encoding_min , encoding_max , bit_width , use_symmetric_encoding
creates input variables to Quantize op and initializes them
creates input variables to Quantize op and initializes them
"""
        creates input variables to Quantize op and initializes them
        :param quant_op_name: quantize op name
        :param quantizer_dict: dictionary of op and QuantizerInfo
        :param quantizer_type: indicate param or activation quantizer
        :param op_mode: Starting mode to configure for the new quant op
        :param bit_width: bit-width to be used (output or param quantization bit-width), default set to 8.
        :return: quant op input variables created
        """ # Note: Last param of TensorQuantizer is a flag to indicate is_symmetric_encoding, # this value is to be read from config file # Add to quantizer dict # Note: Later, is_symmetric_encoding value is to be read from config file
quant op input variables created
quant op input variables created
self
quant_op_name
str
quantize op name
quantize op name
quantizer_dict
Dict[str, QuantizerInfo]
dictionary of op and QuantizerInfo
dictionary of op and QuantizerInfo
quantizer_type
indicate param or activation quantizer
indicate param or activation quantizer
op_mode
libpymo.QuantizationMode
Starting mode to configure for the new quant op
Starting mode to configure for the new quant op
bit_width
int
bit-width to be used (output or param quantization bit-width), default set to 8.
bit - width to be used ( output or param quantization bit - width ) default set to 8 .