DTypeConfig#
- class torch.ao.quantization.backend_config.DTypeConfig(input_dtype=None, output_dtype=None, weight_dtype=None, bias_dtype=None, is_dynamic=None)[source]#
用於指定參考模型規範中量化操作的輸入和輸出啟用、權重和偏置引數所支援的資料型別的配置物件。
例如,考慮以下參考模型
quant1 - [dequant1 - fp32_linear - quant2] - dequant2
方括號中的模式指的是靜態量化線性層的參考模式。在 DTypeConfig 中將輸入 dtype 設定為 torch.quint8 意味著我們將 torch.quint8 作為第一個量化操作 (quant1) 的 dtype 引數傳入。類似地,將輸出 dtype 設定為 torch.quint8 意味著我們將 torch.quint8 作為第二個量化操作 (quant2) 的 dtype 引數傳入。
請注意,這裡的 dtype 指的不是操作的介面 dtype。例如,“輸入 dtype”不是傳遞給量化線性操作的輸入張量的 dtype。雖然它仍然可以與介面 dtype 相同,但這並非總是如此,例如,在動態量化中,介面 dtype 是 fp32,但 DTypeConfig 中指定的“輸入 dtype”仍然是 quint8。這裡的 dtype 的語義與觀察器 (observers) 中指定的 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)