Shortcuts

torch.ao.ns._numeric_suite

警告

此模块是一个早期原型,可能会发生变化。

torch.ao.ns._numeric_suite.compare_weights(float_dict, quantized_dict)[源代码]

比较浮点模块与其对应的量化模块的权重。返回一个字典,键对应模块名称,每个条目是一个包含两个键‘float’和‘quantized’的字典,分别包含浮点和量化的权重。此字典可用于比较和计算浮点和量化模型的权重量化误差。

示例用法:

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

字典,键对应于模块名称,每个条目是一个包含两个键‘float’和‘quantized’的字典,分别包含浮点和量化权重

Return type

权重字典

torch.ao.ns._numeric_suite.get_logger_dict(mod, prefix='')[源代码]

遍历模块并将所有日志记录统计信息保存到目标字典中。 这主要用于量化精度调试。

Type of loggers supported:

ShadowLogger: 用于记录量化模块及其匹配的浮点影子模块的输出, OutputLogger: 用于记录模块的输出

Parameters
  • mod (模块) – 我们想要保存所有日志记录器状态的模块

  • 前缀 (字符串) – 当前模块的前缀

Returns

用于保存所有日志记录器状态的字典

Return type

目标字典

class torch.ao.ns._numeric_suite.Logger[源代码]

用于统计日志记录的基类

forward(x)[源代码]
class torch.ao.ns._numeric_suite.ShadowLogger[源代码]

用于在Shadow模块中记录原始模块和影子模块输出的类。

forward(x, y)[源代码]
class torch.ao.ns._numeric_suite.OutputLogger[源代码]

用于记录模块输出的类

forward(x)[源代码]
class torch.ao.ns._numeric_suite.Shadow(q_module, float_module, logger_cls)[源代码]

影子模块将浮点模块附加到其匹配的量化模块上,作为影子。然后它使用日志记录器模块来处理这两个模块的输出。

Parameters
  • q_module – 从float_module量化得到的模块,我们希望对其进行影子跟踪

  • float_module – 用于遮蔽 q_module 的浮点模块

  • logger_cls – 用于处理 q_module 和 float_module 输出日志的日志记录器类型。可以使用 ShadowLogger 或自定义日志记录器。

forward(*x)[源代码]
Return type

张量

add(x, y)[源代码]
Return type

张量

add_scalar(x, y)[源代码]
Return type

张量

mul(x, y)[源代码]
Return type

张量

mul_scalar(x, y)[源代码]
Return type

张量

cat(x, dim=0)[源代码]
Return type

张量

add_relu(x, y)[源代码]
Return type

张量

torch.ao.ns._numeric_suite.prepare_model_with_stubs(float_module, q_module, module_swap_list, logger_cls)[源代码]

通过将浮点模块附加到其匹配的量化模块作为影子来准备模型,如果浮点模块类型在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)
Parameters
  • float_module (模块) – 用于生成 q_module 的浮点模块

  • q_module (模块) – 从 float_module 量化得到的模块

  • module_swap_list (Set[type]) – 要附加阴影的浮点模块类型的列表

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

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

比较模型中的量化模块与其浮点对应模块,向两者提供相同的输入。返回一个字典,键对应于模块名称,每个条目是一个包含两个键‘float’和‘quantized’的字典,分别包含量化模块及其匹配的浮点影子模块的输出张量。此字典可用于比较和计算模块级别的量化误差。

此函数首先调用 prepare_model_with_stubs() 来交换我们想要与 Shadow 模块进行比较的量化模块,该模块接受量化模块、相应的浮点模块和日志记录器作为输入,并在内部创建一个前向路径,使浮点模块与量化模块共享相同的输入。日志记录器可以自定义,默认的日志记录器是 ShadowLogger,它将保存量化模块和浮点模块的输出,这些输出可用于计算模块级别的量化误差。

示例用法:

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 (模块) – 用于生成 q_model 的浮点模型

  • q_model (模块) – 从 float_model 量化得到的模型

  • module_swap_list (Set[type]) – 在哪些浮点模块类型上将附加阴影模块。

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

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

Return type

字典[字符串, 字典]

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

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

Parameters
  • float_module (模块) – 用于生成 q_module 的浮点模块

  • q_module (模块) – 从 float_module 量化得到的模块

Returns

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

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

如果浮点模块和量化模块在允许列表中,请通过将记录器附加到这两个模块来准备模型。

Parameters
  • float_module (模块) – 用于生成 q_module 的浮点模块

  • q_module (模块) – 从 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()
        )
    )
Parameters
  • float_model (模块) – 用于生成 q_model 的浮点模型

  • q_model (模块) – 从 float_model 量化得到的模型

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

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

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

Returns

字典,键对应于量化模块名称,每个条目是一个包含两个键‘float’和‘quantized’的字典,包含匹配的浮点和量化激活

Return type

act_compare_dict

优云智算