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
方括號中的模式指的是靜態量化線性層的參考模式。在 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)