Examples/torch/quantization/adaround.py
def apply_adaround_and_find_quantized_accuracy(model: torch.nn.Module, evaluator: aimet_common.defs.EvalFunction,
                                               data_loader: torch_data.DataLoader, use_cuda: bool = False,
                                               logdir: str = '') -> Tuple[torch.nn.Module, float]:
    """
    Quantizes the model using AIMET's adaround feature, and saves the model.

    :param model: The loaded model
    :param evaluator: The Eval function to use for evaluation
    :param dataloader: The dataloader to be passed into the AdaroundParameters api
    :param num_val_samples_per_class: No of samples to use from every class in
                                      computing encodings. Not used in pascal voc
                                      dataset
    :param use_cuda: The cuda device.
    :param logdir: Path to a directory for logging.
    :return: A tuple of quantized model and accuracy of model on this quantsim
    """

    bn_folded_model = copy.deepcopy(model)
    _ = fold_all_batch_norms(bn_folded_model, input_shapes=(1, 3, 224, 224))

    input_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'],
                   image_net_config.dataset['image_height'],)
    if use_cuda:
        dummy_input = torch.rand(input_shape).cuda()

    else:
        dummy_input = torch.rand(input_shape)

    # 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

    params = AdaroundParameters(data_loader=data_loader, num_batches=5)
    ada_model = Adaround.apply_adaround(bn_folded_model, dummy_input, params,
                                        path=logdir, filename_prefix='adaround', default_param_bw=8,
                                        default_quant_scheme=QuantScheme.post_training_tf_enhanced)

    quantsim = QuantizationSimModel(model=ada_model, dummy_input=dummy_input,
                                    quant_scheme=QuantScheme.post_training_tf_enhanced,
                                    rounding_mode='nearest', default_output_bw=8, default_param_bw=8,
                                    in_place=False)

    # Set and freeze parameter encodings. These encodings are associated with the Adarounded parameters.
    # This will make sure compute_encodings() doesn't alter the parameter encodings.
    quantsim.set_and_freeze_param_encodings(encoding_path=os.path.join(logdir, 'adaround.encodings'))
    quantsim.compute_encodings(forward_pass_callback=partial(evaluator, use_cuda=use_cuda),
                               forward_pass_callback_args=iterations)
    quantsim.export(path=logdir, filename_prefix='adaround_resnet', dummy_input=dummy_input.cpu())
    accuracy = evaluator(quantsim.model, use_cuda=use_cuda)

    return accuracy

    Quantizes the model using AIMET's adaround feature, and saves the model.

    :param model: The loaded model
    :param evaluator: The Eval function to use for evaluation
    :param dataloader: The dataloader to be passed into the AdaroundParameters api
    :param num_val_samples_per_class: No of samples to use from every class in
                                      computing encodings. Not used in pascal voc
                                      dataset
    :param use_cuda: The cuda device.
    :param logdir: Path to a directory for logging.
    :return: A tuple of quantized model and accuracy of model on this quantsim
    
Quantizes the model using AIMET's adaround feature, and saves the model.
Quantizes the model using AIMET ' s adaround feature and saves the model .
def apply_adaround_and_find_quantized_accuracy(model: torch.nn.Module, evaluator: aimet_common.defs.EvalFunction,
                                               data_loader: torch_data.DataLoader, use_cuda: bool = False,
                                               logdir: str = '') -> Tuple[torch.nn.Module, float]:
    bn_folded_model = copy.deepcopy(model)
    _ = fold_all_batch_norms(bn_folded_model, input_shapes=(1, 3, 224, 224))
    input_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'],
                   image_net_config.dataset['image_height'],)
    if use_cuda:
        dummy_input = torch.rand(input_shape).cuda()
    else:
        dummy_input = torch.rand(input_shape)
    iterations = 5
    params = AdaroundParameters(data_loader=data_loader, num_batches=5)
    ada_model = Adaround.apply_adaround(bn_folded_model, dummy_input, params,
                                        path=logdir, filename_prefix='adaround', default_param_bw=8,
                                        default_quant_scheme=QuantScheme.post_training_tf_enhanced)
    quantsim = QuantizationSimModel(model=ada_model, dummy_input=dummy_input,
                                    quant_scheme=QuantScheme.post_training_tf_enhanced,
                                    rounding_mode='nearest', default_output_bw=8, default_param_bw=8,
                                    in_place=False)
    quantsim.set_and_freeze_param_encodings(encoding_path=os.path.join(logdir, 'adaround.encodings'))
    quantsim.compute_encodings(forward_pass_callback=partial(evaluator, use_cuda=use_cuda),
                               forward_pass_callback_args=iterations)
    quantsim.export(path=logdir, filename_prefix='adaround_resnet', dummy_input=dummy_input.cpu())
    accuracy = evaluator(quantsim.model, use_cuda=use_cuda)
    return accuracy
def apply_adaround_and_find_quantized_accuracy ( model : torch . nn . Module , evaluator : aimet_common . defs . EvalFunction , data_loader : torch_data . DataLoader , use_cuda : bool = False , logdir : str = '' ) -> Tuple [ torch . nn . Module , float ] : bn_folded_model = copy . deepcopy ( model ) _ = fold_all_batch_norms ( bn_folded_model , input_shapes = ( 1 , 3 , 224 , 224 ) ) input_shape = ( 1 , image_net_config . dataset [ 'image_channels' ] , image_net_config . dataset [ 'image_width' ] , image_net_config . dataset [ 'image_height' ] , ) if use_cuda : dummy_input = torch . rand ( input_shape ) . cuda ( ) else : dummy_input = torch . rand ( input_shape ) iterations = 5 params = AdaroundParameters ( data_loader = data_loader , num_batches = 5 ) ada_model = Adaround . apply_adaround ( bn_folded_model , dummy_input , params , path = logdir , filename_prefix = 'adaround' , default_param_bw = 8 , default_quant_scheme = QuantScheme . post_training_tf_enhanced ) quantsim = QuantizationSimModel ( model = ada_model , dummy_input = dummy_input , quant_scheme = QuantScheme . post_training_tf_enhanced , rounding_mode = 'nearest' , default_output_bw = 8 , default_param_bw = 8 , in_place = False ) quantsim . set_and_freeze_param_encodings ( encoding_path = os . path . join ( logdir , 'adaround.encodings' ) ) quantsim . compute_encodings ( forward_pass_callback = partial ( evaluator , use_cuda = use_cuda ) , forward_pass_callback_args = iterations ) quantsim . export ( path = logdir , filename_prefix = 'adaround_resnet' , dummy_input = dummy_input . cpu ( ) ) accuracy = evaluator ( quantsim . model , use_cuda = use_cuda ) return accuracy
Quantizes the model using AIMET's adaround feature, and saves the model.
Quantizes the model using AIMET ' s adaround feature and saves the model .
"""
    Quantizes the model using AIMET's adaround feature, and saves the model.

    :param model: The loaded model
    :param evaluator: The Eval function to use for evaluation
    :param dataloader: The dataloader to be passed into the AdaroundParameters api
    :param num_val_samples_per_class: No of samples to use from every class in
                                      computing encodings. Not used in pascal voc
                                      dataset
    :param use_cuda: The cuda device.
    :param logdir: Path to a directory for logging.
    :return: A tuple of quantized model and accuracy of model on this quantsim
    """ # 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 # Set and freeze parameter encodings. These encodings are associated with the Adarounded parameters. # This will make sure compute_encodings() doesn't alter the parameter encodings.
A tuple of quantized model and accuracy of model on this quantsim
A tuple of quantized model and accuracy of model on this quantsim
model
torch.nn.Module
The loaded model
The loaded model
evaluator
aimet_common.defs.EvalFunction
The Eval function to use for evaluation
The Eval function to use for evaluation
data_loader
torch_data.DataLoader
use_cuda
bool
The cuda device.
The cuda device .
logdir
str
Path to a directory for logging.
Path to a directory for logging .
dataloader
The dataloader to be passed into the AdaroundParameters api
The dataloader to be passed into the AdaroundParameters api
num_val_samples_per_class
No of samples to use from every class in
computing encodings. Not used in pascal voc
dataset
No of samples to use from every class in computing encodings . Not used in pascal voc dataset