torch.ao.ns._numeric_suite¶
警告
此模块为早期原型,可能会发生变化。
- torch.ao.ns._numeric_suite.compare_weights(float_dict, quantized_dict)[source][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][source]¶
遍历模块并将所有日志记录器统计信息保存到目标字典中。这主要用于量化精度调试。
- 支持的日志记录器类型
ShadowLogger: 用于记录量化模块及其匹配的浮点影子模块的输出,OutputLogger: 用于记录模块的输出
- class torch.ao.ns._numeric_suite.Shadow(q_module, float_module, logger_cls)[source][source]¶
Shadow 模块将其匹配的浮点模块作为影子附加到量化模块。然后它使用 Logger 模块处理两个模块的输出。
- 参数
q_module – 从 float_module 量化而来且我们想要进行影子对比的模块
float_module – 用于影子对比 q_module 的浮点模块
logger_cls – 用于处理 q_module 和 float_module 输出的 Logger 类型。可以使用 ShadowLogger 或自定义 Logger。
- torch.ao.ns._numeric_suite.prepare_model_with_stubs(float_module, q_module, module_swap_list, logger_cls)[source][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][source]¶
比较模型中的量化模块与其对应的浮点模块,同时输入相同的数据。返回一个字典,其键对应于模块名称,每个条目是一个包含两个键“float”和“quantized”的字典,分别包含量化模块及其匹配的浮点影子模块的输出张量。此字典可用于比较和计算模块级别的量化误差。
此函数首先调用 prepare_model_with_stubs() 将我们想要比较的量化模块替换为 Shadow 模块,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][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][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][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() ) )