捷徑

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)
classmethod from_dict(dtype_config_dict)[source][source]
從具有以下項目的字典建立 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()[source][source]

將此 DTypeConfig 轉換為具有 from_dict() 中描述的項目的字典。

回傳類型

Dict[str, Any]

文件

存取 PyTorch 的全面開發人員文件

檢視文件

教學

取得適合初學者和進階開發人員的深入教學

檢視教學

資源

尋找開發資源並取得您的問題解答

檢視資源