评价此页

DTypeConfig#

class torch.ao.quantization.backend_config.DTypeConfig(input_dtype=None, output_dtype=None, weight_dtype=None, bias_dtype=None, is_dynamic=None)[source]#

用于指定参考模型规范中量化操作的输入和输出激活、权重和偏置参数所支持的数据类型的配置对象。

例如,考虑以下参考模型

quant1 - [dequant1 - fp32_linear - quant2] - dequant2

方括号中的模式指的是静态量化线性层的参考模式。在 DTypeConfig 中将输入 dtype 设置为 torch.quint8 意味着我们将 torch.quint8 作为第一个量化操作 (quant1) 的 dtype 参数传入。类似地,将输出 dtype 设置为 torch.quint8 意味着我们将 torch.quint8 作为第二个量化操作 (quant2) 的 dtype 参数传入。

请注意,这里的 dtype 指的不是操作的接口 dtype。例如,“输入 dtype”不是传递给量化线性操作的输入张量的 dtype。虽然它仍然可以与接口 dtype 相同,但这并非总是如此,例如,在动态量化中,接口 dtype 是 fp32,但 DTypeConfig 中指定的“输入 dtype”仍然是 quint8。这里的 dtype 的语义与观察器 (observers) 中指定的 dtype 的语义相同。

这些 dtype 将与用户 QConfig 中指定的 dtype 进行匹配。如果存在匹配,并且 QConfig 满足 DTypeConfig 中指定的约束(如果有),那么我们将使用此 DTypeConfig 来量化给定的模式。否则,QConfig 将被忽略,该模式将不会被量化。

使用示例

>>> dtype_config1 = DTypeConfig(
...     input_dtype=torch.quint8,
...     output_dtype=torch.quint8,
...     weight_dtype=torch.qint8,
...     bias_dtype=torch.float)

>>> dtype_config2 = DTypeConfig(
...     input_dtype=DTypeWithConstraints(
...         dtype=torch.quint8,
...         quant_min_lower_bound=0,
...         quant_max_upper_bound=255,
...     ),
...     output_dtype=DTypeWithConstraints(
...         dtype=torch.quint8,
...         quant_min_lower_bound=0,
...         quant_max_upper_bound=255,
...     ),
...     weight_dtype=DTypeWithConstraints(
...         dtype=torch.qint8,
...         quant_min_lower_bound=-128,
...         quant_max_upper_bound=127,
...     ),
...     bias_dtype=torch.float)

>>> dtype_config1.input_dtype
torch.quint8

>>> dtype_config2.input_dtype
torch.quint8

>>> dtype_config2.input_dtype_with_constraints
DTypeWithConstraints(dtype=torch.quint8, quant_min_lower_bound=0, quant_max_upper_bound=255, scale_min_lower_bound=None, scale_max_upper_bound=None)
classmethod from_dict(dtype_config_dict)[source]#
从字典创建 DTypeConfig,该字典包含以下(可选)项:

“input_dtype”: torch.dtype 或 DTypeWithConstraints “output_dtype”: torch.dtype 或 DTypeWithConstraints “weight_dtype”: torch.dtype 或 DTypeWithConstraints “bias_type”: torch.dtype “is_dynamic”: bool

返回类型

DTypeConfig

to_dict()[source]#

将此 DTypeConfig 转换为字典,字典中的项描述参见 from_dict()

返回类型

dict[str, Any]