convert_fx¶
- class torch.ao.quantization.quantize_fx.convert_fx(graph_module, convert_custom_config=None, _remove_qconfig=True, qconfig_mapping=None, backend_config=None)[源代码]¶
将校准或训练过的模型转换为量化模型
- 参数
graph_module (*) – 已准备好的并经过校准/训练的模型 (GraphModule)
convert_custom_config (*) – 转换函数的自定义配置。有关更多详细信息,请参阅
ConvertCustomConfig
_remove_qconfig (*) – 转换后是否删除模型中的 qconfig 属性。
qconfig_mapping (*) –
用于指定如何将模型转换为量化的配置。
键必须包含传递给 prepare_fx 或 prepare_qat_fx 的 qconfig_mapping 中的键,并具有相同的值或 None。可以指定其他键,其值设置为 None。
对于每个值为 None 的条目,我们将跳过对模型中该条目的量化
qconfig_mapping = QConfigMapping .set_global(qconfig_from_prepare) .set_object_type(torch.nn.functional.add, None) # skip quantizing torch.nn.functional.add .set_object_type(torch.nn.functional.linear, qconfig_from_prepare) .set_module_name("foo.bar", None) # skip quantizing module "foo.bar"
- backend_config (BackendConfig): 后端的配置,描述了如何在
后端中量化算子,包括量化模式支持(静态/动态/仅权重)、数据类型支持(quint8/qint8 等)、每个算子的观察器放置和融合算子。有关更多详细信息,请参阅
BackendConfig
- 返回值
量化模型 (torch.nn.Module)
- 返回类型
示例
# prepared_model: the model after prepare_fx/prepare_qat_fx and calibration/training # convert_fx converts a calibrated/trained model to a quantized model for the # target hardware, this includes converting the model first to a reference # quantized model, and then lower the reference quantized model to a backend # Currently, the supported backends are fbgemm (onednn), qnnpack (xnnpack) and # they share the same set of quantized operators, so we are using the same # lowering procedure # # backend_config defines the corresponding reference quantized module for # the weighted modules in the model, e.g. nn.Linear # TODO: add backend_config after we split the backend_config for fbgemm and qnnpack # e.g. backend_config = get_default_backend_config("fbgemm") quantized_model = convert_fx(prepared_model)