转换_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]¶
将校准或训练好的模型转换为量化模型
- 参数:
图模块 (*) – 准备并校准/训练好的模型(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(后端配置):描述后端如何配置的配置。
量化操作应在后端进行,这包括量化模式支持(静态/动态/仅权重),数据类型支持(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)