快捷方式

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, keep_original_weights=False)[source][source]

将经过校准或训练的模型转换为量化模型

参数
  • graph_module (*) – 经过准备和校准/训练的模型 (GraphModule)

  • convert_custom_config (*) – convert 函数的自定义配置。详见 ConvertCustomConfig

  • _remove_qconfig (*) – 转换后移除模型中 qconfig 属性的选项。

  • qconfig_mapping (*) –

    指定如何进行模型量化的配置。

    键必须包含传递给 prepare_fxprepare_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)

返回类型

GraphModule

示例

# 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)

文档

查阅 PyTorch 的全面开发者文档

查看文档

教程

获取针对初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并获得问题解答

查看资源