快捷方式

DTypeConfig

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

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

例如,考虑以下参考模型

quant1 - [dequant1 - fp32_linear - quant2] - dequant2

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

请注意,这里的 dtype 不指算子的接口 dtype。例如,这里的“输入 dtype”不是传递给量化线性算子的输入张量的 dtype。尽管它可能与接口 dtype 相同,但这并非总是如此,例如在动态量化中,接口 dtype 是 fp32,但 DTypeConfig 中指定的“输入 dtype”仍会是 quint8。这里 dtype 的语义与观察器中指定的 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)[源代码][源代码]
从包含以下项(均为可选)的字典创建 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()[源代码][源代码]

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

返回类型

dict[str, Any]

文档

查阅 PyTorch 全面的开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源