BackendConfig¶
- class torch.ao.quantization.backend_config.BackendConfig(name='')[源代码]¶
配置定义了可以在给定后端上量化的模式集,以及如何从这些模式生成参考量化模型。
在此上下文中,模式指的是一个模块、一个功能、一个操作符或上述内容的定向无环图。每个在目标后端支持的模式都可以通过
BackendPatternConfig进行单独配置,具体包括:支持的输入/输出激活、权重和偏置数据类型
观察者和量化/反量化操作是如何插入以构建参考模式的,以及
(可选) 融合、量化感知训练和参考模块映射。
模式的格式在以下链接中描述: https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/backend_config/README.md
示例用法:
import torch from torch.ao.quantization.backend_config import ( BackendConfig, BackendPatternConfig, DTypeConfig, ObservationType, ) weighted_int8_dtype_config = DTypeConfig( input_dtype=torch.quint8, output_dtype=torch.quint8, weight_dtype=torch.qint8, bias_dtype=torch.float) def fuse_conv2d_relu(is_qat, conv, relu): return torch.ao.nn.intrinsic.ConvReLU2d(conv, relu) # 用于量化线性层 linear_config = BackendPatternConfig(torch.nn.Linear) .set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT) .add_dtype_config(weighted_int8_dtype_config) .set_root_module(torch.nn.Linear) .set_qat_module(torch.ao.nn.qat.Linear) .set_reference_quantized_module(torch.ao.nn.quantized.reference.Linear) # 用于融合 Conv2d + ReLU 到 ConvReLU2d conv_relu_config = BackendPatternConfig((torch.nn.Conv2d, torch.nn.ReLU)) .set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT) .add_dtype_config(weighted_int8_dtype_config) .set_fused_module(torch.ao.nn.intrinsic.ConvReLU2d) .set_fuser_method(fuse_conv2d_relu) # 用于量化 ConvReLU2d fused_conv_relu_config = BackendPatternConfig(torch.ao.nn.intrinsic.ConvReLU2d) .set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT) .add_dtype_config(weighted_int8_dtype_config) .set_root_module(torch.nn.Conv2d) .set_qat_module(torch.ao.nn.intrinsic.qat.ConvReLU2d) .set_reference_quantized_module(torch.ao.nn.quantized.reference.Conv2d) backend_config = BackendConfig("my_backend") .set_backend_pattern_config(linear_config) .set_backend_pattern_config(conv_relu_config) .set_backend_pattern_config(fused_conv_relu_config)
- classmethod from_dict(backend_config_dict)[源代码]¶
从字典创建一个
BackendConfig,包含以下项目:“name”: 目标后端名称
“configs”: 一个字典列表,每个字典表示一个 BackendPatternConfig
- Return type