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 并不指代操作接口的 dtypes。例如,这里的“输入 dtype”并不是传递给量化线性操作的输入张量的 dtype。尽管它仍然可以与接口 dtype 相同,但这并不总是如此,例如,在动态量化中,接口 dtype 是 fp32,但在 DTypeConfig 中指定的“输入 dtype”仍然是 quint8。这里的 dtypes 的语义与观察者中指定的 dtypes 的语义相同。
这些数据类型与用户在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)[源代码]¶
- Create a
DTypeConfigfrom a dictionary with the following items (all optional): “input_dtype”: torch.dtype 或
DTypeWithConstraints“output_dtype”: torch.dtype 或DTypeWithConstraints“weight_dtype”: torch.dtype 或DTypeWithConstraints“bias_type”: torch.dtype “is_dynamic”: bool
- Return type
- Create a