快捷方式

BackendConfig

class torch.ao.quantization.backend_config.BackendConfig(name='')[source][source]

定义可在给定后端上量化的一组模式以及如何从这些模式生成参考量化模型的配置。

此上下文中的模式是指模块、函数、算子或上述的有向无环图。目标后端上支持的每个模式都可以通过 BackendPatternConfig 单独配置,配置内容包括:

  1. 支持的输入/输出激活、权重和偏置数据类型

  2. 如何插入观察者和量化/反量化操作以构建参考模式,以及

  3. (可选)融合、QAT 和参考模块映射。

模式的格式在以下位置描述: 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)

# For quantizing Linear
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)

# For fusing Conv2d + ReLU into 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)

# For quantizing 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)
property configs: List[BackendPatternConfig]

返回此 BackendConfig 中设置的配置列表的副本。

classmethod from_dict(backend_config_dict)[source][source]

从具有以下项的字典创建 BackendConfig

“name”:目标后端的名称

“configs”:字典列表,每个字典代表一个 BackendPatternConfig

返回类型

BackendConfig

set_backend_pattern_config(config)[source][source]

为可在目标后端上运行的模式设置配置。这将覆盖给定模式的任何现有配置。

返回类型

BackendConfig

set_backend_pattern_configs(configs)[source][source]

为可在目标后端上运行的模式设置配置。如果给定模式先前已注册,这将覆盖其任何现有配置。

返回类型

BackendConfig

set_name(name)[source][source]

设置目标后端的名称。

返回类型

BackendConfig

to_dict()[source][source]

将此 BackendConfig 转换为具有 from_dict() 中所述项的字典。

返回类型

Dict[str, Any]

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取面向初学者和高级开发者的深入教程

查看教程

资源

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

查看资源