快捷方式

BackendConfig

torch.ao.quantization.backend_config.BackendConfig(name='')[原始碼][原始碼]

定義了給定後端上可量化的模式集合,以及如何從這些模式生成參考量化模型的配置。

在此上下文中,模式指的是模組、函式式、運算元或上述各項的有向無環圖。目標後端上支援的每個模式都可以透過 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)
屬性 configs: list[torch.ao.quantization.backend_config.backend_config.BackendPatternConfig]

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

類方法 from_dict(backend_config_dict)[原始碼][原始碼]

從包含以下項的字典建立 BackendConfig

“name”:目標後端的名稱

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

返回型別

BackendConfig

set_backend_pattern_config(config)[原始碼][原始碼]

為可在目標後端上執行的模式設定配置。這將覆蓋給定模式的任何現有配置。

返回型別

BackendConfig

set_backend_pattern_configs(configs)[原始碼][原始碼]

為可在目標後端上執行的模式設定配置列表。如果給定模式之前已註冊,這將覆蓋其任何現有配置。

返回型別

BackendConfig

set_name(name)[原始碼][原始碼]

設定目標後端的名稱。

返回型別

BackendConfig

to_dict()[原始碼][原始碼]

將此 BackendConfig 轉換為字典,其中包含 from_dict() 中描述的項。

返回型別

dict[str, Any]

文件

訪問 PyTorch 全面的開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源