快捷方式

MinMaxObserver

class torch.ao.quantization.observer.MinMaxObserver(dtype=torch.quint8, qscheme=torch.per_tensor_affine, reduce_range=False, quant_min=None, quant_max=None, factory_kwargs=None, eps=1.1920928955078125e-07, is_dynamic=False, **kwargs)[source][source]

用於根據執行中的最小值和最大值計算量化引數的觀察器模組。

此觀察器使用張量的最小值/最大值統計資訊來計算量化引數。該模組記錄輸入張量的執行中的最小值和最大值,並使用此統計資訊來計算量化引數。

引數
  • dtypequantize 節點所需的 dtype 引數,用於實現參考模型規範。

  • qscheme – 要使用的量化方案

  • reduce_range – 將量化資料型別的範圍減少 1 位元

  • quant_min – 最小量化值。如果未指定,將遵循 8 位元設定。

  • quant_max – 最大量化值。如果未指定,將遵循 8 位元設定。

  • eps (張量) – float32 的 Epsilon 值,預設為 torch.finfo(torch.float32).eps

給定執行中的最小/最大值為 xminx_\text{min}xmaxx_\text{max},標度 ss 和零點 zz 計算如下:

執行中的最小/最大值 xmin/maxx_\text{min/max} 計算如下:

xmin={min(X)if xmin=Nonemin(xmin,min(X))otherwisexmax={max(X)if xmax=Nonemax(xmax,max(X))otherwise\begin{array}{ll} x_\text{min} &= \begin{cases} \min(X) & \text{if~}x_\text{min} = \text{None} \\ \min\left(x_\text{min}, \min(X)\right) & \text{otherwise} \end{cases}\\ x_\text{max} &= \begin{cases} \max(X) & \text{if~}x_\text{max} = \text{None} \\ \max\left(x_\text{max}, \max(X)\right) & \text{otherwise} \end{cases}\\ \end{array}

其中 XX 是觀察到的張量。

隨後,標度 ss 和零點 zz 計算如下:

if Symmetric:s=2max(xmin,xmax)/(QmaxQmin)z={0if dtype is qint8128otherwiseOtherwise:s=(xmaxxmin)/(QmaxQmin)z=Qminround(xmin/s)\begin{aligned} \text{if Symmetric:}&\\ &s = 2 \max(|x_\text{min}|, x_\text{max}) / \left( Q_\text{max} - Q_\text{min} \right) \\ &z = \begin{cases} 0 & \text{if dtype is qint8}\\ 128 & \text{otherwise}\end{cases}\\ \text{Otherwise:}&\\ &s = \left( x_\text{max} - x_\text{min} \right ) / \left( Q_\text{max} - Q_\text{min} \right ) \\ &z = Q_\text{min} - \text{round}(x_\text{min} / s)\end{aligned}

其中 QminQ_\text{min}QmaxQ_\text{max} 分別是量化資料型別的最小值和最大值。

警告

dtype 只能是 torch.qint8torch.quint8

注意

如果執行最小值等於執行最大值,則 scale 和 zero_point 會被設定為 1.0 和 0。

calculate_qparams()[source][source]

計算量化引數。

forward(x_orig)[source][source]

記錄 x 的執行最小值和最大值。

reset_min_max_vals()[source][source]

重置最小值/最大值。

文件

訪問 PyTorch 的完整開發者文件

檢視文件

教程

獲取面向初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得解答

檢視資源