torch.ao.ns._numeric_suite¶
Warning
This module is an early prototype and is subject to change.
- torch.ao.ns._numeric_suite.compare_weights(float_dict, quantized_dict)[source]¶
Compare the weights of the float module with its corresponding quantized module. Return a dict with key corresponding to module names and each entry being a dictionary with two keys ‘float’ and ‘quantized’, containing the float and quantized weights. This dict can be used to compare and compute the quantization error of the weights of float and quantized models.
Example usage:
wt_compare_dict = compare_weights( float_model.state_dict(), qmodel.state_dict()) for key in wt_compare_dict: print( key, compute_error( wt_compare_dict[key]['float'], wt_compare_dict[key]['quantized'].dequantize() ) )
- Parameters
- Returns
dict with key corresponding to module names and each entry being a dictionary with two keys ‘float’ and ‘quantized’, containing the float and quantized weights
- Return type
weight_dict
- torch.ao.ns._numeric_suite.get_logger_dict(mod, prefix='')[source]¶
Traverse the modules and save all logger stats into target dict. This is mainly used for quantization accuracy debug.
- Type of loggers supported:
ShadowLogger: used to log the outputs of the quantized module and its matching float shadow module, OutputLogger: used to log the outputs of the modules
- class torch.ao.ns._numeric_suite.ShadowLogger[source]¶
Class used in Shadow module to record the outputs of the original and shadow modules.
- class torch.ao.ns._numeric_suite.Shadow(q_module, float_module, logger_cls)[source]¶
Shadow module attaches the float module to its matching quantized module as the shadow. Then it uses Logger module to process the outputs of both modules.
- Parameters
q_module – module quantized from float_module that we want to shadow
float_module – float module used to shadow q_module
logger_cls – type of logger used to process the outputs of q_module and float_module. ShadowLogger or custom loggers can be used.
- torch.ao.ns._numeric_suite.prepare_model_with_stubs(float_module, q_module, module_swap_list, logger_cls)[source]¶
Prepare the model by attaching the float module to its matching quantized module as the shadow if the float module type is in module_swap_list.
Example usage:
prepare_model_with_stubs(float_model, q_model, module_swap_list, Logger) q_model(data) ob_dict = get_logger_dict(q_model)
- Parameters
float_module (Module) – float module used to generate the q_module
q_module (Module) – module quantized from float_module
module_swap_list (Set[type]) – list of float module types to attach the shadow
logger_cls (Callable) – type of logger to be used in shadow module to process the outputs of quantized module and its float shadow module
- torch.ao.ns._numeric_suite.compare_model_stub(float_model, q_model, module_swap_list, *data, logger_cls=<class 'torch.ao.ns._numeric_suite.ShadowLogger'>)[source]¶
Compare quantized module in a model with its floating point counterpart, feeding both of them the same input. Return a dict with key corresponding to module names and each entry being a dictionary with two keys ‘float’ and ‘quantized’, containing the output tensors of quantized and its matching float shadow module. This dict can be used to compare and compute the module level quantization error.
This function first call prepare_model_with_stubs() to swap the quantized module that we want to compare with the Shadow module, which takes quantized module, corresponding float module and logger as input, and creates a forward path inside to make the float module to shadow quantized module sharing the same input. The logger can be customizable, default logger is ShadowLogger and it will save the outputs of the quantized module and float module that can be used to compute the module level quantization error.
Example usage:
module_swap_list = [torchvision.models.quantization.resnet.QuantizableBasicBlock] ob_dict = compare_model_stub(float_model,qmodel,module_swap_list, data) for key in ob_dict: print(key, compute_error(ob_dict[key]['float'], ob_dict[key]['quantized'].dequantize()))
- Parameters
float_model (Module) – float model used to generate the q_model
q_model (Module) – model quantized from float_model
module_swap_list (Set[type]) – list of float module types at which shadow modules will be attached.
data – input data used to run the prepared q_model
logger_cls – type of logger to be used in shadow module to process the outputs of quantized module and its float shadow module
- Return type
- torch.ao.ns._numeric_suite.get_matching_activations(float_module, q_module)[source]¶
Find the matching activation between float and quantized modules.
- Parameters
- Returns
dict with key corresponding to quantized module names and each entry being a dictionary with two keys ‘float’ and ‘quantized’, containing the matching float and quantized activations
- Return type
act_dict
- torch.ao.ns._numeric_suite.prepare_model_outputs(float_module, q_module, logger_cls=<class 'torch.ao.ns._numeric_suite.OutputLogger'>, allow_list=None)[source]¶
Prepare the model by attaching the logger to both float module and quantized module if they are in the allow_list.
- torch.ao.ns._numeric_suite.compare_model_outputs(float_model, q_model, *data, logger_cls=<class 'torch.ao.ns._numeric_suite.OutputLogger'>, allow_list=None)[source]¶
Compare output activations between float and quantized models at corresponding locations for the same input. Return a dict with key corresponding to quantized module names and each entry being a dictionary with two keys ‘float’ and ‘quantized’, containing the activations of quantized model and float model at matching locations. This dict can be used to compare and compute the propagation quantization error.
Example usage:
act_compare_dict = compare_model_outputs(float_model, qmodel, data) for key in act_compare_dict: print( key, compute_error( act_compare_dict[key]['float'], act_compare_dict[key]['quantized'].dequantize() ) )
- Parameters
float_model (Module) – float model used to generate the q_model
q_model (Module) – model quantized from float_model
data – input data used to run the prepared float_model and q_model
logger_cls – type of logger to be attached to float_module and q_module
allow_list – list of module types to attach logger
- Returns
dict with key corresponding to quantized module names and each entry being a dictionary with two keys ‘float’ and ‘quantized’, containing the matching float and quantized activations
- Return type
act_compare_dict