TrainingExtensions/tensorflow/src/python/aimet_tensorflow/quantsim.py
def compute_encodings(self, forward_pass_callback: Callable[[tf.compat.v1.Session, Any], None],
                          forward_pass_callback_args):
        """
        Computes encodings for all quantization sim nodes in the model.
        This is also used to set initial encodings for Range Learning.

        :param forward_pass_callback: A callback function that is expected to runs forward passes on a session.
               This callback function should use representative data for the forward pass, so the calculated
               encodings work for all data samples. This callback internally chooses the number of data samples
               it wants to use for calculating encodings.

        :param forward_pass_callback_args: These argument(s) are passed to the forward_pass_callback as-is. Up to
               the user to determine the type of this parameter. E.g. could be simply an integer representing the number
               of data samples to use. Or could be a tuple of parameters or an object representing something more
               complex.

        :return: None

        """

        # Run data through the quantsim so we can compute activation encodings
        forward_pass_callback(self.session, forward_pass_callback_args)

        ops_with_invalid_encodings = []

        # For activations, calculate encodings and update min-max parameters
        for op_name, quantizer_info in self._activation_quantizers.items():
            # Calculate encodings
            if quantizer_info.get_op_mode() != int(libpymo.TensorQuantizerOpMode.passThrough):
                op_bitwidth, op_use_symmetric_encodings = quantizer_info.bitwidth, quantizer_info.use_symmetric_encoding
                encoding = quantizer_info.compute_encoding(op_bitwidth, op_use_symmetric_encodings)
                if quantizer_info.is_encoding_valid():
                    quantizer_info.set_encoding(encoding)
                    quantizer_info.set_op_mode(libpymo.TensorQuantizerOpMode.quantizeDequantize)
                else:
                    quantizer_info.set_op_mode(libpymo.TensorQuantizerOpMode.passThrough)
                    ops_with_invalid_encodings.append(op_name)

        # For post-training mode, params will always be in one-shot mode
        op_mode = QuantizationSimModel._param_op_mode_after_analysis(self._quant_scheme)

        for op_name, quantizer_info in self._param_quantizers.items():
            if quantizer_info.get_op_mode() != int(libpymo.TensorQuantizerOpMode.passThrough):
                op_bitwidth, op_use_symmetric_encodings = quantizer_info.bitwidth, quantizer_info.use_symmetric_encoding
                encoding = quantizer_info.compute_encoding(op_bitwidth, op_use_symmetric_encodings)
                if quantizer_info.is_encoding_valid():
                    quantizer_info.set_encoding(encoding)
                    quantizer_info.set_op_mode(op_mode)
                else:
                    quantizer_info.set_op_mode(libpymo.TensorQuantizerOpMode.passThrough)
                    ops_with_invalid_encodings.append(op_name)

        if ops_with_invalid_encodings:
            _logger.info('The following quantizers did not have valid encodings and have been set to passThrough mode: '
                         '%s', ops_with_invalid_encodings)
            _logger.info('This can be due to the quantizers not having been evaluated during the forward pass in '
                         'compute encodings. Evaluation is required to collect statistics needed to compute valid '
                         'encodings.\n'
                         'As a result, the quantizers have been set to passThrough mode, meaning no quantization noise '
                         'will be simulated for these ops if they are evaluated in the future.\n'
                         'If this is not desired, amend the forward pass to evaluate tensors which require these ops '
                         'to be evaluated, and recompute encodings.')

        Computes encodings for all quantization sim nodes in the model.
        This is also used to set initial encodings for Range Learning.

        :param forward_pass_callback: A callback function that is expected to runs forward passes on a session.
               This callback function should use representative data for the forward pass, so the calculated
               encodings work for all data samples. This callback internally chooses the number of data samples
               it wants to use for calculating encodings.

        :param forward_pass_callback_args: These argument(s) are passed to the forward_pass_callback as-is. Up to
               the user to determine the type of this parameter. E.g. could be simply an integer representing the number
               of data samples to use. Or could be a tuple of parameters or an object representing something more
               complex.

        :return: None

        
Computes encodings for all quantization sim nodes in the model.
This is also used to set initial encodings for Range Learning.
Computes encodings for all quantization sim nodes in the model . This is also used to set initial encodings for Range Learning .
def compute_encodings(self, forward_pass_callback: Callable[[tf.compat.v1.Session, Any], None],
                          forward_pass_callback_args):
        forward_pass_callback(self.session, forward_pass_callback_args)
        ops_with_invalid_encodings = []
        for op_name, quantizer_info in self._activation_quantizers.items():
            if quantizer_info.get_op_mode() != int(libpymo.TensorQuantizerOpMode.passThrough):
                op_bitwidth, op_use_symmetric_encodings = quantizer_info.bitwidth, quantizer_info.use_symmetric_encoding
                encoding = quantizer_info.compute_encoding(op_bitwidth, op_use_symmetric_encodings)
                if quantizer_info.is_encoding_valid():
                    quantizer_info.set_encoding(encoding)
                    quantizer_info.set_op_mode(libpymo.TensorQuantizerOpMode.quantizeDequantize)
                else:
                    quantizer_info.set_op_mode(libpymo.TensorQuantizerOpMode.passThrough)
                    ops_with_invalid_encodings.append(op_name)
        op_mode = QuantizationSimModel._param_op_mode_after_analysis(self._quant_scheme)
        for op_name, quantizer_info in self._param_quantizers.items():
            if quantizer_info.get_op_mode() != int(libpymo.TensorQuantizerOpMode.passThrough):
                op_bitwidth, op_use_symmetric_encodings = quantizer_info.bitwidth, quantizer_info.use_symmetric_encoding
                encoding = quantizer_info.compute_encoding(op_bitwidth, op_use_symmetric_encodings)
                if quantizer_info.is_encoding_valid():
                    quantizer_info.set_encoding(encoding)
                    quantizer_info.set_op_mode(op_mode)
                else:
                    quantizer_info.set_op_mode(libpymo.TensorQuantizerOpMode.passThrough)
                    ops_with_invalid_encodings.append(op_name)
        if ops_with_invalid_encodings:
            _logger.info('The following quantizers did not have valid encodings and have been set to passThrough mode: '
                         '%s', ops_with_invalid_encodings)
            _logger.info('This can be due to the quantizers not having been evaluated during the forward pass in '
                         'compute encodings. Evaluation is required to collect statistics needed to compute valid '
                         'encodings.\n'
                         'As a result, the quantizers have been set to passThrough mode, meaning no quantization noise '
                         'will be simulated for these ops if they are evaluated in the future.\n'
                         'If this is not desired, amend the forward pass to evaluate tensors which require these ops '
                         'to be evaluated, and recompute encodings.')
def compute_encodings ( self , forward_pass_callback : Callable [ [ tf . compat . v1 . Session , Any ] , None ] , forward_pass_callback_args ) : forward_pass_callback ( self . session , forward_pass_callback_args ) ops_with_invalid_encodings = [ ] for op_name , quantizer_info in self . _activation_quantizers . items ( ) : if quantizer_info . get_op_mode ( ) != int ( libpymo . TensorQuantizerOpMode . passThrough ) : op_bitwidth , op_use_symmetric_encodings = quantizer_info . bitwidth , quantizer_info . use_symmetric_encoding encoding = quantizer_info . compute_encoding ( op_bitwidth , op_use_symmetric_encodings ) if quantizer_info . is_encoding_valid ( ) : quantizer_info . set_encoding ( encoding ) quantizer_info . set_op_mode ( libpymo . TensorQuantizerOpMode . quantizeDequantize ) else : quantizer_info . set_op_mode ( libpymo . TensorQuantizerOpMode . passThrough ) ops_with_invalid_encodings . append ( op_name ) op_mode = QuantizationSimModel . _param_op_mode_after_analysis ( self . _quant_scheme ) for op_name , quantizer_info in self . _param_quantizers . items ( ) : if quantizer_info . get_op_mode ( ) != int ( libpymo . TensorQuantizerOpMode . passThrough ) : op_bitwidth , op_use_symmetric_encodings = quantizer_info . bitwidth , quantizer_info . use_symmetric_encoding encoding = quantizer_info . compute_encoding ( op_bitwidth , op_use_symmetric_encodings ) if quantizer_info . is_encoding_valid ( ) : quantizer_info . set_encoding ( encoding ) quantizer_info . set_op_mode ( op_mode ) else : quantizer_info . set_op_mode ( libpymo . TensorQuantizerOpMode . passThrough ) ops_with_invalid_encodings . append ( op_name ) if ops_with_invalid_encodings : _logger . info ( 'The following quantizers did not have valid encodings and have been set to passThrough mode: ' '%s' , ops_with_invalid_encodings ) _logger . info ( 'This can be due to the quantizers not having been evaluated during the forward pass in ' 'compute encodings. Evaluation is required to collect statistics needed to compute valid ' 'encodings.\n' 'As a result, the quantizers have been set to passThrough mode, meaning no quantization noise ' 'will be simulated for these ops if they are evaluated in the future.\n' 'If this is not desired, amend the forward pass to evaluate tensors which require these ops ' 'to be evaluated, and recompute encodings.' )
Computes encodings for all quantization sim nodes in the model.
Computes encodings for all quantization sim nodes in the model .
"""
        Computes encodings for all quantization sim nodes in the model.
        This is also used to set initial encodings for Range Learning.

        :param forward_pass_callback: A callback function that is expected to runs forward passes on a session.
               This callback function should use representative data for the forward pass, so the calculated
               encodings work for all data samples. This callback internally chooses the number of data samples
               it wants to use for calculating encodings.

        :param forward_pass_callback_args: These argument(s) are passed to the forward_pass_callback as-is. Up to
               the user to determine the type of this parameter. E.g. could be simply an integer representing the number
               of data samples to use. Or could be a tuple of parameters or an object representing something more
               complex.

        :return: None

        """ # Run data through the quantsim so we can compute activation encodings # For activations, calculate encodings and update min-max parameters # Calculate encodings # For post-training mode, params will always be in one-shot mode
None
self
forward_pass_callback
Callable[[tf.compat.v1.Session, Any], None]
A callback function that is expected to runs forward passes on a session.
This callback function should use representative data for the forward pass, so the calculated
encodings work for all data samples. This callback internally chooses the number of data samples
it wants to use for calculating encodings.
A callback function that is expected to runs forward passes on a session . This callback function should use representative data for the forward pass so the calculated encodings work for all data samples . This callback internally chooses the number of data samples it wants to use for calculating encodings .
forward_pass_callback_args
These argument(s) are passed to the forward_pass_callback as-is. Up to
the user to determine the type of this parameter. E.g. could be simply an integer representing the number
of data samples to use. Or could be a tuple of parameters or an object representing something more
complex.
These argument ( s ) are passed to the forward_pass_callback as - is . Up to the user to determine the type of this parameter . E . g . could be simply an integer representing the number of data samples to use . Or could be a tuple of parameters or an object representing something more complex .