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 不指運算元的介面 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)