快捷方式

DTypeConfig

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

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

例如,考虑以下引用模型

quant1 - [dequant1 - fp32_linear - quant2] - dequant2

方括号中的模式指的是静态量化线性的引用模式。在 DTypeConfig 中将输入数据类型设置为 torch.quint8 意味着我们将 torch.quint8 作为数据类型参数传递给第一个量化操作 (quant1)。同样,将输出数据类型设置为 torch.quint8 意味着我们将 torch.quint8 作为数据类型参数传递给第二个量化操作 (quant2)。

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

这些数据类型与用户 QConfig 中指定的数据类型进行匹配。如果存在匹配,并且 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][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][source]

将此 DTypeConfig 转换为字典,字典中的项如 from_dict() 中所述。

返回类型

Dict[str, Any]

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

查找开发资源并获得解答

查看资源