DType配置

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

一个配置对象,指定了在参考模型规范中传递给量化操作的支持的数据类型,这些类型用于输入和输出激活、权重以及偏置。

例如,考虑以下参考模型:

quant1 - [dequant1 - 线性变换 - quant2] - dequant2

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

请注意,这里的dtype并不指的是操作的接口dtype。例如,此处所说的“输入dtype”并不是传递给量化线性操作的输入张量的dtype。虽然它可能与接口dtype相同,但这并非总是如此。例如,在动态量化中,接口dtype是fp32,但DTypeConfig中指定的“输入dtype”仍然是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)
classmethodfrom_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]

本页目录