Observer 模組,用於根據執行中的最小值和最大值計算量化參數。
這個觀察者使用張量最小值/最大值統計量來計算量化參數。此模組記錄傳入張量的執行最小值和最大值,並使用此統計量來計算量化參數。
- 參數
dtype – 實現參考模型規範所需的 quantize 節點的 dtype 參數。
qscheme – 要使用的量化方案
reduce_range – 將量化資料類型的範圍減少 1 位元
quant_min – 最小量化值。如果未指定,它將遵循 8 位元的設定。
quant_max – 最大量化值。如果未指定,它將遵循 8 位元的設定。
eps (Tensor) – float32 的 Epsilon 值,預設為 torch.finfo(torch.float32).eps。
給定執行最小值/最大值為 xmin 和 xmax, 縮放 s 和零點 z 計算如下
執行最小值/最大值 xmin/max 計算如下
xminxmax={min(X)min(xmin,min(X))if xmin=Noneotherwise={max(X)max(xmax,max(X))if xmax=Noneotherwise 其中 X 是觀察到的張量。
然後縮放 s 和零點 z 計算如下
if Symmetric:Otherwise:s=2max(∣xmin∣,xmax)/(Qmax−Qmin)z={0128if dtype is qint8otherwises=(xmax−xmin)/(Qmax−Qmin)z=Qmin−round(xmin/s) 其中 Qmin 和 Qmax 是量化資料類型的最小值和最大值。
警告
dtype
只能採用 torch.qint8
或 torch.quint8
。
注意
如果執行最小值等於執行最大值,則 scale 和 zero_point 設定為 1.0 和 0。
-
calculate_qparams()[source][source]
計算量化參數。
-
forward(x_orig)[source][source]
記錄 x
的執行最小值和最大值。
-
reset_min_max_vals()[source][source]
重設最小值/最大值。