(原型) PyTorch BackendConfig 教程¶
创建于: 2023年1月3日 | 最后更新于: 2023年1月18日 | 最后验证: 未验证
作者: Andrew Or
BackendConfig API 使开发者能够将其后端与 PyTorch 量化集成。目前它仅支持 FX 图模式量化,但未来可能会扩展到其他量化模式。在本教程中,我们将演示如何使用此 API 为特定后端定制量化支持。有关 BackendConfig 动机和实现细节的更多信息,请参阅此README。
假设我们是一名后端开发者,希望将我们的后端与 PyTorch 的量化 API 集成。我们的后端仅包含两个算子:量化 Linear 和量化 Conv-ReLU。在本节中,我们将通过使用自定义 BackendConfig,通过 prepare_fx 和 convert_fx 量化示例模型来演示如何实现这一点。
import torch
from torch.ao.quantization import (
default_weight_observer,
get_default_qconfig_mapping,
MinMaxObserver,
QConfig,
QConfigMapping,
)
from torch.ao.quantization.backend_config import (
BackendConfig,
BackendPatternConfig,
DTypeConfig,
DTypeWithConstraints,
ObservationType,
)
from torch.ao.quantization.quantize_fx import prepare_fx, convert_fx
1. 推导每个量化算子的参考模式¶
对于量化 Linear,假设我们的后端期望参考模式 [dequant - fp32_linear - quant] 并将其转换为单个量化 Linear 算子。实现此目的的方法是,首先在浮点 Linear 算子之前和之后插入 quant-dequant 算子,从而生成以下参考模型
quant1 - [dequant1 - fp32_linear - quant2] - dequant2
类似地,对于量化 Conv-ReLU,我们希望生成以下参考模型,其中方括号中的参考模式将被转换为单个量化 Conv-ReLU 算子
quant1 - [dequant1 - fp32_conv_relu - quant2] - dequant2
2. 设置具有后端约束的 DTypeConfigs¶
在上面的参考模式中,DTypeConfig 中指定的输入数据类型将作为数据类型参数传递给 quant1,而输出数据类型将作为数据类型参数传递给 quant2。如果输出数据类型是 fp32(如动态量化的情况),则不会插入输出 quant-dequant 对。此示例还展示了如何指定对特定数据类型的量化和比例范围的限制。
quint8_with_constraints = DTypeWithConstraints(
dtype=torch.quint8,
quant_min_lower_bound=0,
quant_max_upper_bound=255,
scale_min_lower_bound=2 ** -12,
)
# Specify the dtypes passed to the quantized ops in the reference model spec
weighted_int8_dtype_config = DTypeConfig(
input_dtype=quint8_with_constraints,
output_dtype=quint8_with_constraints,
weight_dtype=torch.qint8,
bias_dtype=torch.float)
3. 为 Conv-ReLU 设置融合¶
请注意,原始用户模型包含单独的 conv 和 relu 算子,因此我们需要先将 conv 和 relu 算子融合为单个 conv-relu 算子 (fp32_conv_relu),然后像量化 Linear 算子一样量化此算子。我们可以通过定义一个接受 3 个参数的函数来设置融合,其中第一个参数表示是否用于 QAT,其余参数指向融合模式的单个项。
def fuse_conv2d_relu(is_qat, conv, relu):
"""Return a fused ConvReLU2d from individual conv and relu modules."""
return torch.ao.nn.intrinsic.ConvReLU2d(conv, relu)
4. 定义 BackendConfig¶
现在我们已经具备了所有必要的组件,可以继续定义我们的 BackendConfig。此处我们为 Linear 算子的输入和输出使用不同的观察者(将被重命名),因此传递给两个量化算子(quant1 和 quant2)的量化参数将不同。对于 Linear 和 Conv 等带权算子,这通常是常见情况。
对于 Conv-ReLU 算子,观察类型相同。但是,我们需要两个 BackendPatternConfig 来支持此算子,一个用于融合,一个用于量化。对于 Conv-ReLU 和 Linear,我们都使用上面定义的 DTypeConfig。
linear_config = BackendPatternConfig() \
.set_pattern(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.nn.qat.Linear) \
.set_reference_quantized_module(torch.ao.nn.quantized.reference.Linear)
# For fusing Conv2d + ReLU into ConvReLU2d
# No need to set observation type and dtype config here, since we are not
# inserting quant-dequant ops in this step yet
conv_relu_config = BackendPatternConfig() \
.set_pattern((torch.nn.Conv2d, torch.nn.ReLU)) \
.set_fused_module(torch.ao.nn.intrinsic.ConvReLU2d) \
.set_fuser_method(fuse_conv2d_relu)
# For quantizing ConvReLU2d
fused_conv_relu_config = BackendPatternConfig() \
.set_pattern(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)
5. 设置满足后端约束的 QConfigMapping¶
为了使用上面定义的算子,用户必须定义一个满足 DTypeConfig 中指定约束的 QConfig。有关更多详细信息,请参阅 DTypeConfig 的文档。然后,我们将对希望量化的模式中使用的所有模块使用此 QConfig。
# Note: Here we use a quant_max of 127, but this could be up to 255 (see `quint8_with_constraints`)
activation_observer = MinMaxObserver.with_args(quant_min=0, quant_max=127, eps=2 ** -12)
qconfig = QConfig(activation=activation_observer, weight=default_weight_observer)
# Note: All individual items of a fused pattern, e.g. Conv2d and ReLU in
# (Conv2d, ReLU), must have the same QConfig
qconfig_mapping = QConfigMapping() \
.set_object_type(torch.nn.Linear, qconfig) \
.set_object_type(torch.nn.Conv2d, qconfig) \
.set_object_type(torch.nn.BatchNorm2d, qconfig) \
.set_object_type(torch.nn.ReLU, qconfig)
6. 通过 prepare 和 convert 量化模型¶
最后,我们通过将定义的 BackendConfig 传递给 prepare 和 convert 来量化模型。这将生成一个量化 Linear 模块和一个融合的量化 Conv-ReLU 模块。
class MyModel(torch.nn.Module):
def __init__(self, use_bn: bool):
super().__init__()
self.linear = torch.nn.Linear(10, 3)
self.conv = torch.nn.Conv2d(3, 3, 3)
self.bn = torch.nn.BatchNorm2d(3)
self.relu = torch.nn.ReLU()
self.sigmoid = torch.nn.Sigmoid()
self.use_bn = use_bn
def forward(self, x):
x = self.linear(x)
x = self.conv(x)
if self.use_bn:
x = self.bn(x)
x = self.relu(x)
x = self.sigmoid(x)
return x
example_inputs = (torch.rand(1, 3, 10, 10, dtype=torch.float),)
model = MyModel(use_bn=False)
prepared = prepare_fx(model, qconfig_mapping, example_inputs, backend_config=backend_config)
prepared(*example_inputs) # calibrate
converted = convert_fx(prepared, backend_config=backend_config)
>>> print(converted)
GraphModule(
(linear): QuantizedLinear(in_features=10, out_features=3, scale=0.012136868201196194, zero_point=67, qscheme=torch.per_tensor_affine)
(conv): QuantizedConvReLU2d(3, 3, kernel_size=(3, 3), stride=(1, 1), scale=0.0029353597201406956, zero_point=0)
(sigmoid): Sigmoid()
)
def forward(self, x):
linear_input_scale_0 = self.linear_input_scale_0
linear_input_zero_point_0 = self.linear_input_zero_point_0
quantize_per_tensor = torch.quantize_per_tensor(x, linear_input_scale_0, linear_input_zero_point_0, torch.quint8); x = linear_input_scale_0 = linear_input_zero_point_0 = None
linear = self.linear(quantize_per_tensor); quantize_per_tensor = None
conv = self.conv(linear); linear = None
dequantize_2 = conv.dequantize(); conv = None
sigmoid = self.sigmoid(dequantize_2); dequantize_2 = None
return sigmoid
(7. 尝试有问题的 BackendConfig 设置)¶
作为实验,这里我们将模型修改为使用 Conv-BN-ReLU 而不是 Conv-ReLU,但使用相同的 BackendConfig,该 BackendConfig 不知道如何量化 Conv-BN-ReLU。因此,只有 Linear 被量化,而 Conv-BN-ReLU 既未融合也未量化。
>>> print(converted)
GraphModule(
(linear): QuantizedLinear(in_features=10, out_features=3, scale=0.015307803638279438, zero_point=95, qscheme=torch.per_tensor_affine)
(conv): Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1))
(bn): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU()
(sigmoid): Sigmoid()
)
def forward(self, x):
linear_input_scale_0 = self.linear_input_scale_0
linear_input_zero_point_0 = self.linear_input_zero_point_0
quantize_per_tensor = torch.quantize_per_tensor(x, linear_input_scale_0, linear_input_zero_point_0, torch.quint8); x = linear_input_scale_0 = linear_input_zero_point_0 = None
linear = self.linear(quantize_per_tensor); quantize_per_tensor = None
dequantize_1 = linear.dequantize(); linear = None
conv = self.conv(dequantize_1); dequantize_1 = None
bn = self.bn(conv); conv = None
relu = self.relu(bn); bn = None
sigmoid = self.sigmoid(relu); relu = None
return sigmoid
作为另一个实验,此处我们使用默认的 QConfigMapping,它不满足后端中指定的 dtype 约束。因此,没有任何内容被量化,因为 QConfigs 被简单地忽略了。
>>> print(converted)
GraphModule(
(linear): Linear(in_features=10, out_features=3, bias=True)
(conv): Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1))
(bn): BatchNorm2d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU()
(sigmoid): Sigmoid()
)
def forward(self, x):
linear = self.linear(x); x = None
conv = self.conv(linear); linear = None
bn = self.bn(conv); conv = None
relu = self.relu(bn); bn = None
sigmoid = self.sigmoid(relu); relu = None
return sigmoid
内置 BackendConfigs¶
PyTorch 量化支持 torch.ao.quantization.backend_config
命名空间下的几个内置原生 BackendConfigs
get_fbgemm_backend_config: 用于服务器目标设置
get_qnnpack_backend_config: 用于移动和边缘设备目标设置,也支持 XNNPACK 量化算子
get_native_backend_config (默认): 一个支持 FBGEMM 和 QNNPACK BackendConfig 中支持的算子模式的联合 BackendConfig
目前还有其他正在开发的 BackendConfig(例如用于 TensorRT 和 x86),但这些大部分仍处于实验阶段。如果用户希望将新的自定义后端与 PyTorch 的量化 API 集成,他们可以使用与上面示例中用于定义原生支持的 BackendConfig 相同的 API 集来定义自己的 BackendConfig。
延伸阅读¶
BackendConfig 在 FX 图模式量化中的使用:https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/fx/README.md
BackendConfig 的动机和实现细节:https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/backend_config/README.md
BackendConfig 的早期设计:https://github.com/pytorch/rfcs/blob/master/RFC-0019-Extending-PyTorch-Quantization-to-Custom-Backends.md