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)[source][source]¶
将校准或训练后的模型转换为量化模型
- 参数
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): 后端的配置,描述了如何
算子应在后端量化,这包括量化模式支持(静态/动态/仅权重)、dtype 支持(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)