评价此页

torch.ao.ns._numeric_suite#

创建于: 2025年6月13日 | 最后更新于: 2025年6月13日

警告

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.

使用示例

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(),
        ),
    )
参数
  • float_dict (dict[str, Any]) – state dict of the float model

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

返回

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

返回类型

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

参数
  • mod (Module) – module we want to save all logger stats

  • prefix (str) – prefix for the current module

返回

the dictionary used to save all logger stats

返回类型

target_dict

class torch.ao.ns._numeric_suite.Logger[source]#

Base class for stats logging

forward(x)[source]#
class torch.ao.ns._numeric_suite.ShadowLogger[source]#

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

forward(x, y)[source]#
class torch.ao.ns._numeric_suite.OutputLogger[source]#

Class used to log the outputs of the module

forward(x)[source]#
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.

参数
  • 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)[source]#
返回类型

张量

add(x, y)[source]#
返回类型

张量

add_scalar(x, y)[source]#
返回类型

张量

mul(x, y)[source]#
返回类型

张量

mul_scalar(x, y)[source]#
返回类型

张量

cat(x, dim=0)[source]#
返回类型

张量

add_relu(x, y)[source]#
返回类型

张量

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.

使用示例

prepare_model_with_stubs(float_model, q_model, module_swap_list, Logger)
q_model(data)
ob_dict = get_logger_dict(q_model)
参数
  • 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.

使用示例

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()
        ),
    )
参数
  • float_model (Module) – float model used to generate the q_model

  • q_model (Module) – model quantized from float_model

  • module_swap_list (set[type]) – 浮点模块类型列表,影子模块将附加到这些模块上。

  • data – 用于运行已准备好的 q_model 的输入数据

  • logger_cls – 在影子模块中用于处理量化模块及其浮点影子模块输出的日志记录器类型

返回类型

dict[str, dict]

torch.ao.ns._numeric_suite.get_matching_activations(float_module, q_module)[源代码]#

查找浮点模块和量化模块之间的匹配激活。

参数
  • float_module (Module) – float module used to generate the q_module

  • q_module (Module) – module quantized from float_module

返回

一个字典,键对应量化模块名称,每个条目是一个包含两个键“float”和“quantized”的字典,分别包含匹配的浮点和量化激活。

返回类型

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)[源代码]#

通过将日志记录器附加到浮点模块和量化模块(如果它们在允许列表中)来准备模型。

参数
  • float_module (Module) – float module used to generate the q_module

  • q_module (Module) – module quantized from float_module

  • logger_cls – 要附加到 float_module 和 q_module 的日志记录器类型

  • 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)[源代码]#

比较同一输入下浮点模型和量化模型在相应位置的输出激活。返回一个字典,键对应量化模块名称,每个条目是一个包含两个键“float”和“quantized”的字典,分别包含量化模型和浮点模型在匹配位置的激活。此字典可用于比较和计算量化误差的传播。

使用示例

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(),
        ),
    )
参数
  • float_model (Module) – float model used to generate the q_model

  • q_model (Module) – model quantized from float_model

  • data – 用于运行准备好的 float_model 和 q_model 的输入数据

  • logger_cls – 要附加到 float_module 和 q_module 的日志记录器类型

  • allow_list – 要附加日志记录器的模块类型列表

返回

一个字典,键对应量化模块名称,每个条目是一个包含两个键“float”和“quantized”的字典,分别包含匹配的浮点和量化激活。

返回类型

act_compare_dict