torch.ao.ns._numeric_suite¶
警告
此模块是早期原型,可能会发生变化。
- torch.ao.ns._numeric_suite.compare_weights(float_dict, quantized_dict)[source]¶
比较浮点模型与其对应的量化模型的权重。返回一个字典,键对应于模块名称,每个条目都是一个包含两个键“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() ) )
- torch.ao.ns._numeric_suite.get_logger_dict(mod, prefix='')[source]¶
遍历模块并将所有记录器统计信息保存到目标字典中。这主要用于量化精度调试。
- 支持的记录器类型
ShadowLogger:用于记录量化模块及其匹配的浮点影子模块的输出,OutputLogger:用于记录模块的输出
- class torch.ao.ns._numeric_suite.Shadow(q_module, float_module, logger_cls)[source]¶
阴影模块将浮点模块附加到与其匹配的量化模块作为阴影。然后,它使用 Logger 模块处理两个模块的输出。
- 参数
q_module – 从 float_module 量化得到的模块,我们希望对其进行阴影处理
float_module – 用于对 q_module 进行阴影处理的浮点模块
logger_cls – 用于处理 q_module 和 float_module 输出的日志记录器类型。可以使用 ShadowLogger 或自定义日志记录器。
- torch.ao.ns._numeric_suite.prepare_model_with_stubs(float_module, q_module, module_swap_list, logger_cls)[source]¶
通过将浮点模块附加到其匹配的量化模块作为阴影来准备模型,如果浮点模块类型在 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)
- 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]¶
将模型中的量化模块与其浮点对应部分进行比较,同时为两者提供相同的输入。返回一个字典,其中键对应于模块名称,每个条目是一个包含两个键“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()))
- torch.ao.ns._numeric_suite.get_matching_activations(float_module, q_module)[source]¶
查找浮点模块和量化模块之间的匹配激活。
- 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]¶
如果它们在 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]¶
比较浮点模型和量化模型在相同输入下对应位置的输出激活。返回一个字典,其中键对应于量化模块名称,每个条目是一个包含两个键“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() ) )