Examples/tensorflow/quantization/ada_round.py
def create_quant_sim_model(sess: tf.Session, start_op_names: List[str], output_op_names: List[str],
                           use_cuda: bool, evaluator: Callable[[tf.Session, Any], None],
                           logdir: str, encoding_filename: str = None):
    """
    Apply quantizer simulator on the original model and return its object.

    :param sess: The sess with graph.
    :param start_op_names: The list of input op names of the sess.graph
    :param output_op_names: The list of output op names of the sess.graph
    :param use_cuda: If True then use a GPU for QuantizationSimModel
    :param evaluator: A callback function that is expected to run forward passes on a session
    :param logdir: Path to a directory for logging, required by save_and_load_graph API
    :param encoding_filename: Optional encoding file to set and freeze parameter encodings
                              before computing encodings
    :return: QuantizationSimModel object
    """

    # Since QuantizationSimModel operates on a session inplace, making a
    # copy of original session and use it for further processing to keep
    # original session intact.
    copied_sess = save_and_load_graph(sess=sess, meta_path=logdir)

    # Quant scheme can be 'post_training_tf' or 'post_training_tf_enhanced'
    quant_scheme = QuantScheme.post_training_tf_enhanced

    # Rounding mode can be 'nearest' or 'stochastic'
    rounding_mode = 'nearest'

    # Output bit-width for quantization
    default_output_bw = 8

    # Parameter bit-width for quantization
    default_param_bw = 8

    quant_sim_model = aimet_tf_quantsim.QuantizationSimModel(session=copied_sess,
                                                             starting_op_names=start_op_names,
                                                             output_op_names=output_op_names,
                                                             quant_scheme=quant_scheme, rounding_mode=rounding_mode,
                                                             default_output_bw=default_output_bw,
                                                             default_param_bw=default_param_bw,
                                                             use_cuda=use_cuda)

    if encoding_filename:
        quant_sim_model.set_and_freeze_param_encodings(encoding_path=encoding_filename)

    # Number of batches to use for computing encodings
    # Only 5 batches are used here to speed up the process, also the
    # number of images in these 5 batches should be sufficient for
    # compute encodings
    iterations = 5

    # Here evaluator is used for forward_pass_callback as it is available
    # from Data Pipeline class. But any forward pass function can be used
    # here which doesn't necessarily need to use any labels data or return
    # any output. For Example, following snippet of code can be used for
    # forward_pass_callback:

    # def forward_pass_callback(session: tf.Session, iterations: int):
    #     input_tensor = <input tensor in session>
    #     train_tensor = <train tensor in session>

    #     curr_iter = 1
    #     for input_data, _ in data_loaders:
    #         feed_dict = {input_tensor: input_data,
    #                      train_tensor: False}

    #         session.run([], feed_dict=feed_dict)

    #         curr_iter += 1
    #         if curr_iter > iterations:
    #             break

    quant_sim_model.compute_encodings(forward_pass_callback=evaluator,
                                      forward_pass_callback_args=iterations)

    return quant_sim_model

    Apply quantizer simulator on the original model and return its object.

    :param sess: The sess with graph.
    :param start_op_names: The list of input op names of the sess.graph
    :param output_op_names: The list of output op names of the sess.graph
    :param use_cuda: If True then use a GPU for QuantizationSimModel
    :param evaluator: A callback function that is expected to run forward passes on a session
    :param logdir: Path to a directory for logging, required by save_and_load_graph API
    :param encoding_filename: Optional encoding file to set and freeze parameter encodings
                              before computing encodings
    :return: QuantizationSimModel object
    
Apply quantizer simulator on the original model and return its object.
Apply quantizer simulator on the original model and return its object .
def create_quant_sim_model(sess: tf.Session, start_op_names: List[str], output_op_names: List[str],
                           use_cuda: bool, evaluator: Callable[[tf.Session, Any], None],
                           logdir: str, encoding_filename: str = None):
    copied_sess = save_and_load_graph(sess=sess, meta_path=logdir)
    quant_scheme = QuantScheme.post_training_tf_enhanced
    rounding_mode = 'nearest'
    default_output_bw = 8
    default_param_bw = 8
    quant_sim_model = aimet_tf_quantsim.QuantizationSimModel(session=copied_sess,
                                                             starting_op_names=start_op_names,
                                                             output_op_names=output_op_names,
                                                             quant_scheme=quant_scheme, rounding_mode=rounding_mode,
                                                             default_output_bw=default_output_bw,
                                                             default_param_bw=default_param_bw,
                                                             use_cuda=use_cuda)
    if encoding_filename:
        quant_sim_model.set_and_freeze_param_encodings(encoding_path=encoding_filename)
    iterations = 5
    quant_sim_model.compute_encodings(forward_pass_callback=evaluator,
                                      forward_pass_callback_args=iterations)
    return quant_sim_model
def create_quant_sim_model ( sess : tf . Session , start_op_names : List [ str ] , output_op_names : List [ str ] , use_cuda : bool , evaluator : Callable [ [ tf . Session , Any ] , None ] , logdir : str , encoding_filename : str = None ) : copied_sess = save_and_load_graph ( sess = sess , meta_path = logdir ) quant_scheme = QuantScheme . post_training_tf_enhanced rounding_mode = 'nearest' default_output_bw = 8 default_param_bw = 8 quant_sim_model = aimet_tf_quantsim . QuantizationSimModel ( session = copied_sess , starting_op_names = start_op_names , output_op_names = output_op_names , quant_scheme = quant_scheme , rounding_mode = rounding_mode , default_output_bw = default_output_bw , default_param_bw = default_param_bw , use_cuda = use_cuda ) if encoding_filename : quant_sim_model . set_and_freeze_param_encodings ( encoding_path = encoding_filename ) iterations = 5 quant_sim_model . compute_encodings ( forward_pass_callback = evaluator , forward_pass_callback_args = iterations ) return quant_sim_model
Apply quantizer simulator on the original model and return its object.
Apply quantizer simulator on the original model and return its object .
"""
    Apply quantizer simulator on the original model and return its object.

    :param sess: The sess with graph.
    :param start_op_names: The list of input op names of the sess.graph
    :param output_op_names: The list of output op names of the sess.graph
    :param use_cuda: If True then use a GPU for QuantizationSimModel
    :param evaluator: A callback function that is expected to run forward passes on a session
    :param logdir: Path to a directory for logging, required by save_and_load_graph API
    :param encoding_filename: Optional encoding file to set and freeze parameter encodings
                              before computing encodings
    :return: QuantizationSimModel object
    """ # Since QuantizationSimModel operates on a session inplace, making a # copy of original session and use it for further processing to keep # original session intact. # Quant scheme can be 'post_training_tf' or 'post_training_tf_enhanced' # Rounding mode can be 'nearest' or 'stochastic' # Output bit-width for quantization # Parameter bit-width for quantization # Number of batches to use for computing encodings # Only 5 batches are used here to speed up the process, also the # number of images in these 5 batches should be sufficient for # compute encodings # Here evaluator is used for forward_pass_callback as it is available # from Data Pipeline class. But any forward pass function can be used # here which doesn't necessarily need to use any labels data or return # any output. For Example, following snippet of code can be used for # forward_pass_callback: # def forward_pass_callback(session: tf.Session, iterations: int): #     input_tensor = <input tensor in session> #     train_tensor = <train tensor in session> #     curr_iter = 1 #     for input_data, _ in data_loaders: #         feed_dict = {input_tensor: input_data, #                      train_tensor: False} #         session.run([], feed_dict=feed_dict) #         curr_iter += 1 #         if curr_iter > iterations: #             break
None
sess
tf.Session
The sess with graph.
The sess with graph .
start_op_names
List[str]
The list of input op names of the sess.graph
The list of input op names of the sess . graph
output_op_names
List[str]
The list of output op names of the sess.graph
The list of output op names of the sess . graph
use_cuda
bool
If True then use a GPU for QuantizationSimModel
If True then use a GPU for QuantizationSimModel
evaluator
Callable[[tf.Session, Any], None]
A callback function that is expected to run forward passes on a session
A callback function that is expected to run forward passes on a session
logdir
str
Path to a directory for logging, required by save_and_load_graph API
Path to a directory for logging required by save_and_load_graph API
encoding_filename
str
Optional encoding file to set and freeze parameter encodings
before computing encodings
Optional encoding file to set and freeze parameter encodings before computing encodings