Table of Contents
Shortcuts

# 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)

  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:

    - float_dict (Dict[str, Any]) – state dict of the float model

    - quantized_dict (Dict[str, Any]) – state dict of the quantized model

  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='')

  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

  Parameters:

    - mod (Module) – module we want to save all logger stats

    - prefix (str) – prefix for the current module

  Returns:

    the dictionary used to save all logger stats

  Return type:

    target_dict

class torch.ao.ns._numeric_suite.Logger

  Base class for stats logging

  forward(x)

class torch.ao.ns._numeric_suite.ShadowLogger

  Class used in Shadow module to record the outputs of the original and shadow modules.

  forward(x, y)

class torch.ao.ns._numeric_suite.OutputLogger

  Class used to log the outputs of the module

  forward(x)

class torch.ao.ns._numeric_suite.Shadow(q_module, float_module, logger_cls)

  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.

  forward(*x)
    Return type:

      Tensor

  add(x, y)
    Return type:

      Tensor

  add_scalar(x, y)
    Return type:

      Tensor

  mul(x, y)
    Return type:

      Tensor

  mul_scalar(x, y)
    Return type:

      Tensor

  cat(x, dim=0)
    Return type:

      Tensor

  add_relu(x, y)
    Return type:

      Tensor

torch.ao.ns._numeric_suite.prepare_model_with_stubs(float_module, q_module, module_swap_list, logger_cls)

  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'>)

  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:

    Dict[str, Dict]

torch.ao.ns._numeric_suite.get_matching_activations(float_module, q_module)

  Find the matching activation between float and quantized modules.

  Parameters:

    - float_module (Module) – float module used to generate the q_module

    - q_module (Module) – module quantized from float_module

  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)

  Prepare the model by attaching the logger to both float module and quantized module if they are in the allow_list.

  Parameters:

    - float_module (Module) – float module used to generate the q_module

    - q_module (Module) – module quantized from float_module

    - logger_cls – type of logger to be attached to float_module and q_module

    - allow_list – list of module types to attach logger

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)

  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

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.