• 文档 >
  • 量化 >
  • 量化 API 参考 >
  • DTypeConfig
快捷键

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

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

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

这些数据类型将与用户在 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)
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()[来源][来源] ¶

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

返回类型:

dict[str, Any]


© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,主题由 Read the Docs 提供。

文档

查看 PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源